@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.
- 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
package/src/client/client.ts
CHANGED
|
@@ -1,157 +1,113 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
AdapterInstance,
|
|
4
|
-
AdapterPayloadMappingType,
|
|
5
|
-
AdapterType,
|
|
6
|
-
ExtractAdapterExtraType,
|
|
7
|
-
ExtractAdapterMethodType,
|
|
8
|
-
ExtractAdapterOptionsType,
|
|
9
|
-
ExtractAdapterQueryParamsType,
|
|
10
|
-
ExtractUnionAdapter,
|
|
11
|
-
HeaderMappingType,
|
|
12
|
-
QueryStringifyOptionsType,
|
|
13
|
-
ResponseReturnType,
|
|
14
|
-
xhrExtra,
|
|
15
|
-
ExtractAdapterEndpointType,
|
|
16
|
-
} from "adapter";
|
|
1
|
+
import { AdapterInstance, ResponseType } from "adapter";
|
|
2
|
+
import { HttpAdapterType, parseResponse, HttpAdapter } from "http-adapter";
|
|
17
3
|
import {
|
|
18
4
|
ClientErrorType,
|
|
19
5
|
ClientInstance,
|
|
20
6
|
ClientOptionsType,
|
|
21
|
-
|
|
22
|
-
getAdapterHeaders,
|
|
23
|
-
getAdapterPayload,
|
|
7
|
+
RequestGenericType,
|
|
24
8
|
RequestInterceptorType,
|
|
25
9
|
ResponseInterceptorType,
|
|
26
|
-
StringifyCallbackType,
|
|
27
|
-
stringifyQueryParams,
|
|
28
10
|
} from "client";
|
|
29
11
|
import { Cache } from "cache";
|
|
30
12
|
import { Dispatcher } from "dispatcher";
|
|
31
|
-
import {
|
|
13
|
+
import { PluginInstance, PluginMethodParameters, PluginMethods } from "plugin";
|
|
32
14
|
import { getRequestKey, getSimpleKey, Request, RequestInstance, RequestOptionsType } from "request";
|
|
33
|
-
import { AppManager, LoggerManager, RequestManager,
|
|
15
|
+
import { AppManager, LoggerManager, RequestManager, LogLevel } from "managers";
|
|
34
16
|
import { interceptRequest, interceptResponse } from "./client.utils";
|
|
35
|
-
import {
|
|
36
|
-
|
|
17
|
+
import {
|
|
18
|
+
EmptyTypes,
|
|
19
|
+
TypeWithDefaults,
|
|
20
|
+
ExtractAdapterMethodType,
|
|
21
|
+
ExtractAdapterOptionsType,
|
|
22
|
+
ExtractAdapterQueryParamsType,
|
|
23
|
+
ExtractAdapterEndpointType,
|
|
24
|
+
ExtractUnionAdapter,
|
|
25
|
+
HydrateDataType,
|
|
26
|
+
HydrationOptions,
|
|
27
|
+
ExtractAdapterDefaultQueryParamsType,
|
|
28
|
+
} from "types";
|
|
29
|
+
import { getUniqueRequestId } from "utils";
|
|
37
30
|
|
|
38
31
|
/**
|
|
39
32
|
* **Client** is a class that allows you to configure the connection with the server and then use it to create
|
|
40
|
-
* requests
|
|
41
|
-
*
|
|
33
|
+
* requests. It allows you to set global defaults for the requests configuration, query params configuration.
|
|
34
|
+
* It is also orchestrator for all of the HyperFetch modules like Cache, Dispatcher, AppManager, LoggerManager,
|
|
35
|
+
* RequestManager and more.
|
|
42
36
|
*/
|
|
43
37
|
export class Client<
|
|
44
38
|
GlobalErrorType extends ClientErrorType = Error,
|
|
45
|
-
Adapter extends AdapterInstance =
|
|
46
|
-
EndpointMapper extends DefaultEndpointMapper = DefaultEndpointMapper,
|
|
39
|
+
Adapter extends AdapterInstance = HttpAdapterType,
|
|
47
40
|
> {
|
|
48
41
|
readonly url: string;
|
|
49
42
|
public debug: boolean;
|
|
50
43
|
|
|
51
44
|
// Private
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
45
|
+
unstable_onErrorCallbacks: ResponseInterceptorType<ClientInstance>[] = [];
|
|
46
|
+
unstable_onSuccessCallbacks: ResponseInterceptorType<ClientInstance>[] = [];
|
|
47
|
+
unstable_onResponseCallbacks: ResponseInterceptorType<ClientInstance>[] = [];
|
|
48
|
+
unstable_onAuthCallbacks: RequestInterceptorType[] = [];
|
|
49
|
+
unstable_onRequestCallbacks: RequestInterceptorType[] = [];
|
|
57
50
|
|
|
58
51
|
// Managers
|
|
52
|
+
loggerManager: LoggerManager = new LoggerManager();
|
|
59
53
|
requestManager: RequestManager = new RequestManager();
|
|
60
54
|
appManager: AppManager;
|
|
61
|
-
loggerManager: LoggerManager = new LoggerManager(this);
|
|
62
55
|
|
|
63
56
|
// Config
|
|
64
57
|
adapter: Adapter;
|
|
65
|
-
cache: Cache<
|
|
66
|
-
fetchDispatcher: Dispatcher
|
|
67
|
-
submitDispatcher: Dispatcher
|
|
68
|
-
|
|
69
|
-
defaultMethod: ExtractAdapterMethodType<Adapter> = HttpMethodsEnum.get as ExtractAdapterMethodType<Adapter>;
|
|
70
|
-
defaultExtra: ExtractAdapterExtraType<Adapter> = xhrExtra as ExtractAdapterExtraType<Adapter>;
|
|
71
|
-
|
|
72
|
-
isMockEnabled = true;
|
|
58
|
+
cache: Cache<Adapter>;
|
|
59
|
+
fetchDispatcher: Dispatcher<Adapter>;
|
|
60
|
+
submitDispatcher: Dispatcher<Adapter>;
|
|
61
|
+
isMockerEnabled = true;
|
|
73
62
|
|
|
74
63
|
// Registered requests effect
|
|
75
|
-
|
|
64
|
+
plugins: PluginInstance[] = [];
|
|
76
65
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
adapterDefaultOptions?: (request: RequestInstance) => ExtractAdapterOptionsType<Adapter>;
|
|
80
|
-
requestDefaultOptions?: (
|
|
81
|
-
options: RequestOptionsType<string, ExtractAdapterOptionsType<Adapter>, ExtractAdapterMethodType<Adapter>>,
|
|
82
|
-
) => Partial<RequestOptionsType<string, ExtractAdapterOptionsType<Adapter>, ExtractAdapterMethodType<Adapter>>>;
|
|
83
|
-
abortKeyMapper?: (request: RequestInstance) => string = getSimpleKey;
|
|
84
|
-
cacheKeyMapper?: (request: RequestInstance) => string = getRequestKey;
|
|
85
|
-
queueKeyMapper?: (request: RequestInstance) => string = getRequestKey;
|
|
86
|
-
effectKeyMapper?: (request: RequestInstance) => string = getSimpleKey;
|
|
66
|
+
/** @internal */
|
|
67
|
+
unstable_abortKeyMapper: (request: RequestInstance) => string = getSimpleKey;
|
|
87
68
|
|
|
88
|
-
|
|
69
|
+
/** @internal */
|
|
70
|
+
unstable_cacheKeyMapper: (request: RequestInstance) => string = getRequestKey;
|
|
89
71
|
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Method to get default headers and to map them based on the data format exchange, by default it handles FormData / JSON formats.
|
|
97
|
-
*/
|
|
98
|
-
headerMapper: HeaderMappingType = getAdapterHeaders;
|
|
99
|
-
/**
|
|
100
|
-
* Method to get request data and transform them to the required format. It handles FormData and JSON by default.
|
|
101
|
-
*/
|
|
102
|
-
payloadMapper: AdapterPayloadMappingType = getAdapterPayload;
|
|
103
|
-
/**
|
|
104
|
-
* Method to get request data and transform them to the required format. It handles FormData and JSON by default.
|
|
105
|
-
*/
|
|
106
|
-
// eslint-disable-next-line class-methods-use-this
|
|
107
|
-
endpointMapper: EndpointMapper = ((endpoint) => endpoint) as any;
|
|
72
|
+
/** @internal */
|
|
73
|
+
unstable_queryKeyMapper: (request: RequestInstance) => string = getRequestKey;
|
|
74
|
+
|
|
75
|
+
/** @internal */
|
|
76
|
+
unstable_requestIdMapper: (request: RequestInstance) => string = getUniqueRequestId;
|
|
108
77
|
|
|
109
78
|
// Logger
|
|
110
|
-
logger = this.loggerManager.
|
|
79
|
+
logger = this.loggerManager.initialize(this, "Client");
|
|
111
80
|
|
|
112
|
-
constructor(public options: ClientOptionsType<Client<GlobalErrorType, Adapter
|
|
113
|
-
const { url,
|
|
81
|
+
constructor(public options: ClientOptionsType<Client<GlobalErrorType, Adapter>>) {
|
|
82
|
+
const { url, appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
114
83
|
this.url = url;
|
|
115
|
-
this.adapter = (
|
|
84
|
+
this.adapter = HttpAdapter() as Adapter;
|
|
85
|
+
|
|
86
|
+
this.appManager = appManager?.() || new AppManager();
|
|
87
|
+
this.cache = cache?.() || new Cache();
|
|
88
|
+
this.fetchDispatcher = fetchDispatcher?.() || new Dispatcher();
|
|
89
|
+
this.submitDispatcher = submitDispatcher?.() || new Dispatcher();
|
|
116
90
|
|
|
117
91
|
// IMPORTANT: Do not change initialization order as it's crucial for dependencies injection
|
|
118
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
this.
|
|
121
|
-
this.
|
|
92
|
+
this.adapter.initialize(this);
|
|
93
|
+
this.appManager.initialize();
|
|
94
|
+
this.cache.initialize(this as unknown as ClientInstance<{ adapter: Adapter }>);
|
|
95
|
+
this.fetchDispatcher.initialize(this as unknown as ClientInstance<{ adapter: Adapter }>);
|
|
96
|
+
this.submitDispatcher.initialize(this as unknown as ClientInstance<{ adapter: Adapter }>);
|
|
122
97
|
}
|
|
123
98
|
|
|
124
|
-
/**
|
|
125
|
-
* This method allows to configure global defaults for the request configuration like method, auth, deduplication etc.
|
|
126
|
-
*/
|
|
127
|
-
setRequestDefaultOptions = (
|
|
128
|
-
callback: (
|
|
129
|
-
request: RequestInstance,
|
|
130
|
-
) => Partial<RequestOptionsType<string, ExtractAdapterOptionsType<Adapter>, ExtractAdapterMethodType<Adapter>>>,
|
|
131
|
-
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
132
|
-
this.requestDefaultOptions = callback;
|
|
133
|
-
return this;
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
setAdapterDefaultOptions = (
|
|
137
|
-
callback: (request: RequestInstance) => ExtractAdapterOptionsType<Adapter>,
|
|
138
|
-
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
139
|
-
this.adapterDefaultOptions = callback;
|
|
140
|
-
return this;
|
|
141
|
-
};
|
|
142
|
-
|
|
143
99
|
/**
|
|
144
100
|
* This method enables the logger usage and display the logs in console
|
|
145
101
|
*/
|
|
146
|
-
setDebug = (
|
|
147
|
-
this.debug =
|
|
102
|
+
setDebug = (enabled: boolean): Client<GlobalErrorType, Adapter> => {
|
|
103
|
+
this.debug = enabled;
|
|
148
104
|
return this;
|
|
149
105
|
};
|
|
150
106
|
|
|
151
107
|
/**
|
|
152
108
|
* Set the logger severity of the messages displayed to the console
|
|
153
109
|
*/
|
|
154
|
-
|
|
110
|
+
setLogLevel = (severity: LogLevel): Client<GlobalErrorType, Adapter> => {
|
|
155
111
|
this.loggerManager.setSeverity(severity);
|
|
156
112
|
return this;
|
|
157
113
|
};
|
|
@@ -159,118 +115,43 @@ export class Client<
|
|
|
159
115
|
/**
|
|
160
116
|
* Set the new logger instance to the Client
|
|
161
117
|
*/
|
|
162
|
-
setLogger = (
|
|
163
|
-
callback: (Client: ClientInstance) => LoggerManager,
|
|
164
|
-
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
118
|
+
setLogger = (callback: (Client: ClientInstance) => LoggerManager): Client<GlobalErrorType, Adapter> => {
|
|
165
119
|
this.loggerManager = callback(this);
|
|
166
|
-
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more
|
|
171
|
-
*/
|
|
172
|
-
setQueryParamsConfig = (
|
|
173
|
-
queryParamsConfig: QueryStringifyOptionsType,
|
|
174
|
-
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
175
|
-
this.queryParamsConfig = queryParamsConfig;
|
|
176
|
-
return this;
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Set the custom query params stringify method to the Client
|
|
181
|
-
* @param stringifyFn Custom callback handling query params stringify
|
|
182
|
-
*/
|
|
183
|
-
setStringifyQueryParams = (stringifyFn: StringifyCallbackType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
184
|
-
this.stringifyQueryParams = stringifyFn;
|
|
185
|
-
return this;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Set the custom header mapping function
|
|
190
|
-
*/
|
|
191
|
-
setHeaderMapper = (headerMapper: HeaderMappingType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
192
|
-
this.headerMapper = headerMapper;
|
|
193
|
-
return this;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Set the request payload mapping function which get triggered before request get send
|
|
198
|
-
*/
|
|
199
|
-
setPayloadMapper = (payloadMapper: AdapterPayloadMappingType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
200
|
-
this.payloadMapper = payloadMapper;
|
|
120
|
+
this.loggerManager.initialize(this, "Client");
|
|
201
121
|
return this;
|
|
202
122
|
};
|
|
203
123
|
|
|
204
124
|
/**
|
|
205
125
|
* Set globally if mocking should be enabled or disabled for all client requests.
|
|
206
|
-
* @param
|
|
126
|
+
* @param isMockerEnabled
|
|
207
127
|
*/
|
|
208
|
-
setEnableGlobalMocking = (
|
|
209
|
-
this.
|
|
128
|
+
setEnableGlobalMocking = (isMockerEnabled: boolean) => {
|
|
129
|
+
this.isMockerEnabled = isMockerEnabled;
|
|
210
130
|
return this;
|
|
211
131
|
};
|
|
212
132
|
|
|
213
|
-
/**
|
|
214
|
-
* Set the request payload mapping function which get triggered before request get send
|
|
215
|
-
*/
|
|
216
|
-
setEndpointMapper = <NewEndpointMapper extends DefaultEndpointMapper>(endpointMapper: NewEndpointMapper) => {
|
|
217
|
-
this.endpointMapper = endpointMapper as any;
|
|
218
|
-
return this as unknown as Client<GlobalErrorType, Adapter, NewEndpointMapper>;
|
|
219
|
-
};
|
|
220
|
-
|
|
221
133
|
/**
|
|
222
134
|
* Set custom http adapter to handle graphql, rest, firebase or others
|
|
223
135
|
*/
|
|
224
|
-
setAdapter = <NewAdapter extends AdapterInstance
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
): Client<
|
|
229
|
-
GlobalErrorType,
|
|
230
|
-
Returns extends AdapterInstance ? NewAdapter : ExtractAdapterType<NewAdapter>,
|
|
231
|
-
EndpointMapper
|
|
232
|
-
> => {
|
|
233
|
-
const value = callback(this) as unknown as Adapter;
|
|
234
|
-
|
|
235
|
-
if (value instanceof Client) {
|
|
236
|
-
return value as any;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
this.adapter = value;
|
|
240
|
-
return this as any;
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Set default method for requests.
|
|
245
|
-
*/
|
|
246
|
-
setDefaultMethod = (defaultMethod: ExtractAdapterMethodType<Adapter>) => {
|
|
247
|
-
this.defaultMethod = defaultMethod;
|
|
248
|
-
return this as ClientInstance;
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Set default additional data for initial state.
|
|
253
|
-
*/
|
|
254
|
-
setDefaultExtra = (defaultExtra: ExtractAdapterExtraType<Adapter>) => {
|
|
255
|
-
this.defaultExtra = defaultExtra;
|
|
256
|
-
return this as ClientInstance;
|
|
136
|
+
setAdapter = <NewAdapter extends AdapterInstance>(adapter: NewAdapter): Client<GlobalErrorType, NewAdapter> => {
|
|
137
|
+
this.adapter = adapter as unknown as Adapter;
|
|
138
|
+
this.adapter.initialize(this);
|
|
139
|
+
return this as unknown as Client<GlobalErrorType, NewAdapter>;
|
|
257
140
|
};
|
|
258
141
|
|
|
259
142
|
/**
|
|
260
143
|
* Method of manipulating requests before sending the request. We can for example add custom header with token to the request which request had the auth set to true.
|
|
261
144
|
*/
|
|
262
|
-
onAuth = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter
|
|
263
|
-
this.
|
|
145
|
+
onAuth = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter> => {
|
|
146
|
+
this.unstable_onAuthCallbacks.push(callback);
|
|
264
147
|
return this;
|
|
265
148
|
};
|
|
266
149
|
|
|
267
150
|
/**
|
|
268
151
|
* Method for removing listeners on auth.
|
|
269
152
|
* */
|
|
270
|
-
removeOnAuthInterceptors = (
|
|
271
|
-
callbacks
|
|
272
|
-
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
273
|
-
this.__onAuthCallbacks = this.__onAuthCallbacks.filter((callback) => !callbacks.includes(callback));
|
|
153
|
+
removeOnAuthInterceptors = (callbacks: RequestInterceptorType[]): Client<GlobalErrorType, Adapter> => {
|
|
154
|
+
this.unstable_onAuthCallbacks = this.unstable_onAuthCallbacks.filter((callback) => !callbacks.includes(callback));
|
|
274
155
|
return this;
|
|
275
156
|
};
|
|
276
157
|
|
|
@@ -278,9 +159,9 @@ export class Client<
|
|
|
278
159
|
* Method for intercepting error responses. It can be used for example to refresh tokens.
|
|
279
160
|
*/
|
|
280
161
|
onError = <ErrorType = null>(
|
|
281
|
-
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType
|
|
282
|
-
): Client<GlobalErrorType, Adapter
|
|
283
|
-
this.
|
|
162
|
+
callback: ResponseInterceptorType<ClientInstance, any, ErrorType | GlobalErrorType>,
|
|
163
|
+
): Client<GlobalErrorType, Adapter> => {
|
|
164
|
+
this.unstable_onErrorCallbacks.push(callback);
|
|
284
165
|
return this;
|
|
285
166
|
};
|
|
286
167
|
|
|
@@ -288,9 +169,9 @@ export class Client<
|
|
|
288
169
|
* Method for removing listeners on error.
|
|
289
170
|
* */
|
|
290
171
|
removeOnErrorInterceptors = (
|
|
291
|
-
callbacks: ResponseInterceptorType<any, null | GlobalErrorType
|
|
292
|
-
): Client<GlobalErrorType, Adapter
|
|
293
|
-
this.
|
|
172
|
+
callbacks: ResponseInterceptorType<ClientInstance, any, null | GlobalErrorType>[],
|
|
173
|
+
): Client<GlobalErrorType, Adapter> => {
|
|
174
|
+
this.unstable_onErrorCallbacks = this.unstable_onErrorCallbacks.filter((callback) => !callbacks.includes(callback));
|
|
294
175
|
return this;
|
|
295
176
|
};
|
|
296
177
|
|
|
@@ -298,9 +179,9 @@ export class Client<
|
|
|
298
179
|
* Method for intercepting success responses.
|
|
299
180
|
*/
|
|
300
181
|
onSuccess = <ErrorType = null>(
|
|
301
|
-
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType
|
|
302
|
-
): Client<GlobalErrorType, Adapter
|
|
303
|
-
this.
|
|
182
|
+
callback: ResponseInterceptorType<ClientInstance, any, ErrorType | GlobalErrorType>,
|
|
183
|
+
): Client<GlobalErrorType, Adapter> => {
|
|
184
|
+
this.unstable_onSuccessCallbacks.push(callback);
|
|
304
185
|
return this;
|
|
305
186
|
};
|
|
306
187
|
|
|
@@ -308,27 +189,29 @@ export class Client<
|
|
|
308
189
|
* Method for removing listeners on success.
|
|
309
190
|
* */
|
|
310
191
|
removeOnSuccessInterceptors = (
|
|
311
|
-
callbacks: ResponseInterceptorType<any, null | GlobalErrorType
|
|
312
|
-
): Client<GlobalErrorType, Adapter
|
|
313
|
-
this.
|
|
192
|
+
callbacks: ResponseInterceptorType<ClientInstance, any, null | GlobalErrorType>[],
|
|
193
|
+
): Client<GlobalErrorType, Adapter> => {
|
|
194
|
+
this.unstable_onSuccessCallbacks = this.unstable_onSuccessCallbacks.filter(
|
|
195
|
+
(callback) => !callbacks.includes(callback),
|
|
196
|
+
);
|
|
314
197
|
return this;
|
|
315
198
|
};
|
|
316
199
|
|
|
317
200
|
/**
|
|
318
201
|
* Method of manipulating requests before sending the request.
|
|
319
202
|
*/
|
|
320
|
-
onRequest = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter
|
|
321
|
-
this.
|
|
203
|
+
onRequest = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter> => {
|
|
204
|
+
this.unstable_onRequestCallbacks.push(callback);
|
|
322
205
|
return this;
|
|
323
206
|
};
|
|
324
207
|
|
|
325
208
|
/**
|
|
326
209
|
* Method for removing listeners on request.
|
|
327
210
|
* */
|
|
328
|
-
removeOnRequestInterceptors = (
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
211
|
+
removeOnRequestInterceptors = (callbacks: RequestInterceptorType[]): Client<GlobalErrorType, Adapter> => {
|
|
212
|
+
this.unstable_onRequestCallbacks = this.unstable_onRequestCallbacks.filter(
|
|
213
|
+
(callback) => !callbacks.includes(callback),
|
|
214
|
+
);
|
|
332
215
|
return this;
|
|
333
216
|
};
|
|
334
217
|
|
|
@@ -336,9 +219,9 @@ export class Client<
|
|
|
336
219
|
* Method for intercepting any responses.
|
|
337
220
|
*/
|
|
338
221
|
onResponse = <ErrorType = null>(
|
|
339
|
-
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType
|
|
340
|
-
): Client<GlobalErrorType, Adapter
|
|
341
|
-
this.
|
|
222
|
+
callback: ResponseInterceptorType<ClientInstance, any, ErrorType | GlobalErrorType>,
|
|
223
|
+
): Client<GlobalErrorType, Adapter> => {
|
|
224
|
+
this.unstable_onResponseCallbacks.push(callback);
|
|
342
225
|
return this;
|
|
343
226
|
};
|
|
344
227
|
|
|
@@ -346,27 +229,48 @@ export class Client<
|
|
|
346
229
|
* Method for removing listeners on request.
|
|
347
230
|
* */
|
|
348
231
|
removeOnResponseInterceptors = (
|
|
349
|
-
callbacks: ResponseInterceptorType<any, null | GlobalErrorType
|
|
350
|
-
): Client<GlobalErrorType, Adapter
|
|
351
|
-
this.
|
|
232
|
+
callbacks: ResponseInterceptorType<ClientInstance, any, null | GlobalErrorType>[],
|
|
233
|
+
): Client<GlobalErrorType, Adapter> => {
|
|
234
|
+
this.unstable_onResponseCallbacks = this.unstable_onResponseCallbacks.filter(
|
|
235
|
+
(callback) => !callbacks.includes(callback),
|
|
236
|
+
);
|
|
352
237
|
return this;
|
|
353
238
|
};
|
|
354
239
|
|
|
355
240
|
/**
|
|
356
|
-
* Add persistent
|
|
241
|
+
* Add persistent plugins which trigger on the request lifecycle
|
|
357
242
|
*/
|
|
358
|
-
|
|
359
|
-
this.
|
|
243
|
+
addPlugin = (plugin: PluginInstance) => {
|
|
244
|
+
this.plugins.push(plugin);
|
|
245
|
+
|
|
246
|
+
plugin.initialize(this);
|
|
247
|
+
plugin.trigger("onMount", { client: this });
|
|
360
248
|
|
|
361
249
|
return this;
|
|
362
250
|
};
|
|
363
251
|
|
|
364
252
|
/**
|
|
365
|
-
* Remove
|
|
253
|
+
* Remove plugins from Client
|
|
366
254
|
*/
|
|
367
|
-
|
|
368
|
-
const
|
|
369
|
-
this.
|
|
255
|
+
removePlugin = (plugin: PluginInstance) => {
|
|
256
|
+
const pluginCount = this.plugins.length;
|
|
257
|
+
this.plugins = this.plugins.filter((p) => p !== plugin);
|
|
258
|
+
|
|
259
|
+
if (this.plugins.length !== pluginCount) {
|
|
260
|
+
plugin.trigger("onUnmount", { client: this });
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return this;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
triggerPlugins = <Key extends keyof PluginMethods<Client>>(key: Key, data: PluginMethodParameters<Key, Client>) => {
|
|
267
|
+
if (!this.plugins.length) {
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
this.plugins.forEach((plugin) => {
|
|
272
|
+
plugin.trigger(key, data);
|
|
273
|
+
});
|
|
370
274
|
|
|
371
275
|
return this;
|
|
372
276
|
};
|
|
@@ -376,49 +280,59 @@ export class Client<
|
|
|
376
280
|
*/
|
|
377
281
|
|
|
378
282
|
setAbortKeyMapper = (callback: (request: RequestInstance) => string) => {
|
|
379
|
-
this.
|
|
283
|
+
this.unstable_abortKeyMapper = callback;
|
|
284
|
+
return this;
|
|
380
285
|
};
|
|
381
286
|
setCacheKeyMapper = (callback: (request: RequestInstance) => string) => {
|
|
382
|
-
this.
|
|
287
|
+
this.unstable_cacheKeyMapper = callback;
|
|
288
|
+
return this;
|
|
383
289
|
};
|
|
384
|
-
|
|
385
|
-
this.
|
|
290
|
+
setQueryKeyMapper = (callback: (request: RequestInstance) => string) => {
|
|
291
|
+
this.unstable_queryKeyMapper = callback;
|
|
292
|
+
return this;
|
|
386
293
|
};
|
|
387
|
-
|
|
388
|
-
this.
|
|
294
|
+
setRequestIdMapper = (callback: (request: RequestInstance) => string) => {
|
|
295
|
+
this.unstable_requestIdMapper = callback;
|
|
296
|
+
return this;
|
|
389
297
|
};
|
|
390
298
|
|
|
391
299
|
/**
|
|
392
300
|
* Helper used by http adapter to apply the modifications on response error
|
|
301
|
+
* @private
|
|
393
302
|
*/
|
|
394
|
-
|
|
303
|
+
unstable_modifyAuth = async (request: RequestInstance) => interceptRequest(this.unstable_onAuthCallbacks, request);
|
|
395
304
|
|
|
396
305
|
/**
|
|
397
306
|
* Private helper to run async pre-request processing
|
|
307
|
+
* @private
|
|
398
308
|
*/
|
|
399
|
-
|
|
309
|
+
unstable_modifyRequest = async (request: RequestInstance) =>
|
|
310
|
+
interceptRequest(this.unstable_onRequestCallbacks, request);
|
|
400
311
|
|
|
401
312
|
/**
|
|
402
313
|
* Private helper to run async on-error response processing
|
|
314
|
+
* @private
|
|
403
315
|
*/
|
|
404
|
-
|
|
405
|
-
response:
|
|
316
|
+
unstable_modifyErrorResponse = async (
|
|
317
|
+
response: ResponseType<any, GlobalErrorType, Adapter>,
|
|
406
318
|
request: RequestInstance,
|
|
407
|
-
) => interceptResponse<GlobalErrorType,
|
|
319
|
+
) => interceptResponse<GlobalErrorType, ClientInstance>(this.unstable_onErrorCallbacks, response, request);
|
|
408
320
|
|
|
409
321
|
/**
|
|
410
322
|
* Private helper to run async on-success response processing
|
|
323
|
+
* @private
|
|
411
324
|
*/
|
|
412
|
-
|
|
413
|
-
response:
|
|
325
|
+
unstable_modifySuccessResponse = async (
|
|
326
|
+
response: ResponseType<any, GlobalErrorType, Adapter>,
|
|
414
327
|
request: RequestInstance,
|
|
415
|
-
) => interceptResponse<GlobalErrorType,
|
|
328
|
+
) => interceptResponse<GlobalErrorType, ClientInstance>(this.unstable_onSuccessCallbacks, response, request);
|
|
416
329
|
|
|
417
330
|
/**
|
|
418
331
|
* Private helper to run async response processing
|
|
332
|
+
* @private
|
|
419
333
|
*/
|
|
420
|
-
|
|
421
|
-
interceptResponse<GlobalErrorType,
|
|
334
|
+
unstable_modifyResponse = async (response: ResponseType<any, GlobalErrorType, Adapter>, request: RequestInstance) =>
|
|
335
|
+
interceptResponse<GlobalErrorType, ClientInstance>(this.unstable_onResponseCallbacks, response, request);
|
|
422
336
|
|
|
423
337
|
/**
|
|
424
338
|
* Clears the Client instance and remove all listeners on it's dependencies
|
|
@@ -436,21 +350,90 @@ export class Client<
|
|
|
436
350
|
this.submitDispatcher.emitter.removeAllListeners();
|
|
437
351
|
this.cache.emitter.removeAllListeners();
|
|
438
352
|
|
|
439
|
-
this.
|
|
440
|
-
this.
|
|
441
|
-
this.fetchDispatcher = fetchDispatcher?.(
|
|
442
|
-
this.submitDispatcher = submitDispatcher?.(
|
|
353
|
+
this.cache = cache?.() || new Cache();
|
|
354
|
+
this.appManager = appManager?.() || new AppManager();
|
|
355
|
+
this.fetchDispatcher = fetchDispatcher?.() || new Dispatcher();
|
|
356
|
+
this.submitDispatcher = submitDispatcher?.() || new Dispatcher();
|
|
357
|
+
|
|
358
|
+
// DO NOT CHANGE INITIALIZATION ORDER
|
|
359
|
+
this.appManager.initialize();
|
|
360
|
+
this.cache.initialize(this as unknown as ClientInstance<{ adapter: Adapter }>);
|
|
361
|
+
this.fetchDispatcher.initialize(this as unknown as ClientInstance<{ adapter: Adapter }>);
|
|
362
|
+
this.submitDispatcher.initialize(this as unknown as ClientInstance<{ adapter: Adapter }>);
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Hydrate your SSR cache data
|
|
367
|
+
* @param hydrationData
|
|
368
|
+
* @param options
|
|
369
|
+
*/
|
|
370
|
+
hydrate = (
|
|
371
|
+
hydrationData: (HydrateDataType | EmptyTypes)[],
|
|
372
|
+
options?: Partial<HydrationOptions> | ((item: HydrateDataType) => Partial<HydrationOptions>),
|
|
373
|
+
) => {
|
|
374
|
+
hydrationData?.forEach((item) => {
|
|
375
|
+
if (!item) return;
|
|
376
|
+
|
|
377
|
+
const { cacheKey, response, ...fallbackOptions } = item;
|
|
378
|
+
const defaults = {
|
|
379
|
+
cache: true,
|
|
380
|
+
override: true,
|
|
381
|
+
} satisfies Partial<HydrationOptions>;
|
|
382
|
+
const config =
|
|
383
|
+
typeof options === "function"
|
|
384
|
+
? { ...defaults, ...fallbackOptions, ...options(item) }
|
|
385
|
+
: { ...defaults, ...fallbackOptions, ...options };
|
|
386
|
+
|
|
387
|
+
if (!config.override) {
|
|
388
|
+
const cachedData = this.cache.get(cacheKey);
|
|
389
|
+
if (cachedData) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const parsedData = parseResponse(response);
|
|
395
|
+
this.cache.set({ ...config, cacheKey }, parsedData);
|
|
396
|
+
});
|
|
443
397
|
};
|
|
444
398
|
|
|
445
399
|
/**
|
|
446
400
|
* Create requests based on the Client setup
|
|
401
|
+
*
|
|
402
|
+
* @template Response Your response
|
|
447
403
|
*/
|
|
448
404
|
createRequest = <
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
405
|
+
RequestProperties extends RequestGenericType<ExtractAdapterQueryParamsType<Adapter>> = {
|
|
406
|
+
error?: Error;
|
|
407
|
+
},
|
|
408
|
+
>(
|
|
409
|
+
/**
|
|
410
|
+
* `createRequest` must be initialized twice(currying).
|
|
411
|
+
*
|
|
412
|
+
* ✅ Good:
|
|
413
|
+
* ```ts
|
|
414
|
+
* const request = createRequest<RequestProperties>()(params)
|
|
415
|
+
* ```
|
|
416
|
+
* ⛔ Bad:
|
|
417
|
+
* ```ts
|
|
418
|
+
* const request = createRequest<RequestProperties>(params)
|
|
419
|
+
* ```
|
|
420
|
+
*
|
|
421
|
+
* We are using currying to achieve auto generated types for the endpoint string.
|
|
422
|
+
*
|
|
423
|
+
* This solution will be removed once https://github.com/microsoft/TypeScript/issues/10571 get resolved.
|
|
424
|
+
*/
|
|
425
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
426
|
+
_USE_DOUBLE_INITIALIZATION?: never,
|
|
427
|
+
) => {
|
|
428
|
+
type Response = TypeWithDefaults<RequestProperties, "response", undefined>;
|
|
429
|
+
type Payload = TypeWithDefaults<RequestProperties, "payload", undefined>;
|
|
430
|
+
type LocalError = TypeWithDefaults<RequestProperties, "error", GlobalErrorType>;
|
|
431
|
+
type QueryParams = TypeWithDefaults<
|
|
432
|
+
RequestProperties,
|
|
433
|
+
"queryParams",
|
|
434
|
+
ExtractAdapterDefaultQueryParamsType<Adapter>
|
|
435
|
+
>;
|
|
436
|
+
|
|
454
437
|
return <
|
|
455
438
|
EndpointType extends ExtractAdapterEndpointType<Adapter>,
|
|
456
439
|
AdapterOptions extends ExtractAdapterOptionsType<Adapter>,
|
|
@@ -458,44 +441,42 @@ export class Client<
|
|
|
458
441
|
>(
|
|
459
442
|
params: RequestOptionsType<EndpointType, AdapterOptions, MethodType>,
|
|
460
443
|
) => {
|
|
461
|
-
|
|
444
|
+
type Endpoint = TypeWithDefaults<RequestProperties, "endpoint", EndpointType>;
|
|
445
|
+
|
|
446
|
+
const endpoint = this.adapter.unstable_endpointMapper(params.endpoint);
|
|
462
447
|
|
|
463
|
-
type
|
|
448
|
+
// Splitting this type prevents "Type instantiation is excessively deep and possibly infinite" error
|
|
449
|
+
type ExtractedAdapter = ExtractUnionAdapter<
|
|
464
450
|
Adapter,
|
|
465
451
|
{
|
|
466
452
|
method: MethodType;
|
|
467
453
|
options: AdapterOptions;
|
|
468
454
|
queryParams: QueryParams;
|
|
469
455
|
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
: ExtractUnionAdapter<
|
|
473
|
-
Adapter,
|
|
474
|
-
{
|
|
475
|
-
method: MethodType;
|
|
476
|
-
options: AdapterOptions;
|
|
477
|
-
queryParams: QueryParams;
|
|
478
|
-
}
|
|
479
|
-
>;
|
|
456
|
+
>;
|
|
457
|
+
type ExtractedAdapterType = ExtractedAdapter extends EmptyTypes ? Adapter : ExtractedAdapter;
|
|
480
458
|
|
|
481
459
|
const mappedParams: RequestOptionsType<
|
|
482
|
-
|
|
460
|
+
Endpoint extends string ? Endpoint : typeof endpoint,
|
|
483
461
|
AdapterOptions,
|
|
484
462
|
MethodType
|
|
485
463
|
> = {
|
|
486
464
|
...params,
|
|
487
|
-
endpoint: endpoint as
|
|
465
|
+
endpoint: String(endpoint) as Endpoint extends string ? Endpoint : typeof endpoint,
|
|
488
466
|
};
|
|
489
467
|
|
|
490
|
-
|
|
468
|
+
const request = new Request<
|
|
491
469
|
Response,
|
|
492
470
|
Payload,
|
|
493
471
|
QueryParams,
|
|
494
|
-
GlobalErrorType,
|
|
495
472
|
LocalError,
|
|
496
|
-
|
|
497
|
-
ExtractedAdapterType
|
|
498
|
-
>(this as
|
|
473
|
+
Endpoint extends string ? Endpoint : typeof endpoint,
|
|
474
|
+
Client<GlobalErrorType, ExtractedAdapterType>
|
|
475
|
+
>(this as unknown as Client<GlobalErrorType, ExtractedAdapterType>, mappedParams);
|
|
476
|
+
|
|
477
|
+
this.plugins.forEach((plugin) => plugin.trigger("onRequestCreate", { request }));
|
|
478
|
+
|
|
479
|
+
return request;
|
|
499
480
|
};
|
|
500
481
|
};
|
|
501
482
|
}
|