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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@vexjs/http-request

v1.0.0

Published

前端通用http请求封装

Readme

HTTP Request Utilities

功能与参数说明

本项目封装的 HTTP 请求工具支持以下功能和参数:

  • method: HTTP 请求方法,支持 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH',默认是 'GET'
  • headers: 请求头信息,可自定义。
  • body: 请求体,适用于 'POST''PUT' 方法。
  • isStream: 是否是流式数据请求,默认 false
  • retryOnError: 是否在错误时重试请求,默认 false
  • maxRetries: 最大重试次数,默认 3
  • tokenErrorCode: Token 失效错误的状态码,默认 401
  • token: 授权令牌,可通过初始化或请求参数传递。
  • supportCancel: 是否支持请求取消,默认 false
  • showLoading: 显示加载指示器的回调函数,类型为 () => void,无参数,无返回值。
  • hideLoading: 隐藏加载指示器的回调函数,类型为 () => void,无参数,无返回值。
  • onProgress: 监控请求进度的回调函数,类型为 (progress: number) => void,参数为进度百分比(number),无返回值。
  • logRequest: 记录请求日志的回调函数,类型为 (endpoint: string, options: RequestInit) => void,参数为请求的端点(string)和请求选项(RequestInit),无返回值。
  • handleCancel: 处理请求取消的回调函数,类型为 (controller: AbortController) => void,参数为 AbortController 实例,无返回值。
  • handleError: 处理请求过程中发生的错误的回调函数,类型为 (error: Error) => void,参数为错误对象(Error),无返回值。
  • handleUnauthorized: 处理未授权错误的回调函数,类型为 () => void,无参数,无返回值。

HTTP Request Utilities

本项目包含三个主要的 HTTP 请求工具文件:fetch.tsuseFetch.tsuseReactFetch.ts。以下是它们的使用文档和示例。


fetch.ts

简介

fetch.ts 提供了一个基于类的封装,用于处理 HTTP 请求。它支持 GET、POST、PUT、DELETE 和 PATCH 请求,并提供了丰富的选项来定制请求行为。

使用方法

import FetchWrapper from "./request/fetch";

const api = new FetchWrapper("https://api.example.com", "your-initial-token");

// 发送 GET 请求
api.get("/endpoint").then((response) => {
  console.log(response.data);
});

// 发送 POST 请求
api.post("/endpoint", { key: "value" }).then((response) => {
  console.log(response.data);
});

useFetch.ts

简介

useFetch.ts 提供了一个基于函数的封装,用于处理 HTTP 请求。它支持类似于 fetch.ts 的功能,但更适合函数式编程风格。

使用方法

import useFetchWrapper from "./request/useFetch";

const { get, post, put, del, patch } = useFetchWrapper(
  "https://api.example.com",
  "your-initial-token"
);

// 发送 GET 请求
get("/endpoint").then((response) => {
  console.log(response.data);
});

// 发送 POST 请求
post("/endpoint", { key: "value" }).then((response) => {
  console.log(response.data);
});

useReactFetch.ts

简介

useReactFetch.ts 提供了一个基于 React 的自定义 Hook,用于处理 HTTP 请求。它支持状态管理和 React 的生命周期。

TIP:你需要先安装好react环境再使用

使用方法

import useFetchWrapper from "./request/useReactFetch";

function App() {
  const { get, post, put, del, patch, setToken } = useFetchWrapper(
    "https://api.example.com",
    "your-initial-token"
  );

  const fetchData = async () => {
    const response = await get("/endpoint");
    console.log(response.data);
  };

  return (
    <div>
      <button onClick={fetchData}>Fetch Data</button>
    </div>
  );
}

export default App;