Comparing version 1.1.0-beta.1 to 1.1.0-beta.2
80
cli.ts
import { program } from "commander"; | ||
import picocolors from "picocolors"; | ||
import fetchToCurl from "fetch-to-curl"; | ||
import { getStory, serialize, visit } from "./lib/functions"; | ||
import { findStory, run, serialize, visit } from "./lib/functions"; | ||
program.name("fetchbook"); | ||
program | ||
.command("run") | ||
.description("run a story file") | ||
.argument("<story>", "story file path") | ||
.name("fetchbook") | ||
.description("Manage your HTTP requests") | ||
.argument("[story]", "story file path") | ||
.option("-v, --verbose", "verbose") | ||
.option("-d, --dry-run", "dry run") | ||
.action(async (storyFilePath, options) => | ||
visit(await getStory(storyFilePath), async (request, story) => { | ||
let response: Response | undefined; | ||
if (!options.dryRun) { | ||
response = await fetch(request.clone()); | ||
} | ||
console.log( | ||
picocolors.green("✓"), | ||
story.name, | ||
response?.status ?? picocolors.yellow("Dry run"), | ||
.option("-c, --curl", "convert to curl") | ||
.action(async (storyFilePath, options) => { | ||
const story = await findStory(storyFilePath); | ||
if (options.curl) { | ||
visit(story, async (request) => | ||
console.log( | ||
fetchToCurl({ | ||
url: request.url, | ||
method: request.method, | ||
headers: request.headers, | ||
body: await serialize(await request.blob()), | ||
}) | ||
) | ||
); | ||
if (options.verbose) { | ||
console.log( | ||
await serialize({ | ||
request: { | ||
url: request.url, | ||
method: request.method, | ||
headers: request.headers.count > 0 ? request.headers : undefined, | ||
body: await serialize(await request.blob()), | ||
}, | ||
response: response | ||
? { | ||
status: response.status, | ||
headers: | ||
response.headers.count > 0 ? response.headers : undefined, | ||
body: await serialize(await response.blob()), | ||
} | ||
: undefined, | ||
}), | ||
); | ||
} | ||
}), | ||
); | ||
program | ||
.command("curl") | ||
.description("convert a story file to CURL") | ||
.argument("<story>", "story file path") | ||
.action(async (storyFilePath) => | ||
visit(await getStory(storyFilePath), async (request) => | ||
console.log( | ||
fetchToCurl({ | ||
url: request.url, | ||
method: request.method, | ||
headers: request.headers, | ||
body: await serialize(await request.blob()), | ||
}), | ||
), | ||
), | ||
); | ||
program.parse(); | ||
} else { | ||
run(story, options); | ||
} | ||
}) | ||
.parse(); |
import colorizer from "json-colorizer"; | ||
import { RequestStory } from ".."; | ||
import path from "path"; | ||
import picocolors from "picocolors"; | ||
import select from "@inquirer/select"; | ||
import { glob } from "glob"; | ||
export const getStory = (storyFilePath: string) => | ||
import(path.resolve(storyFilePath)).then( | ||
(mod) => mod.default as RequestStory, | ||
(mod) => mod.default as RequestStory | ||
); | ||
export const findStory = async (storyFilePath?: string) => { | ||
if (storyFilePath) { | ||
return getStory(storyFilePath); | ||
} else { | ||
const storyFiles = await glob(`${process.cwd()}/**/*.fetch.ts`); | ||
if (storyFiles.length === 0) { | ||
console.log("No story files (*.fetch.ts) found"); | ||
process.exit(0); | ||
} | ||
const stories = await Promise.all(storyFiles.map(getStory)); | ||
return select({ | ||
message: "Select a fetch story", | ||
choices: stories.map((story) => ({ | ||
name: story.name, | ||
value: story, | ||
description: story.url, | ||
})), | ||
}); | ||
} | ||
}; | ||
export const visit = async ( | ||
story: RequestStory, | ||
visitor: (request: Request, story: RequestStory) => Promise<void>, | ||
visitor: (request: Request, story: RequestStory) => Promise<void> | ||
) => { | ||
@@ -31,1 +55,38 @@ for (const beforeStory of story.before ?? []) { | ||
: undefined; | ||
export const run = async ( | ||
story: RequestStory, | ||
options: { dryRun?: boolean; verbose?: boolean } | ||
) => { | ||
visit(story, async (request, story) => { | ||
let response: Response | undefined; | ||
if (!options.dryRun) { | ||
response = await fetch(request.clone()); | ||
} | ||
console.log( | ||
picocolors.green("✓"), | ||
story.name, | ||
response?.status ?? picocolors.yellow("Dry run") | ||
); | ||
if (options.verbose) { | ||
console.log( | ||
await serialize({ | ||
request: { | ||
url: request.url, | ||
method: request.method, | ||
headers: request.headers.count > 0 ? request.headers : undefined, | ||
body: await serialize(await request.blob()), | ||
}, | ||
response: response | ||
? { | ||
status: response.status, | ||
headers: | ||
response.headers.count > 0 ? response.headers : undefined, | ||
body: await serialize(await response.blob()), | ||
} | ||
: undefined, | ||
}) | ||
); | ||
} | ||
}); | ||
}; |
{ | ||
"name": "fetchbook", | ||
"version": "1.1.0-beta.1", | ||
"version": "1.1.0-beta.2", | ||
"description": "Manage your HTTP requests", | ||
@@ -8,5 +8,7 @@ "author": "Alejandro Tardín <alejandro@tardin.com>", | ||
"dependencies": { | ||
"bun": "^1.0.3", | ||
"@inquirer/select": "^1.3.0", | ||
"bun": "^1.0.4", | ||
"commander": "^11.0.0", | ||
"fetch-to-curl": "^0.6.0", | ||
"glob": "^10.3.10", | ||
"json-colorizer": "^2.2.2", | ||
@@ -27,3 +29,3 @@ "picocolors": "^1.0.0" | ||
"test": "start-test test:start 3000 test:run", | ||
"test:run": "npx fetchbook run test/story.ts -v", | ||
"test:run": "npx fetchbook test/test.fetch.ts -v", | ||
"test:start": "json-server --quiet --host :: test/db.json", | ||
@@ -30,0 +32,0 @@ "semantic-release": "semantic-release" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7045
197
7
+ Added@inquirer/select@^1.3.0
+ Addedglob@^10.3.10
+ Added@inquirer/core@6.0.0(transitive)
+ Added@inquirer/select@1.3.3(transitive)
+ Added@inquirer/type@1.5.5(transitive)
+ Added@isaacs/cliui@8.0.2(transitive)
+ Added@pkgjs/parseargs@0.11.0(transitive)
+ Added@types/mute-stream@0.0.4(transitive)
+ Added@types/node@20.17.23(transitive)
+ Added@types/wrap-ansi@3.0.0(transitive)
+ Addedansi-escapes@4.3.2(transitive)
+ Addedansi-regex@5.0.16.1.0(transitive)
+ Addedansi-styles@4.3.06.2.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcli-spinners@2.9.2(transitive)
+ Addedcli-width@4.1.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addedeastasianwidth@0.2.0(transitive)
+ Addedemoji-regex@8.0.09.2.2(transitive)
+ Addedfigures@3.2.0(transitive)
+ Addedforeground-child@3.3.1(transitive)
+ Addedglob@10.4.5(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjackspeak@3.4.3(transitive)
+ Addedlru-cache@10.4.3(transitive)
+ Addedminimatch@9.0.5(transitive)
+ Addedminipass@7.1.2(transitive)
+ Addedmute-stream@1.0.0(transitive)
+ Addedpackage-json-from-dist@1.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-scurry@1.11.1(transitive)
+ Addedrun-async@3.0.0(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedstring-width@4.2.35.1.2(transitive)
+ Addedstrip-ansi@6.0.17.1.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedtype-fest@0.21.3(transitive)
+ Addedundici-types@6.19.8(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrap-ansi@6.2.07.0.08.1.0(transitive)
Updatedbun@^1.0.4