@hyper-fetch/core 2.1.0 → 3.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/README.md +1 -1
- package/dist/adapter/adapter.bindings.d.ts +25 -0
- package/dist/adapter/adapter.browser.d.ts +2 -0
- package/dist/{client/fetch.client.constants.d.ts → adapter/adapter.constants.d.ts} +0 -0
- package/dist/adapter/adapter.d.ts +2 -0
- package/dist/adapter/adapter.server.d.ts +2 -0
- package/dist/adapter/adapter.types.d.ts +57 -0
- package/dist/{client/fetch.client.utils.d.ts → adapter/adapter.utils.d.ts} +3 -3
- package/dist/adapter/index.d.ts +7 -0
- package/dist/cache/cache.d.ts +9 -9
- package/dist/cache/cache.types.d.ts +4 -4
- package/dist/cache/cache.utils.d.ts +3 -3
- package/dist/{builder/builder.constants.d.ts → client/client.constants.d.ts} +0 -0
- package/dist/client/client.d.ts +142 -0
- package/dist/client/client.types.d.ts +41 -0
- package/dist/client/client.utils.d.ts +10 -0
- package/dist/client/index.d.ts +4 -6
- package/dist/dispatcher/dispatcher.d.ts +23 -23
- package/dist/dispatcher/dispatcher.events.d.ts +8 -8
- package/dist/dispatcher/dispatcher.types.d.ts +10 -10
- package/dist/dispatcher/dispatcher.utils.d.ts +5 -5
- package/dist/effect/index.d.ts +2 -2
- package/dist/effect/request.effect.d.ts +14 -0
- package/dist/effect/request.effect.types.d.ts +32 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/dist/managers/index.d.ts +1 -1
- package/dist/managers/logger/logger.manager.d.ts +5 -5
- package/dist/managers/logger/logger.manager.types.d.ts +7 -7
- package/dist/managers/request/index.d.ts +4 -0
- package/dist/managers/request/request.manager.d.ts +43 -0
- package/dist/managers/request/request.manager.events.d.ts +36 -0
- package/dist/managers/{command/command.manager.types.d.ts → request/request.manager.types.d.ts} +6 -6
- package/dist/managers/{command/command.manager.utils.d.ts → request/request.manager.utils.d.ts} +0 -0
- package/dist/request/index.d.ts +3 -0
- package/dist/request/request.d.ts +98 -0
- package/dist/request/request.types.d.ts +202 -0
- package/dist/request/request.utils.d.ts +22 -0
- package/dist/types/fetch.types.d.ts +16 -16
- package/package.json +1 -1
- package/src/{client/fetch.client.bindings.ts → adapter/adapter.bindings.ts} +58 -58
- package/src/{client/fetch.client.browser.ts → adapter/adapter.browser.ts} +6 -6
- package/src/{client/fetch.client.constants.ts → adapter/adapter.constants.ts} +0 -0
- package/src/{client/fetch.client.server.ts → adapter/adapter.server.ts} +12 -12
- package/src/adapter/adapter.ts +8 -0
- package/src/adapter/adapter.types.ts +79 -0
- package/src/{client/fetch.client.utils.ts → adapter/adapter.utils.ts} +3 -3
- package/src/adapter/index.ts +7 -0
- package/src/cache/cache.ts +19 -19
- package/src/cache/cache.types.ts +4 -4
- package/src/cache/cache.utils.ts +6 -6
- package/src/{builder/builder.constants.ts → client/client.constants.ts} +0 -0
- package/src/client/client.ts +310 -0
- package/src/client/client.types.ts +52 -0
- package/src/{builder/builder.utils.ts → client/client.utils.ts} +34 -31
- package/src/client/index.ts +4 -6
- package/src/dispatcher/dispatcher.events.ts +11 -11
- package/src/dispatcher/dispatcher.ts +68 -68
- package/src/dispatcher/dispatcher.types.ts +10 -10
- package/src/dispatcher/dispatcher.utils.ts +7 -7
- package/src/effect/index.ts +2 -2
- package/src/effect/request.effect.ts +28 -0
- package/src/effect/request.effect.types.ts +35 -0
- package/src/index.ts +4 -4
- package/src/managers/index.ts +1 -1
- package/src/managers/logger/logger.manager.ts +8 -8
- package/src/managers/logger/logger.manager.types.ts +7 -7
- package/src/managers/request/index.ts +4 -0
- package/src/managers/{command/command.manager.events.ts → request/request.manager.events.ts} +47 -51
- package/src/managers/{command/command.manager.ts → request/request.manager.ts} +5 -5
- package/src/managers/request/request.manager.types.ts +25 -0
- package/src/managers/{command/command.manager.utils.ts → request/request.manager.utils.ts} +0 -0
- package/src/request/index.ts +3 -0
- package/src/{command/command.ts → request/request.ts} +188 -194
- package/src/request/request.types.ts +282 -0
- package/src/{command/command.utils.ts → request/request.utils.ts} +56 -53
- package/src/types/fetch.types.ts +18 -18
- package/dist/builder/builder.d.ts +0 -141
- package/dist/builder/builder.types.d.ts +0 -41
- package/dist/builder/builder.utils.d.ts +0 -10
- package/dist/builder/index.d.ts +0 -4
- package/dist/client/fetch.client.bindings.d.ts +0 -25
- package/dist/client/fetch.client.browser.d.ts +0 -2
- package/dist/client/fetch.client.d.ts +0 -2
- package/dist/client/fetch.client.server.d.ts +0 -2
- package/dist/client/fetch.client.types.d.ts +0 -57
- package/dist/command/command.d.ts +0 -98
- package/dist/command/command.types.d.ts +0 -202
- package/dist/command/command.utils.d.ts +0 -22
- package/dist/command/index.d.ts +0 -3
- package/dist/effect/fetch.effect.d.ts +0 -14
- package/dist/effect/fetch.effect.types.d.ts +0 -32
- package/dist/managers/command/command.manager.d.ts +0 -43
- package/dist/managers/command/command.manager.events.d.ts +0 -36
- package/dist/managers/command/index.d.ts +0 -4
- package/src/builder/builder.ts +0 -308
- package/src/builder/builder.types.ts +0 -53
- package/src/builder/index.ts +0 -4
- package/src/client/fetch.client.ts +0 -9
- package/src/client/fetch.client.types.ts +0 -83
- package/src/command/command.types.ts +0 -273
- package/src/command/index.ts +0 -3
- package/src/effect/fetch.effect.ts +0 -28
- package/src/effect/fetch.effect.types.ts +0 -35
- package/src/managers/command/command.manager.types.ts +0 -25
- package/src/managers/command/index.ts +0 -4
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AdapterType,
|
|
3
|
+
adapter as defaultAdapter,
|
|
4
|
+
AdapterOptionsType,
|
|
5
|
+
ResponseType,
|
|
6
|
+
QueryParamsType,
|
|
7
|
+
QueryStringifyOptionsType,
|
|
8
|
+
HeaderMappingType,
|
|
9
|
+
AdapterPayloadMappingType,
|
|
10
|
+
} from "adapter";
|
|
11
|
+
import {
|
|
12
|
+
stringifyQueryParams,
|
|
13
|
+
getAdapterHeaders,
|
|
14
|
+
getAdapterPayload,
|
|
15
|
+
ClientOptionsType,
|
|
16
|
+
ClientInstance,
|
|
17
|
+
ClientErrorType,
|
|
18
|
+
StringifyCallbackType,
|
|
19
|
+
RequestInterceptorType,
|
|
20
|
+
ResponseInterceptorType,
|
|
21
|
+
} from "client";
|
|
22
|
+
import { Cache } from "cache";
|
|
23
|
+
import { Dispatcher } from "dispatcher";
|
|
24
|
+
import { RequestEffectInstance } from "effect";
|
|
25
|
+
import { Request, RequestOptionsType, RequestInstance } from "request";
|
|
26
|
+
import { AppManager, RequestManager, LoggerManager, SeverityType } from "managers";
|
|
27
|
+
import { interceptRequest, interceptResponse } from "./client.utils";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* **Client** is a class that allows you to configure the connection with the server and then use it to create
|
|
31
|
+
* requests which, when called using the appropriate method, will cause the server to be queried for the endpoint and
|
|
32
|
+
* method specified in the request.
|
|
33
|
+
* @position 1
|
|
34
|
+
*/
|
|
35
|
+
export class Client<GlobalErrorType extends ClientErrorType = Error, AdapterOptions = AdapterOptionsType> {
|
|
36
|
+
readonly url: string;
|
|
37
|
+
public debug: boolean;
|
|
38
|
+
|
|
39
|
+
// Private
|
|
40
|
+
__onErrorCallbacks: ResponseInterceptorType[] = [];
|
|
41
|
+
__onSuccessCallbacks: ResponseInterceptorType[] = [];
|
|
42
|
+
__onResponseCallbacks: ResponseInterceptorType[] = [];
|
|
43
|
+
__onAuthCallbacks: RequestInterceptorType[] = [];
|
|
44
|
+
__onRequestCallbacks: RequestInterceptorType[] = [];
|
|
45
|
+
|
|
46
|
+
// Managers
|
|
47
|
+
requestManager: RequestManager = new RequestManager();
|
|
48
|
+
appManager: AppManager;
|
|
49
|
+
loggerManager: LoggerManager = new LoggerManager(this);
|
|
50
|
+
|
|
51
|
+
// Config
|
|
52
|
+
adapter: AdapterType;
|
|
53
|
+
cache: Cache;
|
|
54
|
+
fetchDispatcher: Dispatcher;
|
|
55
|
+
submitDispatcher: Dispatcher;
|
|
56
|
+
|
|
57
|
+
// Registered requests effect
|
|
58
|
+
effects: RequestEffectInstance[] = [];
|
|
59
|
+
|
|
60
|
+
// Options
|
|
61
|
+
queryParamsConfig?: QueryStringifyOptionsType;
|
|
62
|
+
adapterDefaultOptions?: (request: RequestInstance) => AdapterOptions;
|
|
63
|
+
requestDefaultOptions?: (
|
|
64
|
+
options: RequestOptionsType<string, AdapterOptions>,
|
|
65
|
+
) => Partial<RequestOptionsType<string, AdapterOptions>>;
|
|
66
|
+
|
|
67
|
+
// Utils
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Method to stringify query params from objects.
|
|
71
|
+
*/
|
|
72
|
+
stringifyQueryParams: StringifyCallbackType = (queryParams) =>
|
|
73
|
+
stringifyQueryParams(queryParams, this.queryParamsConfig);
|
|
74
|
+
/**
|
|
75
|
+
* Method to get default headers and to map them based on the data format exchange, by default it handles FormData / JSON formats.
|
|
76
|
+
*/
|
|
77
|
+
headerMapper: HeaderMappingType = getAdapterHeaders;
|
|
78
|
+
/**
|
|
79
|
+
* Method to get request data and transform them to the required format. It handles FormData and JSON by default.
|
|
80
|
+
*/
|
|
81
|
+
payloadMapper: AdapterPayloadMappingType = getAdapterPayload;
|
|
82
|
+
|
|
83
|
+
// Logger
|
|
84
|
+
logger = this.loggerManager.init("Client");
|
|
85
|
+
|
|
86
|
+
constructor(public options: ClientOptionsType) {
|
|
87
|
+
const { url, adapter, appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
88
|
+
this.url = url;
|
|
89
|
+
this.adapter = adapter || defaultAdapter;
|
|
90
|
+
|
|
91
|
+
// IMPORTANT: Do not change initialization order as it's crucial for dependencies injection
|
|
92
|
+
this.appManager = appManager?.(this) || new AppManager();
|
|
93
|
+
this.cache = cache?.(this) || new Cache(this);
|
|
94
|
+
this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
|
|
95
|
+
this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* This method allows to configure global defaults for the request configuration like method, auth, deduplication etc.
|
|
100
|
+
*/
|
|
101
|
+
setRequestDefaultOptions = (
|
|
102
|
+
callback: (request: RequestInstance) => Partial<RequestOptionsType<string, AdapterOptions>>,
|
|
103
|
+
): Client<GlobalErrorType, AdapterOptions> => {
|
|
104
|
+
this.requestDefaultOptions = callback;
|
|
105
|
+
return this;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
setAdapterDefaultOptions = (
|
|
109
|
+
callback: (request: RequestInstance) => AdapterOptions,
|
|
110
|
+
): Client<GlobalErrorType, AdapterOptions> => {
|
|
111
|
+
this.adapterDefaultOptions = callback;
|
|
112
|
+
return this;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* This method enables the logger usage and display the logs in console
|
|
117
|
+
*/
|
|
118
|
+
setDebug = (debug: boolean): Client<GlobalErrorType, AdapterOptions> => {
|
|
119
|
+
this.debug = debug;
|
|
120
|
+
return this;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Set the logger severity of the messages displayed to the console
|
|
125
|
+
*/
|
|
126
|
+
setLoggerSeverity = (severity: SeverityType): Client<GlobalErrorType, AdapterOptions> => {
|
|
127
|
+
this.loggerManager.setSeverity(severity);
|
|
128
|
+
return this;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Set the new logger instance to the Client
|
|
133
|
+
*/
|
|
134
|
+
setLogger = (callback: (Client: ClientInstance) => LoggerManager): Client<GlobalErrorType, AdapterOptions> => {
|
|
135
|
+
this.loggerManager = callback(this);
|
|
136
|
+
return this;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more
|
|
141
|
+
*/
|
|
142
|
+
setQueryParamsConfig = (queryParamsConfig: QueryStringifyOptionsType): Client<GlobalErrorType, AdapterOptions> => {
|
|
143
|
+
this.queryParamsConfig = queryParamsConfig;
|
|
144
|
+
return this;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Set the custom query params stringify method to the Client
|
|
149
|
+
* @param stringifyFn Custom callback handling query params stringify
|
|
150
|
+
*/
|
|
151
|
+
setStringifyQueryParams = (stringifyFn: StringifyCallbackType): Client<GlobalErrorType, AdapterOptions> => {
|
|
152
|
+
this.stringifyQueryParams = stringifyFn;
|
|
153
|
+
return this;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Set the custom header mapping function
|
|
158
|
+
*/
|
|
159
|
+
setHeaderMapper = (headerMapper: HeaderMappingType): Client<GlobalErrorType, AdapterOptions> => {
|
|
160
|
+
this.headerMapper = headerMapper;
|
|
161
|
+
return this;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Set the request payload mapping function which get triggered before request get send
|
|
166
|
+
*/
|
|
167
|
+
setPayloadMapper = (payloadMapper: AdapterPayloadMappingType): Client<GlobalErrorType, AdapterOptions> => {
|
|
168
|
+
this.payloadMapper = payloadMapper;
|
|
169
|
+
return this;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Set custom http adapter to handle graphql, rest, firebase or other
|
|
174
|
+
*/
|
|
175
|
+
setAdapter = (callback: (Client: ClientInstance) => AdapterType): Client<GlobalErrorType, AdapterOptions> => {
|
|
176
|
+
this.adapter = callback(this);
|
|
177
|
+
return this;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 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.
|
|
182
|
+
*/
|
|
183
|
+
onAuth = (callback: RequestInterceptorType): Client<GlobalErrorType, AdapterOptions> => {
|
|
184
|
+
this.__onAuthCallbacks.push(callback);
|
|
185
|
+
return this;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Method for intercepting error responses. It can be used for example to refresh tokens.
|
|
190
|
+
*/
|
|
191
|
+
onError = <ErrorType = null>(
|
|
192
|
+
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType>,
|
|
193
|
+
): Client<GlobalErrorType, AdapterOptions> => {
|
|
194
|
+
this.__onErrorCallbacks.push(callback);
|
|
195
|
+
return this;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Method for intercepting success responses.
|
|
200
|
+
*/
|
|
201
|
+
onSuccess = <ErrorType = null>(
|
|
202
|
+
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType>,
|
|
203
|
+
): Client<GlobalErrorType, AdapterOptions> => {
|
|
204
|
+
this.__onSuccessCallbacks.push(callback);
|
|
205
|
+
return this;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Method of manipulating requests before sending the request.
|
|
210
|
+
*/
|
|
211
|
+
onRequest = (callback: RequestInterceptorType): Client<GlobalErrorType, AdapterOptions> => {
|
|
212
|
+
this.__onRequestCallbacks.push(callback);
|
|
213
|
+
return this;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Method for intercepting any responses.
|
|
218
|
+
*/
|
|
219
|
+
onResponse = <ErrorType = null>(
|
|
220
|
+
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType>,
|
|
221
|
+
): Client<GlobalErrorType, AdapterOptions> => {
|
|
222
|
+
this.__onResponseCallbacks.push(callback);
|
|
223
|
+
return this;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Add persistent effects which trigger on the request lifecycle
|
|
228
|
+
*/
|
|
229
|
+
addEffect = (effect: RequestEffectInstance | RequestEffectInstance[]) => {
|
|
230
|
+
this.effects = this.effects.concat(effect);
|
|
231
|
+
|
|
232
|
+
return this;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Remove effects from Client
|
|
237
|
+
*/
|
|
238
|
+
removeEffect = (effect: RequestEffectInstance | string) => {
|
|
239
|
+
const name = typeof effect === "string" ? effect : effect.getEffectKey();
|
|
240
|
+
this.effects = this.effects.filter((currentEffect) => currentEffect.getEffectKey() !== name);
|
|
241
|
+
|
|
242
|
+
return this;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Helper used by http adapter to apply the modifications on response error
|
|
247
|
+
*/
|
|
248
|
+
__modifyAuth = async (request: RequestInstance) => interceptRequest(this.__onAuthCallbacks, request);
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Private helper to run async pre-request processing
|
|
252
|
+
*/
|
|
253
|
+
__modifyRequest = async (request: RequestInstance) => interceptRequest(this.__onRequestCallbacks, request);
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Private helper to run async on-error response processing
|
|
257
|
+
*/
|
|
258
|
+
__modifyErrorResponse = async (response: ResponseType<any, GlobalErrorType>, request: RequestInstance) =>
|
|
259
|
+
interceptResponse<GlobalErrorType>(this.__onErrorCallbacks, response, request);
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Private helper to run async on-success response processing
|
|
263
|
+
*/
|
|
264
|
+
__modifySuccessResponse = async (response: ResponseType<any, GlobalErrorType>, request: RequestInstance) =>
|
|
265
|
+
interceptResponse<GlobalErrorType>(this.__onSuccessCallbacks, response, request);
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Private helper to run async response processing
|
|
269
|
+
*/
|
|
270
|
+
__modifyResponse = async (response: ResponseType<any, GlobalErrorType>, request: RequestInstance) =>
|
|
271
|
+
interceptResponse<GlobalErrorType>(this.__onResponseCallbacks, response, request);
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Clears the Client instance and remove all listeners on it's dependencies
|
|
275
|
+
*/
|
|
276
|
+
clear = () => {
|
|
277
|
+
const { appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
278
|
+
|
|
279
|
+
this.requestManager.abortControllers.clear();
|
|
280
|
+
this.fetchDispatcher.clear();
|
|
281
|
+
this.submitDispatcher.clear();
|
|
282
|
+
this.cache.clear();
|
|
283
|
+
|
|
284
|
+
this.requestManager.emitter.removeAllListeners();
|
|
285
|
+
this.fetchDispatcher.emitter.removeAllListeners();
|
|
286
|
+
this.submitDispatcher.emitter.removeAllListeners();
|
|
287
|
+
this.cache.emitter.removeAllListeners();
|
|
288
|
+
|
|
289
|
+
this.appManager = appManager?.(this) || new AppManager();
|
|
290
|
+
this.cache = cache?.(this) || new Cache(this);
|
|
291
|
+
this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
|
|
292
|
+
this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Create requests based on the Client setup
|
|
297
|
+
*/
|
|
298
|
+
createRequest = <
|
|
299
|
+
Response,
|
|
300
|
+
Payload = undefined,
|
|
301
|
+
LocalError extends ClientErrorType | undefined = undefined,
|
|
302
|
+
QueryParams extends QueryParamsType | string = QueryParamsType | string,
|
|
303
|
+
>() => {
|
|
304
|
+
return <EndpointType extends string>(params: RequestOptionsType<EndpointType, AdapterOptions>) =>
|
|
305
|
+
new Request<Response, Payload, QueryParams, GlobalErrorType, LocalError, EndpointType, AdapterOptions>(
|
|
306
|
+
this,
|
|
307
|
+
params,
|
|
308
|
+
);
|
|
309
|
+
};
|
|
310
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Cache } from "cache";
|
|
2
|
+
import { RequestInstance } from "request";
|
|
3
|
+
import { Dispatcher } from "dispatcher";
|
|
4
|
+
import { ResponseType, AdapterType, QueryParamsType } from "adapter";
|
|
5
|
+
import { Client } from "client";
|
|
6
|
+
import { AppManager } from "managers";
|
|
7
|
+
import { NegativeTypes } from "types";
|
|
8
|
+
|
|
9
|
+
export type ClientErrorType = Record<string, any> | string;
|
|
10
|
+
export type ClientInstance = Client<any, any>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration setup for the client
|
|
14
|
+
*/
|
|
15
|
+
export type ClientOptionsType = {
|
|
16
|
+
/**
|
|
17
|
+
* Url to your server
|
|
18
|
+
*/
|
|
19
|
+
url: string;
|
|
20
|
+
/**
|
|
21
|
+
* Custom adapter initialization prop
|
|
22
|
+
*/
|
|
23
|
+
adapter?: AdapterType;
|
|
24
|
+
/**
|
|
25
|
+
* Custom cache initialization prop
|
|
26
|
+
*/
|
|
27
|
+
cache?: <B extends ClientInstance, C extends Cache>(client: B) => C;
|
|
28
|
+
/**
|
|
29
|
+
* Custom app manager initialization prop
|
|
30
|
+
*/
|
|
31
|
+
appManager?: <B extends ClientInstance, A extends AppManager>(client: B) => A;
|
|
32
|
+
/**
|
|
33
|
+
* Custom fetch dispatcher initialization prop
|
|
34
|
+
*/
|
|
35
|
+
fetchDispatcher?: <B extends ClientInstance, D extends Dispatcher>(client: B) => D;
|
|
36
|
+
/**
|
|
37
|
+
* Custom submit dispatcher initialization prop
|
|
38
|
+
*/
|
|
39
|
+
submitDispatcher?: <B extends ClientInstance, D extends Dispatcher>(client: B) => D;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Interceptors
|
|
43
|
+
|
|
44
|
+
export type RequestInterceptorType = (request: RequestInstance) => Promise<RequestInstance> | RequestInstance;
|
|
45
|
+
export type ResponseInterceptorType<Response = any, Error = any> = (
|
|
46
|
+
response: ResponseType<Response, Error>,
|
|
47
|
+
request: RequestInstance,
|
|
48
|
+
) => Promise<ResponseType<any, any>> | ResponseType<any, any>;
|
|
49
|
+
|
|
50
|
+
// Stringify
|
|
51
|
+
|
|
52
|
+
export type StringifyCallbackType = (queryParams: QueryParamsType | string | NegativeTypes) => string;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "
|
|
8
|
-
import {
|
|
9
|
-
import { stringifyDefaultOptions } from "
|
|
2
|
+
QueryParamType,
|
|
3
|
+
QueryParamsType,
|
|
4
|
+
QueryParamValuesType,
|
|
5
|
+
ResponseType,
|
|
6
|
+
QueryStringifyOptionsType,
|
|
7
|
+
} from "adapter";
|
|
8
|
+
import { RequestInstance } from "request";
|
|
9
|
+
import { stringifyDefaultOptions } from "client";
|
|
10
10
|
import { NegativeTypes } from "types";
|
|
11
|
-
import {
|
|
11
|
+
import { RequestInterceptorType, ResponseInterceptorType } from "./client.types";
|
|
12
12
|
|
|
13
13
|
// Utils
|
|
14
14
|
|
|
@@ -20,30 +20,30 @@ export const stringifyValue = (response: string | unknown): string => {
|
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export const interceptRequest = async (interceptors:
|
|
24
|
-
let
|
|
25
|
-
if (!
|
|
23
|
+
export const interceptRequest = async (interceptors: RequestInterceptorType[], request: RequestInstance) => {
|
|
24
|
+
let newRequest = request;
|
|
25
|
+
if (!request.requestOptions.disableRequestInterceptors) {
|
|
26
26
|
// eslint-disable-next-line no-restricted-syntax
|
|
27
27
|
for (const interceptor of interceptors) {
|
|
28
28
|
// eslint-disable-next-line no-await-in-loop
|
|
29
|
-
|
|
30
|
-
if (!
|
|
29
|
+
newRequest = (await interceptor(request)) as RequestInstance;
|
|
30
|
+
if (!newRequest) throw new Error("Request modifier must return request");
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
return
|
|
33
|
+
return newRequest;
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
export const interceptResponse = async <GlobalErrorType>(
|
|
37
|
-
interceptors:
|
|
38
|
-
response:
|
|
39
|
-
|
|
37
|
+
interceptors: ResponseInterceptorType[],
|
|
38
|
+
response: ResponseType<any, GlobalErrorType>,
|
|
39
|
+
request: RequestInstance,
|
|
40
40
|
) => {
|
|
41
41
|
let newResponse = response;
|
|
42
|
-
if (!
|
|
42
|
+
if (!request.requestOptions.disableResponseInterceptors) {
|
|
43
43
|
// eslint-disable-next-line no-restricted-syntax
|
|
44
44
|
for (const interceptor of interceptors) {
|
|
45
45
|
// eslint-disable-next-line no-await-in-loop
|
|
46
|
-
newResponse = await interceptor(response,
|
|
46
|
+
newResponse = await interceptor(response, request);
|
|
47
47
|
if (!newResponse) throw new Error("Response modifier must return data");
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -51,17 +51,17 @@ export const interceptResponse = async <GlobalErrorType>(
|
|
|
51
51
|
};
|
|
52
52
|
// Mappers
|
|
53
53
|
|
|
54
|
-
export const
|
|
55
|
-
const isFormData =
|
|
54
|
+
export const getAdapterHeaders = (request: RequestInstance) => {
|
|
55
|
+
const isFormData = request.data instanceof FormData;
|
|
56
56
|
const headers: HeadersInit = {};
|
|
57
57
|
|
|
58
58
|
if (!isFormData) headers["Content-Type"] = "application/json";
|
|
59
59
|
|
|
60
|
-
Object.assign(headers,
|
|
60
|
+
Object.assign(headers, request.headers);
|
|
61
61
|
return headers as HeadersInit;
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
-
export const
|
|
64
|
+
export const getAdapterPayload = (data: unknown): string | FormData => {
|
|
65
65
|
const isFormData = data instanceof FormData;
|
|
66
66
|
if (isFormData) return data;
|
|
67
67
|
|
|
@@ -70,8 +70,8 @@ export const getClientPayload = (data: unknown): string | FormData => {
|
|
|
70
70
|
|
|
71
71
|
// Stringify
|
|
72
72
|
|
|
73
|
-
const isValidValue = (options:
|
|
74
|
-
return (value:
|
|
73
|
+
const isValidValue = (options: QueryStringifyOptionsType) => {
|
|
74
|
+
return (value: QueryParamType) => {
|
|
75
75
|
const { skipNull, skipEmptyString } = options;
|
|
76
76
|
|
|
77
77
|
if (skipEmptyString && value === undefined) {
|
|
@@ -87,7 +87,10 @@ const isValidValue = (options: QueryStringifyOptions) => {
|
|
|
87
87
|
};
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
-
const encodeValue = (
|
|
90
|
+
const encodeValue = (
|
|
91
|
+
value: string,
|
|
92
|
+
{ encode, strict }: Pick<QueryStringifyOptionsType, "encode" | "strict">,
|
|
93
|
+
): string => {
|
|
91
94
|
if (encode && strict) {
|
|
92
95
|
return encodeURIComponent(value).replace(/[!'()*]/g, (s) => `%${s.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
93
96
|
}
|
|
@@ -97,7 +100,7 @@ const encodeValue = (value: string, { encode, strict }: Pick<QueryStringifyOptio
|
|
|
97
100
|
return value;
|
|
98
101
|
};
|
|
99
102
|
|
|
100
|
-
const encodeParams = (key: string, value:
|
|
103
|
+
const encodeParams = (key: string, value: QueryParamType, options: QueryStringifyOptionsType) => {
|
|
101
104
|
const shouldSkip = !isValidValue(options)(value);
|
|
102
105
|
|
|
103
106
|
if (!key || shouldSkip) {
|
|
@@ -107,7 +110,7 @@ const encodeParams = (key: string, value: ClientQueryParam, options: QueryString
|
|
|
107
110
|
return `${encodeValue(key, options)}=${encodeValue(String(value), options)}`;
|
|
108
111
|
};
|
|
109
112
|
|
|
110
|
-
const encodeArray = (key: string, array: Array<
|
|
113
|
+
const encodeArray = (key: string, array: Array<QueryParamValuesType>, options: QueryStringifyOptionsType): string => {
|
|
111
114
|
const { arrayFormat, arraySeparator } = options;
|
|
112
115
|
|
|
113
116
|
return array
|
|
@@ -148,8 +151,8 @@ const encodeArray = (key: string, array: Array<ClientQueryParamValues>, options:
|
|
|
148
151
|
};
|
|
149
152
|
|
|
150
153
|
export const stringifyQueryParams = (
|
|
151
|
-
queryParams:
|
|
152
|
-
options:
|
|
154
|
+
queryParams: QueryParamsType | string | NegativeTypes,
|
|
155
|
+
options: QueryStringifyOptionsType = stringifyDefaultOptions,
|
|
153
156
|
): string => {
|
|
154
157
|
if (!queryParams || !Object.keys(queryParams).length) {
|
|
155
158
|
return "";
|
package/src/client/index.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./fetch.client.bindings";
|
|
6
|
-
export * from "./fetch.client.constants";
|
|
1
|
+
export * from "./client";
|
|
2
|
+
export * from "./client.utils";
|
|
3
|
+
export * from "./client.types";
|
|
4
|
+
export * from "./client.constants";
|
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
4
|
+
QueueDataType,
|
|
5
5
|
getDispatcherChangeEventKey,
|
|
6
6
|
getDispatcherStatusEventKey,
|
|
7
7
|
getDispatcherDrainedEventKey,
|
|
8
8
|
} from "dispatcher";
|
|
9
|
-
import {
|
|
9
|
+
import { RequestInstance } from "request";
|
|
10
10
|
|
|
11
11
|
export const getDispatcherEvents = (emitter: EventEmitter) => ({
|
|
12
|
-
setDrained: <
|
|
12
|
+
setDrained: <Request extends RequestInstance>(queueKey: string, values: QueueDataType<Request>): void => {
|
|
13
13
|
emitter.emit(getDispatcherDrainedEventKey(queueKey), values);
|
|
14
14
|
},
|
|
15
|
-
setQueueStatus: <
|
|
15
|
+
setQueueStatus: <Request extends RequestInstance>(queueKey: string, values: QueueDataType<Request>): void => {
|
|
16
16
|
emitter.emit(getDispatcherStatusEventKey(queueKey), values);
|
|
17
17
|
},
|
|
18
|
-
setQueueChanged: <
|
|
18
|
+
setQueueChanged: <Request extends RequestInstance>(queueKey: string, values: QueueDataType<Request>): void => {
|
|
19
19
|
emitter.emit(getDispatcherChangeEventKey(queueKey), values);
|
|
20
20
|
},
|
|
21
|
-
onDrained: <
|
|
21
|
+
onDrained: <Request extends RequestInstance>(
|
|
22
22
|
queueKey: string,
|
|
23
|
-
callback: (values:
|
|
23
|
+
callback: (values: QueueDataType<Request>) => void,
|
|
24
24
|
): VoidFunction => {
|
|
25
25
|
emitter.on(getDispatcherDrainedEventKey(queueKey), callback);
|
|
26
26
|
return () => emitter.removeListener(getDispatcherDrainedEventKey(queueKey), callback);
|
|
27
27
|
},
|
|
28
|
-
onQueueStatus: <
|
|
28
|
+
onQueueStatus: <Request extends RequestInstance>(
|
|
29
29
|
queueKey: string,
|
|
30
|
-
callback: (values:
|
|
30
|
+
callback: (values: QueueDataType<Request>) => void,
|
|
31
31
|
): VoidFunction => {
|
|
32
32
|
emitter.on(getDispatcherStatusEventKey(queueKey), callback);
|
|
33
33
|
return () => emitter.removeListener(getDispatcherStatusEventKey(queueKey), callback);
|
|
34
34
|
},
|
|
35
|
-
onQueueChange: <
|
|
35
|
+
onQueueChange: <Request extends RequestInstance>(
|
|
36
36
|
queueKey: string,
|
|
37
|
-
callback: (values:
|
|
37
|
+
callback: (values: QueueDataType<Request>) => void,
|
|
38
38
|
): VoidFunction => {
|
|
39
39
|
emitter.on(getDispatcherChangeEventKey(queueKey), callback);
|
|
40
40
|
return () => emitter.removeListener(getDispatcherChangeEventKey(queueKey), callback);
|