Skip to content

Commit 4c3762a

Browse files
authoredSep 19, 2022
Update cacheable-request (#2146)
1 parent e049e94 commit 4c3762a

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed
 

‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@
4747
"dependencies": {
4848
"@sindresorhus/is": "^5.2.0",
4949
"@szmarczak/http-timer": "^5.0.1",
50-
"@types/cacheable-request": "^6.0.2",
5150
"cacheable-lookup": "^6.0.4",
52-
"cacheable-request": "^7.0.2",
51+
"cacheable-request": "^10.1.2",
5352
"decompress-response": "^6.0.0",
5453
"form-data-encoder": "^2.1.2",
5554
"get-stream": "^6.0.1",

‎source/core/index.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import http, {ServerResponse} from 'node:http';
66
import type {ClientRequest, RequestOptions} from 'node:http';
77
import type {Socket} from 'node:net';
88
import timer from '@szmarczak/http-timer';
9-
import CacheableRequest from 'cacheable-request';
9+
import CacheableRequest, {
10+
CacheError as CacheableCacheError,
11+
type StorageAdapter,
12+
type CacheableRequestFunction,
13+
type CacheableOptions,
14+
} from 'cacheable-request';
1015
import decompressResponse from 'decompress-response';
1116
import is from '@sindresorhus/is';
1217
import {buffer as getBuffer} from 'get-stream';
@@ -124,12 +129,7 @@ export type RequestEvents<T> = {
124129
off: GotEventFunction<T>;
125130
};
126131

127-
export type CacheableRequestFunction = (
128-
options: string | URL | NativeRequestOptions,
129-
cb?: (response: ServerResponse | ResponseLike) => void
130-
) => CacheableRequest.Emitter;
131-
132-
const cacheableStore = new WeakableMap<string | CacheableRequest.StorageAdapter, CacheableRequestFunction>();
132+
const cacheableStore = new WeakableMap<string | StorageAdapter, CacheableRequestFunction>();
133133

134134
const redirectCodes: ReadonlySet<number> = new Set([300, 301, 302, 303, 304, 307, 308]);
135135

@@ -969,9 +969,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
969969
}
970970
}
971971

972-
private _prepareCache(cache: string | CacheableRequest.StorageAdapter) {
972+
private _prepareCache(cache: string | StorageAdapter) {
973973
if (!cacheableStore.has(cache)) {
974-
cacheableStore.set(cache, new CacheableRequest(
974+
const cacheableRequest = new CacheableRequest(
975975
((requestOptions: RequestOptions, handler?: (response: IncomingMessageWithTimings) => void): ClientRequest => {
976976
const result = (requestOptions as any)._request(requestOptions, handler);
977977

@@ -1010,8 +1010,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
10101010

10111011
return result;
10121012
}) as typeof http.request,
1013-
cache as CacheableRequest.StorageAdapter,
1014-
));
1013+
cache as StorageAdapter,
1014+
);
1015+
cacheableStore.set(cache, cacheableRequest.request());
10151016
}
10161017
}
10171018

@@ -1023,7 +1024,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
10231024
let request: ClientRequest | Promise<ClientRequest>;
10241025

10251026
// TODO: Fix `cacheable-response`. This is ugly.
1026-
const cacheRequest = cacheableStore.get((options as any).cache)!(options, async (response: any) => {
1027+
const cacheRequest = cacheableStore.get((options as any).cache)!(options as CacheableOptions, async (response: any) => {
10271028
response._readableState.autoDestroy = false;
10281029

10291030
if (request) {
@@ -1109,7 +1110,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
11091110
if (options.cache) {
11101111
(this._requestOptions as any)._request = request;
11111112
(this._requestOptions as any).cache = options.cache;
1112-
this._prepareCache(options.cache as CacheableRequest.StorageAdapter);
1113+
this._prepareCache(options.cache as StorageAdapter);
11131114
}
11141115

11151116
// Cache support
@@ -1145,7 +1146,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
11451146
void this._onResponse(requestOrResponse as IncomingMessageWithTimings);
11461147
}
11471148
} catch (error) {
1148-
if (error instanceof CacheableRequest.CacheError) {
1149+
if (error instanceof CacheableCacheError) {
11491150
throw new CacheError(error, this);
11501151
}
11511152

‎source/core/options.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import CacheableLookup from 'cacheable-lookup';
2424
import http2wrapper, {type ClientHttp2Session} from 'http2-wrapper';
2525
import {isFormData} from 'form-data-encoder';
2626
import type {FormDataLike} from 'form-data-encoder';
27-
import type CacheableRequest from 'cacheable-request';
27+
import type {StorageAdapter} from 'cacheable-request';
2828
import type ResponseLike from 'responselike';
2929
import type {IncomingMessageWithTimings} from '@szmarczak/http-timer';
3030
import type {CancelableRequest} from '../as-promise/types.js';
@@ -1806,11 +1806,11 @@ export default class Options {
18061806
18071807
@default false
18081808
*/
1809-
get cache(): string | CacheableRequest.StorageAdapter | boolean | undefined {
1809+
get cache(): string | StorageAdapter | boolean | undefined {
18101810
return this._internals.cache;
18111811
}
18121812

1813-
set cache(value: string | CacheableRequest.StorageAdapter | boolean | undefined) {
1813+
set cache(value: string | StorageAdapter | boolean | undefined) {
18141814
assert.any([is.object, is.string, is.boolean, is.undefined], value);
18151815

18161816
if (value === true) {

0 commit comments

Comments
 (0)
Please sign in to comment.