1.0.0 • Published 5 years ago

memoized-fetch v1.0.0

Weekly downloads
14
License
MIT
Repository
-
Last release
5 years ago

memoizedFetch Build Status

Wrapper for window.fetch that caches JSON responses using memoization. The function accepts the same arguments as fetch with an added third argument to enable cache busting. If the third argument is omitted, subsequent calls with the same arguments will serve the JSON response from memory or sessionStorage.

JSON only

memoizedFetch is intended for use with endpoints returning JSON data. Unlike fetch, it doesn't require calling the Response.json() method on the response. It handles that step interally.

Install

npm install memoized-fetch

OR

yarn add memoized-fetch

Examples

import memoizedFetch from 'memoized-fetch';

(async () => {
    let json;

    json = await memoizedFetch('http://mysite.com/data.json'. {}); // From web request.
    json = await memoizedFetch('http://mysite.com/data.json'. {}); // From cache.

    json = await memoizedFetch('http://mysite.com/data.json'. {}, 'some-string'); // From web request.
    json = await memoizedFetch('http://mysite.com/data.json'. {}, 'some-string'); // From cache.

    json = await memoizedFetch('http://mysite.com/data.json'. {}, 1); // From web request.
    json = await memoizedFetch('http://mysite.com/data.json'. {}, 1); // From cache.

    json = await memoizedFetch('http://mysite.com/data.json'. {}, null); // From web request.
    json = await memoizedFetch('http://mysite.com/data.json'. {}, null); // From cache.

    json = await memoizedFetch('http://mysite.com/data.json'. {}, 1); // From web request.
    json = await memoizedFetch('http://mysite.com/data.json'. {}, 1); // From cache.
})()

Accepted memo types

The third argument passed to memoizedFetch must be a string, a number, a boolean, a date, null, or undefined. An error is thrown if a value of any other type is passed.

1.0.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.0

5 years ago