npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@newaifree-tool/request-fetch-imp

v2.1.0

Published

基于fetch的Requestor实现

Readme

@newaifree-tool/request-fetch-imp

request-fetch-imp 是基于原生 fetch 的 Requestor 实现包。

它适合不想引入 axios、或者运行环境更偏向标准 fetch API 的项目。

安装

pnpm add @newaifree-tool/request-fetch-imp @newaifree-tool/request-core

基础用法

import { FetchRequestor } from "@newaifree-tool/request-fetch-imp";

const requestor = new FetchRequestor({
  baseURL: "https://api.example.com",
  timeout: 10_000,
  withCredentials: true,
});

const response = await requestor.get<User[]>("/users", {
  params: { page: 1, pageSize: 20 },
});

console.log(response.data);

POST 请求

const response = await requestor.post<User>(
  "/users",
  {
    name: "Tom",
    age: 18,
  },
);

普通对象会自动序列化为 JSON,并设置 Content-Type: application/json

FormDataBlobURLSearchParams 会直接传给 fetch,不会被 JSON 序列化。

请求参数

params 会被拼接到 URL 查询字符串:

await requestor.get("/users", {
  params: {
    keyword: "tom",
    roles: ["admin", "user"],
  },
});

最终请求类似:

/users?keyword=tom&roles=admin&roles=user

添加拦截器

requestor.interceptors.request.use((config) => {
  config.headers = {
    ...config.headers,
    Authorization: `Bearer ${accessToken}`,
  };

  return config;
});

requestor.interceptors.response.use((response) => {
  return response;
});

requestor.interceptors.error.use((error) => {
  throw error;
});

拦截器应该注册在 FetchRequestor 实例上。createXxxRequestor 包装器只负责缓存、重试、幂等等能力。

超时和取消

FetchRequestor 内部使用 AbortController 实现超时控制。

const controller = new AbortController();

const promise = requestor.get("/users", {
  signal: controller.signal,
  timeout: 5_000,
});

controller.abort();

await promise;

非 2xx 响应

fetch 原生不会因为 4xx/5xx 自动抛错,但 FetchRequestor 会把非 2xx 响应转换为错误,并进入错误拦截器。

requestor.interceptors.error.use((error) => {
  const status = error?.response?.status;

  if (status === 401) {
    // 处理登录过期。
  }

  throw error;
});

和 request-core 能力组合

import {
  createRetryRequestor,
  createCacheRequestor,
  createIdempotentRequestor,
} from "@newaifree-tool/request-core";
import { FetchRequestor } from "@newaifree-tool/request-fetch-imp";

const baseRequestor = new FetchRequestor({
  baseURL: "https://api.example.com",
});

let requestor = baseRequestor;
requestor = createRetryRequestor(requestor, { maxCount: 3 });
requestor = createCacheRequestor(requestor, { duration: 30_000 });
requestor = createIdempotentRequestor(requestor, { mode: "concurrent" });

和 request-bus 组合

import { createBus, defineEndpoint } from "@newaifree-tool/request-bus";
import { FetchRequestor } from "@newaifree-tool/request-fetch-imp";

interface User {
  id: number;
  name: string;
}

const requestor = new FetchRequestor({
  baseURL: "https://api.example.com",
});

const bus = createBus({ requestor });

const userApi = bus.createApiService({
  listUsers: defineEndpoint<User[]>({
    path: "/users",
    method: "GET",
    cache: true,
  }),
});

const users = await userApi.listUsers();

适用场景

  • 不想引入 axios。
  • 浏览器或 Node 18+ 环境可直接使用 fetch。
  • 希望用统一 Requestor 契约接入 request-corerequest-bus