From 847b3304892eeb7ab176fd760103f25b380a1b9e Mon Sep 17 00:00:00 2001 From: Randall Schmidt Date: Fri, 9 Jul 2021 13:41:18 -0400 Subject: [PATCH] support Request object --- index.js | 28 ++++++++++++++++++++++------ test/tests.js | 16 ++++++++-------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 36020b7..76e4562 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -import fetch from 'node-fetch'; +import fetch, { Request } from 'node-fetch'; import fs from 'fs'; import { URLSearchParams } from 'url'; import crypto from 'crypto'; @@ -47,26 +47,42 @@ function getBodyCacheKeyJson(body) { return body.path; } if (body.toString && body.toString() === '[object FormData]') { 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'); } +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) { const resource = requestArguments[0]; const init = requestArguments[1] || {}; - if (typeof resource !== 'string') { - throw new Error('The first argument must be a string (fetch.Request is not supported).'); - } + const resourceCacheKeyJson = resource instanceof Request + ? getRequestCacheKey(resource) + : { url: resource }; - const resourceCacheKeyJson = { url: resource }; const initCacheKeyJson = { ...init }; resourceCacheKeyJson.body = getBodyCacheKeyJson(resourceCacheKeyJson.body); initCacheKeyJson.body = getBodyCacheKeyJson(initCacheKeyJson.body); - delete resourceCacheKeyJson.agent; delete initCacheKeyJson.agent; return md5(JSON.stringify([resourceCacheKeyJson, initCacheKeyJson, CACHE_VERSION])); diff --git a/test/tests.js b/test/tests.js index dc5d16a..9293ad5 100644 --- a/test/tests.js +++ b/test/tests.js @@ -316,14 +316,14 @@ describe('Cache tests', function() { }).timeout(10000); describe('Data tests', function() { - it('Does not support Request objects', async function() { - try { - const request = new standardFetch.Request('https://google.com'); - await cachedFetch(request); - throw new Error('The above line should have thrown.'); - } catch (err) { - assert(err.message.includes('The first argument must be a string (fetch.Request is not supported).')); - } + it('Supports request objects', async function() { + let request = new standardFetch.Request('https://google.com', { body: 'test', method: 'POST' }); + res = await cachedFetch(request); + assert.strictEqual(res.fromCache, false); + + request = new standardFetch.Request('https://google.com', { body: 'test', method: 'POST' }); + res = await cachedFetch(request); + assert.strictEqual(res.fromCache, true); }); it('Refuses to consume body twice', async function() {