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

simple-fetch-cache

v2.0.0

Published

A simple fetch module using node-fetch, offering some basic caching functionality.

Downloads

53

Readme

Simple Fetch Cache

This module performs fetch requests using the node-fetch package, offering some basic caching functionality.

The motivation behind it is to have a simple but functional module to avoid hitting the live servers more than needed / allowed (ie. using an API with a quota)

Simple fetch cache calls the requested url and cache the results in memory using its own Map Object.

The reply is cached only if the server issue a 200 status code http response. Also the reply from the module indicates if the request comes from the cache:

// First request

> const {fetch} = require('simple-fetch-cache')
> const wikiprefix = 'https://en.wikipedia.org/w/api.php?action=query&format=json'
>
> fetch(`${wikiprefix}&prop=coordinates&utf8=&titles=dublin`).then(r => console.log(r))
> { reply:
   { batchcomplete: '',
     query: { normalized: [Object], pages: [Object] } },
  headers:
   { date: [ 'Sat, 29 Dec 2018 20:27:44 GMT' ],
     'content-type': [ 'application/json; charset=utf-8' ],
     server: [ 'mw1286.eqiad.wmnet' ] },
  status: 200,
  cached: false }

// Next request
> fetch(`${wikiprefix}&prop=coordinates&utf8=&titles=dublin`).then(r => console.log(r))
> { reply:
   { batchcomplete: '',
     query: { normalized: [Object], pages: [Object] } },
  headers:
   { date: [ 'Sat, 29 Dec 2018 20:27:44 GMT' ],
     'content-type': [ 'application/json; charset=utf-8' ],
     server: [ 'mw1286.eqiad.wmnet' ] },
  status: 200,
  cached: true }

The module only has an external node-fetch dependency to fetch the requested urls and Jest as dev dependency to run the tests.

Install

You can install with [npm]:

$ npm install --save simple-fetch-cache

Usage

The module provides two main functions: fetch to get the required url and cache it if possible and fetchFresh to get a fresh copy of the url and refresh the cache. So following up with the previous example

// First request

> const {fetch, fetchFresh} = require('simple-fetch-cache')
> const wikiprefix = 'https://en.wikipedia.org/w/api.php?action=query&format=json'
>
> fetch(`${wikiprefix}&prop=coordinates&utf8=&titles=dublin`).then(r => console.log(r))
> { reply:
   { batchcomplete: '',
     query: { normalized: [Object], pages: [Object] } },
  headers:
   { date: [ 'Sat, 29 Dec 2018 20:27:44 GMT' ],
     'content-type': [ 'application/json; charset=utf-8' ],
     server: [ 'mw1286.eqiad.wmnet' ] },
  status: 200,
  cached: false }

// Next request
> fetch(`${wikiprefix}&prop=coordinates&utf8=&titles=dublin`).then(r => console.log(r))
> { reply:
   { batchcomplete: '',
     query: { normalized: [Object], pages: [Object] } },
  headers:
   { date: [ 'Sat, 29 Dec 2018 20:27:44 GMT' ],
     'content-type': [ 'application/json; charset=utf-8' ],
     server: [ 'mw1286.eqiad.wmnet' ] },
  status: 200,
  cached: true }

// Fresh request

> fetchFresh(`${wikiprefix}&prop=coordinates&utf8=&titles=dublin`).then(r => console.log(r))
> { reply:
   { batchcomplete: '',
     query: { normalized: [Object], pages: [Object] } },
  headers:
   { date: [ 'Sat, 29 Dec 2018 20:38:49 GMT' ],
     'content-type': [ 'application/json; charset=utf-8' ],
     server: [ 'mw1342.eqiad.wmnet' ] },
  status: 200,
  cached: false }

Response format

As exposed in the previous example, the module returns in the response an object with the following 4 parameters:

| Parameter | Description | | ------------- |:-------------:| | reply | Message body from the server, converted to JSON if the mime-type is application/json, String if the type is text/html. Otherwise it will return a Buffer so you can further deal with it (ie. pipe image to a file)| | headers | Abridged version of the headers, including date, server, content-type & content-length | | status | Status code returned from the server (ie. 200, 404, 500 or 418) | | cached | Boolean from simple-fetch-cache indicating if the request is cached (true or false) |

Time to live (TTL)

All the requests cached in memory will persist there as long as the process in running. Nevertheless if you require the cache to expire after X miliseconds, you can pass it as a second parameter to the fetch function:

// Cache will expire after 1 hour.

> const {fetch} = require('simple-fetch-cache')
> const wikiprefix = 'https://en.wikipedia.org/w/api.php?action=query&format=json'
> const onehourMsec = 3600000
> fetch(`${wikiprefix}&prop=coordinates&utf8=&titles=dublin`, onehourMsec).then(r => console.log(r))

License

Copyright © 2018, Juan Convers. Released under the MIT License.