cacheable-request
Advanced tools
Comparing version 11.0.0 to 12.0.0
import { RequestFn, StorageAdapter, CacheResponse, CacheValue, CacheableOptions, Emitter } from './types.js'; | ||
type Func = (...args: any[]) => any; | ||
type Function_ = (...arguments_: any[]) => any; | ||
declare class CacheableRequest { | ||
cache: StorageAdapter; | ||
cacheRequest: RequestFn; | ||
hooks: Map<string, Func>; | ||
hooks: Map<string, Function_>; | ||
constructor(cacheRequest: RequestFn, cacheAdapter?: StorageAdapter | string); | ||
request: () => (options: CacheableOptions, cb?: (response: CacheResponse) => void) => Emitter; | ||
addHook: (name: string, fn: Func) => void; | ||
request: () => (options: CacheableOptions, callback?: (response: CacheResponse) => void) => Emitter; | ||
addHook: (name: string, function_: Function_) => void; | ||
removeHook: (name: string) => boolean; | ||
getHook: (name: string) => Func; | ||
runHook: (name: string, ...args: any[]) => Promise<CacheValue>; | ||
getHook: (name: string) => Function_; | ||
runHook: (name: string, ...arguments_: any[]) => Promise<CacheValue>; | ||
} | ||
@@ -14,0 +14,0 @@ export default CacheableRequest; |
@@ -11,7 +11,7 @@ import EventEmitter from 'node:events'; | ||
import mimicResponse from 'mimic-response'; | ||
import { CacheError, RequestError } from './types.js'; | ||
import { CacheError, RequestError, } from './types.js'; | ||
class CacheableRequest { | ||
constructor(cacheRequest, cacheAdapter) { | ||
this.hooks = new Map(); | ||
this.request = () => (options, cb) => { | ||
this.request = () => (options, callback) => { | ||
let url; | ||
@@ -90,3 +90,5 @@ if (typeof options === 'string') { | ||
const headers = convertHeaders(revalidatedPolicy.policy.responseHeaders()); | ||
response = new Response({ statusCode: revalidate.statusCode, headers, body: revalidate.body, url: revalidate.url }); | ||
response = new Response({ | ||
statusCode: revalidate.statusCode, headers, body: revalidate.body, url: revalidate.url, | ||
}); | ||
response.cachePolicy = revalidatedPolicy.policy; | ||
@@ -147,4 +149,4 @@ response.fromCache = true; | ||
ee.emit('response', clonedResponse ?? response); | ||
if (typeof cb === 'function') { | ||
cb(clonedResponse ?? response); | ||
if (typeof callback === 'function') { | ||
callback(clonedResponse ?? response); | ||
} | ||
@@ -174,8 +176,10 @@ }; | ||
const headers = convertHeaders(policy.responseHeaders()); | ||
const response = new Response({ statusCode: cacheEntry.statusCode, headers, body: cacheEntry.body, url: cacheEntry.url }); | ||
const response = new Response({ | ||
statusCode: cacheEntry.statusCode, headers, body: cacheEntry.body, url: cacheEntry.url, | ||
}); | ||
response.cachePolicy = policy; | ||
response.fromCache = true; | ||
ee.emit('response', response); | ||
if (typeof cb === 'function') { | ||
cb(response); | ||
if (typeof callback === 'function') { | ||
callback(response); | ||
} | ||
@@ -213,5 +217,5 @@ } | ||
}; | ||
this.addHook = (name, fn) => { | ||
this.addHook = (name, function_) => { | ||
if (!this.hooks.has(name)) { | ||
this.hooks.set(name, fn); | ||
this.hooks.set(name, function_); | ||
} | ||
@@ -221,3 +225,3 @@ }; | ||
this.getHook = (name) => this.hooks.get(name); | ||
this.runHook = async (name, ...args) => this.hooks.get(name)?.(...args); | ||
this.runHook = async (name, ...arguments_) => this.hooks.get(name)?.(...arguments_); | ||
if (cacheAdapter instanceof Keyv) { | ||
@@ -224,0 +228,0 @@ this.cache = cacheAdapter; |
@@ -15,3 +15,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
export type CacheResponse = ServerResponse | typeof ResponseLike; | ||
export type CacheableRequestFunction = (options: CacheableOptions, cb?: (response: CacheResponse) => void) => Emitter; | ||
export type CacheableRequestFunction = (options: CacheableOptions, callback?: (response: CacheResponse) => void) => Emitter; | ||
export type CacheableOptions = Options & RequestOptions | string | URL; | ||
@@ -18,0 +18,0 @@ export type StorageAdapter = Store<any>; |
{ | ||
"name": "cacheable-request", | ||
"version": "11.0.0", | ||
"version": "12.0.0", | ||
"description": "Wrap native HTTP requests with RFC compliant cache support", | ||
@@ -12,6 +12,6 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=14.16" | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"test": "xo && NODE_OPTIONS=--experimental-vm-modules jest --coverage ", | ||
"test": "xo --fix && NODE_OPTIONS=--experimental-vm-modules jest --coverage ", | ||
"prepare": "npm run build", | ||
@@ -49,5 +49,5 @@ "build": "tsc --project tsconfig.build.json", | ||
"devDependencies": { | ||
"@keyv/sqlite": "^3.6.6", | ||
"@types/jest": "^29.5.11", | ||
"@types/node": "^20.11.5", | ||
"@keyv/sqlite": "^3.6.7", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.11.19", | ||
"@types/responselike": "^1.0.3", | ||
@@ -58,3 +58,3 @@ "@types/sqlite3": "^3.1.11", | ||
"eslint": "^8.56.0", | ||
"eslint-plugin-jest": "^27.6.3", | ||
"eslint-plugin-jest": "^27.9.0", | ||
"express": "^4.18.2", | ||
@@ -64,7 +64,7 @@ "jest": "^29.7.0", | ||
"sqlite3": "^5.1.7", | ||
"ts-jest": "^29.1.1", | ||
"ts-jest": "^29.1.2", | ||
"ts-jest-resolver": "^2.0.1", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.3.3", | ||
"xo": "^0.56.0" | ||
"xo": "^0.57.0" | ||
}, | ||
@@ -71,0 +71,0 @@ "jest": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71253
417