diff --git a/package-lock.json b/package-lock.json index f9537f2..0b2d255 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "commander": "^11.0.0", "graphql": "^16.7.1", - "postman-collection": "^4.1.7", + "postman-collection": "^4.2.0", "prettier": "^3.0.0" }, "bin": { @@ -11350,9 +11350,9 @@ } }, "node_modules/postman-collection": { - "version": "4.1.7", - "resolved": "https://registry.npmjs.org/postman-collection/-/postman-collection-4.1.7.tgz", - "integrity": "sha512-fMICmDa6megCH/jKq66MZVcR26wrSn1G/rjIkqrtdB6Df4u/I+XLRbWueQnz91Jwm3FR+su1refy4gwIjLLGLg==", + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/postman-collection/-/postman-collection-4.2.0.tgz", + "integrity": "sha512-tvOLgN1h6Kab6dt43PmBoV5kYO/YUta3x0C2QqfmbzmHZe47VTpZ/+gIkGlbNhjKNPUUub5X6ehxYKoaTYdy1w==", "dependencies": { "@faker-js/faker": "5.5.3", "file-type": "3.9.0", @@ -11363,7 +11363,7 @@ "mime-format": "2.0.1", "mime-types": "2.1.35", "postman-url-encoder": "3.0.5", - "semver": "7.3.8", + "semver": "7.5.4", "uuid": "8.3.2" }, "engines": { @@ -11382,9 +11382,9 @@ } }, "node_modules/postman-collection/node_modules/semver": { - "version": "7.3.8", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", - "integrity": "sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", "dependencies": { "lru-cache": "^6.0.0" }, diff --git a/package.json b/package.json index 39b2d50..5331cd1 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "dependencies": { "commander": "^11.0.0", "graphql": "^16.7.1", - "postman-collection": "^4.1.7", + "postman-collection": "^4.2.0", "prettier": "^3.0.0" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index 8ccd52b..c4595ec 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,8 @@ const main = async ({ destDirPath, collectionName, rawUrl, + apiKey, + generatePostmanOnly = 'false', useVariables = 'false', maxDepth = 10, encoding = 'utf-8', @@ -15,11 +17,17 @@ const main = async ({ const parsedSchema = await parseSchema(gqlSchema, JSON.parse(useVariables), maxDepth); - saveAsFiles(destDirPath, parsedSchema); + if (!JSON.parse(generatePostmanOnly)) saveAsFiles(destDirPath, parsedSchema); - saveAsPostman(destDirPath, parsedSchema, collectionName, rawUrl); + if (JSON.parse(generatePostmanOnly) && (!collectionName || !rawUrl)) + console.warn( + '--generatePostmanOnly was set to true but no valid --collectionName or --rawUrl where provided', + ); + + if (collectionName && rawUrl) + saveAsPostman(destDirPath, parsedSchema, collectionName, rawUrl, apiKey); }; module.exports = { main, -}; \ No newline at end of file +}; diff --git a/src/io/saveAsPostman.js b/src/io/saveAsPostman.js index 0379884..30bb752 100644 --- a/src/io/saveAsPostman.js +++ b/src/io/saveAsPostman.js @@ -1,4 +1,4 @@ -const { Collection, ItemGroup, Item, Url } = require('postman-collection'); +const { Collection, ItemGroup, Item, Url, RequestAuth } = require('postman-collection'); const { mkdirSync, writeFileSync } = require('fs'); const { resolve, join } = require('path'); @@ -22,19 +22,42 @@ const buildItemList = (operations, rawUrl) => }), ); -const saveAsPostman = (destinationDirectory, parsedSchema, collectionName, rawUrl) => { +const saveAsPostman = (destinationDirectory, parsedSchema, collectionName, rawUrl, apiKey) => { if (!parsedSchema.mutations && !parsedSchema.queries && !parsedSchema.subscriptions) { // eslint-disable-next-line no-console console.error('No operations found to be saved'); return; } - + const newCollection = new Collection({ info: { name: collectionName, }, }); + if (apiKey?.length > 0) { + newCollection.auth = new RequestAuth({ + type: 'apikey', + apikey: [ + { + key: 'value', + value: apiKey, + type: 'string', + }, + { + key: 'key', + value: 'x-api-key', + type: 'string', + }, + { + key: 'in', + value: 'header', + type: 'string', + }, + ], + }); + } + if (parsedSchema.mutations) { newCollection.items.add( new ItemGroup({ diff --git a/src/standAlone.js b/src/standAlone.js index 7e1a111..35dcbfa 100644 --- a/src/standAlone.js +++ b/src/standAlone.js @@ -10,6 +10,8 @@ program ) .option('--collectionName [value]', 'Name to use name the Postman Collection') .option('--rawUrl [value]', 'GraphQL API endpoint URL, used to generate the Postman Collection') + .option('--apiKey [value]', 'API Key, will be used to generate a x-api-key header') + .option('--generatePostmanOnly [value]', 'Defines if only the postman collection should be generated', 'false') .option('--maxDepth [value]', 'query depth you want to limit (The default is 10)', 10) .option('--useVariables [value]', 'Defines if variables should be used while generating the requests', 'true') .option('--encoding [value]', 'The encoding to use while reading the files (The default is utf-8)', 'utf-8')