Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Oct 14, 2024
1 parent 85727cf commit bff7c42
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
20 changes: 13 additions & 7 deletions lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
18 changes: 11 additions & 7 deletions lib/util/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}
}
Expand Down

0 comments on commit bff7c42

Please sign in to comment.