New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fetchbook

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetchbook - npm Package Compare versions

Comparing version 1.1.0-beta.1 to 1.1.0-beta.2

test/test.fetch.ts

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"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc