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-axios-imp

v2.1.0

Published

基于axios的Requestor实现

Readme

@newaifree-tool/request-axios-imp

request-axios-imp 是基于 axios 的 Requestor 实现包。

它负责把 request-core 的统一请求契约转换成 axios 请求,并把 axios 响应转换成统一的 Response

安装

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

基础用法

import { AxiosRequestor } from "@newaifree-tool/request-axios-imp";

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

const response = await requestor.get<User>("/users/:id", {
  params: { id: 1 },
});

console.log(response.data);

AxiosRequestor 返回的是统一的 Response<T>

interface Response<T> {
  data: T;
  status: number;
  statusText: string;
  headers: Record<string, string>;
  config: RequestConfig;
  toPlain(): {
    data: T;
    status: number;
    statusText: string;
    headers: Record<string, string>;
  };
}

添加请求拦截器

通用 token、traceId、租户 ID 等应通过基础请求器拦截器处理。

requestor.interceptors.request.use((config) => {
  config.headers = {
    ...config.headers,
    Authorization: `Bearer ${accessToken}`,
    "X-Trace-Id": crypto.randomUUID(),
  };

  return config;
});

添加响应拦截器

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

如果公司接口有统一响应结构,例如 { code, data, message },可以在这里做统一校验或解包。

添加错误拦截器

requestor.interceptors.error.use((error) => {
  if (error?.response?.status === 401) {
    // 这里可以做刷新 token、跳转登录等逻辑。
  }

  throw error;
});

和 request-core 能力组合

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

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

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

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

注意:拦截器注册在 baseRequestor 上,不注册在 createXxxRequestor 返回的包装器上。

和 request-bus 组合

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

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

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

const bus = createBus({ requestor });

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

const user = await userApi.getUser({ id: 1 });

baseURL 属于 AxiosRequestor,不属于 request-bus

适用场景

  • 项目已经使用 axios。
  • 需要 axios 的取消请求、进度回调、请求配置能力。
  • 需要和 request-corerequest-bus 组合使用。