update readme
This commit is contained in:
parent
95973cb2ce
commit
14061ba617
36
README.md
36
README.md
@ -8,32 +8,25 @@ By default responses are cached in memory, but you can also cache to files on di
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Require it with a directory path to cache in, and then use it the same way you would use fetch.
|
Require it and use it the same way you would use node-fetch:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const fetch = require('node-fetch-cache')('./path/to/cache/dir');
|
const fetch = require('node-fetch-cache');
|
||||||
|
|
||||||
fetch('http://google.com')
|
fetch('http://google.com')
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(text => console.log(text));
|
.then(text => console.log(text));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The next time you `fetch('http://google.com')`, the response will be returned from the cache. No HTTP request will be made.
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
This module aims to expose the same API as `node-fetch` does for the most common use cases, but may not support some of the less common use cases.
|
This module aims to expose the same API as `node-fetch` does for the most common use cases, but may not support some of the less common functions and use cases.
|
||||||
|
|
||||||
### const fetch = require('node-fetch-cache')('./path/to/cache/dir')
|
### const fetch = require('node-fetch-cache');
|
||||||
|
|
||||||
Load the module and specify the directory to cache responses in.
|
Load the module.
|
||||||
|
|
||||||
If this syntax looks at all foreign to you, you can do this instead:
|
|
||||||
|
|
||||||
```js
|
|
||||||
const createNodeFetchCache = require('node-fetch-cache');
|
|
||||||
const fetch = createNodeFetchCache('./path/to/cache/dir');
|
|
||||||
```
|
|
||||||
|
|
||||||
Same thing.
|
|
||||||
|
|
||||||
### async fetch(resource [, init])
|
### async fetch(resource [, init])
|
||||||
|
|
||||||
@ -121,8 +114,8 @@ This is the default cache delegate. It caches responses in-process in a POJO.
|
|||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const createNodeFetchCache, { MemoryCache } = require('node-fetch-cache');
|
const fetchBuilder, { MemoryCache } = require('node-fetch-cache');
|
||||||
const fetch = createNodeFetchCache(new MemoryCache(options));
|
const fetch = fetchBuilder.withCache(new MemoryCache(options));
|
||||||
```
|
```
|
||||||
|
|
||||||
Supported options:
|
Supported options:
|
||||||
@ -136,13 +129,13 @@ Supported options:
|
|||||||
|
|
||||||
### FileSystemCache
|
### FileSystemCache
|
||||||
|
|
||||||
Cache to a directory on disk. This allows the cache to survive process restarts, reboots, etc.
|
Cache to a directory on disk. This allows the cache to survive the process exiting.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const createNodeFetchCache, { FileSystemCache } = require('node-fetch-cache');
|
const fetchBuilder, { FileSystemCache } = require('node-fetch-cache');
|
||||||
const fetch = createNodeFetchCache(new FileSystemCache(options));
|
const fetch = fetchBuilder.withCache(new FileSystemCache(options));
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -164,7 +157,7 @@ Both functions can be async.
|
|||||||
For example you could make and use your own simple memory cache like this:
|
For example you could make and use your own simple memory cache like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const createNodeFetchCache = require('node-fetch-cache');
|
const fetchBuilder = require('node-fetch-cache');
|
||||||
|
|
||||||
class MyMemoryCache {
|
class MyMemoryCache {
|
||||||
set(key, value) {
|
set(key, value) {
|
||||||
@ -176,7 +169,8 @@ class MyMemoryCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch = createNodeFetchCache(new MyMemoryCache());
|
fetch = fetchBuilder.withCache(new MyMemoryCache());
|
||||||
|
|
||||||
fetch('http://google.com')
|
fetch('http://google.com')
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(text => console.log(text));
|
.then(text => console.log(text));
|
||||||
|
Reference in New Issue
Block a user