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

listpage-http

v0.0.320

Published

基于原生 fetch 的配置驱动 HTTP 客户端,用于统一处理 JSON 请求与 SSE 流(浏览器与 Node 18+)。

Readme

listpage-http

基于浏览器原生 fetch 的配置驱动 HTTP 客户端,用于统一处理 JSON 请求与 SSE 流。

核心能力

  • 配置驱动:通过集中式 requestConfig 描述所有接口(method / path / 默认超时 / 缓存 / 是否 SSE)。
  • 类型安全:使用 defineEndpoint<Req, Res>() 明确每个接口的请求 / 响应类型。
  • 统一错误模型:约定后端返回 { code, message, data },内部根据 successCodes / unauthorizedCodes 进行解析。
  • 可选超时与缓存:仅在接口级或调用级显式配置时才启用超时 / 缓存逻辑。
  • SSE 支持:基于 fetch + ReadableStream,统一由 coreSseRequest 封装建连与错误处理。

主要导出

  • 类型:
    • ClientOptions, RequestOptions
    • EndpointConfig, EndpointMode, ApiEnvelope
  • 配置工具:
    • defineEndpoint
  • 客户端:
    • createApiClient, ApiClient
  • Core 层:
    • coreRequest, coreSseRequest
  • 缓存工具:
    • getCache, setCache, invalidateCache, invalidateByPrefix, buildDefaultCacheKey
  • 错误类型:
    • ApiError, RequestTimeoutException

快速上手

  1. 定义 requestConfig:
import { defineEndpoint } from "listpage-http";

export const requestConfig = {
  auth: {
    login: defineEndpoint<{ username: string; password: string }, { token: string }>({
      method: "POST",
      path: "/auth/login",
      authRequired: false,
      defaultOptions: {
        timeout: 10_000,
      },
    }),
  },
} as const;
  1. 初始化客户端:
import { createApiClient } from "listpage-http";
import { requestConfig } from "./request-config";

export const api = createApiClient(requestConfig, {
  baseURL: import.meta.env.DEV ? "http://localhost:3000/api" : "/api",
  tokenKey: "__REQUEST_TOKEN__",
  defaultTimeout: 10_000,
  defaultCacheTime: 5_000,
  successCodes: [0],
  unauthorizedCodes: [401, 403],
  unauthorizedRedirectPath: "/login",
  onError: (code, message) => {
    console.error("API Error:", code, message);
  },
});
  1. 业务中调用:
const res = await api.auth.login(
  { username, password },
  {
    timeout: 5_000,
  }
);
  1. SSE 示例:
const stream = await api.agent.generateCode(
  { id },
  {
    timeout: 3_000, // 建连超时
  }
);