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

@bwrong/request

v1.0.1

Published

基于 axios 二次封装的请求库

Downloads

12

Readme

@bwrong/request

npm npm

基于 axios 二次封装的请求库。

  1. 多实例,在存在多个请求服务时使用
  2. 拦截器增强,支持全局拦截器和实例拦截器,执行顺序采用洋葱模型
  3. 支持重复请求取消功能,重复请求:url、method、参数均一致为重复,也可以自己传入判断函数
  4. 支持直接请求绝对地址
  5. ts 类型推断增强,更好的类型提示

安装

npm install @bwrong/request
// 或
yarn add @bwrong/request
// 或
pnpm install @bwrong/request

基本使用

// 全局响应类型声明
export type ResponseType = {
  code: number;
  msg: string;
  data: {
    current: number;
    list: any[];
    size: number;
    total: number;
  };
  [key: string]: any;
};
const request = new Request<ResponseType>({
  baseURL:'/api',
  timeout: 10 * 1000,
  interceptors,
  transforms,
   // 是否取消重复请求
  cancelDuplicateRequest: false,
  // 拦截器设置
  interceptors: {
    requestInterceptor(config) {
      return config;
    },
    responseInterceptor({ data, config }) {
      return Promise.resolve(data);
    },
    responseInterceptorCatch(error) {
      return Promise.reject(error);
    }
  }
  // 更多axios支持的配置
});
export const get = request.get.bind(request);
export const post = request.post.bind(request);
export const put = request.put.bind(request);
export const patch = request.patch.bind(request);
export const del = request.delete.bind(request);

类型说明

// 拦截器
interface RequestInterceptors<DATA = any, RES = any> {
  // 请求拦截
  requestInterceptor?: (config: InternalAxiosRequestConfig<DATA>) => InternalAxiosRequestConfig<DATA>;
  requestInterceptorCatch?: (err: any) => any;
  requestInterceptorOption?: AxiosInterceptorOptions;
  // 响应拦截
  responseInterceptor?: <T extends RES = RES>(response: AxiosResponse<T, DATA>) => any;
  responseInterceptorCatch?: (err: AxiosError<RES>) => any;
  responseInterceptorOption?: AxiosInterceptorOptions;
}
// 自定义传入的参数
export interface RequestConfig<DATA = any> extends AxiosRequestConfig<DATA> {
  /** 开启取消重复请求功能:
   * 可以在实例配置,也可以单独在接口开启
   */
  cancelDuplicateRequest?: boolean;
  /**
   * 设置在多长时间内不允许发送重复请求
   * 可以在实例配置,也可以单独在接口开启
   */
  cancelPendingTime?: number;
  /** 指定当前请求的key,
   * 如果传递该参数,则会覆盖generateRequestKey生成的key
   * */
  requestUniqueKey?: string;
}
// 请求配置
interface InstanceRequestConfig<DATA = any, RES = any> extends Omit<RequestConfig<DATA>, 'requestUniqueKey'> {
  /**
   * 拦截器配置:
   * 请求拦截及配置:requestInterceptor、requestInterceptorCatch、requestInterceptorOption
   * 相应拦截及配置:responseInterceptor、responseInterceptorCatch、responseInterceptorOption
   */
  interceptors?: RequestInterceptors<DATA, RES>;
  /**
   * 创建请求创建唯一标识,重复请求用此作区分, 默认基于url、method、参数生成
   * 在实例设置时
   * @param requestConfig
   * @returns
   */
  generateRequestKey?: (config: AxiosRequestConfig) => string;
}

方法

  • static setInterceptors<DATA = any, RES = any>(interceptors: RequestInterceptors<DATA, RES>) 静态方法,设置全局拦截器

  • new Request(config: InstanceRequestConfig<D, R>) 实例化

  • getInstance() 获取axios实例

  • getInterceptors() 获取配置的拦截器,包含全局和实例

  • removeInterceptors(interceptorId: number) 移除拦截器

  • clearInterceptors(type: 'request' | 'response' | 'all' = 'all') 清除拦截器

  • setHeader(headers: Partial<AxiosRequestHeaders>) 用来改变request默认的header配置,不要在拦截器中使用

  • cancelAllRequest() 取消全部请求

  • cancelRequest(config: RequestConfig) 取消特定请求

  • request<RES = R, DATA = D>(config: RequestConfig<DATA>) 请求

  • get<RES = R, DATA = D>(url: string, params: any = {}, config: RequestConfig<DATA> = {})

  • post<RES = R, DATA = D>(url: string, params: any = {}, config: RequestConfig<DATA> = {})

  • put<RES = R, DATA = D>(url: string, params: any = {}, config: RequestConfig<DATA> = {})

  • patch<RES = R, DATA = D>(url: string, params: any = {}, config: RequestConfig<DATA> = {})

  • delete<RES = R, DATA = D>(url: string, params: any = {}, config: RequestConfig<DATA> = {}) 一些快捷方法