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

@wuipkg/request

v2.2.3

Published

基于 `axios` 环境扩展的网络请求类基元应用。具有面向对象深层解耦特性,实现了多后端与前置配置结合的分阶段控制,支持局部代理及无感知Loading状态托管。

Downloads

269

Readme

@wuipkg/request

基于 axios 环境扩展的网络请求类基元应用。具有面向对象深层解耦特性,实现了多后端与前置配置结合的分阶段控制,支持局部代理及无感知Loading状态托管。

安装

pnpm install @wuipkg/request

API 详细说明与完整使用示例

初始化参数设立 (CreateRequestConfig)

实例化 new Request(config) 初始化类所需的配置定义:

  • appEnv: 必须 ('dev'|'test'|'prod'),环境标识 (dev 时配合下置指令可代理)。
  • baseURL / localProxyPrefix: 基址(默认为本地服务路由或 /local 代发)。
  • successStatus / tokenExpiredStatus: 指定映射返回的 JSON 对象态势判断字典。可以配置成如 { code: [200, 0] }
  • defaultMessageField: 获取后端提示报错文本的字段名提取口(如 msg)。
  • onShowLoading / onHideLoading / onShowErrorMessage: 由外部 UI 或组件包负责接管实现的全局拦截阻截通知系统。

业务使用示例 (声明和配置)

通常在独立的文件内进行基础对象 Request 的架构和拦截器总线注入:

// request.ts
import Request from '@wuipkg/request'
import { Message, Loading } from 'my-ui-library' // 换成具体的项目UI库例如 Element 或 base

// 1. 下发实例化网络桥接基座
const requestObject = new Request({
  appEnv: import.meta.env.MODE,
  baseURL: import.meta.env.VITE_API_URL,
  successStatus: { code: [0, 200] },
  tokenExpiredStatus: { code: [401] },
  defaultMessageField: 'msg',
  // 加载 Loading 全局挂载总系统钩子(自带请求去重控制)
  onShowLoading: ({ loadingText }) => {
     Loading.show(loadingText) 
  },
  onHideLoading: ({ inProgressReq }) => {
     // 由于内部有并发控制,等没有请求在了即关销
     if (inProgressReq <= 0) Loading.hide()
  },
  // 服务层统一的弹框抛错,只要不阻止弹出的阻绝点全部会被它消化
  onShowErrorMessage: ({ message, buttonText }, response) => {
     Message.error(message || '未知网络系统错误')
  }
})

// 2. 将特殊拦截逻辑注册推入 (例如往 Request Header 写入凭证 Token)
requestObject.registerRequestInterceptor((config) => {
  const token = localStorage.getItem('APP_TOKEN')
  if (token) {
    config.headers = config.headers || {}
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

// 3. 处理过期凭证并触发登出路由
requestObject.registerTokenExpiredInterceptor((response) => {
  Message.error('您的系统登录态已过期失效,请重新登权')
  // ... Router push 到 Login ...
})

// 4. 对外导出这颗包装完善带骨血的基础底层 axios instance 实例即可
export default requestObject.getInstance()

发起请求特性延伸定义 (RequestConfig)

通过上侧导出的对象就可以发起各种被扩展特性包围的独立调用:

  • localProxy: true 表示忽略所在生产环境强制切换到本地发信代理头(处理联调)。
  • hideLoading: true 表示单条摘除屏蔽并隐藏全局 Loading 的闪现阻断影响。
  • hideAlert: 遇到验证失败、解析中断态势主动屏蔽全屏 Message。
  • ignoreStatus: StatusMap 配置局部白名单,使这笔调用不抛走 fail 的异常回调而顺利返回。
  • retryTimes / retryInterval: 定义无缝自动二次重试容错(常用于容易断线的上传接口或弱网络环境重发)。

单次业务接口调用示例:

import request from './request.ts'

// 发起常规获取
export const fetchUserData = (userId) => {
  return request.get('/api/user/info', {
    params: { id: userId },
    // 完全隐藏弹窗提示,使得异常可以由我们手工在 Catch 处私有消化处理
    hideAlert: true,
    // 隐藏由于网络过慢引发的全局 Loading 遮罩
    hideLoading: true, 
  })
}

// 附带重试及特殊忽略状态的高管请求
export const checkHealthTask = () => {
  return request.get('/api/task/status', {
    retryTimes: 3,         // 自动错误补发 3 次
    retryInterval: 2000,   // 每次失败 2秒 后再试
    // 自定特例白名单(就算后端吐回 status 为 510 也当它请求畅通,在 then 里接管返回值)
    ignoreStatus: { code: [510] } 
  })
}