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

fetchetera

v1.0.9

Published

node-fetch wrapper with cache

Downloads

12

Readme

Fetchetera

NPM

Build Status Maintainability Test Coverage

Warp node-fetch adding cache support.

fetchetera doesn't implement the cache storage itself because there are plenty of them on npm.

The following example uses the well known lru-cache package.

Example:

import fetchetera from 'fetchetera';
import LRU from 'lru-cache';

// prepare the fetch function initializing the cache
const fetch = fetchetera(new LRU({max: 100}));

// use like regular fetch
const res = await fetch('https://swapi.co/api/people/1/}');
console.log('response: ', await res.json());

Universal usage

For a universal use of fetchetera, simply assing the initilized fetch to global.fetch object. Make sure to do so only on the server.

...
const fetch = fetchetera(new LRU({max: 100}));
global.fetch = fetch;

Using with a distribuited cache

This example uses another package Keyv that supports Redis as storage.

import fetchetera from 'fetchetera';
import Keyv from 'keyv';
import '@keyv/redis';

// prepare the fetch function initializing the cache
const fetch = fetchetera(new Keyv<CacheEntry>(`redis://localhost:6379`));

// use like regular fetch
const res = await fetch('https://swapi.co/api/people/1/}');
console.log('response: ', await res.json());

To try it, you need redis running on localhost:

docker run --rm --name my-redis -p 6379:6379 -d redis

See caching in action

Debugging or just watch cache happening is a bit tricky because caching is transparent to the user. You don't know if a cached response is being used or if the origin returned a 304 for a revalidation.

Instead of putting console.log here and there, you can setup a reverse proxy to inspect the communication with the origin and then address the requests to the proxy.

For example the following command redirect all traffic from port 1337 to 3000 port and dumps all the communications:

docker run --rm -it -p 1337:1337 mitmproxy/mitmproxy mitmdump -v -p 1337 -m reverse:http://host.docker.internal:3000