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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fetch-impl/fetcher

v1.0.0

Published

User Configurable Fetch Implementation.

Downloads

2,067

Readme

User Configurable Fetch Implementation

Can be used as a global fetch implementation or as a local fetch implementation specific to a package.

Adapters

Usage

The default fetch implementation is globalThis.fetch. Which works in almost all modern environments (browsers, Node.js, Deno, Cloudflare Workers, etc).

import fetch from "@fetch-impl/fetcher";

// use fetch as usual
fetch("https://example.com").then(async (res) => {
    console.log(res.status, await res.text());
});

You can replace the default fetch implementation with a custom one using fetcher.set:

import fetch, { fetcher } from "@fetch-impl/fetcher";

// configure fetch to use a custom fetch implementation
fetcher.set(async (...args) => {
    return new Response("Hello, World!", { status: 200 });
});

// use fetch as usual, but it will use the custom fetch implementation and always return "Hello, World!"
fetch("https://example.com").then(async (res) => {
    console.log(res.status, await res.text());
});

If you are a package author and want to let your users configure the fetch implementation, you can use Fetcher to create a configurable local fetch implementation:

// add a fetch.ts file like this to your package
import { Fetcher } from "@fetch-impl/fetcher";

export const fetcher = new Fetcher();
export const fetch = (...args) => fetcher.fetch(...args);
export default fetch;
// and use your own fetch implementation like this
import fetch from "./fetch";

// use fetch as usual
export async function makeRequest(url: string): Promise<string> {
    const res = await fetch(url);
    return res.text();
}

// don't forget to re-export the fetcher instance so that your users can configure the fetch implementation
export * from "./fetch";

Your users can then configure the fetch implementation like this:

import { makeRequest, fetcher } from "your-package";
import { useCrossFetch } from "@fetch-impl/cross-fetch";

// user configure fetch to use a custom fetch implementation
useCrossFetch(fetcher);

// user use makeRequest as usual, but it will use the custom fetch implementation
makeRequest("https://example.com").then((text) => {
    console.log(text);
});