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

req-hook

v1.0.4

Published

Easily intercept `fetch` or `XMLHttpRequest` in your project or Tampermonkey.

Downloads

33

Readme

req-hook

npm

Easily intercept fetch and XMLHttpRequest in your project or Tampermonkey.

Install

npm install req-hook

Usage

ES Module

import { init, add } from 'req-hook';

init();

add({
  url: 'api.example.com', // string or RegExp
  onBeforeRequest: ({ url, request }) => {
    // Modify request headers
    const headers = new Headers(request.headers);
    headers.set('Authorization', 'Bearer token');
    return new Request(request, { headers });
  },
  onAfterResponse: async ({ url, response }) => {
    const data = await response.json();
    data.modified = true;
    return new Response(JSON.stringify(data), {
      status: response.status,
      headers: response.headers
    });
  }
});

Script Tag (CDN)

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/req-hook.iife.js"></script>
<script>
  reqHook.init();

  reqHook.add({
    url: 'api.example.com', // string or RegExp
    onBeforeRequest: ({ url, request }) => {
      const headers = new Headers(request.headers);
      headers.set('X-Custom-Header', 'value');
      return new Request(request, { headers });
    },
    onAfterResponse: async ({ url, response }) => {
      const data = await response.json();
      data.modified = true;
      return new Response(JSON.stringify(data), {
        status: response.status,
        headers: response.headers
      });
    }
  });
</script>

Tampermonkey

// ==UserScript==
// @name         My ReqHook Script
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/req-hook.iife.js
// @match        *://api.example.com/*
// ==/UserScript==

(function() {
    reqHook.init();
    reqHook.add({
        url: 'api.example.com',
        onBeforeRequest: ({ url, request }) => request,
        onAfterResponse: ({ url, response }) => response
    });
})();

API

init(options?)

Initialize req-hook. Must be called before adding rules.

init();                          // defaults
init({ mode: 'local' });         // use window.fetch/XHR directly
init({ mode: 'iframe' });         // get native fetch/XHR from iframe (for Tampermonkey)
init({ log: { init: true } });   // enable init logging

| Option | Type | Default | Description | |--------|------|---------|-------------| | mode | 'local' \| 'iframe' | 'local' | 'iframe' mode gets native fetch/XHR from a hidden iframe, useful when other scripts have already overridden them | | log.init | boolean | true | Log initialization messages | | log.blocked | boolean | true | Log when something tries to override fetch/XHR | | log.request | boolean | true | Log intercepted requests | | log.response | boolean | true | Log intercepted responses |

add({ url, onBeforeRequest?, onAfterResponse? })

Add a rule to intercept matching requests.

add({
  url: 'api.example.com',        // string or RegExp
  onBeforeRequest: ({ url, request }) => {
    // Modify request before it goes out
    return request;              // return modified request or undefined
  },
  onAfterResponse: ({ url, request, response }) => {
    // Modify response before it reaches your code
    return response;             // return modified response or undefined
  }
});

| Parameter | Type | Description | |-----------|------|-------------| | url | string \| RegExp | URL pattern to match requests | | onBeforeRequest | (data) => Request \| XHRRequest \| undefined | Called before request is sent. Return modified request to change it. | | onAfterResponse | (data) => Response \| XHRResponse \| undefined | Called after response is received. Return modified response to change it. |

remove(url)

Remove a rule by its URL pattern.

remove('api.example.com');       // string
remove(/api\.example\.com/);      // RegExp