Skip to content

Commit

Permalink
Merge pull request #18 from Lorenzohidalgo/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzohidalgo authored Aug 7, 2023
2 parents 51186d0 + f0ea43f commit 93c8b69
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const main = async ({
destDirPath,
collectionName,
rawUrl,
apiKey,
generatePostmanOnly = 'false',
useVariables = 'false',
maxDepth = 10,
encoding = 'utf-8',
Expand All @@ -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,
};
};
29 changes: 26 additions & 3 deletions src/io/saveAsPostman.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions src/standAlone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 93c8b69

Please sign in to comment.