This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
node-fetch-cache/README.md

45 lines
1.4 KiB
Markdown
Raw Normal View History

2020-04-17 20:56:05 +00:00
# node-fetch-cache
node-fetch with caching to a directory on disk.
2020-04-17 21:09:24 +00:00
The first usage with any given arguments will result in an HTTP request and any subsequent usage with the same arguments and body function (text, json, buffer, or textConverted) will read the response body from the cache on disk.
2020-04-17 20:56:05 +00:00
## Usage
2020-04-17 21:09:24 +00:00
Require it with a directory path to cache in, and then use it the same way you would use fetch.
2020-04-17 20:56:05 +00:00
```js
const fetch = require('node-fetch-cache')('./path/to/cache/dir');
fetch('http://google.com')
.then(response => response.text())
.then(text => console.log(text));
```
## API
2020-04-17 21:09:24 +00:00
Note that this does not support the full fetch API. Headers and some other things are not accessible.
2020-04-17 20:56:05 +00:00
### async fetch(resource [, init])
2020-04-17 21:02:32 +00:00
Same arguments as [browser fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch).
2020-04-17 20:56:05 +00:00
Returns a **CachedResponse**.
### async CachedResponse.text()
Returns the body as a string.
### async CachedResponse.json()
Returns the body as a JavaScript object, parsed from JSON.
### async CachedResponse.buffer()
Returns the body as a Buffer.
### async CachedResponse.textConverted()
Identical to CachedResponse.text(), except instead of always converting to UTF-8, encoding sniffing will be performed and text converted to UTF-8, if possible.
2020-04-17 21:09:24 +00:00
(textConverted requires an optional dependency on [npm package encoding](https://www.npmjs.com/package/encoding), which you need to install manually.