@fetch-impl/fetcher

中文文档
1.0.0发布于 2024/03/11MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2043

User Configurable Fetch Implementation.

原始使用文档

User Configurable Fetch Implementation

Can be used as a global fetch implementation or as a local fetch implementation specific to a package.

Adapters

Usage

The default fetch implementation is globalThis.fetch. Which works in almost all modern environments (browsers, Node.js, Deno, Cloudflare Workers, etc).

import fetch from "@fetch-impl/fetcher";

// use fetch as usual
fetch("https://example.com").then(async (res) => {
    console.log(res.status, await res.text());
});

You can replace the default fetch implementation with a custom one using fetcher.set:

import fetch, { fetcher } from "@fetch-impl/fetcher";

// configure fetch to use a custom fetch implementation
fetcher.set(async (...args) => {
    return new Response("Hello, World!", { status: 200 });
});

// use fetch as usual, but it will use the custom fetch implementation and always return "Hello, World!"
fetch("https://example.com").then(async (res) => {
    console.log(res.status, await res.text());
});

If you are a package author and want to let your users configure the fetch implementation, you can use Fetcher to create a configurable local fetch implementation:

// add a fetch.ts file like this to your package
import { Fetcher } from "@fetch-impl/fetcher";

export const fetcher = new Fetcher();
export const fetch = (...args) => fetcher.fetch(...args);
export default fetch;
// and use your own fetch implementation like this
import fetch from "./fetch";

// use fetch as usual
export async function makeRequest(url: string): Promise<string> {
    const res = await fetch(url);
    return res.text();
}

// don't forget to re-export the fetcher instance so that your users can configure the fetch implementation
export * from "./fetch";

Your users can then configure the fetch implementation like this:

import { makeRequest, fetcher } from "your-package";
import { useCrossFetch } from "@fetch-impl/cross-fetch";

// user configure fetch to use a custom fetch implementation
useCrossFetch(fetcher);

// user use makeRequest as usual, but it will use the custom fetch implementation
makeRequest("https://example.com").then((text) => {
    console.log(text);
});

版本与发布记录

@fetch-impl/fetcher 共发布 1 个版本,首次发布于 2024/03/11;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0latest2024/03/112 年前

关于 @fetch-impl/fetcher 的常见问题

@fetch-impl/fetcher 是什么?

User Configurable Fetch Implementation.

如何安装 @fetch-impl/fetcher?

在项目目录运行 npm install @fetch-impl/fetcher,或使用 pnpm add / yarn add / bun add。

@fetch-impl/fetcher 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fetch-impl/fetcher 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fetch-impl/fetcher 可以商用吗?

@fetch-impl/fetcher 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fetch-impl/fetcher 的下载量有多少?

根据 npm 官方下载统计,@fetch-impl/fetcher 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,043 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fetch-impl/fetcher 还在维护吗?

最新版本 1.0.0 发布于 2024/03/11(2 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/03/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。