support Request object
This commit is contained in:
parent
dae57d2604
commit
847b330489
28
index.js
28
index.js
@ -1,4 +1,4 @@
|
|||||||
import fetch from 'node-fetch';
|
import fetch, { Request } from 'node-fetch';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { URLSearchParams } from 'url';
|
import { URLSearchParams } from 'url';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
@ -47,26 +47,42 @@ function getBodyCacheKeyJson(body) {
|
|||||||
return body.path;
|
return body.path;
|
||||||
} if (body.toString && body.toString() === '[object FormData]') {
|
} if (body.toString && body.toString() === '[object FormData]') {
|
||||||
return getFormDataCacheKey(body);
|
return getFormDataCacheKey(body);
|
||||||
|
} if (body instanceof Buffer) {
|
||||||
|
return body.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('Unsupported body type. Supported body types are: string, number, undefined, null, url.URLSearchParams, fs.ReadStream, FormData');
|
throw new Error('Unsupported body type. Supported body types are: string, number, undefined, null, url.URLSearchParams, fs.ReadStream, FormData');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getRequestCacheKey(req) {
|
||||||
|
return {
|
||||||
|
cache: req.cache,
|
||||||
|
credentials: req.credentials,
|
||||||
|
destination: req.destination,
|
||||||
|
headers: req.headers,
|
||||||
|
integrity: req.integrity,
|
||||||
|
method: req.method,
|
||||||
|
redirect: req.redirect,
|
||||||
|
referrer: req.referrer,
|
||||||
|
referrerPolicy: req.referrerPolicy,
|
||||||
|
url: req.url,
|
||||||
|
body: getBodyCacheKeyJson(req.body),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function getCacheKey(requestArguments) {
|
function getCacheKey(requestArguments) {
|
||||||
const resource = requestArguments[0];
|
const resource = requestArguments[0];
|
||||||
const init = requestArguments[1] || {};
|
const init = requestArguments[1] || {};
|
||||||
|
|
||||||
if (typeof resource !== 'string') {
|
const resourceCacheKeyJson = resource instanceof Request
|
||||||
throw new Error('The first argument must be a string (fetch.Request is not supported).');
|
? getRequestCacheKey(resource)
|
||||||
}
|
: { url: resource };
|
||||||
|
|
||||||
const resourceCacheKeyJson = { url: resource };
|
|
||||||
const initCacheKeyJson = { ...init };
|
const initCacheKeyJson = { ...init };
|
||||||
|
|
||||||
resourceCacheKeyJson.body = getBodyCacheKeyJson(resourceCacheKeyJson.body);
|
resourceCacheKeyJson.body = getBodyCacheKeyJson(resourceCacheKeyJson.body);
|
||||||
initCacheKeyJson.body = getBodyCacheKeyJson(initCacheKeyJson.body);
|
initCacheKeyJson.body = getBodyCacheKeyJson(initCacheKeyJson.body);
|
||||||
|
|
||||||
delete resourceCacheKeyJson.agent;
|
|
||||||
delete initCacheKeyJson.agent;
|
delete initCacheKeyJson.agent;
|
||||||
|
|
||||||
return md5(JSON.stringify([resourceCacheKeyJson, initCacheKeyJson, CACHE_VERSION]));
|
return md5(JSON.stringify([resourceCacheKeyJson, initCacheKeyJson, CACHE_VERSION]));
|
||||||
|
@ -316,14 +316,14 @@ describe('Cache tests', function() {
|
|||||||
}).timeout(10000);
|
}).timeout(10000);
|
||||||
|
|
||||||
describe('Data tests', function() {
|
describe('Data tests', function() {
|
||||||
it('Does not support Request objects', async function() {
|
it('Supports request objects', async function() {
|
||||||
try {
|
let request = new standardFetch.Request('https://google.com', { body: 'test', method: 'POST' });
|
||||||
const request = new standardFetch.Request('https://google.com');
|
res = await cachedFetch(request);
|
||||||
await cachedFetch(request);
|
assert.strictEqual(res.fromCache, false);
|
||||||
throw new Error('The above line should have thrown.');
|
|
||||||
} catch (err) {
|
request = new standardFetch.Request('https://google.com', { body: 'test', method: 'POST' });
|
||||||
assert(err.message.includes('The first argument must be a string (fetch.Request is not supported).'));
|
res = await cachedFetch(request);
|
||||||
}
|
assert.strictEqual(res.fromCache, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Refuses to consume body twice', async function() {
|
it('Refuses to consume body twice', async function() {
|
||||||
|
Reference in New Issue
Block a user