diff --git a/lib/interceptor/cache.js b/lib/interceptor/cache.js index c7de4ea862f..666a87ef3e5 100644 --- a/lib/interceptor/cache.js +++ b/lib/interceptor/cache.js @@ -9,20 +9,26 @@ const { assertCacheStore, assertCacheMethods } = require('../util/cache.js') const AGE_HEADER = Buffer.from('age') /** - * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions} [globalOpts] + * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions} [opts] * @returns {import('../../types/dispatcher.d.ts').default.DispatcherComposeInterceptor} */ -module.exports = (globalOpts = {}) => { +module.exports = (opts = {}) => { const { store = new MemoryCacheStore(), methods = ['GET'] - } = globalOpts + } = opts - assertCacheStore(store) - assertCacheMethods(methods) + if (typeof opts !== 'object' || opts === null) { + throw new TypeError(`expected type of opts to be an Object, got ${store === null ? 'null' : typeof store}`) + } + + assertCacheStore(store, 'opts.store') + assertCacheMethods(methods, 'opts.methods') - globalOpts.store = store - globalOpts.methods = methods + const globalOpts = { + store, + methods + } return dispatch => { return (opts, handler) => { diff --git a/lib/util/cache.js b/lib/util/cache.js index 41aaa8de4d2..48a91da3e74 100644 --- a/lib/util/cache.js +++ b/lib/util/cache.js @@ -181,33 +181,37 @@ function parseVaryHeader (varyHeader, headers) { * @param {unknown} store * @returns {asserts store is import('../../types/cache-interceptor.d.ts').default.CacheStore} */ -function assertCacheStore (store) { +function assertCacheStore (store, name = 'CacheStore') { if (typeof store !== 'object' || store === null) { - throw new TypeError(`expected type to be an store, got ${typeof store}`) + throw new TypeError(`expected type of ${name} to be a CacheStore, got ${store === null ? 'null' : typeof store}`) } for (const fn of ['createReadStream', 'createWriteStream', 'deleteByOrigin']) { if (typeof store[fn] !== 'function') { - throw new TypeError(`CacheStore needs a \`${fn}()\` function`) + throw new TypeError(`${name} needs to have a \`${fn}()\` function`) } } if (typeof store.isFull !== 'boolean') { - throw new TypeError(`CacheStore needs a isFull getter with type boolean, current type: ${typeof store.isFull}`) + throw new TypeError(`${name} needs a isFull getter with type boolean, current type: ${typeof store.isFull}`) } } /** * @param {unknown} methods * @returns {asserts methods is import('../../types/cache-interceptor.d.ts').default.CacheMethods[]} */ -function assertCacheMethods (methods) { +function assertCacheMethods (methods, name = 'CacheMethods') { if (!Array.isArray(methods)) { - throw new TypeError(`expected type to be an array, got ${typeof methods}`) + throw new TypeError(`expected type of ${name} needs to be an array, got ${methods === null ? 'null' : typeof methods}`) + } + + if (methods.length === 0) { + throw new TypeError(`${name} needs to have at least one method`) } for (const method of methods) { if (!safeHTTPMethods.includes(method)) { - throw new TypeError(`CacheMethods needs to be one of ${safeHTTPMethods.join(', ')}, got ${method}`) + throw new TypeError(`element of ${name}-array needs to be one of following values: ${safeHTTPMethods.join(', ')}, got ${method}`) } } }