cacheable-request
Advanced tools
Comparing version 10.1.2 to 10.2.0
@@ -1,4 +0,2 @@ | ||
import { ServerResponse } from 'node:http'; | ||
import Response from 'responselike'; | ||
import { RequestFn, StorageAdapter, CacheableOptions, Emitter } from './types.js'; | ||
import { RequestFn, StorageAdapter, CacheResponse, CacheValue, CacheableOptions, Emitter } from './types.js'; | ||
declare type Func = (...args: any[]) => any; | ||
@@ -10,10 +8,11 @@ declare class CacheableRequest { | ||
constructor(cacheRequest: RequestFn, cacheAdapter?: StorageAdapter | string); | ||
request: () => (options: CacheableOptions, cb?: ((response: ServerResponse | typeof Response) => void) | undefined) => Emitter; | ||
request: () => (options: CacheableOptions, cb?: ((response: CacheResponse) => void) | undefined) => Emitter; | ||
addHook: (name: string, fn: Func) => void; | ||
removeHook: (name: string) => boolean; | ||
getHook: (name: string) => Func | undefined; | ||
runHook: (name: string, response: any) => Promise<any>; | ||
runHook: (name: string, ...args: any[]) => Promise<CacheValue>; | ||
} | ||
export default CacheableRequest; | ||
export * from './types.js'; | ||
export declare const onResponse = "onResponse"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -103,7 +103,7 @@ import EventEmitter from 'node:events'; | ||
const body = await bodyPromise; | ||
const value = { | ||
cachePolicy: response.cachePolicy.toObject(), | ||
let value = { | ||
url: response.url, | ||
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode, | ||
body, | ||
cachePolicy: response.cachePolicy.toObject(), | ||
}; | ||
@@ -117,3 +117,3 @@ let ttl = options_.strictTtl ? response.cachePolicy.timeToLive() : undefined; | ||
for (const key_ of this.hooks.keys()) { | ||
value.body = await this.runHook(key_, value.body); | ||
value = await this.runHook(key_, value, response); | ||
} | ||
@@ -209,8 +209,3 @@ /* eslint-enable no-await-in-loop */ | ||
this.getHook = (name) => this.hooks.get(name); | ||
this.runHook = async (name, response) => { | ||
if (!response) { | ||
return new CacheError(new Error('runHooks requires response argument')); | ||
} | ||
return this.hooks.get(name)?.(response); | ||
}; | ||
this.runHook = async (name, ...args) => this.hooks.get(name)?.(...args); | ||
if (cacheAdapter instanceof Keyv) { | ||
@@ -273,2 +268,3 @@ this.cache = cacheAdapter; | ||
export * from './types.js'; | ||
export const onResponse = 'onResponse'; | ||
//# sourceMappingURL=index.js.map |
@@ -11,5 +11,7 @@ /// <reference types="node" resolution-mode="require"/> | ||
import ResponseLike from 'responselike'; | ||
import { CachePolicyObject } from 'http-cache-semantics'; | ||
export declare type RequestFn = typeof request; | ||
export declare type RequestFunction = typeof request; | ||
export declare type CacheableRequestFunction = (options: CacheableOptions, cb?: (response: ServerResponse | typeof ResponseLike) => void) => Emitter; | ||
export declare type CacheResponse = ServerResponse | typeof ResponseLike; | ||
export declare type CacheableRequestFunction = (options: CacheableOptions, cb?: (response: CacheResponse) => void) => Emitter; | ||
export declare type CacheableOptions = Options & RequestOptions | string | URL; | ||
@@ -48,2 +50,3 @@ export declare type StorageAdapter = Store<any>; | ||
forceRefresh?: boolean | undefined; | ||
remoteAddress?: boolean | undefined; | ||
url?: string | undefined; | ||
@@ -53,33 +56,39 @@ headers?: Record<string, string | string[] | undefined>; | ||
} | ||
export interface CacheValue extends Record<string, any> { | ||
url: string; | ||
statusCode: number; | ||
body: Buffer | string; | ||
cachePolicy: CachePolicyObject; | ||
} | ||
export interface Emitter extends EventEmitter { | ||
addListener(event: 'request', listener: (request: ClientRequest) => void): this; | ||
addListener(event: 'response', listener: (response: ServerResponse | typeof ResponseLike) => void): this; | ||
addListener(event: 'response', listener: (response: CacheResponse) => void): this; | ||
addListener(event: 'error', listener: (error: RequestError | CacheError) => void): this; | ||
on(event: 'request', listener: (request: ClientRequest) => void): this; | ||
on(event: 'response', listener: (response: ServerResponse | typeof ResponseLike) => void): this; | ||
on(event: 'response', listener: (response: CacheResponse) => void): this; | ||
on(event: 'error', listener: (error: RequestError | CacheError) => void): this; | ||
once(event: 'request', listener: (request: ClientRequest) => void): this; | ||
once(event: 'response', listener: (response: ServerResponse | typeof ResponseLike) => void): this; | ||
once(event: 'response', listener: (response: CacheResponse) => void): this; | ||
once(event: 'error', listener: (error: RequestError | CacheError) => void): this; | ||
prependListener(event: 'request', listener: (request: ClientRequest) => void): this; | ||
prependListener(event: 'response', listener: (response: ServerResponse | typeof ResponseLike) => void): this; | ||
prependListener(event: 'response', listener: (response: CacheResponse) => void): this; | ||
prependListener(event: 'error', listener: (error: RequestError | CacheError) => void): this; | ||
prependOnceListener(event: 'request', listener: (request: ClientRequest) => void): this; | ||
prependOnceListener(event: 'response', listener: (response: ServerResponse | typeof ResponseLike) => void): this; | ||
prependOnceListener(event: 'response', listener: (response: CacheResponse) => void): this; | ||
prependOnceListener(event: 'error', listener: (error: RequestError | CacheError) => void): this; | ||
removeListener(event: 'request', listener: (request: ClientRequest) => void): this; | ||
removeListener(event: 'response', listener: (response: ServerResponse | typeof ResponseLike) => void): this; | ||
removeListener(event: 'response', listener: (response: CacheResponse) => void): this; | ||
removeListener(event: 'error', listener: (error: RequestError | CacheError) => void): this; | ||
off(event: 'request', listener: (request: ClientRequest) => void): this; | ||
off(event: 'response', listener: (response: ServerResponse | typeof ResponseLike) => void): this; | ||
off(event: 'response', listener: (response: CacheResponse) => void): this; | ||
off(event: 'error', listener: (error: RequestError | CacheError) => void): this; | ||
removeAllListeners(event?: 'request' | 'response' | 'error'): this; | ||
listeners(event: 'request'): Array<(request: ClientRequest) => void>; | ||
listeners(event: 'response'): Array<(response: ServerResponse | typeof ResponseLike) => void>; | ||
listeners(event: 'response'): Array<(response: CacheResponse) => void>; | ||
listeners(event: 'error'): Array<(error: RequestError | CacheError) => void>; | ||
rawListeners(event: 'request'): Array<(request: ClientRequest) => void>; | ||
rawListeners(event: 'response'): Array<(response: ServerResponse | typeof ResponseLike) => void>; | ||
rawListeners(event: 'response'): Array<(response: CacheResponse) => void>; | ||
rawListeners(event: 'error'): Array<(error: RequestError | CacheError) => void>; | ||
emit(event: 'request', request: ClientRequest): boolean; | ||
emit(event: 'response', response: ServerResponse | typeof ResponseLike): boolean; | ||
emit(event: 'response', response: CacheResponse): boolean; | ||
emit(event: 'error', error: RequestError | CacheError): boolean; | ||
@@ -86,0 +95,0 @@ eventNames(): Array<'request' | 'response' | 'error'>; |
{ | ||
"name": "cacheable-request", | ||
"version": "10.1.2", | ||
"version": "10.2.0", | ||
"description": "Wrap native HTTP requests with RFC compliant cache support", | ||
@@ -44,3 +44,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@keyv/sqlite": "^3.5.3", | ||
"@keyv/sqlite": "^3.6.1", | ||
"@types/create-test-server": "^3.0.1", | ||
@@ -50,4 +50,4 @@ "@types/delay": "^3.1.0", | ||
"@types/http-cache-semantics": "^4.0.1", | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^18.7.16", | ||
"@types/jest": "^29.0.3", | ||
"@types/node": "^18.7.18", | ||
"@types/responselike": "^1.0.0", | ||
@@ -59,8 +59,8 @@ "@types/sqlite3": "^3.1.8", | ||
"jest": "^29.0.3", | ||
"sqlite3": "^5.0.11", | ||
"ts-jest": "^29.0.0", | ||
"sqlite3": "^5.1.1", | ||
"ts-jest": "^29.0.1", | ||
"ts-jest-resolver": "^2.0.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.3", | ||
"xo": "^0.51.0" | ||
"xo": "^0.52.3" | ||
}, | ||
@@ -107,5 +107,7 @@ "jest": { | ||
"@typescript-eslint/restrict-plus-operands": 0, | ||
"@typescript-eslint/no-implicit-any-catch": 0 | ||
"@typescript-eslint/no-implicit-any-catch": 0, | ||
"@typescript-eslint/consistent-type-imports": 0, | ||
"@typescript-eslint/consistent-type-definitions": 0 | ||
} | ||
} | ||
} |
@@ -50,3 +50,4 @@ # cacheable-request | ||
### Usage After v10 | ||
### Usage After v10.1.0 | ||
```js | ||
@@ -277,9 +278,27 @@ ```js | ||
const cacheableRequest = new CacheableRequest(request, cache).createCacheableRequest(); | ||
cacheableRequest.addHook('response', async (response: any) => { | ||
const buffer = await pm(gunzip)(response); | ||
return buffer.toString(); | ||
const cacheableRequest = new CacheableRequest(request, cache).request(); | ||
// adding a hook to decompress response | ||
cacheableRequest.addHook('onResponse', async (value: CacheValue, response: any) => { | ||
const buffer = await pm(gunzip)(value.body); | ||
value.body = buffer.toString(); | ||
return value; | ||
}); | ||
``` | ||
here is also an example of how to add in the remote address | ||
```js | ||
import CacheableRequest, {CacheValue} from 'cacheable-request'; | ||
const cacheableRequest = new CacheableRequest(request, cache).request(); | ||
cacheableRequest.addHook('onResponse', (value: CacheValue, response: any) => { | ||
if (response.connection) { | ||
value.remoteAddress = response.connection.remoteAddress; | ||
} | ||
return value; | ||
}); | ||
``` | ||
### Remove Hooks | ||
@@ -290,3 +309,3 @@ | ||
```js | ||
CacheableRequest.removeHook('response'); | ||
CacheableRequest.removeHook('onResponse'); | ||
``` | ||
@@ -314,6 +333,7 @@ | ||
## License [MIT](LICENSE) | ||
## License | ||
This project is under the [MIT](LICENSE) license. | ||
MIT © Luke Childs 2017-2021 | ||
MIT © Jared Wray 2022 |
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
51539
404
336