@hyper-fetch/core 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +1 -2
- package/dist/browser/index.cjs.js +2 -0
- package/dist/browser/index.cjs.js.map +7 -0
- package/dist/index.d.ts +1269 -11
- package/dist/server/index.cjs.js +2 -0
- package/dist/server/index.cjs.js.map +7 -0
- package/package.json +7 -4
- package/src/adapter/adapter.bindings.ts +6 -3
- package/src/adapter/adapter.browser.ts +1 -1
- package/src/adapter/adapter.server.ts +9 -12
- package/src/adapter/adapter.ts +12 -4
- package/src/adapter/index.ts +0 -2
- package/src/client/client.types.ts +4 -7
- package/src/dispatcher/dispatcher.ts +5 -1
- package/src/request/request.ts +10 -2
- package/src/request/request.types.ts +2 -2
- package/tsconfig.json +6 -1
- package/tsconfig.node.json +13 -0
- package/dist/adapter/adapter.bindings.d.ts +0 -25
- package/dist/adapter/adapter.browser.d.ts +0 -2
- package/dist/adapter/adapter.constants.d.ts +0 -1
- package/dist/adapter/adapter.d.ts +0 -2
- package/dist/adapter/adapter.server.d.ts +0 -2
- package/dist/adapter/adapter.types.d.ts +0 -57
- package/dist/adapter/adapter.utils.d.ts +0 -8
- package/dist/adapter/index.d.ts +0 -7
- package/dist/cache/cache.d.ts +0 -76
- package/dist/cache/cache.events.d.ts +0 -28
- package/dist/cache/cache.types.d.ts +0 -50
- package/dist/cache/cache.utils.d.ts +0 -6
- package/dist/cache/index.d.ts +0 -4
- package/dist/client/client.constants.d.ts +0 -9
- package/dist/client/client.d.ts +0 -142
- package/dist/client/client.types.d.ts +0 -41
- package/dist/client/client.utils.d.ts +0 -10
- package/dist/client/index.d.ts +0 -4
- package/dist/constants/http.constants.d.ts +0 -7
- package/dist/constants/time.constants.d.ts +0 -11
- package/dist/dispatcher/dispatcher.constants.d.ts +0 -6
- package/dist/dispatcher/dispatcher.d.ts +0 -155
- package/dist/dispatcher/dispatcher.events.d.ts +0 -11
- package/dist/dispatcher/dispatcher.types.d.ts +0 -31
- package/dist/dispatcher/dispatcher.utils.d.ts +0 -10
- package/dist/dispatcher/index.d.ts +0 -5
- package/dist/effect/index.d.ts +0 -2
- package/dist/effect/request.effect.d.ts +0 -14
- package/dist/effect/request.effect.types.d.ts +0 -32
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -7
- package/dist/index.esm.js +0 -2
- package/dist/index.esm.js.map +0 -7
- package/dist/managers/app/app.manager.constants.d.ts +0 -9
- package/dist/managers/app/app.manager.d.ts +0 -34
- package/dist/managers/app/app.manager.events.d.ts +0 -11
- package/dist/managers/app/app.manager.types.d.ts +0 -6
- package/dist/managers/app/app.manager.utils.d.ts +0 -4
- package/dist/managers/app/index.d.ts +0 -5
- package/dist/managers/index.d.ts +0 -3
- package/dist/managers/logger/index.d.ts +0 -4
- package/dist/managers/logger/logger.manager.constants.d.ts +0 -4
- package/dist/managers/logger/logger.manager.d.ts +0 -19
- package/dist/managers/logger/logger.manager.types.d.ts +0 -30
- package/dist/managers/logger/logger.manager.utils.d.ts +0 -3
- package/dist/managers/request/index.d.ts +0 -4
- package/dist/managers/request/request.manager.d.ts +0 -43
- package/dist/managers/request/request.manager.events.d.ts +0 -36
- package/dist/managers/request/request.manager.types.d.ts +0 -23
- package/dist/managers/request/request.manager.utils.d.ts +0 -16
- package/dist/request/index.d.ts +0 -3
- package/dist/request/request.d.ts +0 -98
- package/dist/request/request.types.d.ts +0 -202
- package/dist/request/request.utils.d.ts +0 -22
- package/dist/types/fetch.types.d.ts +0 -16
- package/dist/types/helpers.types.d.ts +0 -11
- package/dist/types/http.types.d.ts +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/uuid.utils.d.ts +0 -1
|
@@ -29,8 +29,6 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
29
29
|
// Request Setup
|
|
30
30
|
const { client, abortKey, queueKey, endpoint, data } = request;
|
|
31
31
|
|
|
32
|
-
requestManager.addAbortController(abortKey, requestId);
|
|
33
|
-
|
|
34
32
|
const fullUrl = url + endpoint;
|
|
35
33
|
const effects = client.effects.filter((effect) => request.effectKey === effect.getEffectKey());
|
|
36
34
|
const headers = headerMapper(request);
|
|
@@ -171,7 +169,6 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
171
169
|
}
|
|
172
170
|
|
|
173
171
|
const progressTimestamp = +new Date();
|
|
174
|
-
requestManager.removeAbortController(abortKey, requestId);
|
|
175
172
|
handleResponseProgress(responseStartTimestamp, progressTimestamp, {
|
|
176
173
|
total: responseTotal,
|
|
177
174
|
loaded: responseTotal,
|
|
@@ -260,6 +257,12 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
260
257
|
requestManager.events.emitAbort(abortKey, requestId, request);
|
|
261
258
|
};
|
|
262
259
|
|
|
260
|
+
// Instant abort when we stack many requests triggered at once and we receive aborted controller
|
|
261
|
+
if (controller.signal.aborted) {
|
|
262
|
+
fn();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Abort during the request
|
|
263
266
|
controller.signal.addEventListener("abort", fn);
|
|
264
267
|
|
|
265
268
|
return () => controller.signal.removeEventListener("abort", fn);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getAdapterBindings, defaultTimeout, ResponseType, AdapterType } from "adapter";
|
|
2
2
|
import { parseErrorResponse, parseResponse } from "./adapter.utils";
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const adapter: AdapterType = async (request, requestId) => {
|
|
5
5
|
const {
|
|
6
6
|
fullUrl,
|
|
7
7
|
headers,
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
+
import http, { OutgoingHttpHeaders } from "http";
|
|
2
|
+
import https from "https";
|
|
3
|
+
import stream from "stream";
|
|
4
|
+
|
|
1
5
|
import { getAdapterBindings, defaultTimeout, ResponseType, AdapterType, AdapterOptionsType } from "adapter";
|
|
2
6
|
import { parseErrorResponse, parseResponse, getUploadSize, getStreamPayload } from "./adapter.utils";
|
|
3
7
|
|
|
4
|
-
export const
|
|
5
|
-
/**
|
|
6
|
-
* Prevent issues related to the missing Node.js polyfills
|
|
7
|
-
*/
|
|
8
|
-
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
9
|
-
const http = require("http");
|
|
10
|
-
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
11
|
-
const stream = require("stream");
|
|
12
|
-
|
|
8
|
+
export const adapter: AdapterType = async (request, requestId) => {
|
|
13
9
|
const {
|
|
14
10
|
fullUrl,
|
|
15
11
|
headers,
|
|
@@ -27,14 +23,15 @@ export const serverAdapter: AdapterType = async (request, requestId) => {
|
|
|
27
23
|
onError,
|
|
28
24
|
onResponseEnd,
|
|
29
25
|
} = await getAdapterBindings<AdapterOptionsType>(request, requestId);
|
|
30
|
-
const { method } = request;
|
|
26
|
+
const { method, client } = request;
|
|
27
|
+
const httpClient = client.url.includes("https://") ? https : http;
|
|
31
28
|
|
|
32
29
|
return new Promise<ResponseType<unknown, unknown>>((resolve) => {
|
|
33
30
|
const execute = async () => {
|
|
34
31
|
const options = {
|
|
35
32
|
path: fullUrl,
|
|
36
33
|
method,
|
|
37
|
-
headers,
|
|
34
|
+
headers: headers as OutgoingHttpHeaders,
|
|
38
35
|
timeout: defaultTimeout,
|
|
39
36
|
};
|
|
40
37
|
|
|
@@ -51,7 +48,7 @@ export const serverAdapter: AdapterType = async (request, requestId) => {
|
|
|
51
48
|
|
|
52
49
|
const payloadChunks = await getStreamPayload(payload);
|
|
53
50
|
|
|
54
|
-
const httpRequest =
|
|
51
|
+
const httpRequest = httpClient.request(options, (response) => {
|
|
55
52
|
response.setEncoding("utf8");
|
|
56
53
|
|
|
57
54
|
let chunks = "";
|
package/src/adapter/adapter.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as browser from "@browser-adapter";
|
|
2
|
+
import * as server from "@server-adapter";
|
|
3
|
+
|
|
4
|
+
import { AdapterType } from "adapter";
|
|
2
5
|
|
|
3
6
|
export const adapter: AdapterType = async (request, requestId) => {
|
|
4
|
-
if (
|
|
5
|
-
return
|
|
7
|
+
if (typeof XMLHttpRequest !== "undefined") {
|
|
8
|
+
return browser.adapter(request, requestId);
|
|
9
|
+
}
|
|
10
|
+
if (typeof process !== "undefined" && Object.prototype.toString.call(process) === "[object process]") {
|
|
11
|
+
return server.adapter(request, requestId);
|
|
6
12
|
}
|
|
7
|
-
return
|
|
13
|
+
return (() => {
|
|
14
|
+
return [null, new Error("Cannot find the right environment for http adapter"), 0];
|
|
15
|
+
}) as unknown as ReturnType<AdapterType>;
|
|
8
16
|
};
|
package/src/adapter/index.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { Cache } from "cache";
|
|
2
1
|
import { RequestInstance } from "request";
|
|
3
|
-
import { Dispatcher } from "dispatcher";
|
|
4
2
|
import { ResponseType, AdapterType, QueryParamsType } from "adapter";
|
|
5
3
|
import { Client } from "client";
|
|
6
|
-
import { AppManager } from "managers";
|
|
7
4
|
import { NegativeTypes } from "types";
|
|
8
5
|
|
|
9
6
|
export type ClientErrorType = Record<string, any> | string;
|
|
@@ -24,19 +21,19 @@ export type ClientOptionsType = {
|
|
|
24
21
|
/**
|
|
25
22
|
* Custom cache initialization prop
|
|
26
23
|
*/
|
|
27
|
-
cache?: <B extends ClientInstance
|
|
24
|
+
cache?: <B extends ClientInstance>(client: B) => B["cache"];
|
|
28
25
|
/**
|
|
29
26
|
* Custom app manager initialization prop
|
|
30
27
|
*/
|
|
31
|
-
appManager?: <B extends ClientInstance
|
|
28
|
+
appManager?: <B extends ClientInstance>(client: B) => B["appManager"];
|
|
32
29
|
/**
|
|
33
30
|
* Custom fetch dispatcher initialization prop
|
|
34
31
|
*/
|
|
35
|
-
fetchDispatcher?: <B extends ClientInstance
|
|
32
|
+
fetchDispatcher?: <B extends ClientInstance>(client: B) => B["submitDispatcher"];
|
|
36
33
|
/**
|
|
37
34
|
* Custom submit dispatcher initialization prop
|
|
38
35
|
*/
|
|
39
|
-
submitDispatcher?: <B extends ClientInstance
|
|
36
|
+
submitDispatcher?: <B extends ClientInstance>(client: B) => B["fetchDispatcher"];
|
|
40
37
|
};
|
|
41
38
|
|
|
42
39
|
// Interceptors
|
|
@@ -428,7 +428,7 @@ export class Dispatcher {
|
|
|
428
428
|
return requestId;
|
|
429
429
|
}
|
|
430
430
|
case DispatcherRequestType.deduplicated: {
|
|
431
|
-
// Return the running requestId to
|
|
431
|
+
// Return the running requestId to fulfill the events
|
|
432
432
|
return queue.requests[0].requestId;
|
|
433
433
|
}
|
|
434
434
|
default: {
|
|
@@ -508,9 +508,13 @@ export class Dispatcher {
|
|
|
508
508
|
|
|
509
509
|
// Trigger Request
|
|
510
510
|
this.incrementQueueRequestCount(queueKey);
|
|
511
|
+
// Listen for aborting
|
|
512
|
+
requestManager.addAbortController(abortKey, requestId);
|
|
511
513
|
|
|
512
514
|
const response = await adapter(request, requestId);
|
|
513
515
|
|
|
516
|
+
// Stop listening for aborting
|
|
517
|
+
requestManager.removeAbortController(abortKey, requestId);
|
|
514
518
|
// Do not continue the request handling when it got stopped and request was unsuccessful
|
|
515
519
|
// Or when the request was aborted/canceled
|
|
516
520
|
const isOfflineResponseStatus = !appManager.isOnline;
|
package/src/request/request.ts
CHANGED
|
@@ -417,12 +417,20 @@ export class Request<
|
|
|
417
417
|
>
|
|
418
418
|
>,
|
|
419
419
|
) => {
|
|
420
|
-
const { adapter } = this.client;
|
|
420
|
+
const { adapter, requestManager } = this.client;
|
|
421
421
|
const request = this.clone(options as any);
|
|
422
422
|
|
|
423
423
|
const requestId = getUniqueRequestId(this.queueKey);
|
|
424
424
|
|
|
425
|
-
|
|
425
|
+
// Listen for aborting
|
|
426
|
+
requestManager.addAbortController(this.abortKey, requestId);
|
|
427
|
+
|
|
428
|
+
const response = await adapter(request, requestId);
|
|
429
|
+
|
|
430
|
+
// Stop listening for aborting
|
|
431
|
+
requestManager.removeAbortController(this.abortKey, requestId);
|
|
432
|
+
|
|
433
|
+
return response;
|
|
426
434
|
};
|
|
427
435
|
|
|
428
436
|
/**
|
|
@@ -271,10 +271,10 @@ export type RequestSendType<Request extends RequestInstance> =
|
|
|
271
271
|
options?: RequestSendOptionsType<Request>,
|
|
272
272
|
) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>
|
|
273
273
|
: (
|
|
274
|
-
options
|
|
274
|
+
options?: RequestSendOptionsType<Request>,
|
|
275
275
|
) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>
|
|
276
276
|
: (
|
|
277
|
-
options
|
|
277
|
+
options?: RequestSendOptionsType<Request>,
|
|
278
278
|
) => Promise<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>>>;
|
|
279
279
|
|
|
280
280
|
// Instance
|
package/tsconfig.json
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
"extends": "../../tsconfig.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
4
|
"rootDir": "./src",
|
|
5
|
-
"baseUrl": "./src"
|
|
5
|
+
"baseUrl": "./src",
|
|
6
|
+
"paths": {
|
|
7
|
+
"@browser-adapter": ["./adapter/adapter.browser.ts"],
|
|
8
|
+
"@server-adapter": ["./adapter/adapter.browser.ts"]
|
|
9
|
+
}
|
|
6
10
|
},
|
|
11
|
+
"include": ["./src/index.ts"],
|
|
7
12
|
"exclude": ["dist", "__tests__", "jest.config.ts"]
|
|
8
13
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"baseUrl": "./src",
|
|
6
|
+
"paths": {
|
|
7
|
+
"@browser-adapter": ["./adapter/adapter.browser.ts"],
|
|
8
|
+
"@server-adapter": ["./adapter/adapter.server.ts"]
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"include": ["./src/index.ts"],
|
|
12
|
+
"exclude": ["dist", "__tests__", "jest.config.ts"]
|
|
13
|
+
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { ResponseSuccessType, ResponseErrorType, ProgressDataType } from "adapter";
|
|
2
|
-
import { RequestInstance } from "request";
|
|
3
|
-
import { ExtractResponseType, ExtractErrorType } from "types";
|
|
4
|
-
export declare const getAdapterBindings: <ConfigType = any>(cmd: RequestInstance, requestId: string) => Promise<{
|
|
5
|
-
fullUrl: string;
|
|
6
|
-
headers: HeadersInit;
|
|
7
|
-
payload: string | FormData;
|
|
8
|
-
config: ConfigType;
|
|
9
|
-
getAbortController: () => AbortController;
|
|
10
|
-
getRequestStartTimestamp: () => number;
|
|
11
|
-
getResponseStartTimestamp: () => number;
|
|
12
|
-
createAbortListener: <T extends RequestInstance>(callback: () => void, resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void) => () => void;
|
|
13
|
-
onBeforeRequest: () => void;
|
|
14
|
-
onRequestStart: (progress?: ProgressDataType) => number;
|
|
15
|
-
onRequestProgress: (progress: ProgressDataType) => number;
|
|
16
|
-
onRequestEnd: () => number;
|
|
17
|
-
onResponseStart: (progress?: ProgressDataType) => number;
|
|
18
|
-
onResponseProgress: (progress: ProgressDataType) => number;
|
|
19
|
-
onResponseEnd: () => number;
|
|
20
|
-
onSuccess: <T_1 extends RequestInstance>(responseData: unknown, status: number, resolve: (value: ResponseErrorType<ExtractErrorType<T_1>>) => void) => Promise<ResponseSuccessType<ExtractResponseType<T_1>>>;
|
|
21
|
-
onAbortError: <T_2 extends RequestInstance>(resolve: (value: ResponseErrorType<ExtractErrorType<T_2>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_2>>>;
|
|
22
|
-
onTimeoutError: <T_3 extends RequestInstance>(resolve: (value: ResponseErrorType<ExtractErrorType<T_3>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_3>>>;
|
|
23
|
-
onUnexpectedError: <T_4 extends RequestInstance>(resolve: (value: ResponseErrorType<ExtractErrorType<T_4>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_4>>>;
|
|
24
|
-
onError: <T_5 extends RequestInstance>(error: Error | ExtractErrorType<T_5>, status: number, resolve: (value: ResponseErrorType<ExtractErrorType<T_5>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_5>>>;
|
|
25
|
-
}>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const defaultTimeout: number;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { RequestInstance } from "request";
|
|
2
|
-
export type AdapterType = (request: RequestInstance, requestId: string) => Promise<ResponseType<any, any>>;
|
|
3
|
-
export type AdapterOptionsType = Partial<XMLHttpRequest>;
|
|
4
|
-
export type AdapterPayloadMappingType = (data: unknown) => string | FormData;
|
|
5
|
-
export type ResponseType<GenericDataType, GenericErrorType> = [
|
|
6
|
-
GenericDataType | null,
|
|
7
|
-
GenericErrorType | null,
|
|
8
|
-
number | null
|
|
9
|
-
];
|
|
10
|
-
export type ResponseSuccessType<GenericDataType> = [GenericDataType, null, number | null];
|
|
11
|
-
export type ResponseErrorType<GenericErrorType> = [null, GenericErrorType, number | null];
|
|
12
|
-
export type QueryParamValuesType = number | string | boolean | null | undefined;
|
|
13
|
-
export type QueryParamType = QueryParamValuesType | Array<QueryParamValuesType> | Record<string, QueryParamValuesType>;
|
|
14
|
-
export type QueryParamsType = Record<string, QueryParamType>;
|
|
15
|
-
export type HeaderMappingType = <T extends RequestInstance>(request: T) => HeadersInit;
|
|
16
|
-
export type AdapterHeadersProps = {
|
|
17
|
-
isFormData: boolean;
|
|
18
|
-
headers: HeadersInit | undefined;
|
|
19
|
-
};
|
|
20
|
-
export type QueryStringifyOptionsType = {
|
|
21
|
-
/**
|
|
22
|
-
* Strict URI encoding
|
|
23
|
-
*/
|
|
24
|
-
strict?: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Encode keys and values
|
|
27
|
-
*/
|
|
28
|
-
encode?: boolean;
|
|
29
|
-
/**
|
|
30
|
-
* Array encoding type
|
|
31
|
-
*/
|
|
32
|
-
arrayFormat?: "bracket" | "index" | "comma" | "separator" | "bracket-separator" | "none";
|
|
33
|
-
/**
|
|
34
|
-
* Array format separator
|
|
35
|
-
*/
|
|
36
|
-
arraySeparator?: string;
|
|
37
|
-
/**
|
|
38
|
-
* Skip keys with null values
|
|
39
|
-
*/
|
|
40
|
-
skipNull?: boolean;
|
|
41
|
-
/**
|
|
42
|
-
* Skip keys with empty string
|
|
43
|
-
*/
|
|
44
|
-
skipEmptyString?: boolean;
|
|
45
|
-
};
|
|
46
|
-
export type ProgressDataType = {
|
|
47
|
-
total?: number;
|
|
48
|
-
loaded?: number;
|
|
49
|
-
};
|
|
50
|
-
export type ProgressType = {
|
|
51
|
-
progress: number;
|
|
52
|
-
timeLeft: number | null;
|
|
53
|
-
sizeLeft: number;
|
|
54
|
-
total: number;
|
|
55
|
-
loaded: number;
|
|
56
|
-
startTimestamp: number;
|
|
57
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { RequestInstance } from "request";
|
|
2
|
-
import { ExtractErrorType } from "types";
|
|
3
|
-
export declare const getErrorMessage: (errorCase?: "timeout" | "abort" | "deleted") => Error;
|
|
4
|
-
export declare const parseResponse: (response: string | unknown) => any;
|
|
5
|
-
export declare const parseErrorResponse: <T extends RequestInstance>(response: unknown) => ExtractErrorType<T>;
|
|
6
|
-
export declare const getUploadSize: (payload: FormData | string) => number;
|
|
7
|
-
export declare const fileToBuffer: (file: File | Blob) => Promise<Uint8Array>;
|
|
8
|
-
export declare const getStreamPayload: (payload: FormData | string | null) => string | Promise<(string | Uint8Array)[]>;
|
package/dist/adapter/index.d.ts
DELETED
package/dist/cache/cache.d.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import EventEmitter from "events";
|
|
4
|
-
import { ResponseType } from "adapter";
|
|
5
|
-
import { ClientInstance } from "client";
|
|
6
|
-
import { ResponseDetailsType } from "managers";
|
|
7
|
-
import { CacheOptionsType, CacheAsyncStorageType, CacheStorageType, getCacheEvents, CacheValueType } from "cache";
|
|
8
|
-
import { RequestDump, RequestInstance } from "request";
|
|
9
|
-
/**
|
|
10
|
-
* Cache class handles the data exchange with the dispatchers.
|
|
11
|
-
*
|
|
12
|
-
* @note
|
|
13
|
-
* Keys used to save the values are created dynamically on the Request class
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
export declare class Cache {
|
|
17
|
-
client: ClientInstance;
|
|
18
|
-
options?: CacheOptionsType;
|
|
19
|
-
emitter: EventEmitter;
|
|
20
|
-
events: ReturnType<typeof getCacheEvents>;
|
|
21
|
-
storage: CacheStorageType;
|
|
22
|
-
lazyStorage?: CacheAsyncStorageType;
|
|
23
|
-
clearKey: string;
|
|
24
|
-
garbageCollectors: Map<string, NodeJS.Timeout>;
|
|
25
|
-
private logger;
|
|
26
|
-
constructor(client: ClientInstance, options?: CacheOptionsType);
|
|
27
|
-
/**
|
|
28
|
-
* Set the cache data to the storage
|
|
29
|
-
* @param request
|
|
30
|
-
* @param response
|
|
31
|
-
* @param details
|
|
32
|
-
* @returns
|
|
33
|
-
*/
|
|
34
|
-
set: <Response_1, Error_1>(request: RequestInstance | RequestDump<RequestInstance>, response: ResponseType<Response_1, Error_1>, details: ResponseDetailsType) => void;
|
|
35
|
-
/**
|
|
36
|
-
* Get particular record from storage by cacheKey. It will trigger lazyStorage to emit lazy load event for reading it's data.
|
|
37
|
-
* @param cacheKey
|
|
38
|
-
* @returns
|
|
39
|
-
*/
|
|
40
|
-
get: <Response_1, Error_1>(cacheKey: string) => CacheValueType<Response_1, Error_1>;
|
|
41
|
-
/**
|
|
42
|
-
* Get sync storage keys, lazyStorage keys will not be included
|
|
43
|
-
* @returns
|
|
44
|
-
*/
|
|
45
|
-
keys: () => string[];
|
|
46
|
-
/**
|
|
47
|
-
* Delete record from storages and trigger revalidation
|
|
48
|
-
* @param cacheKey
|
|
49
|
-
*/
|
|
50
|
-
delete: (cacheKey: string) => void;
|
|
51
|
-
/**
|
|
52
|
-
* Revalidate cache by cacheKey or partial matching with RegExp
|
|
53
|
-
* @param cacheKey
|
|
54
|
-
*/
|
|
55
|
-
revalidate: (cacheKey: string | RegExp) => Promise<void>;
|
|
56
|
-
/**
|
|
57
|
-
* Used to receive data from lazy storage
|
|
58
|
-
* @param cacheKey
|
|
59
|
-
*/
|
|
60
|
-
getLazyResource: <Response_1, Error_1>(cacheKey: string) => Promise<CacheValueType<Response_1, Error_1>>;
|
|
61
|
-
/**
|
|
62
|
-
* Used to receive keys from sync storage and lazy storage
|
|
63
|
-
* @param cacheKey
|
|
64
|
-
*/
|
|
65
|
-
getLazyKeys: () => Promise<string[]>;
|
|
66
|
-
/**
|
|
67
|
-
* Schedule garbage collection for given key
|
|
68
|
-
* @param cacheKey
|
|
69
|
-
* @returns
|
|
70
|
-
*/
|
|
71
|
-
scheduleGarbageCollector: (cacheKey: string) => Promise<void>;
|
|
72
|
-
/**
|
|
73
|
-
* Clear cache storages
|
|
74
|
-
*/
|
|
75
|
-
clear: () => Promise<void>;
|
|
76
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "events";
|
|
2
|
-
import { CacheValueType } from "cache";
|
|
3
|
-
export declare const getCacheEvents: (emitter: EventEmitter) => {
|
|
4
|
-
/**
|
|
5
|
-
* Set cache data
|
|
6
|
-
* @param cacheKey
|
|
7
|
-
* @param data
|
|
8
|
-
*/
|
|
9
|
-
emitCacheData: <Response_1, Error_1>(cacheKey: string, data: CacheValueType<Response_1, Error_1>) => void;
|
|
10
|
-
/**
|
|
11
|
-
* Revalidate cache values event
|
|
12
|
-
*/
|
|
13
|
-
emitRevalidation: (cacheKey: string) => void;
|
|
14
|
-
/**
|
|
15
|
-
* Cache data listener
|
|
16
|
-
* @param cacheKey
|
|
17
|
-
* @param callback
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
|
-
onData: <Response_2, Error_2>(cacheKey: string, callback: (data: CacheValueType<Response_2, Error_2>) => void) => VoidFunction;
|
|
21
|
-
/**
|
|
22
|
-
* Cache invalidation listener
|
|
23
|
-
* @param cacheKey
|
|
24
|
-
* @param callback
|
|
25
|
-
* @returns
|
|
26
|
-
*/
|
|
27
|
-
onRevalidate: (cacheKey: string, callback: () => void) => VoidFunction;
|
|
28
|
-
};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { Cache } from "cache";
|
|
2
|
-
import { ResponseType } from "adapter";
|
|
3
|
-
import { ResponseDetailsType } from "managers";
|
|
4
|
-
export type CacheOptionsType = {
|
|
5
|
-
/**
|
|
6
|
-
* Assign your custom sync storage
|
|
7
|
-
*/
|
|
8
|
-
storage?: CacheStorageType;
|
|
9
|
-
/**
|
|
10
|
-
* Lazy loading from remote resources - possibly persistent
|
|
11
|
-
*/
|
|
12
|
-
lazyStorage?: CacheAsyncStorageType;
|
|
13
|
-
/**
|
|
14
|
-
* Key to clear lazy storage data
|
|
15
|
-
*/
|
|
16
|
-
clearKey?: string;
|
|
17
|
-
/**
|
|
18
|
-
* Initialization callback
|
|
19
|
-
*/
|
|
20
|
-
onInitialization?: (cache: Cache) => void;
|
|
21
|
-
/**
|
|
22
|
-
* Callback for every change in the storage
|
|
23
|
-
*/
|
|
24
|
-
onChange?: <Response = any, Error = any>(key: string, data: CacheValueType<Response, Error>) => void;
|
|
25
|
-
/**
|
|
26
|
-
* Callback for every delete in the storage
|
|
27
|
-
*/
|
|
28
|
-
onDelete?: (key: string) => void;
|
|
29
|
-
};
|
|
30
|
-
export type CacheValueType<Response = any, Error = any> = {
|
|
31
|
-
data: ResponseType<Response, Error>;
|
|
32
|
-
details: ResponseDetailsType;
|
|
33
|
-
cacheTime: number;
|
|
34
|
-
clearKey: string;
|
|
35
|
-
garbageCollection: number;
|
|
36
|
-
};
|
|
37
|
-
export type CacheAsyncStorageType = {
|
|
38
|
-
set: <Response, Error>(key: string, data: CacheValueType<Response, Error>) => Promise<void>;
|
|
39
|
-
get: <Response, Error>(key: string) => Promise<CacheValueType<Response, Error> | undefined>;
|
|
40
|
-
keys: () => Promise<string[] | IterableIterator<string> | string[]>;
|
|
41
|
-
delete: (key: string) => Promise<void>;
|
|
42
|
-
};
|
|
43
|
-
export type CacheStorageType = {
|
|
44
|
-
set: <Response, Error>(key: string, data: CacheValueType<Response, Error>) => void;
|
|
45
|
-
get: <Response, Error>(key: string) => CacheValueType<Response, Error> | undefined;
|
|
46
|
-
keys: () => string[] | IterableIterator<string> | string[];
|
|
47
|
-
delete: (key: string) => void;
|
|
48
|
-
clear: () => void;
|
|
49
|
-
};
|
|
50
|
-
export type CacheInitialData = Record<string, CacheValueType>;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { RequestInstance } from "request";
|
|
2
|
-
import { ExtractAdapterReturnType } from "types";
|
|
3
|
-
export declare const getCacheData: <T extends RequestInstance>(previousResponse: ExtractAdapterReturnType<T>, response: ExtractAdapterReturnType<T>) => ExtractAdapterReturnType<T>;
|
|
4
|
-
export declare const getRevalidateEventKey: (key: string) => string;
|
|
5
|
-
export declare const getCacheKey: (key: string) => string;
|
|
6
|
-
export declare const getCacheIdKey: (key: string) => string;
|
package/dist/cache/index.d.ts
DELETED