@hyper-fetch/core 6.0.1 → 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.
Files changed (83) hide show
  1. package/.eslintrc.json +8 -3
  2. package/README.md +5 -5
  3. package/dist/browser/index.js +3711 -0
  4. package/dist/browser/index.js.map +7 -0
  5. package/dist/index.d.ts +1669 -1091
  6. package/dist/index.js +3949 -0
  7. package/dist/index.js.map +7 -0
  8. package/package.json +16 -18
  9. package/src/adapter/adapter.bindings.ts +261 -165
  10. package/src/adapter/adapter.ts +480 -8
  11. package/src/adapter/adapter.types.ts +111 -139
  12. package/src/adapter/adapter.utils.ts +71 -25
  13. package/src/adapter/index.ts +1 -2
  14. package/src/cache/cache.events.ts +58 -14
  15. package/src/cache/cache.ts +158 -91
  16. package/src/cache/cache.types.ts +15 -25
  17. package/src/cache/cache.utils.ts +17 -10
  18. package/src/client/client.create.ts +17 -0
  19. package/src/client/client.ts +247 -266
  20. package/src/client/client.types.ts +43 -21
  21. package/src/client/client.utils.ts +7 -168
  22. package/src/client/index.ts +1 -0
  23. package/src/constants/http.constants.ts +6 -6
  24. package/src/constants/time.constants.ts +10 -10
  25. package/src/dispatcher/dispatcher.constants.ts +5 -5
  26. package/src/dispatcher/dispatcher.events.ts +85 -20
  27. package/src/dispatcher/dispatcher.ts +265 -199
  28. package/src/dispatcher/dispatcher.types.ts +6 -7
  29. package/src/dispatcher/dispatcher.utils.ts +26 -10
  30. package/src/http-adapter/http-adapter.browser.ts +101 -0
  31. package/src/http-adapter/http-adapter.constants.ts +8 -0
  32. package/src/http-adapter/http-adapter.server.ts +134 -0
  33. package/src/http-adapter/http-adapter.ts +11 -0
  34. package/src/http-adapter/http-adapter.types.ts +86 -0
  35. package/src/http-adapter/http-adapter.utils.ts +177 -0
  36. package/src/http-adapter/index.ts +4 -0
  37. package/src/index.ts +2 -1
  38. package/src/managers/app/app.manager.constants.ts +4 -4
  39. package/src/managers/app/app.manager.events.ts +12 -12
  40. package/src/managers/app/app.manager.ts +10 -7
  41. package/src/managers/app/app.manager.utils.ts +12 -2
  42. package/src/managers/logger/logger.manager.constants.ts +13 -18
  43. package/src/managers/logger/logger.manager.ts +37 -49
  44. package/src/managers/logger/logger.manager.types.ts +32 -21
  45. package/src/managers/logger/logger.manager.utils.ts +23 -17
  46. package/src/managers/request/request.manager.events.ts +222 -109
  47. package/src/managers/request/request.manager.ts +5 -2
  48. package/src/managers/request/request.manager.types.ts +35 -7
  49. package/src/managers/request/request.manager.utils.ts +29 -17
  50. package/src/mocker/mocker.ts +115 -81
  51. package/src/mocker/mocker.types.ts +29 -25
  52. package/src/plugin/index.ts +2 -0
  53. package/src/plugin/plugin.ts +159 -0
  54. package/src/plugin/plugin.types.ts +181 -0
  55. package/src/request/request.ts +371 -323
  56. package/src/request/request.types.ts +136 -145
  57. package/src/request/request.utils.ts +31 -30
  58. package/src/types/adapter.types.ts +63 -0
  59. package/src/types/client.types.ts +3 -3
  60. package/src/types/helpers.types.ts +16 -3
  61. package/src/types/hydration.types.ts +16 -0
  62. package/src/types/index.ts +3 -1
  63. package/src/types/request.types.ts +73 -0
  64. package/src/utils/emitter.utils.ts +52 -0
  65. package/src/utils/index.ts +1 -0
  66. package/src/utils/uuid.utils.ts +4 -2
  67. package/tsconfig.base.json +4 -5
  68. package/tsconfig.json +5 -4
  69. package/dist/browser/index.cjs.js +0 -2420
  70. package/dist/browser/index.cjs.js.map +0 -7
  71. package/dist/browser/index.esm.js +0 -2395
  72. package/dist/browser/index.esm.js.map +0 -7
  73. package/dist/index.cjs.js +0 -2579
  74. package/dist/index.cjs.js.map +0 -7
  75. package/dist/index.esm.js +0 -2474
  76. package/dist/index.esm.js.map +0 -7
  77. package/src/adapter/adapter.browser.ts +0 -100
  78. package/src/adapter/adapter.constants.ts +0 -8
  79. package/src/adapter/adapter.server.ts +0 -93
  80. package/src/effect/index.ts +0 -2
  81. package/src/effect/request.effect.ts +0 -31
  82. package/src/effect/request.effect.types.ts +0 -44
  83. package/src/types/fetch.types.ts +0 -50
@@ -1,166 +1,143 @@
1
1
  import { RequestInstance } from "request";
2
- import { HttpMethodsType, HttpStatusType } from "../types";
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 = AdapterType<any, any, any, any, any, any>;
22
+ export type AdapterInstance = Adapter<any, any, any, any, any, any, any, any, any, any, any>;
9
23
 
10
- export type AdapterType<
11
- AdapterOptions = AdapterOptionsType,
12
- MethodType = HttpMethodsType,
13
- StatusType = HttpStatusType,
14
- Extra extends Record<string, any> = AdapterExtraType,
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
- request: RequestInstance,
19
- requestId: string,
20
- // This is never used in the application, we pass this type to have unions extracting possibilities
21
- DO_NOT_USE?: {
22
- method?: MethodType;
23
- options?: AdapterOptions;
24
- status?: StatusType;
25
- extra?: Extra;
26
- queryParams?: QueryParams;
27
- endpointType?: EndpointType;
28
- },
29
- // [any any any] as a way to avoid circular reference that destroyed request type.
30
- ) => Promise<ResponseReturnType<any, any, any>>;
31
-
32
- /**
33
- * Extractors
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 ExtractAdapterOptionsType<T> = T extends AdapterType<infer O, any, any, any, any> ? O : never;
37
- export type ExtractAdapterMethodType<T> = T extends AdapterType<any, infer M, any, any, any> ? M : never;
38
- export type ExtractAdapterStatusType<T> = T extends AdapterType<any, any, infer S, any, any> ? S : never;
39
- export type ExtractAdapterExtraType<T> = T extends AdapterType<any, any, any, infer A, any> ? A : never;
40
- export type ExtractAdapterQueryParamsType<T> = T extends AdapterType<any, any, any, any, infer Q> ? Q : never;
41
- export type ExtractAdapterEndpointType<T> = T extends AdapterType<any, any, any, any, any, infer E> ? E : never;
42
- // Special type only for selecting appropriate AdapterType union version (check FirebaseAdapterType).
43
- export type ExtractUnionAdapter<
44
- Adapter extends AdapterInstance,
45
- Values extends {
46
- method?: any;
47
- options?: any;
48
- status?: any;
49
- extra?: any;
50
- queryParams?: any;
51
- endpointType?: any;
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
- > = Extract<
54
- Adapter,
55
- AdapterType<
56
- Values["options"],
57
- Values["method"],
58
- Values["status"],
59
- Values["extra"],
60
- Values["queryParams"],
61
- Values["endpointType"]
62
- >
63
- > extends AdapterInstance
64
- ? Extract<
65
- Adapter,
66
- AdapterType<
67
- Values["options"],
68
- Values["method"],
69
- Values["status"],
70
- Values["extra"],
71
- Values["queryParams"],
72
- Values["endpointType"]
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
- export type AdapterOptionsType = Partial<XMLHttpRequest>;
82
-
83
- export type AdapterExtraType = {
84
- headers: Record<string, string>;
85
- };
98
+ // QueryParams
86
99
 
87
- export type AdapterPayloadMappingType = (data: unknown) => string | FormData;
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 ResponseReturnType<GenericDataType, GenericErrorType, Adapter extends AdapterInstance> = {
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: boolean;
119
+ success: true | false;
96
120
  extra: ExtractAdapterExtraType<Adapter> | null;
121
+ responseTimestamp: number;
122
+ requestTimestamp: number;
97
123
  };
98
- export type ResponseReturnSuccessType<GenericDataType, Adapter extends AdapterInstance> = {
124
+ export type ResponseSuccessType<GenericDataType, Adapter extends AdapterInstance> = {
99
125
  data: GenericDataType;
100
126
  error: null;
101
127
  status: ExtractAdapterStatusType<Adapter> | null;
102
- success: boolean;
128
+ success: true;
103
129
  extra: ExtractAdapterExtraType<Adapter> | null;
130
+ responseTimestamp: number;
131
+ requestTimestamp: number;
104
132
  };
105
- export type ResponseReturnErrorType<GenericErrorType, Adapter extends AdapterInstance> = {
133
+ export type ResponseErrorType<GenericErrorType, Adapter extends AdapterInstance> = {
106
134
  data: null;
107
135
  error: GenericErrorType;
108
136
  status: ExtractAdapterStatusType<Adapter> | null;
109
- success: boolean;
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
- // Utils
6
+ export class TimeoutError extends Error {
7
+ constructor() {
8
+ super("Request timeout");
9
+ this.name = "TimeoutError";
10
+ }
11
+ }
5
12
 
6
- export const getErrorMessage = (errorCase?: "timeout" | "abort" | "deleted") => {
7
- if (errorCase === "timeout") {
8
- return new Error("Request timeout");
13
+ export class AbortError extends Error {
14
+ constructor() {
15
+ super("Request aborted");
16
+ this.name = "AbortError";
9
17
  }
10
- if (errorCase === "abort") {
11
- return new Error("Request cancelled");
18
+ }
19
+
20
+ export class DeletedError extends Error {
21
+ constructor() {
22
+ super("Request deleted");
23
+ this.name = "DeletedError";
12
24
  }
13
- return new Error("Unexpected error");
14
- };
25
+ }
26
+
27
+ export class RequestProcessingError extends Error {
28
+ description?: string;
15
29
 
16
- export const getResponseHeaders = (headersString: string): Record<string, string> => {
17
- const arr = headersString.trim().split(/[\r\n]+/);
30
+ constructor(description?: string) {
31
+ super("Request processing error");
32
+ this.description = description;
33
+ this.name = "RequestProcessingError";
34
+ }
35
+ }
18
36
 
19
- const headers = {};
20
- arr.forEach((line) => {
21
- const parts = line.split(": ");
22
- const header = parts.shift();
23
- const value = parts.join(": ");
24
- headers[header] = value;
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
- return headers;
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
- // Responses
63
+ // Mappers
31
64
 
32
- export const parseResponse = (response: string | unknown) => {
65
+ export const stringifyValue = (response: string | unknown): string => {
33
66
  try {
34
- return JSON.parse(response as string);
67
+ return JSON.stringify(response as string);
35
68
  } catch (err) {
36
- return response;
69
+ return "";
37
70
  }
38
71
  };
39
72
 
40
- export const parseErrorResponse = <T extends RequestInstance>(response: unknown): ExtractErrorType<T> => {
41
- return response ? parseResponse(response) : getErrorMessage();
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
  };
@@ -1,5 +1,4 @@
1
1
  export * from "./adapter";
2
2
  export * from "./adapter.types";
3
- export * from "./adapter.utils";
4
3
  export * from "./adapter.bindings";
5
- export * from "./adapter.constants";
4
+ export * from "./adapter.utils";
@@ -1,38 +1,74 @@
1
1
  import EventEmitter from "events";
2
2
 
3
- import { CacheValueType, getInvalidateEventKey, getCacheKey } from "cache";
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
- cacheKey: string,
14
- data: CacheValueType<Response, Error, Adapter>,
20
+ data: CacheValueType<Response, Error, Adapter> & { cached: boolean },
21
+ isTriggeredExternally = false,
15
22
  ): void => {
16
- emitter.emit(getCacheKey(cacheKey), data);
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(getInvalidateEventKey(cacheKey));
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
- /** StatusType
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(getCacheKey(cacheKey), callback);
35
- return () => emitter.removeListener(getCacheKey(cacheKey), callback);
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
- onInvalidate: (cacheKey: string, callback: () => void): VoidFunction => {
44
- emitter.on(getInvalidateEventKey(cacheKey), callback);
45
- return () => emitter.removeListener(getInvalidateEventKey(cacheKey), callback);
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
  });