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

iota-ts-fetch

v0.0.0

Published

一个基于 **axios** 的二次封装请求库,提供统一的 `get/post/delete/put/patch` 调用方式,并内置**请求队列**能力(可按 key 中断单个请求或中断全部请求)。支持自定义 axios 默认配置、请求/响应拦截器,以及可选的 loading 显示/隐藏钩子。

Downloads

125

Readme

fetch

一个基于 axios 的二次封装请求库,提供统一的 get/post/delete/put/patch 调用方式,并内置请求队列能力(可按 key 中断单个请求或中断全部请求)。支持自定义 axios 默认配置、请求/响应拦截器,以及可选的 loading 显示/隐藏钩子。

安装

npm i fetch axios

本项目本身依赖 axios,如果你在业务工程里已安装 axios,可按你的依赖管理策略处理(避免重复安装/版本冲突)。

快速开始

import { createFetch } from "fetch";

const fetch = createFetch({
  requestConfig: {
    baseURL: "https://api.example.com",
    headers: {
      "Content-Type": "application/json;charset=utf-8",
    },
  },
});

// GET:第二个参数会作为 query params
const list = await fetch.get("/items", { page: 1, pageSize: 10 });

// POST:第二个参数会作为 body data
const created = await fetch.post("/items", { name: "foo" });

API

createFetch(options)

创建一个包含 get/post/delete/put/patch 的请求对象。

参数 options

  • requestConfig: CreateAxiosDefaults
    • axios 实例默认配置(如 baseURLtimeoutheaders 等)
  • requestIntercept?: () => { onFulfilled, onRejected }
    • 请求拦截器工厂函数(内部会 instance.interceptors.request.use(...)
  • responseIntercept?: () => { onFulfilled, onRejected }
    • 响应拦截器工厂函数(内部会 instance.interceptors.response.use(...)
  • loading?: { show(): void; hide(): void }
    • 传入后可配合单次请求的 config.loading 控制显示/隐藏

返回值

返回 fetch 对象:

  • fetch.get(url, data?, config?)
  • fetch.delete(url, data?, config?)
  • fetch.post(url, data?, config?)
  • fetch.put(url, data?, config?)
  • fetch.patch(url, data?, config?)

其中:

  • url: string
  • data?: any
  • config?: AxiosRequestConfig & { loading?: boolean }

行为规则:

  • get/deletedata 会被放到 params
  • post/put/patchdata 会被放到 data
  • 返回值:默认 Promise<R>,内部 resolve(res.data)

拦截器示例

import type { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from "axios";
import { createFetch } from "fetch";

const fetch = createFetch({
  requestConfig: { baseURL: "https://api.example.com" },
  requestIntercept: () => ({
    onFulfilled: (config: InternalAxiosRequestConfig) => {
      config.headers.set("X-Token", "your-token");
      return config;
    },
    onRejected: (error: AxiosError) => Promise.reject(error),
  }),
  responseIntercept: () => ({
    onFulfilled: (resp: AxiosResponse) => resp,
    onRejected: (error: AxiosError) => Promise.reject(error),
  }),
});

Loading 示例

import { createFetch } from "fetch";

const loading = {
  show: () => console.log("loading show"),
  hide: () => console.log("loading hide"),
};

const fetch = createFetch({
  requestConfig: { baseURL: "https://api.example.com" },
  loading,
});

// 只有当 config.loading 为 true 才会触发 show/hide
await fetch.get("/items", { page: 1 }, { loading: true });

请求队列与取消请求

包对外导出 requestQueue,内部用 AbortController 管理请求。

import { requestQueue } from "fetch";

// 获取当前队列(Map<key, AbortController>)
const q = requestQueue.getQueue();

key 规则

key 默认由以下规则生成:

method + "-" + url + (data ? "-" + JSON.stringify(data) : "")

你也可以手动生成:

const key = requestQueue.createKey("get", "/items", { page: 1 });

中断单个请求

requestQueue.abortive(key);

中断所有请求

requestQueue.removeResAll();

同 key 自动取消旧请求(内置行为)

当发起新请求时,如果队列里已存在同一个 key,会先取消旧请求再登记新请求(避免相同请求并发造成的覆盖与浪费)。

注意:是否算“同一个请求”取决于 key(包含 method/url/data 的 JSON 字符串)。

开发与构建

# 构建产物到 lib/(CJS + ESM + d.ts)
npm run build

# 开发模式(watch + 本地静态服务 + livereload)
npm run start

构建输出(默认):

  • lib/bundle.cjs.js
  • lib/bundle.esm.js
  • lib/main.d.ts