Skip to content

Commit

Permalink
Add Support for Postman Authorization via API Key (#52)
Browse files Browse the repository at this point in the history
* Add support for Postman Authorization via API key

* fix: adjust auth header

* fix: pass auth header to introspection

* fix: allheaders

---------

Co-authored-by: nohehf <[email protected]>
  • Loading branch information
sl4wa and nohehf authored Aug 26, 2024
1 parent ce3cd07 commit 2759def
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 148 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ 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: 26 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { parseHeaders, saveJsonFormatted } from "./lib.ts";
import { parseHeader, parseHeaders, saveJsonFormatted } from "./lib.ts";
import {
ensureDirSync,
ensureFileSync,
Expand All @@ -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,28 @@ 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 authHeaderRaw = args.AuthHeader ?? args.A;
let authHeader: [string, string] | undefined;
if (authHeaderRaw) {
authHeader = parseHeader(authHeaderRaw);
}

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 +81,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 2759def

Please sign in to comment.