Created
January 7, 2024 16:22
-
-
Save amustaque97/485ce66248a5301b1841fdda5e6562c4 to your computer and use it in GitHub Desktop.
create-postman-collection.js
This file contains 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
const axios = require("axios"); | |
const Converter = require("openapi-to-postmanv2"); | |
require("dotenv").config(); | |
module.exports = function createCollection(key, value) { | |
// Load the Swagger JSON file | |
const swaggerUrl = `${value}-json`; | |
axios | |
.get(swaggerUrl) | |
.then((response) => { | |
const swaggerJson = response.data; | |
// Put JSON content `swaggerJson` | |
Converter.convert({ type: "json", data: swaggerJson }, {}, (err, res) => { | |
axios | |
.post( | |
"https://api.getpostman.com/collections", | |
{ collection: res.output[0].data }, | |
{ | |
headers: { | |
"X-API-Key": process.env.POSTMAN_TOKEN, | |
"Content-Type": "application/json", | |
}, | |
} | |
) | |
.then((response) => { | |
const collectionId = response.data.collection.uid; | |
console.log( | |
`[*] Service: ${key} collection created successfully:`, | |
collectionId | |
); | |
}) | |
.catch((error) => { | |
console.error("[x] Failed to create Postman collection:", error); | |
}); | |
}); | |
}) | |
.catch((error) => { | |
console.error("[x] Failed to load Swagger JSON:", error); | |
}); | |
}; |
This file contains 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
const axios = require("axios"); | |
require("dotenv").config(); | |
module.exports = function deleteCollection() { | |
console.log("[*] Removing existing collections"); | |
return new Promise((resolve, reject) => { | |
// get all collections | |
axios | |
.get("https://api.getpostman.com/collections", { | |
headers: { | |
"X-API-Key": process.env.POSTMAN_TOKEN, | |
"Content-Type": "application/json", | |
}, | |
}) | |
.then((response) => { | |
const list = response.data.collections; | |
for (const c of list) { | |
// delete collection | |
axios | |
.delete("https://api.getpostman.com/collections/" + c.id, { | |
headers: { | |
"X-API-Key": process.env.POSTMAN_TOKEN, | |
"Content-Type": "application/json", | |
}, | |
}) | |
.then((response) => { | |
console.log("[*] Collection removed successfully", c.id, c.name); | |
return resolve(); | |
}) | |
.catch((error) => { | |
console.error( | |
"[x] Unable to remove collection", | |
c.id, | |
c.name, | |
error | |
); | |
return reject(error); | |
}); | |
} | |
}) | |
.catch((error) => { | |
console.error(`[x] Unable to get collections`, error); | |
return reject(error); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment