@hyper-fetch/core 6.0.0 → 7.0.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 +8 -3
- package/README.md +5 -5
- package/dist/browser/index.js +3711 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/index.d.ts +1669 -1091
- package/dist/index.js +3949 -0
- package/dist/index.js.map +7 -0
- package/package.json +16 -18
- package/src/adapter/adapter.bindings.ts +261 -165
- package/src/adapter/adapter.ts +480 -8
- package/src/adapter/adapter.types.ts +111 -139
- package/src/adapter/adapter.utils.ts +71 -25
- package/src/adapter/index.ts +1 -2
- package/src/cache/cache.events.ts +58 -14
- package/src/cache/cache.ts +158 -91
- package/src/cache/cache.types.ts +15 -25
- package/src/cache/cache.utils.ts +17 -10
- package/src/client/client.create.ts +17 -0
- package/src/client/client.ts +247 -266
- package/src/client/client.types.ts +43 -21
- package/src/client/client.utils.ts +7 -168
- package/src/client/index.ts +1 -0
- package/src/constants/http.constants.ts +6 -6
- package/src/constants/time.constants.ts +10 -10
- package/src/dispatcher/dispatcher.constants.ts +5 -5
- package/src/dispatcher/dispatcher.events.ts +85 -20
- package/src/dispatcher/dispatcher.ts +265 -199
- package/src/dispatcher/dispatcher.types.ts +6 -7
- package/src/dispatcher/dispatcher.utils.ts +26 -10
- package/src/http-adapter/http-adapter.browser.ts +101 -0
- package/src/http-adapter/http-adapter.constants.ts +8 -0
- package/src/http-adapter/http-adapter.server.ts +134 -0
- package/src/http-adapter/http-adapter.ts +11 -0
- package/src/http-adapter/http-adapter.types.ts +86 -0
- package/src/http-adapter/http-adapter.utils.ts +177 -0
- package/src/http-adapter/index.ts +4 -0
- package/src/index.ts +2 -1
- package/src/managers/app/app.manager.constants.ts +4 -4
- package/src/managers/app/app.manager.events.ts +12 -12
- package/src/managers/app/app.manager.ts +10 -7
- package/src/managers/app/app.manager.utils.ts +12 -2
- package/src/managers/logger/logger.manager.constants.ts +13 -18
- package/src/managers/logger/logger.manager.ts +37 -49
- package/src/managers/logger/logger.manager.types.ts +32 -21
- package/src/managers/logger/logger.manager.utils.ts +23 -17
- package/src/managers/request/request.manager.events.ts +222 -109
- package/src/managers/request/request.manager.ts +5 -2
- package/src/managers/request/request.manager.types.ts +35 -7
- package/src/managers/request/request.manager.utils.ts +29 -17
- package/src/mocker/mocker.ts +115 -81
- package/src/mocker/mocker.types.ts +29 -25
- package/src/plugin/index.ts +2 -0
- package/src/plugin/plugin.ts +159 -0
- package/src/plugin/plugin.types.ts +181 -0
- package/src/request/request.ts +371 -323
- package/src/request/request.types.ts +136 -145
- package/src/request/request.utils.ts +31 -30
- package/src/types/adapter.types.ts +63 -0
- package/src/types/client.types.ts +3 -3
- package/src/types/helpers.types.ts +16 -3
- package/src/types/hydration.types.ts +16 -0
- package/src/types/index.ts +3 -1
- package/src/types/request.types.ts +73 -0
- package/src/utils/emitter.utils.ts +52 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +4 -2
- package/tsconfig.base.json +4 -5
- package/tsconfig.json +5 -4
- package/dist/browser/index.cjs.js +0 -2420
- package/dist/browser/index.cjs.js.map +0 -7
- package/dist/browser/index.esm.js +0 -2395
- package/dist/browser/index.esm.js.map +0 -7
- package/dist/index.cjs.js +0 -2579
- package/dist/index.cjs.js.map +0 -7
- package/dist/index.esm.js +0 -2474
- package/dist/index.esm.js.map +0 -7
- package/src/adapter/adapter.browser.ts +0 -100
- package/src/adapter/adapter.constants.ts +0 -8
- package/src/adapter/adapter.server.ts +0 -93
- package/src/effect/index.ts +0 -2
- package/src/effect/request.effect.ts +0 -31
- package/src/effect/request.effect.types.ts +0 -44
- package/src/types/fetch.types.ts +0 -50
|
@@ -1,166 +1,143 @@
|
|
|
1
1
|
import { RequestInstance } from "request";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
EmptyTypes,
|
|
4
|
+
ExtractAdapterEndpointMapperType,
|
|
5
|
+
ExtractAdapterExtraType,
|
|
6
|
+
ExtractAdapterPayloadMapperType,
|
|
7
|
+
ExtractAdapterHeaderMapperType,
|
|
8
|
+
ExtractAdapterQueryParamsMapperType,
|
|
9
|
+
ExtractAdapterStatusType,
|
|
10
|
+
ExtractAdapterType,
|
|
11
|
+
ExtractErrorType,
|
|
12
|
+
ExtractResponseType,
|
|
13
|
+
TypeWithDefaults,
|
|
14
|
+
} from "../types";
|
|
15
|
+
import { Adapter, DefaultMapperType } from "./adapter";
|
|
16
|
+
import { getAdapterBindings } from "./adapter.bindings";
|
|
3
17
|
|
|
4
18
|
/**
|
|
5
19
|
* Base Adapter
|
|
6
20
|
*/
|
|
7
21
|
|
|
8
|
-
export type AdapterInstance =
|
|
22
|
+
export type AdapterInstance = Adapter<any, any, any, any, any, any, any, any, any, any, any>;
|
|
9
23
|
|
|
10
|
-
export type
|
|
11
|
-
AdapterOptions
|
|
12
|
-
MethodType
|
|
13
|
-
StatusType
|
|
14
|
-
Extra extends Record<string, any
|
|
15
|
-
QueryParams = QueryParamsType | string,
|
|
24
|
+
export type AdapterGenericType<
|
|
25
|
+
AdapterOptions,
|
|
26
|
+
MethodType extends string,
|
|
27
|
+
StatusType extends number | string,
|
|
28
|
+
Extra extends Record<string, any>,
|
|
29
|
+
QueryParams = QueryParamsType | string | EmptyTypes,
|
|
30
|
+
DefaultQueryParams = undefined,
|
|
16
31
|
EndpointType = string,
|
|
17
|
-
> =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*/
|
|
32
|
+
EndpointMapperType extends EndpointMapper<EndpointType> | DefaultMapperType = DefaultMapperType,
|
|
33
|
+
QueryParamsMapperType extends QueryParamsMapper<QueryParams> | DefaultMapperType = DefaultMapperType,
|
|
34
|
+
HeaderMapperType extends HeaderMappingType | DefaultMapperType = DefaultMapperType,
|
|
35
|
+
PayloadMapperType extends AdapterPayloadMappingType | DefaultMapperType = DefaultMapperType,
|
|
36
|
+
> = {
|
|
37
|
+
adapterOptions: AdapterOptions;
|
|
38
|
+
methodType: MethodType;
|
|
39
|
+
statusType: StatusType;
|
|
40
|
+
extra: Extra;
|
|
41
|
+
queryParams?: QueryParams;
|
|
42
|
+
defaultQueryParams?: DefaultQueryParams;
|
|
43
|
+
endpointType: EndpointType;
|
|
44
|
+
endpointMapperType?: EndpointMapperType;
|
|
45
|
+
queryParamsMapperType?: QueryParamsMapperType;
|
|
46
|
+
headerMapperType?: HeaderMapperType;
|
|
47
|
+
payloadMapperType?: PayloadMapperType;
|
|
48
|
+
};
|
|
35
49
|
|
|
36
|
-
export type
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
export type DeclareAdapterType<
|
|
51
|
+
Properties extends AdapterGenericType<any, any, any, any, any, any, any, any, any, any>,
|
|
52
|
+
> = Adapter<
|
|
53
|
+
TypeWithDefaults<Properties, "adapterOptions", any>,
|
|
54
|
+
TypeWithDefaults<Properties, "methodType", any>,
|
|
55
|
+
TypeWithDefaults<Properties, "statusType", any>,
|
|
56
|
+
TypeWithDefaults<Properties, "extra", any>,
|
|
57
|
+
TypeWithDefaults<Properties, "queryParams", QueryParamsType | string | EmptyTypes>,
|
|
58
|
+
TypeWithDefaults<Properties, "defaultQueryParams", undefined>,
|
|
59
|
+
TypeWithDefaults<Properties, "endpointType", string>,
|
|
60
|
+
TypeWithDefaults<Properties, "endpointMapperType", DefaultMapperType>,
|
|
61
|
+
TypeWithDefaults<Properties, "queryParamsMapperType", DefaultMapperType>,
|
|
62
|
+
TypeWithDefaults<Properties, "headerMapperType", DefaultMapperType>,
|
|
63
|
+
NonNullable<TypeWithDefaults<Properties, "payloadMapperType", DefaultMapperType>>
|
|
64
|
+
>;
|
|
65
|
+
|
|
66
|
+
export type AdapterFetcherType<Adapter extends AdapterInstance> = (
|
|
67
|
+
options: Omit<Awaited<ReturnType<typeof getAdapterBindings<Adapter>>>, "payload"> & {
|
|
68
|
+
url: string;
|
|
69
|
+
endpoint: ReturnType<ExtractAdapterEndpointMapperType<Adapter>>;
|
|
70
|
+
queryParams: ReturnType<ExtractAdapterQueryParamsMapperType<Adapter>>;
|
|
71
|
+
headers: ReturnType<ExtractAdapterHeaderMapperType<Adapter>>;
|
|
72
|
+
payload: ReturnType<ExtractAdapterPayloadMapperType<Adapter>>;
|
|
73
|
+
requestId: string;
|
|
74
|
+
request: RequestInstance;
|
|
52
75
|
},
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
>
|
|
75
|
-
: never;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Options
|
|
79
|
-
*/
|
|
76
|
+
) => void;
|
|
77
|
+
|
|
78
|
+
// Mappers
|
|
79
|
+
|
|
80
|
+
export type HeaderMappingType<Config = never> = (request: RequestInstance, config?: Config) => HeadersInit;
|
|
81
|
+
export type EndpointMapper<EndpointType, Config = never> = (endpoint: EndpointType, config?: Config) => string;
|
|
82
|
+
export type QueryParamsMapper<QueryParams, Config = never> = (
|
|
83
|
+
queryParams: QueryParams | EmptyTypes,
|
|
84
|
+
config?: Config,
|
|
85
|
+
) => any;
|
|
86
|
+
export type AdapterPayloadMappingType<Config = never> = (
|
|
87
|
+
options: { request: RequestInstance; payload: unknown },
|
|
88
|
+
config?: Config,
|
|
89
|
+
) => any;
|
|
90
|
+
|
|
91
|
+
export type DefaultEndpointMapper = (endpoint: string) => string;
|
|
92
|
+
export type DefaultQueryParamsMapper = (
|
|
93
|
+
queryParams: QueryParamsType | string | EmptyTypes,
|
|
94
|
+
) => QueryParamsType | string | EmptyTypes;
|
|
95
|
+
export type DefaultHeaderMapper = (request: RequestInstance) => HeadersInit;
|
|
96
|
+
export type DefaultPayloadMapper = (options: { request: RequestInstance; payload: unknown }) => string;
|
|
80
97
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
export type AdapterExtraType = {
|
|
84
|
-
headers: Record<string, string>;
|
|
85
|
-
};
|
|
98
|
+
// QueryParams
|
|
86
99
|
|
|
87
|
-
export type
|
|
100
|
+
export type QueryParamValuesType = number | string | boolean | null | undefined | Record<any, any>;
|
|
101
|
+
export type QueryParamType = QueryParamValuesType | Array<QueryParamValuesType> | Record<string, QueryParamValuesType>;
|
|
102
|
+
export type QueryParamsType = Record<string, QueryParamType>;
|
|
88
103
|
|
|
89
104
|
// Responses
|
|
90
105
|
|
|
91
|
-
export type
|
|
106
|
+
export type RequestResponseType<Request extends RequestInstance> = {
|
|
107
|
+
data: ExtractResponseType<Request> | null;
|
|
108
|
+
error: ExtractErrorType<Request> | null;
|
|
109
|
+
status: ExtractAdapterStatusType<ExtractAdapterType<Request>> | null;
|
|
110
|
+
success: true | false;
|
|
111
|
+
extra: ExtractAdapterExtraType<ExtractAdapterType<Request>> | null;
|
|
112
|
+
responseTimestamp: number;
|
|
113
|
+
requestTimestamp: number;
|
|
114
|
+
};
|
|
115
|
+
export type ResponseType<GenericDataType, GenericErrorType, Adapter extends AdapterInstance> = {
|
|
92
116
|
data: GenericDataType | null;
|
|
93
117
|
error: GenericErrorType | null;
|
|
94
118
|
status: ExtractAdapterStatusType<Adapter> | null;
|
|
95
|
-
success:
|
|
119
|
+
success: true | false;
|
|
96
120
|
extra: ExtractAdapterExtraType<Adapter> | null;
|
|
121
|
+
responseTimestamp: number;
|
|
122
|
+
requestTimestamp: number;
|
|
97
123
|
};
|
|
98
|
-
export type
|
|
124
|
+
export type ResponseSuccessType<GenericDataType, Adapter extends AdapterInstance> = {
|
|
99
125
|
data: GenericDataType;
|
|
100
126
|
error: null;
|
|
101
127
|
status: ExtractAdapterStatusType<Adapter> | null;
|
|
102
|
-
success:
|
|
128
|
+
success: true;
|
|
103
129
|
extra: ExtractAdapterExtraType<Adapter> | null;
|
|
130
|
+
responseTimestamp: number;
|
|
131
|
+
requestTimestamp: number;
|
|
104
132
|
};
|
|
105
|
-
export type
|
|
133
|
+
export type ResponseErrorType<GenericErrorType, Adapter extends AdapterInstance> = {
|
|
106
134
|
data: null;
|
|
107
135
|
error: GenericErrorType;
|
|
108
136
|
status: ExtractAdapterStatusType<Adapter> | null;
|
|
109
|
-
success:
|
|
137
|
+
success: false;
|
|
110
138
|
extra: ExtractAdapterExtraType<Adapter> | null;
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
// QueryParams
|
|
114
|
-
|
|
115
|
-
export type QueryParamValuesType = number | string | boolean | null | undefined | Record<any, any>;
|
|
116
|
-
export type QueryParamType = QueryParamValuesType | Array<QueryParamValuesType> | Record<string, QueryParamValuesType>;
|
|
117
|
-
export type QueryParamsType = Record<string, QueryParamType>;
|
|
118
|
-
|
|
119
|
-
// Headers
|
|
120
|
-
|
|
121
|
-
export type HeaderMappingType = <T extends RequestInstance>(request: T) => HeadersInit;
|
|
122
|
-
|
|
123
|
-
export type AdapterHeadersProps = {
|
|
124
|
-
isFormData: boolean;
|
|
125
|
-
headers: HeadersInit | undefined;
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
// Stringify
|
|
129
|
-
|
|
130
|
-
export type QueryStringifyOptionsType = {
|
|
131
|
-
/**
|
|
132
|
-
* Strict URI encoding
|
|
133
|
-
*/
|
|
134
|
-
strict?: boolean;
|
|
135
|
-
/**
|
|
136
|
-
* Encode keys and values
|
|
137
|
-
*/
|
|
138
|
-
encode?: boolean;
|
|
139
|
-
/**
|
|
140
|
-
* Array encoding type
|
|
141
|
-
*/
|
|
142
|
-
arrayFormat?: "bracket" | "index" | "comma" | "separator" | "bracket-separator" | "none";
|
|
143
|
-
/**
|
|
144
|
-
* Array format separator
|
|
145
|
-
*/
|
|
146
|
-
arraySeparator?: string;
|
|
147
|
-
/**
|
|
148
|
-
* Skip keys with null values
|
|
149
|
-
*/
|
|
150
|
-
skipNull?: boolean;
|
|
151
|
-
/**
|
|
152
|
-
* Skip keys with empty string
|
|
153
|
-
*/
|
|
154
|
-
skipEmptyString?: boolean;
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Parsing function for date type query param
|
|
158
|
-
*/
|
|
159
|
-
dateParser?: (value: QueryParamType) => string;
|
|
160
|
-
/**
|
|
161
|
-
* Parsing function for object type query param
|
|
162
|
-
*/
|
|
163
|
-
objectParser?: (value: QueryParamType) => string;
|
|
139
|
+
responseTimestamp: number;
|
|
140
|
+
requestTimestamp: number;
|
|
164
141
|
};
|
|
165
142
|
|
|
166
143
|
// Progress
|
|
@@ -178,8 +155,3 @@ export type ProgressType = {
|
|
|
178
155
|
loaded: number;
|
|
179
156
|
startTimestamp: number;
|
|
180
157
|
};
|
|
181
|
-
|
|
182
|
-
export type AdapterBindingsReturnType = {
|
|
183
|
-
fullUrl: string;
|
|
184
|
-
config;
|
|
185
|
-
};
|
|
@@ -1,42 +1,88 @@
|
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
|
2
|
+
// Utils
|
|
3
|
+
import { hasWindow } from "managers";
|
|
1
4
|
import { RequestInstance } from "request";
|
|
2
|
-
import { ExtractErrorType } from "types";
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
export class TimeoutError extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super("Request timeout");
|
|
9
|
+
this.name = "TimeoutError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
5
12
|
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
export class AbortError extends Error {
|
|
14
|
+
constructor() {
|
|
15
|
+
super("Request aborted");
|
|
16
|
+
this.name = "AbortError";
|
|
9
17
|
}
|
|
10
|
-
|
|
11
|
-
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class DeletedError extends Error {
|
|
21
|
+
constructor() {
|
|
22
|
+
super("Request deleted");
|
|
23
|
+
this.name = "DeletedError";
|
|
12
24
|
}
|
|
13
|
-
|
|
14
|
-
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class RequestProcessingError extends Error {
|
|
28
|
+
description?: string;
|
|
15
29
|
|
|
16
|
-
|
|
17
|
-
|
|
30
|
+
constructor(description?: string) {
|
|
31
|
+
super("Request processing error");
|
|
32
|
+
this.description = description;
|
|
33
|
+
this.name = "RequestProcessingError";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
18
36
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
37
|
+
export class UnexpectedError extends Error {
|
|
38
|
+
description?: string;
|
|
39
|
+
|
|
40
|
+
constructor(description?: string) {
|
|
41
|
+
super("Unexpected error");
|
|
42
|
+
this.description = description;
|
|
43
|
+
this.name = "UnexpectedError";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
26
46
|
|
|
27
|
-
|
|
47
|
+
export const getErrorMessage = (errorCase?: "timeout" | "abort" | "deleted" | "processing" | "unexpected") => {
|
|
48
|
+
if (errorCase === "timeout") {
|
|
49
|
+
return new TimeoutError();
|
|
50
|
+
}
|
|
51
|
+
if (errorCase === "abort") {
|
|
52
|
+
return new AbortError();
|
|
53
|
+
}
|
|
54
|
+
if (errorCase === "deleted") {
|
|
55
|
+
return new DeletedError();
|
|
56
|
+
}
|
|
57
|
+
if (errorCase === "processing") {
|
|
58
|
+
return new RequestProcessingError();
|
|
59
|
+
}
|
|
60
|
+
return new UnexpectedError();
|
|
28
61
|
};
|
|
29
62
|
|
|
30
|
-
//
|
|
63
|
+
// Mappers
|
|
31
64
|
|
|
32
|
-
export const
|
|
65
|
+
export const stringifyValue = (response: string | unknown): string => {
|
|
33
66
|
try {
|
|
34
|
-
return JSON.
|
|
67
|
+
return JSON.stringify(response as string);
|
|
35
68
|
} catch (err) {
|
|
36
|
-
return
|
|
69
|
+
return "";
|
|
37
70
|
}
|
|
38
71
|
};
|
|
39
72
|
|
|
40
|
-
export const
|
|
41
|
-
|
|
73
|
+
export const getAdapterHeaders = (request: RequestInstance) => {
|
|
74
|
+
const isFormData = hasWindow() && request.payload instanceof FormData;
|
|
75
|
+
const headers: HeadersInit = {};
|
|
76
|
+
|
|
77
|
+
if (!isFormData) headers["Content-Type"] = "application/json";
|
|
78
|
+
|
|
79
|
+
Object.assign(headers, request.headers);
|
|
80
|
+
return headers as HeadersInit;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const getAdapterPayload = (data: unknown): string | FormData => {
|
|
84
|
+
const isFormData = hasWindow() && data instanceof FormData;
|
|
85
|
+
if (isFormData) return data;
|
|
86
|
+
|
|
87
|
+
return stringifyValue(data);
|
|
42
88
|
};
|
package/src/adapter/index.ts
CHANGED
|
@@ -1,38 +1,74 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
CacheValueType,
|
|
5
|
+
getInvalidateByKey,
|
|
6
|
+
getInvalidateKey,
|
|
7
|
+
getCacheByKey,
|
|
8
|
+
getCacheKey,
|
|
9
|
+
getDeleteKey,
|
|
10
|
+
getDeleteByKey,
|
|
11
|
+
} from "cache";
|
|
4
12
|
import { AdapterInstance } from "adapter";
|
|
5
13
|
|
|
6
14
|
export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
7
15
|
/**
|
|
8
16
|
* Set cache data
|
|
9
|
-
* @param cacheKey
|
|
10
17
|
* @param data
|
|
11
18
|
*/
|
|
12
19
|
emitCacheData: <Response, Error, Adapter extends AdapterInstance>(
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
data: CacheValueType<Response, Error, Adapter> & { cached: boolean },
|
|
21
|
+
isTriggeredExternally = false,
|
|
15
22
|
): void => {
|
|
16
|
-
emitter.emit(getCacheKey(
|
|
23
|
+
emitter.emit(getCacheKey(), data, isTriggeredExternally);
|
|
24
|
+
emitter.emit(getCacheByKey(data.cacheKey), data, isTriggeredExternally);
|
|
17
25
|
},
|
|
18
26
|
/**
|
|
19
27
|
* Invalidate cache values event
|
|
20
28
|
*/
|
|
21
|
-
emitInvalidation: (cacheKey: string): void => {
|
|
22
|
-
emitter.emit(
|
|
29
|
+
emitInvalidation: (cacheKey: string, isTriggeredExternally = false): void => {
|
|
30
|
+
emitter.emit(getInvalidateKey(), cacheKey, isTriggeredExternally);
|
|
31
|
+
emitter.emit(getInvalidateByKey(cacheKey), isTriggeredExternally);
|
|
32
|
+
},
|
|
33
|
+
/**
|
|
34
|
+
* Delete of cache values
|
|
35
|
+
*/
|
|
36
|
+
emitDelete: (cacheKey: string, isTriggeredExternally = false): void => {
|
|
37
|
+
emitter.emit(getDeleteKey(), cacheKey, isTriggeredExternally);
|
|
38
|
+
emitter.emit(getDeleteByKey(cacheKey), isTriggeredExternally);
|
|
23
39
|
},
|
|
24
|
-
/**
|
|
40
|
+
/**
|
|
25
41
|
* Cache data listener
|
|
26
|
-
* @param cacheKey
|
|
27
42
|
* @param callback
|
|
28
43
|
* @returns
|
|
29
44
|
*/
|
|
30
45
|
onData: <Response, Error, Adapter extends AdapterInstance>(
|
|
46
|
+
callback: (data: CacheValueType<Response, Error, Adapter> & { cached: boolean }) => void,
|
|
47
|
+
): VoidFunction => {
|
|
48
|
+
emitter.on(getCacheKey(), callback);
|
|
49
|
+
return () => emitter.removeListener(getCacheKey(), callback);
|
|
50
|
+
},
|
|
51
|
+
/**
|
|
52
|
+
* Cache data listener
|
|
53
|
+
* @param cacheKey
|
|
54
|
+
* @param callback
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
onDataByKey: <Response, Error, Adapter extends AdapterInstance>(
|
|
31
58
|
cacheKey: string,
|
|
32
59
|
callback: (data: CacheValueType<Response, Error, Adapter>) => void,
|
|
33
60
|
): VoidFunction => {
|
|
34
|
-
emitter.on(
|
|
35
|
-
return () => emitter.removeListener(
|
|
61
|
+
emitter.on(getCacheByKey(cacheKey), callback);
|
|
62
|
+
return () => emitter.removeListener(getCacheByKey(cacheKey), callback);
|
|
63
|
+
},
|
|
64
|
+
/**
|
|
65
|
+
* Cache invalidation listener
|
|
66
|
+
* @param callback
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
onInvalidate: (callback: (cacheKey: string) => void): VoidFunction => {
|
|
70
|
+
emitter.on(getInvalidateKey(), callback);
|
|
71
|
+
return () => emitter.removeListener(getInvalidateKey(), callback);
|
|
36
72
|
},
|
|
37
73
|
/**
|
|
38
74
|
* Cache invalidation listener
|
|
@@ -40,8 +76,16 @@ export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
|
40
76
|
* @param callback
|
|
41
77
|
* @returns
|
|
42
78
|
*/
|
|
43
|
-
|
|
44
|
-
emitter.on(
|
|
45
|
-
return () => emitter.removeListener(
|
|
79
|
+
onInvalidateByKey: (cacheKey: string, callback: () => void): VoidFunction => {
|
|
80
|
+
emitter.on(getInvalidateByKey(cacheKey), callback);
|
|
81
|
+
return () => emitter.removeListener(getInvalidateByKey(cacheKey), callback);
|
|
82
|
+
},
|
|
83
|
+
onDelete: (callback: (cacheKey: string) => void): VoidFunction => {
|
|
84
|
+
emitter.on(getDeleteKey(), callback);
|
|
85
|
+
return () => emitter.removeListener(getDeleteKey(), callback);
|
|
86
|
+
},
|
|
87
|
+
onDeleteByKey: (cacheKey: string, callback: () => void): VoidFunction => {
|
|
88
|
+
emitter.on(getDeleteByKey(cacheKey), callback);
|
|
89
|
+
return () => emitter.removeListener(getDeleteByKey(cacheKey), callback);
|
|
46
90
|
},
|
|
47
91
|
});
|