Skip to content

Instantly share code, notes, and snippets.

@djfdyuruiry
Last active October 4, 2018 07:52
Show Gist options
  • Save djfdyuruiry/e52c2dd710ac2d419a5c258436907761 to your computer and use it in GitHub Desktop.
Save djfdyuruiry/e52c2dd710ac2d419a5c258436907761 to your computer and use it in GitHub Desktop.
NodeJS Script to Generate Postman Collections/Environments for the Alfresco v1 REST API (uses https://www.npmjs.com/package/swagger2-postman-generator)
// see: https://www.npmjs.com/package/swagger2-postman-generator
const Swagger2Postman = require("swagger2-postman-generator");
function alfrescoRequestPostProcessor(postmanRequest, swaggerSpec) {
if (postmanRequest.url.includes("/authentication/versions/1/tickets")) {
// add a test post login event to save the ticket to an environment variable
postmanRequest.events = [
{
"listen": "test",
"script": {
"id": "57da8de9-78e9-4796-a310-358c73e74667",
"type": "text/javascript",
"exec": [
"var CryptoJS = require('crypto-js')",
"",
"pm.environment.set('ticket', CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(pm.response.json().entry.id)))",
""
]
}
}
]
}
}
let apis = ["alfresco-auth", "alfresco-core", "alfresco-search"]
apis.forEach(api => {
let alfrescoApi = Swagger2Postman.convertSwagger().fromUrl(`https://api-explorer.alfresco.com/api-explorer/definitions/${api}.json`)
alfrescoApi.toPostmanCollectionFile(`${api}_postman_collection.json`, {
globalHeaders: [
"Authorization: Basic {{ticket}}"
],
requestPostProcessor: alfrescoRequestPostProcessor
})
alfrescoApi.toPostmanEnvironmentFile(`${api}_postman_environment.json`, {
environment: {
name: `${api} Environment`,
customVariables: [{
name: "ticket"
}]
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment