|
// mostly copy pasted from https://blog.postman.com/sync-your-specs/ but changed for openapi.yaml files vs json files |
|
|
|
const fs = require("fs"); |
|
const Converter = require("openapi-to-postmanv2"); |
|
const request = require('request'); |
|
const config = require('./config.js'); |
|
|
|
|
|
|
|
// ############################################### |
|
// Get collection ids from here https://api.getpostman.com/collections |
|
|
|
|
|
// ############################################### |
|
// define functions to handle conversion and update collection |
|
function handleConversion(originalFileName, newFileName) { |
|
var openapiData = fs.readFileSync(originalFileName, {encoding: "UTF8"}); |
|
Converter.convert({ |
|
type: "string", |
|
data: openapiData |
|
}, {}, (err, conversionResult) => { |
|
if (!conversionResult.result) { |
|
console.log("Could not convert", conversionResult.reason); |
|
} else { |
|
fs.writeFileSync(newFileName, JSON.stringify(conversionResult.output[0].data, null, 2), function (err) { |
|
if (err) { |
|
return console.error(err); |
|
} |
|
console.log('Converted ' + originalFileName + ' to ' + newFileName); |
|
}); |
|
} |
|
}); |
|
} |
|
|
|
function updatePostman(newFileName, collection_uid) { |
|
// read the updated local file and update the Postman collection using Postman API |
|
var data = fs.readFileSync('./' + newFileName, 'utf8'); |
|
// Postman users can get a Postman API key here: https://app.getpostman.com/dashboard/integrations |
|
var postmanAPIKey = config.key; |
|
// compile PUT request to update the Postman collection |
|
var putOptions = { |
|
method: 'PUT', |
|
url: 'https://api.getpostman.com/collections/' + collection_uid, |
|
qs: { |
|
format: '2.1.0' |
|
}, |
|
headers: { |
|
'Postman-Token': 'fake-for-now', |
|
'Cache-Control': 'no-cache', |
|
'X-Api-Key': postmanAPIKey, |
|
'Content-Type': 'application/json' |
|
}, |
|
body: JSON.parse(data), |
|
json: true |
|
}; |
|
// submit PUT request to update the Postman collection |
|
request(putOptions, function (error, response, body) { |
|
if (error) throw new Error(error); |
|
console.log(body); |
|
}); |
|
} |
|
function updateLocalCollection(newFileName, newFile) { |
|
// update the local Postman collection file |
|
fs.writeFileSync('./' + newFileName, JSON.stringify(newFile, null, 2)); |
|
console.log('writing to ' + newFileName); |
|
|
|
} |
|
function updateCollection(newFileName, collection) { |
|
// update the Postman collection locally and in the cloud |
|
var data = fs.readFileSync('./' + newFileName, 'utf8'); |
|
var file = JSON.parse(data); |
|
file.info.id = collection.collection_id; // update id according to config file |
|
file.info._postman_id = file.info.id; // add new property that is identical to id |
|
var newFile = {}; |
|
newFile.collection = file; // wrap JSON object in new "collection" property |
|
updateLocalCollection(newFileName, newFile); |
|
updatePostman(newFileName, collection.collection_uid); |
|
} |
|
|
|
// ############################################### |
|
// loop through collections in config file to call functions to handle conversion and update collection |
|
config.collections.forEach( function (collection) { |
|
var originalFileName = collection.from; |
|
var newFileName = collection.to ; |
|
handleConversion(originalFileName, newFileName); |
|
updateCollection(newFileName,collection); |
|
}); |