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

request-cached

v1.0.1

Published

This library adds to request library the ability to cache data retrieved from a main server into a local server.

Downloads

200

Readme

Request cached

Build Status Coverage Status NPM Version

This library adds to request library the ability to cache data retrieved from a main server into a local server.

So, when you are requesting data for the first time, it will be retrieved from the main server and saved locally. Afterwards whenever you do the request for a second time, the data is retrieved locally.

Useful when scrapping.

Index

Install

Install request-cached with npm.

npm install request-cached --save

Use

In order to use request-cached you need to provide the request object you want to use.

const request = require('request'),
    requestCached = require('request-cached')(request);

return requestCached(params)
  .then((result) => {
    // Do something with the result
  })
  .catch((error) => {
    // Do something with the error
  });

Different request objects for main and cache requests

The request object can be any other request-compatible object like throttled-request. You can even provide a different object for the cache and for the main requests, as you could probably have some limitations for the requests to the main server which you don't for the local server.

const cacheRequest = require('request'),
    mainRequest = require('throttled-request')(cacheRequest),
    requestCached = require('request-cached')(mainRequest, cacheRequest);

mainRequest.configure({
  requests: 2,
  milliseconds: 1000
});

return requestCached(params)
  .then((result) => {
    // Do something with the result
  })
  .catch((error) => {
    // Do something with the error
  });

Params

  • main: Compulsory object where are set the params to access online data
  • cache: Optional object where are set the params to access cached data
  • save: Optional object where are set the params to save online data
    • path: Path to save data to
    • parseFn: Function used to modify data before saving it
  • request: Optional object where are set the additional params to access online and cached data
    • All possible request params
    • customErrorFn: Custom function to determine when a response is considered an error. By default:
    customErrorFn: (error, response, body) => error

Result parameter if resolved

Object with the following keys:

  • response: Second argument of a request callback
  • body: Third argument of a request callback
  • isCached: A boolean indicating whether the data is retrieved from cache or not.

Error parameter if rejected

It is the first argument of a request callback

Example

const request = require('request'),
    requestCached = require('request-cached')(request);

function getParams(userId) {
  return {
    main: {
      uri: 'http://real/data/domain/mainUrl',
      qs: {
        id: userId
      }
    },
    cache: {
      uri: `http://localhost/cachedUrl/${userId}`
    },
    save: {
      path: `/var/www/cachedUrl/${userId}`
    },
    request: {
      proxy: 'http://localhost:8080',
      encoding: 'utf8'
    }
  };
}

var userId = 12345;
return requestCached(getParams(userId))
  .then((result) => {
    console.log(result.body);
  })
  .catch((error) => {
    console.error(error);
  });

Code testing

If you wanna test the code, follow these steps:

> git clone https://github.com/germanrio/request-cached.git
> cd request-cached
> npm install
> npm test

Note: Of course is also needed to have properly installed node and npm.

Code coverage

If you also want to see the code coverage of tests:

> npm run cov

It will generate a coverage folder with the coverage report.

License

MIT

More details in the LICENSE file in root folder.