This commit is contained in:
parent
73601127b2
commit
d5382860f3
@ -11,11 +11,12 @@ jobs:
|
||||
node-version: [20.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v1
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
registry-url: "https://registry.npmjs.org"
|
||||
- run: npm ci
|
||||
- run: npm run lint
|
||||
- run: npm run coverage
|
||||
|
43
README.md
43
README.md
@ -1,5 +1,7 @@
|
||||
# node-fetch-cache
|
||||
|
||||
Forked from: <https://github.com/mistval/node-fetch-cache>
|
||||
|
||||
node-fetch with caching of responses.
|
||||
|
||||
The first fetch with any given arguments will result in an HTTP request and any subsequent fetch with the same arguments will read the response from the cache.
|
||||
@ -11,11 +13,11 @@ By default responses are cached in memory, but you can also cache to files on di
|
||||
Require it and use it the same way you would use node-fetch:
|
||||
|
||||
```js
|
||||
import fetch from 'node-fetch-cache';
|
||||
import fetch from "node-fetch-cache";
|
||||
|
||||
fetch('http://google.com')
|
||||
.then(response => response.text())
|
||||
.then(text => console.log(text));
|
||||
fetch("http://google.com")
|
||||
.then((response) => response.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.
|
||||
@ -33,17 +35,18 @@ This function can be used to eject the response from the cache, so that the next
|
||||
This module caches ALL responses, even those with 4xx and 5xx response statuses. You can use this function to uncache such responses if desired. For example:
|
||||
|
||||
```js
|
||||
import fetch from 'node-fetch-cache';
|
||||
import fetch from "node-fetch-cache";
|
||||
|
||||
fetch('http://google.com')
|
||||
.then(async response => {
|
||||
fetch("http://google.com")
|
||||
.then(async (response) => {
|
||||
if (!response.ok) {
|
||||
await response.ejectFromCache();
|
||||
throw new Error('Non-okay response from google.com');
|
||||
throw new Error("Non-okay response from google.com");
|
||||
} else {
|
||||
return response.text();
|
||||
}
|
||||
}).then(text => console.log(text));
|
||||
})
|
||||
.then((text) => console.log(text));
|
||||
```
|
||||
|
||||
## Cache Customization
|
||||
@ -57,7 +60,7 @@ This is the default cache delegate. It caches responses in-process in a POJO.
|
||||
Usage:
|
||||
|
||||
```js
|
||||
import { fetchBuilder, MemoryCache } from 'node-fetch-cache';
|
||||
import { fetchBuilder, MemoryCache } from "node-fetch-cache";
|
||||
const fetch = fetchBuilder.withCache(new MemoryCache(options));
|
||||
```
|
||||
|
||||
@ -69,7 +72,7 @@ Options:
|
||||
}
|
||||
```
|
||||
|
||||
Note that by default (if you don't use `withCache()`) a **shared** MemoryCache will be used (you can import this module in multiple files and they will all share the same cache). If you instantiate and provide a `new MemoryCache()` as shown above however, the cache is *NOT* shared unless you explicitly pass it around and pass it into `withCache()` in each of your source files.
|
||||
Note that by default (if you don't use `withCache()`) a **shared** MemoryCache will be used (you can import this module in multiple files and they will all share the same cache). If you instantiate and provide a `new MemoryCache()` as shown above however, the cache is _NOT_ shared unless you explicitly pass it around and pass it into `withCache()` in each of your source files.
|
||||
|
||||
### FileSystemCache
|
||||
|
||||
@ -78,7 +81,7 @@ Cache to a directory on disk. This allows the cache to survive the process exiti
|
||||
Usage:
|
||||
|
||||
```js
|
||||
import { fetchBuilder, FileSystemCache } from 'node-fetch-cache';
|
||||
import { fetchBuilder, FileSystemCache } from "node-fetch-cache";
|
||||
const fetch = fetchBuilder.withCache(new FileSystemCache(options));
|
||||
```
|
||||
|
||||
@ -118,10 +121,12 @@ Streams don't quite play nice with the concept of caching based on request chara
|
||||
Requests with the same cache key are queued. For example, you might wonder if making the same request 100 times simultaneously would result in 100 HTTP requests:
|
||||
|
||||
```js
|
||||
import fetch from 'node-fetch-cache';
|
||||
import fetch from "node-fetch-cache";
|
||||
|
||||
await Promise.all(
|
||||
Array(100).fill().map(() => fetch('https://google.com')),
|
||||
Array(100)
|
||||
.fill()
|
||||
.map(() => fetch("https://google.com"))
|
||||
);
|
||||
```
|
||||
|
||||
@ -132,10 +137,10 @@ The answer is no. Only one request would be made, and 99 of the `fetch()`s will
|
||||
The HTTP standard describes a [Cache-Control request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#request_directives) to control certain aspects of cache behavior. Node-fetch ignores these, but node-fetch-cache respects the `Cache-Control: only-if-cached` directive. When `only-if-cached` is specified, node-fetch-cache will return `undefined` if there is no cached response. No HTTP request will be made. For example:
|
||||
|
||||
```js
|
||||
import fetch from 'node-fetch-cache';
|
||||
import fetch from "node-fetch-cache";
|
||||
|
||||
const response = await fetch('https://google.com', {
|
||||
headers: { 'Cache-Control': 'only-if-cached' }
|
||||
const response = await fetch("https://google.com", {
|
||||
headers: { "Cache-Control": "only-if-cached" },
|
||||
});
|
||||
|
||||
if (response === undefined) {
|
||||
@ -150,12 +155,12 @@ Note that this is slightly different from browser fetch, which returns a `504 Ga
|
||||
This module exports a `getCacheKey()` function to calculate a cache key string from request parameters, which may be useful for enabling some advanced use cases (especially if you want to call cache functions directly). Call `getCacheKey()` exactly like you would call `fetch()`.
|
||||
|
||||
```js
|
||||
import { fetchBuilder, MemoryCache, getCacheKey } from 'node-fetch-cache';
|
||||
import { fetchBuilder, MemoryCache, getCacheKey } from "node-fetch-cache";
|
||||
|
||||
const cache = new MemoryCache();
|
||||
const fetch = fetchBuilder.withCache(cache);
|
||||
|
||||
const rawCacheData = await cache.get(getCacheKey('https://google.com'));
|
||||
const rawCacheData = await cache.get(getCacheKey("https://google.com"));
|
||||
```
|
||||
|
||||
## Bugs / Help / Feature Requests / Contributing
|
||||
|
@ -12,8 +12,8 @@
|
||||
"buildcjs": "rollup src/index.js --file commonjs/index.cjs --format cjs",
|
||||
"test": "npm run lintfix && npm run buildcjs && mocha --timeout 10000 --exit",
|
||||
"coverage": "nyc --reporter=lcov --reporter=text npm test",
|
||||
"lint": "./node_modules/.bin/eslint .",
|
||||
"lintfix": "./node_modules/.bin/eslint . --fix",
|
||||
"lint": "eslint .",
|
||||
"lintfix": "eslint . --fix",
|
||||
"prepublishOnly": "npm test"
|
||||
},
|
||||
"repository": {
|
||||
|
Reference in New Issue
Block a user