Last active
January 18, 2020 21:53
-
-
Save hendrixroa/2709dec0affb1fa52550a6877ec228e1 to your computer and use it in GitHub Desktop.
Script to pass swagger to postman.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs = require('fs'); | |
import minimist = require('minimist'); | |
import request = require('request'); | |
const args: any = minimist(process.argv); | |
import { jsonc } from 'jsonc'; | |
import * as swagger2Postman2Parser from 'swagger2-postman2-parser'; | |
const POSTMAN_API_URL: string = 'https://api.getpostman.com'; | |
export class SwaggerPostman { | |
public doRequest(options: any) { | |
return new Promise((resolve: any, reject: any) => { | |
request(options, (error: any, res: any, body: any) => { | |
if (!error) { | |
resolve({ res, body }); | |
} | |
reject(error); | |
}); | |
}); | |
} | |
public async existCollection(key: string, collectionName: string) { | |
const responseCollections: any = await this.doRequest({ | |
headers: { | |
'X-Api-Key': key, | |
}, | |
method: 'GET', | |
uri: `${POSTMAN_API_URL}/collections`, | |
}); | |
const data: any = JSON.parse(responseCollections.body); | |
const existCollection: any = data.collections.find((collection: any) => { | |
return collection.name === collectionName; | |
}); | |
return existCollection; | |
} | |
public async addPostmanIntegration() { | |
const app = args.app; | |
const swaggerPath: string = `your/path/swagger.json`; | |
const fileContent: any = jsonc.parse(fs.readFileSync(swaggerPath, 'utf8')); | |
fileContent.host = args.host; | |
fileContent.basePath = `/${args.basePath}`; | |
fileContent.schemes = ['https']; | |
const dataCollection: any = swagger2Postman2Parser.convert(fileContent); | |
const dataCompress: string = jsonc.uglify( | |
jsonc.stringify(dataCollection.collection), | |
); | |
const existApi: any = await this.existCollection( | |
args.apiKey, | |
fileContent.info.title, | |
); | |
let paramsPostman: any = { | |
headers: { | |
'X-Api-Key': args.apiKey, | |
}, | |
json: { | |
collection: JSON.parse(dataCompress), | |
}, | |
method: 'POST', | |
uri: `${POSTMAN_API_URL}/collections`, | |
}; | |
if (existApi) { | |
paramsPostman = { | |
...paramsPostman, | |
method: 'put', | |
uri: `${POSTMAN_API_URL}/collections/${existApi.uid}`, | |
}; | |
} | |
return new Promise(async (resolve: any, reject: any) => { | |
try { | |
const result: any = await this.doRequest(paramsPostman); | |
resolve(result.body); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
} | |
} | |
const swPm: SwaggerPostman = new SwaggerPostman(); | |
swPm | |
.addPostmanIntegration() | |
// tslint:disable-next-line: no-console | |
.then((data: any) => console.log('Collection Updated successfully: ', data)) | |
.catch((err: any) => { | |
// tslint:disable-next-line: no-console | |
console.error('Error to update Collection: ', err); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment