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

@jswork/composite-fetch

v1.0.16

Published

A minimal fetch wrapper with middleware support and customizable behavior.

Downloads

0

Readme

composite-fetch

A minimal fetch wrapper with middleware support and customizable behavior.

version license size download

installation

yarn add @jswork/composite-fetch

mmd

graph LR
    subgraph 请求阶段
        M1R[中间件1.request] --> M2R[中间件2.request] --> M3R[中间件3.request]
    end

    subgraph Fetch请求
        F[执行实际fetch请求]
    end

    subgraph 响应阶段
        M1P[中间件1.response] --> M2P[中间件2.response] --> M3P[中间件3.response]
    end

    请求阶段 --> Fetch请求
    Fetch请求 --> 响应阶段

    style 请求阶段 fill:#f9f,stroke:#333,stroke-width:2px
    style Fetch请求 fill:#bbf,stroke:#333,stroke-width:2px
    style 响应阶段 fill:#bfb,stroke:#333,stroke-width:2px

usage

import compositeFetch from '@jswork/composite-fetch';

// 第一个中间件:错误处理
const errorMiddleware = {
  request: async (ctx) => {
    console.log('1. errorMiddleware.request');
    // 第一个执行的 request 钩子
    ctx.options.headers = {
      ...ctx.options.headers,
      'Accept': 'application/json'
    };
  },
  response: async (ctx) => {
    console.log('5. errorMiddleware.response');
    // 第一个执行的 response 钩子
    if (!ctx.response.ok) {
      throw new Error(`HTTP Error: ${ctx.response.status}`);
    }
  }
};

// 第二个中间件:认证信息
const authMiddleware = {
  request: async (ctx) => {
    console.log('2. authMiddleware.request');
    // 第二个执行的 request 钩子
    ctx.options.headers = {
      ...ctx.options.headers,
      'Authorization': 'Bearer your-token-here'
    };
  },
  response: async (ctx) => {
    console.log('6. authMiddleware.response');
  }
};

// 第三个中间件:JSON 响应处理
const jsonMiddleware = {
  request: async (ctx) => {
    console.log('3. jsonMiddleware.request');
  },
  response: async (ctx) => {
    console.log('7. jsonMiddleware.response');
    // 第二个执行的 response 钩子
    const response = ctx.response;
    const contentType = response.headers.get('Content-Type');

    if (contentType?.includes('application/json')) {
      const data = await response.json();
      ctx.extra = { data };
    }
  }
};

// 使用示例
try {
  console.log('开始执行中间件链:');
  // 中间件按照数组顺序依次执行
  // 1. errorMiddleware.request
  // 2. authMiddleware.request
  // 3. jsonMiddleware.request (如果有)
  // 4. 发起实际的 fetch 请求
  // 5. errorMiddleware.response
  // 6. authMiddleware.response (如果有)
  // 7. jsonMiddleware.response
  const response = await compositeFetch(
    'https://httpbin.org/get',
    {
      method: 'GET'
    },
    [errorMiddleware, authMiddleware, jsonMiddleware]
  );
  console.log('Response data:', response);
} catch (error) {
  console.error('Request failed:', error);
}

middleware execution order

The middleware execution follows a sequential order:

  1. Request hooks are executed in array order
  2. Actual fetch request is made
  3. Response hooks are executed in array order

For example, with [errorMiddleware, authMiddleware, jsonMiddleware]:

  1. errorMiddleware.request
  2. authMiddleware.request
  3. jsonMiddleware.request
  4. Actual fetch request
  5. errorMiddleware.response
  6. authMiddleware.response
  7. jsonMiddleware.response

middleware signature

type Middleware = {
  request?: (ctx: Context) => void | Promise<void>;
  response?: (ctx: Context) => void | Promise<void>;
};

interface Context {
  url: string;
  options?: RequestInit;
  extra?: any;
  response: Response | null;
  error: Error | null;
}

license

Code released under the MIT license.