@hyper-fetch/core 1.6.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 +7 -0
- package/README.md +83 -0
- package/dist/builder/builder.constants.d.ts +9 -0
- package/dist/builder/builder.d.ts +141 -0
- package/dist/builder/builder.types.d.ts +45 -0
- package/dist/builder/builder.utils.d.ts +10 -0
- package/dist/builder/index.d.ts +4 -0
- package/dist/cache/cache.d.ts +76 -0
- package/dist/cache/cache.events.d.ts +28 -0
- package/dist/cache/cache.types.d.ts +49 -0
- package/dist/cache/cache.utils.d.ts +6 -0
- package/dist/cache/index.d.ts +4 -0
- package/dist/client/fetch.client.bindings.d.ts +25 -0
- package/dist/client/fetch.client.constants.d.ts +1 -0
- package/dist/client/fetch.client.d.ts +2 -0
- package/dist/client/fetch.client.types.d.ts +57 -0
- package/dist/client/fetch.client.utils.d.ts +5 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/command/command.d.ts +87 -0
- package/dist/command/command.types.d.ts +196 -0
- package/dist/command/command.utils.d.ts +22 -0
- package/dist/command/index.d.ts +3 -0
- package/dist/constants/http.constants.d.ts +7 -0
- package/dist/constants/time.constants.d.ts +11 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +6 -0
- package/dist/dispatcher/dispatcher.d.ts +155 -0
- package/dist/dispatcher/dispatcher.events.d.ts +11 -0
- package/dist/dispatcher/dispatcher.types.d.ts +31 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +10 -0
- package/dist/dispatcher/index.d.ts +5 -0
- package/dist/effect/fetch.effect.d.ts +14 -0
- package/dist/effect/fetch.effect.types.d.ts +32 -0
- package/dist/effect/index.d.ts +2 -0
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/managers/app/app.manager.constants.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.events.d.ts +11 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.utils.d.ts +4 -0
- package/dist/managers/app/index.d.ts +5 -0
- package/dist/managers/command/command.manager.d.ts +43 -0
- package/dist/managers/command/command.manager.events.d.ts +36 -0
- package/dist/managers/command/command.manager.types.d.ts +23 -0
- package/dist/managers/command/command.manager.utils.d.ts +16 -0
- package/dist/managers/command/index.d.ts +4 -0
- package/dist/managers/index.d.ts +3 -0
- package/dist/managers/logger/index.d.ts +4 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +4 -0
- package/dist/managers/logger/logger.manager.d.ts +19 -0
- package/dist/managers/logger/logger.manager.types.d.ts +30 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +3 -0
- package/dist/types/fetch.types.d.ts +15 -0
- package/dist/types/helpers.types.d.ts +11 -0
- package/dist/types/http.types.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/uuid.utils.d.ts +1 -0
- package/package.json +60 -0
- package/src/builder/builder.constants.ts +9 -0
- package/src/builder/builder.ts +309 -0
- package/src/builder/builder.types.ts +57 -0
- package/src/builder/builder.utils.ts +178 -0
- package/src/builder/index.ts +4 -0
- package/src/cache/cache.events.ts +44 -0
- package/src/cache/cache.ts +221 -0
- package/src/cache/cache.types.ts +56 -0
- package/src/cache/cache.utils.ts +29 -0
- package/src/cache/index.ts +4 -0
- package/src/client/fetch.client.bindings.ts +290 -0
- package/src/client/fetch.client.constants.ts +3 -0
- package/src/client/fetch.client.ts +98 -0
- package/src/client/fetch.client.types.ts +83 -0
- package/src/client/fetch.client.utils.ts +28 -0
- package/src/client/index.ts +5 -0
- package/src/command/command.ts +539 -0
- package/src/command/command.types.ts +268 -0
- package/src/command/command.utils.ts +179 -0
- package/src/command/index.ts +3 -0
- package/src/constants/http.constants.ts +7 -0
- package/src/constants/time.constants.ts +11 -0
- package/src/dispatcher/dispatcher.constants.ts +6 -0
- package/src/dispatcher/dispatcher.events.ts +42 -0
- package/src/dispatcher/dispatcher.ts +596 -0
- package/src/dispatcher/dispatcher.types.ts +39 -0
- package/src/dispatcher/dispatcher.utils.ts +55 -0
- package/src/dispatcher/index.ts +5 -0
- package/src/effect/fetch.effect.ts +28 -0
- package/src/effect/fetch.effect.types.ts +35 -0
- package/src/effect/index.ts +2 -0
- package/src/index.ts +12 -0
- package/src/managers/app/app.manager.constants.ts +24 -0
- package/src/managers/app/app.manager.events.ts +34 -0
- package/src/managers/app/app.manager.ts +74 -0
- package/src/managers/app/app.manager.types.ts +8 -0
- package/src/managers/app/app.manager.utils.ts +34 -0
- package/src/managers/app/index.ts +5 -0
- package/src/managers/command/command.manager.events.ts +212 -0
- package/src/managers/command/command.manager.ts +68 -0
- package/src/managers/command/command.manager.types.ts +25 -0
- package/src/managers/command/command.manager.utils.ts +19 -0
- package/src/managers/command/index.ts +4 -0
- package/src/managers/index.ts +3 -0
- package/src/managers/logger/index.ts +4 -0
- package/src/managers/logger/logger.manager.constants.ts +24 -0
- package/src/managers/logger/logger.manager.ts +70 -0
- package/src/managers/logger/logger.manager.types.ts +35 -0
- package/src/managers/logger/logger.manager.utils.ts +27 -0
- package/src/types/fetch.types.ts +39 -0
- package/src/types/helpers.types.ts +15 -0
- package/src/types/http.types.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +3 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { getErrorMessage, ClientResponseSuccessType, ClientResponseErrorType, ProgressRequestDataType } from "client";
|
|
2
|
+
import { CommandInstance, getProgressData, ClientProgressEvent } from "command";
|
|
3
|
+
import { ExtractResponse, ExtractError } from "types";
|
|
4
|
+
|
|
5
|
+
export const getClientBindings = async (cmd: CommandInstance, requestId: string) => {
|
|
6
|
+
const { baseUrl, commandManager, loggerManager, headerMapper, payloadMapper } = cmd.builder;
|
|
7
|
+
|
|
8
|
+
const logger = loggerManager.init("Client");
|
|
9
|
+
|
|
10
|
+
let requestStartTimestamp: null | number = null;
|
|
11
|
+
let responseStartTimestamp: null | number = null;
|
|
12
|
+
let command = cmd;
|
|
13
|
+
|
|
14
|
+
// Progress
|
|
15
|
+
let requestTotal = 1;
|
|
16
|
+
let responseTotal = 1;
|
|
17
|
+
let previousRequestTotal = 0;
|
|
18
|
+
let previousResponseTotal = 0;
|
|
19
|
+
|
|
20
|
+
// Pre request modifications
|
|
21
|
+
logger.debug(`Starting command middleware callbacks`);
|
|
22
|
+
|
|
23
|
+
command = await command.builder.__modifyRequest(cmd);
|
|
24
|
+
|
|
25
|
+
if (command.auth) {
|
|
26
|
+
command = await command.builder.__modifyAuth(cmd);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Command Setup
|
|
30
|
+
const { builder, abortKey, queueKey, endpoint, data } = command;
|
|
31
|
+
|
|
32
|
+
commandManager.addAbortController(abortKey, requestId);
|
|
33
|
+
|
|
34
|
+
const fullUrl = baseUrl + endpoint;
|
|
35
|
+
const effects = builder.effects.filter((effect) => command.effectKey === effect.getEffectKey());
|
|
36
|
+
const headers = headerMapper(command);
|
|
37
|
+
const payload = payloadMapper(data);
|
|
38
|
+
const config = { ...command.commandOptions.options };
|
|
39
|
+
|
|
40
|
+
const getRequestStartTimestamp = () => {
|
|
41
|
+
return requestStartTimestamp;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const getResponseStartTimestamp = () => {
|
|
45
|
+
return responseStartTimestamp;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Progress
|
|
49
|
+
|
|
50
|
+
const getTotal = (previousTotal: number, progress?: ProgressRequestDataType) => {
|
|
51
|
+
if (!progress) return previousTotal;
|
|
52
|
+
const total = Number(progress.total || 0);
|
|
53
|
+
const loaded = Number(progress.loaded || 0);
|
|
54
|
+
return Math.max(total, loaded, previousTotal);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const handleRequestProgress = (
|
|
58
|
+
startTimestamp: number,
|
|
59
|
+
progressTimestamp: number,
|
|
60
|
+
progressEvent: ClientProgressEvent,
|
|
61
|
+
) => {
|
|
62
|
+
const progress = getProgressData(new Date(startTimestamp), new Date(progressTimestamp), progressEvent);
|
|
63
|
+
|
|
64
|
+
if (previousRequestTotal !== 100) {
|
|
65
|
+
previousRequestTotal = progress.total;
|
|
66
|
+
commandManager.events.emitUploadProgress(queueKey, requestId, progress, { requestId, command });
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const handleResponseProgress = (
|
|
71
|
+
startTimestamp: number,
|
|
72
|
+
progressTimestamp: number,
|
|
73
|
+
progressEvent: ClientProgressEvent,
|
|
74
|
+
) => {
|
|
75
|
+
const progress = getProgressData(new Date(startTimestamp), new Date(progressTimestamp), progressEvent);
|
|
76
|
+
|
|
77
|
+
if (previousResponseTotal !== 100) {
|
|
78
|
+
previousResponseTotal = progress.total;
|
|
79
|
+
commandManager.events.emitDownloadProgress(queueKey, requestId, progress, { requestId, command });
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Pre-request
|
|
84
|
+
|
|
85
|
+
const onBeforeRequest = () => {
|
|
86
|
+
effects.forEach((effect) => effect.onTrigger(command));
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// Request
|
|
90
|
+
|
|
91
|
+
const onRequestStart = (progress?: ProgressRequestDataType) => {
|
|
92
|
+
effects.forEach((action) => action.onStart(command));
|
|
93
|
+
|
|
94
|
+
if (progress?.total) {
|
|
95
|
+
requestTotal = getTotal(requestTotal, progress);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const initialPayload = {
|
|
99
|
+
total: requestTotal,
|
|
100
|
+
loaded: progress?.loaded || 0,
|
|
101
|
+
};
|
|
102
|
+
requestStartTimestamp = +new Date();
|
|
103
|
+
handleRequestProgress(requestStartTimestamp, requestStartTimestamp, initialPayload);
|
|
104
|
+
commandManager.events.emitRequestStart(queueKey, requestId, { requestId, command });
|
|
105
|
+
return requestStartTimestamp;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const onRequestProgress = (progress: ProgressRequestDataType) => {
|
|
109
|
+
if (!requestStartTimestamp) {
|
|
110
|
+
requestStartTimestamp = +new Date();
|
|
111
|
+
}
|
|
112
|
+
requestTotal = getTotal(requestTotal, progress);
|
|
113
|
+
|
|
114
|
+
const progressTimestamp = +new Date();
|
|
115
|
+
|
|
116
|
+
handleRequestProgress(requestStartTimestamp, progressTimestamp, {
|
|
117
|
+
total: requestTotal,
|
|
118
|
+
loaded: progress.loaded || 0,
|
|
119
|
+
});
|
|
120
|
+
return progressTimestamp;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const onRequestEnd = () => {
|
|
124
|
+
if (!requestStartTimestamp) {
|
|
125
|
+
requestStartTimestamp = +new Date();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const progressTimestamp = +new Date();
|
|
129
|
+
handleRequestProgress(requestStartTimestamp, progressTimestamp, {
|
|
130
|
+
total: requestTotal,
|
|
131
|
+
loaded: requestTotal,
|
|
132
|
+
});
|
|
133
|
+
return progressTimestamp;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Response
|
|
137
|
+
|
|
138
|
+
const onResponseStart = (progress?: ProgressRequestDataType) => {
|
|
139
|
+
responseStartTimestamp = +new Date();
|
|
140
|
+
|
|
141
|
+
responseTotal = getTotal(responseTotal, progress);
|
|
142
|
+
|
|
143
|
+
const initialPayload = {
|
|
144
|
+
total: responseTotal,
|
|
145
|
+
loaded: progress?.loaded || 0,
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
handleResponseProgress(responseStartTimestamp, responseStartTimestamp, initialPayload);
|
|
149
|
+
commandManager.events.emitResponseStart(queueKey, requestId, { requestId, command });
|
|
150
|
+
return responseStartTimestamp;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const onResponseProgress = (progress: ProgressRequestDataType) => {
|
|
154
|
+
if (!responseStartTimestamp) {
|
|
155
|
+
responseStartTimestamp = +new Date();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const progressTimestamp = +new Date();
|
|
159
|
+
responseTotal = getTotal(responseTotal, progress);
|
|
160
|
+
|
|
161
|
+
handleResponseProgress(responseStartTimestamp, progressTimestamp, {
|
|
162
|
+
total: progress.total || responseTotal,
|
|
163
|
+
loaded: progress.loaded || 0,
|
|
164
|
+
});
|
|
165
|
+
return progressTimestamp;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const onResponseEnd = () => {
|
|
169
|
+
if (!responseStartTimestamp) {
|
|
170
|
+
responseStartTimestamp = +new Date();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const progressTimestamp = +new Date();
|
|
174
|
+
commandManager.removeAbortController(abortKey, requestId);
|
|
175
|
+
handleResponseProgress(responseStartTimestamp, progressTimestamp, {
|
|
176
|
+
total: responseTotal,
|
|
177
|
+
loaded: responseTotal,
|
|
178
|
+
});
|
|
179
|
+
return progressTimestamp;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// Success
|
|
183
|
+
|
|
184
|
+
const onSuccess = async <T extends CommandInstance>(
|
|
185
|
+
responseData: unknown,
|
|
186
|
+
status: number,
|
|
187
|
+
resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
|
|
188
|
+
): Promise<ClientResponseSuccessType<ExtractResponse<T>>> => {
|
|
189
|
+
let response = [responseData, null, status] as ClientResponseSuccessType<ExtractResponse<T>>;
|
|
190
|
+
response = await command.builder.__modifyResponse(response, command);
|
|
191
|
+
response = await command.builder.__modifySuccessResponse(response, command);
|
|
192
|
+
|
|
193
|
+
effects.forEach((effect) => effect.onSuccess(response, command));
|
|
194
|
+
effects.forEach((effect) => effect.onFinished(response, command));
|
|
195
|
+
|
|
196
|
+
resolve(response);
|
|
197
|
+
|
|
198
|
+
return response;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// Errors
|
|
202
|
+
|
|
203
|
+
const onError = async <T extends CommandInstance>(
|
|
204
|
+
error: Error | ExtractError<T>,
|
|
205
|
+
status: number,
|
|
206
|
+
resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
|
|
207
|
+
): Promise<ClientResponseErrorType<ExtractError<T>>> => {
|
|
208
|
+
let responseData = [null, error, status] as ClientResponseErrorType<ExtractError<T>>;
|
|
209
|
+
|
|
210
|
+
responseData = await command.builder.__modifyResponse(responseData, command);
|
|
211
|
+
responseData = await command.builder.__modifyErrorResponse(responseData, command);
|
|
212
|
+
|
|
213
|
+
effects.forEach((effect) => effect.onError(responseData, command));
|
|
214
|
+
effects.forEach((effect) => effect.onFinished(responseData, command));
|
|
215
|
+
|
|
216
|
+
resolve(responseData);
|
|
217
|
+
|
|
218
|
+
return responseData;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const onAbortError = <T extends CommandInstance>(
|
|
222
|
+
resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
|
|
223
|
+
) => {
|
|
224
|
+
const error = getErrorMessage("abort");
|
|
225
|
+
return onError(error, 0, resolve);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const onTimeoutError = <T extends CommandInstance>(
|
|
229
|
+
resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
|
|
230
|
+
) => {
|
|
231
|
+
const error = getErrorMessage("timeout");
|
|
232
|
+
return onError(error, 0, resolve);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const onUnexpectedError = <T extends CommandInstance>(
|
|
236
|
+
resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
|
|
237
|
+
) => {
|
|
238
|
+
const error = getErrorMessage();
|
|
239
|
+
return onError(error, 0, resolve);
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// Abort
|
|
243
|
+
|
|
244
|
+
const getAbortController = () => {
|
|
245
|
+
return commandManager.getAbortController(abortKey, requestId);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const createAbortListener = <T extends CommandInstance>(
|
|
249
|
+
callback: () => void,
|
|
250
|
+
resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
|
|
251
|
+
) => {
|
|
252
|
+
const controller = getAbortController();
|
|
253
|
+
if (!controller) {
|
|
254
|
+
throw new Error("Controller is not found");
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const fn = () => {
|
|
258
|
+
callback();
|
|
259
|
+
onAbortError(resolve);
|
|
260
|
+
commandManager.events.emitAbort(abortKey, requestId, command);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
controller.signal.addEventListener("abort", fn);
|
|
264
|
+
|
|
265
|
+
return () => controller.signal.removeEventListener("abort", fn);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
fullUrl,
|
|
270
|
+
headers,
|
|
271
|
+
payload,
|
|
272
|
+
config,
|
|
273
|
+
getAbortController,
|
|
274
|
+
getRequestStartTimestamp,
|
|
275
|
+
getResponseStartTimestamp,
|
|
276
|
+
createAbortListener,
|
|
277
|
+
onBeforeRequest,
|
|
278
|
+
onRequestStart,
|
|
279
|
+
onRequestProgress,
|
|
280
|
+
onRequestEnd,
|
|
281
|
+
onResponseStart,
|
|
282
|
+
onResponseProgress,
|
|
283
|
+
onResponseEnd,
|
|
284
|
+
onSuccess,
|
|
285
|
+
onAbortError,
|
|
286
|
+
onTimeoutError,
|
|
287
|
+
onUnexpectedError,
|
|
288
|
+
onError,
|
|
289
|
+
};
|
|
290
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { getClientBindings, defaultTimeout, ClientResponseType, ClientType } from "client";
|
|
2
|
+
import { parseErrorResponse, parseResponse } from "./fetch.client.utils";
|
|
3
|
+
|
|
4
|
+
export const fetchClient: ClientType = async (command, requestId) => {
|
|
5
|
+
if (!window.XMLHttpRequest) {
|
|
6
|
+
throw new Error("There is no XMLHttpRequest, make sure it's provided to use Hyper Fetch built-in client.");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
fullUrl,
|
|
11
|
+
headers,
|
|
12
|
+
payload,
|
|
13
|
+
config,
|
|
14
|
+
createAbortListener,
|
|
15
|
+
onBeforeRequest,
|
|
16
|
+
onRequestStart,
|
|
17
|
+
onRequestProgress,
|
|
18
|
+
onRequestEnd,
|
|
19
|
+
onResponseStart,
|
|
20
|
+
onResponseProgress,
|
|
21
|
+
onSuccess,
|
|
22
|
+
// onAbortError,
|
|
23
|
+
// onTimeoutError,
|
|
24
|
+
onError,
|
|
25
|
+
onResponseEnd,
|
|
26
|
+
} = await getClientBindings(command, requestId);
|
|
27
|
+
|
|
28
|
+
const { method } = command;
|
|
29
|
+
|
|
30
|
+
const xhr = new XMLHttpRequest();
|
|
31
|
+
xhr.timeout = defaultTimeout;
|
|
32
|
+
|
|
33
|
+
const abort = () => xhr.abort();
|
|
34
|
+
|
|
35
|
+
return new Promise<ClientResponseType<unknown, unknown>>((resolve) => {
|
|
36
|
+
// Inject xhr options
|
|
37
|
+
Object.entries(config).forEach(([name, value]) => {
|
|
38
|
+
xhr[name] = value;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Open connection
|
|
42
|
+
xhr.open(method, fullUrl, true);
|
|
43
|
+
|
|
44
|
+
// Set Headers
|
|
45
|
+
Object.entries(headers).forEach(([name, value]) => xhr.setRequestHeader(name, value));
|
|
46
|
+
|
|
47
|
+
// Listen to abort signal
|
|
48
|
+
const unmountListener = createAbortListener(abort, resolve);
|
|
49
|
+
|
|
50
|
+
// Request handlers
|
|
51
|
+
xhr.upload.onprogress = onRequestProgress;
|
|
52
|
+
|
|
53
|
+
// Response handlers
|
|
54
|
+
xhr.onloadstart = (): void => {
|
|
55
|
+
onRequestEnd();
|
|
56
|
+
onResponseStart();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
xhr.onprogress = onResponseProgress;
|
|
60
|
+
|
|
61
|
+
xhr.onloadend = () => {
|
|
62
|
+
onResponseEnd();
|
|
63
|
+
unmountListener();
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Error listeners
|
|
67
|
+
// xhr.onabort = () => onAbortError(resolve);
|
|
68
|
+
// xhr.ontimeout = () => onTimeoutError(resolve);
|
|
69
|
+
// xhr.upload.onabort = () => onAbortError(resolve);
|
|
70
|
+
// xhr.upload.ontimeout = () => onTimeoutError(resolve);
|
|
71
|
+
|
|
72
|
+
// Data handler
|
|
73
|
+
xhr.onreadystatechange = (e: Event) => {
|
|
74
|
+
const event = e as unknown as ProgressEvent<XMLHttpRequest>;
|
|
75
|
+
const finishedState = 4;
|
|
76
|
+
|
|
77
|
+
if (event.target && event.target.readyState === finishedState) {
|
|
78
|
+
const { status } = event.target;
|
|
79
|
+
const isSuccess = String(status).startsWith("2") || String(status).startsWith("3");
|
|
80
|
+
|
|
81
|
+
if (isSuccess) {
|
|
82
|
+
const data = parseResponse(event.target.response);
|
|
83
|
+
onSuccess(data, status, resolve);
|
|
84
|
+
} else {
|
|
85
|
+
// delay to finish after onabort/ontimeout
|
|
86
|
+
const data = parseErrorResponse(event.target.response);
|
|
87
|
+
onError(data, status, resolve);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Start request
|
|
93
|
+
onBeforeRequest();
|
|
94
|
+
onRequestStart();
|
|
95
|
+
|
|
96
|
+
xhr.send(payload);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { CommandInstance } from "command";
|
|
2
|
+
|
|
3
|
+
// Client
|
|
4
|
+
|
|
5
|
+
export type ClientType = (command: CommandInstance, requestId: string) => Promise<ClientResponseType<any, any>>;
|
|
6
|
+
|
|
7
|
+
export type ClientDefaultOptionsType = Partial<XMLHttpRequest>;
|
|
8
|
+
|
|
9
|
+
export type ClientPayloadMappingCallback = (data: unknown) => string | FormData;
|
|
10
|
+
|
|
11
|
+
// Responses
|
|
12
|
+
|
|
13
|
+
export type ClientResponseType<GenericDataType, GenericErrorType> = [
|
|
14
|
+
GenericDataType | null,
|
|
15
|
+
GenericErrorType | null,
|
|
16
|
+
number | null,
|
|
17
|
+
];
|
|
18
|
+
export type ClientResponseSuccessType<GenericDataType> = [GenericDataType, null, number | null];
|
|
19
|
+
export type ClientResponseErrorType<GenericErrorType> = [null, GenericErrorType, number | null];
|
|
20
|
+
|
|
21
|
+
// QueryParams
|
|
22
|
+
|
|
23
|
+
export type ClientQueryParamValues = number | string | boolean | null | undefined;
|
|
24
|
+
export type ClientQueryParam =
|
|
25
|
+
| ClientQueryParamValues
|
|
26
|
+
| Array<ClientQueryParamValues>
|
|
27
|
+
| Record<string, ClientQueryParamValues>;
|
|
28
|
+
|
|
29
|
+
export type ClientQueryParamsType = Record<string, ClientQueryParam>;
|
|
30
|
+
|
|
31
|
+
// Headers
|
|
32
|
+
|
|
33
|
+
export type ClientHeaderMappingCallback = <T extends CommandInstance>(command: T) => HeadersInit;
|
|
34
|
+
|
|
35
|
+
export type ClientHeadersProps = {
|
|
36
|
+
isFormData: boolean;
|
|
37
|
+
headers: HeadersInit | undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Stringify
|
|
41
|
+
|
|
42
|
+
export type QueryStringifyOptions = {
|
|
43
|
+
/**
|
|
44
|
+
* Strict URI encoding
|
|
45
|
+
*/
|
|
46
|
+
strict?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Encode keys and values
|
|
49
|
+
*/
|
|
50
|
+
encode?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Array encoding type
|
|
53
|
+
*/
|
|
54
|
+
arrayFormat?: "bracket" | "index" | "comma" | "separator" | "bracket-separator" | "none";
|
|
55
|
+
/**
|
|
56
|
+
* Array format separator
|
|
57
|
+
*/
|
|
58
|
+
arraySeparator?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Skip keys with null values
|
|
61
|
+
*/
|
|
62
|
+
skipNull?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Skip keys with empty string
|
|
65
|
+
*/
|
|
66
|
+
skipEmptyString?: boolean;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Progress
|
|
70
|
+
|
|
71
|
+
export type ProgressRequestDataType = {
|
|
72
|
+
total?: number;
|
|
73
|
+
loaded?: number;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type FetchProgressType = {
|
|
77
|
+
progress: number;
|
|
78
|
+
timeLeft: number | null;
|
|
79
|
+
sizeLeft: number;
|
|
80
|
+
total: number;
|
|
81
|
+
loaded: number;
|
|
82
|
+
startTimestamp: number;
|
|
83
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CommandInstance } from "command";
|
|
2
|
+
import { ExtractError } from "types";
|
|
3
|
+
|
|
4
|
+
// Utils
|
|
5
|
+
|
|
6
|
+
export const getErrorMessage = (errorCase?: "timeout" | "abort" | "deleted") => {
|
|
7
|
+
if (errorCase === "timeout") {
|
|
8
|
+
return new Error("Request timeout");
|
|
9
|
+
}
|
|
10
|
+
if (errorCase === "abort") {
|
|
11
|
+
return new Error("Request cancelled");
|
|
12
|
+
}
|
|
13
|
+
return new Error("Unexpected error");
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Responses
|
|
17
|
+
|
|
18
|
+
export const parseResponse = (response: string | unknown) => {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(response as string);
|
|
21
|
+
} catch (err) {
|
|
22
|
+
return response;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const parseErrorResponse = <T extends CommandInstance>(response: unknown): ExtractError<T> => {
|
|
27
|
+
return response ? parseResponse(response) : getErrorMessage();
|
|
28
|
+
};
|