Skip to content

Commit

Permalink
Add support for Postman Authorization via API key
Browse files Browse the repository at this point in the history
  • Loading branch information
sl4wa committed Jul 31, 2024
1 parent ce3cd07 commit e99a717
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 145 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ previous versions if needed.

### CLI Options

- Custom output filename: `--out=FILNAME`
- Headers: `-H="header:value"`, can be used multiple times.
- Custom output filename: `--out=FILENAME`
- Headers: `-H="header: value"`, can be used multiple times.
- Global Postman collection authorization header: `--AuthHeader="header: value"` or `-A="header: value"`
- Get help: `--help` or `-h`

### Examples
Expand Down
4 changes: 1 addition & 3 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"ci": "deno fmt --check && deno lint"
},
"fmt": {
"files": {
"exclude": ["out"]
}
"exclude": ["out"]
}
}
106 changes: 3 additions & 103 deletions deno.lock

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

33 changes: 27 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@ import { createPostmanCollection } from "./index.ts";
function help() {
console.log(`Error: not enough arguments.
Usage:
deno run index.ts <GRAPHQL_ENDPOINT_URL>
deno run index.ts <GRAPHQL_ENDPOINT_URL>
Options:
--out=OUTPUT_FILE Output file path
-H="header: value" Header to add to the request, the flag can be used multiple times.
--AuthHeader="header: value", -A="header: value" Global header for Postman collection authorization.
Help:
deno run index.ts [--help | -h]
`);
}

const args = parse(Deno.args, { boolean: ["help", "h"], collect: ["H"] }) as {
const args = parse(Deno.args, {
boolean: ["help", "h"],
collect: ["H"],
string: ["AuthHeader", "A"],
alias: {
AuthHeader: "A",
},
}) as {
_: [string];
help?: boolean;
h?: boolean;
out?: string;
H?: [string];
AuthHeader?: string;
A?: string;
};

if (Deno.args.length < 1 || args.help || args.h) {
Expand All @@ -35,19 +45,30 @@ if (Deno.args.length < 1 || args.help || args.h) {

const url = args._[0];
let path = args.out;
const headers = parseHeaders(args.H);

const headers = parseHeaders(args.H || []);

// Handle the AuthHeader separately
const authHeader = (args.AuthHeader ?? args.A)
? (args.AuthHeader ?? args.A)!.split(": ", 2) as [string, string]
: undefined;

if (authHeader) {
headers.push(authHeader); // Add AuthHeader to headers for API access
}

const urlRegexp = /https?:\/\/*/;
if (!urlRegexp.test(url)) {
console.error(`${url} is not a valid url`);
Deno.exit(1);
}

console.log(`Creating the postman collection for ${url}`);
console.log(`Creating the Postman collection for ${url}`);

const { postmanCollection } = await createPostmanCollection(
const postmanCollection = await createPostmanCollection(
url,
headers,
authHeader,
);

path = path ||
Expand All @@ -62,4 +83,4 @@ try {
saveJsonFormatted(postmanCollection, path);
console.log(`Collection saved at ${path}`);

console.log(`Import it in postman and complete the queries ! 🚀`);
console.log(`Import it in Postman and complete the queries! 🚀`);
Loading

0 comments on commit e99a717

Please sign in to comment.