-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
273 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ tsconfig.json | |
coverage | ||
*.lcov | ||
node_modules/ | ||
src/ | ||
src/ | ||
bin/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/// <reference path="../src/@types/core-js.d.ts" /> | ||
import yargs from "yargs"; | ||
import colors from "colors"; | ||
import { resolve } from "path"; | ||
import { readFileSync } from "fs"; | ||
import fetch, { Request, Headers } from "node-fetch"; | ||
import { signedFetchFactory, SignedRequestInit } from "../src"; | ||
import type { AuthIdentity } from "dcl-crypto"; | ||
|
||
const parser = yargs | ||
.help( | ||
"Fetch the contents of the URL and sign the request with and identity if there is any" | ||
) | ||
.option("method", { | ||
alias: "X", | ||
description: | ||
"Specifies a custom request method to use when communicating with the HTTP server.", | ||
default: "GET", | ||
choices: ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"], | ||
}) | ||
.options("include", { | ||
alias: "i", | ||
type: "boolean", | ||
description: | ||
"Include the HTTP response headers in the output. The HTTP response headers can include things like server name, cookies, date of the document, HTTP version and more...", | ||
}) | ||
.option("identity", { | ||
description: "Path to identity.json file", | ||
type: "string", | ||
}) | ||
.option("header", { | ||
alias: "H", | ||
type: "array", | ||
description: "Request header", | ||
}) | ||
.options("data", { | ||
alias: "d", | ||
type: "string", | ||
description: | ||
"Sends the specified data in a POST request to the HTTP server", | ||
}) | ||
.options("no-color", { | ||
description: "Disbled color output", | ||
}); | ||
|
||
const args = parser.parse(); | ||
|
||
// Disabled colors | ||
if (args["no-color"]) { | ||
colors.disable(); | ||
} | ||
|
||
// Print help | ||
if (args.h || args.help) { | ||
parser.showHelp(); | ||
process.exit(0); | ||
} | ||
|
||
// Parse URL | ||
const url = new URL(args._[0] as any); | ||
|
||
// Parse Identity | ||
const readOption = (value: string) => { | ||
if (value.startsWith("@")) { | ||
const identityPath = "./" + value.slice(1); | ||
const absolutePath = resolve(process.cwd(), identityPath); | ||
return readFileSync(absolutePath, "utf-8"); | ||
} else { | ||
return value; | ||
} | ||
}; | ||
|
||
let identity: AuthIdentity | undefined = undefined; | ||
if (args.identity) { | ||
const value = readOption(args.identity); | ||
try { | ||
identity = JSON.parse(value); | ||
} catch (err) { | ||
console.error(colors.red(`Unspected identity format: ` + err.message)); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
Promise.resolve() | ||
.then(async () => { | ||
const signedFetch = signedFetchFactory({ fetch, Request, Headers } as any); | ||
const init: SignedRequestInit = { identity }; | ||
|
||
if (args.method) { | ||
init.method = String(args.method || "GET").toLowerCase(); | ||
} | ||
|
||
if (args.header && args.header.length > 0) { | ||
const headers: globalThis.Headers = new Headers() as any; | ||
for (const h of args.header) { | ||
const header = String(h); | ||
const separator = header.indexOf(":"); | ||
const key = header.slice(0, separator).trim(); | ||
const value = header.slice(separator + 1).trim(); | ||
headers.append(key, value); | ||
} | ||
init.headers = headers; | ||
} | ||
|
||
if (args.data) { | ||
init.body = readOption(args.data); | ||
} | ||
|
||
const response = await signedFetch(url, init); | ||
|
||
// Print headers | ||
if (args.include) { | ||
console.log(response.status, colors.green(response.statusText)); | ||
response.headers.forEach((value, key) => { | ||
console.log(colors.blue(key.toLowerCase() + ":"), colors.green(value)); | ||
}); | ||
} | ||
|
||
const body = await response.text(); | ||
console.log(); | ||
console.log(body); | ||
}) | ||
.catch((err) => { | ||
console.error(colors.red(err.message)); | ||
if (err.stack) { | ||
err.stack | ||
.split("/n") | ||
.slice(0) | ||
.forEach((line: string) => { | ||
console.error(colors.green(line)); | ||
}); | ||
} | ||
process.exit(1); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.