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

@o2h/http

v0.3.2

Published

web http client

Readme

o2h/http

基于 fetch 的浏览器 http 客户端

技术细节

Backend

抽象并包装默认的请求和响应对象,以适配不同运行环境和网络请求库。该层旨在统一处理各种请求实例(例如,浏览器原生 fetch、Node.js 环境下的 fetch 兼容库等),从而屏蔽底层网络实现的差异。适配过程中,会根据运行环境自动选择或切换到合适的请求实现,以保证请求行为的一致性。

当前已实现对浏览器原生 fetch(即 browser fetch) API 的适配,确保在浏览器环境下使用原生 fetch 发起请求时行为符合预期。

Interceptor

拦截器是一种设计模式,用于在请求和响应的生命周期中插入自定义逻辑,从而实现对请求和响应的拦截和处理。

关于拦截器中抛出的异常

具体代码在h2o-http\src\interceptor\index.ts L46-L57 拦截器执行过程中抛出的异常会被捕获并统一处理为HttpResult类型返回给下一个interceptor处理。 注意此时HttpResult的error属性将被设置为InterceptorError,且response属性将被置空。 拦截器抛出的异常会被捕获并统一处理为HttpResult类型返回。

Parser

处理请求和响应的数据解析(调研是否需要拆分为 requestParser 和 responseParser)

  • JSONParser - JSON 数据解析
  • BlobParser - Blob 数据解析

HttpClient

HttpClient 是一个 HTTP 客户端包装类,提供具体的请求方法供用户调用。 其内部包含以下核心组件:

  • Interceptor
  • Backend
  • Parser (待实装)

目前支持实例配置和触发配置 配置优先级如下

  • 触发配置
  • 实例配置

使用方法

安装

npm install @o2h/http
pnpm add @o2h/http
yarn add @o2h/http

基本使用

import { createFetchHttpClient } from "@o2h/http";

// 创建拦截器函数
const handleError: HttpInterceptorFn = async (request, next) => {
  // before request send
  const res = await next(request);
  // after request send
  return res;
};

// 创建基于fetch的http实例
const httpClient = createFetchHttpClient({
  // 添加拦截器
  interceptors: [handleError],
});

// 动态添加拦截器
httpClient.useInterceptor(handleError);

// 发送请求
const res = await httpClient.get("https://www.baidu.com");

// 获取响应数据
const data = res.data;

// 获取错误信息
const error = res.error;

发起GET请求

import { createFetchHttpClient } from "@o2h/http";
const httpClient = createFetchHttpClient();
const res = await httpClient.get("https://www.google.com");

How to test

基于 vitest 实现基础单元测试