@hyper-fetch/core 3.3.4 → 4.0.1
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/dist/browser/index.cjs.js +1 -1
- package/dist/browser/index.cjs.js.map +4 -4
- package/dist/browser/index.esm.js +1 -1
- package/dist/browser/index.esm.js.map +4 -4
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +330 -180
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/package.json +4 -2
- package/src/adapter/adapter.bindings.ts +124 -35
- package/src/adapter/adapter.browser.ts +29 -24
- package/src/adapter/adapter.server.ts +81 -81
- package/src/adapter/adapter.ts +1 -6
- package/src/adapter/adapter.types.ts +100 -12
- package/src/adapter/adapter.utils.ts +16 -23
- package/src/cache/cache.events.ts +9 -6
- package/src/cache/cache.ts +37 -30
- package/src/cache/cache.types.ts +32 -15
- package/src/cache/cache.utils.ts +7 -9
- package/src/client/client.constants.ts +6 -0
- package/src/client/client.ts +130 -59
- package/src/client/client.types.ts +11 -10
- package/src/client/client.utils.ts +20 -6
- package/src/dispatcher/dispatcher.ts +20 -26
- package/src/dispatcher/dispatcher.types.ts +4 -4
- package/src/dispatcher/dispatcher.utils.ts +2 -11
- package/src/effect/request.effect.ts +8 -5
- package/src/effect/request.effect.types.ts +14 -5
- package/src/index.ts +1 -0
- package/src/managers/logger/logger.manager.ts +8 -8
- package/src/managers/logger/logger.manager.types.ts +5 -8
- package/src/managers/logger/logger.manager.utils.ts +2 -2
- package/src/managers/request/request.manager.events.ts +7 -7
- package/src/managers/request/request.manager.types.ts +0 -1
- package/src/mocker/index.ts +2 -0
- package/src/mocker/mocker.ts +96 -0
- package/src/mocker/mocker.types.ts +26 -0
- package/src/request/request.ts +227 -224
- package/src/request/request.types.ts +75 -35
- package/src/request/request.utils.ts +61 -28
- package/src/types/client.types.ts +4 -0
- package/src/types/fetch.types.ts +16 -25
- package/src/types/http.types.ts +1 -0
- package/src/types/index.ts +1 -0
- package/{tsconfig.node.json → tsconfig.base.json} +3 -3
- package/tsconfig.json +3 -4
- package/dist/server/index.cjs.js +0 -2
- package/dist/server/index.cjs.js.map +0 -7
- package/dist/server/index.esm.js +0 -2
- package/dist/server/index.esm.js.map +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
6
6
|
"author": "Maciej Pyrc <m.pyrc@interia.pl>",
|
|
@@ -60,7 +60,9 @@
|
|
|
60
60
|
"dependencies": {
|
|
61
61
|
"events": "^3.3.0"
|
|
62
62
|
},
|
|
63
|
-
"devDependencies": {
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"abortcontroller-polyfill": "^1.7.5"
|
|
65
|
+
},
|
|
64
66
|
"lint-staged": {
|
|
65
67
|
"*.{js,jsx,ts,tsx}": [
|
|
66
68
|
"yarn jest --passWithNoTests"
|
|
@@ -1,15 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getErrorMessage,
|
|
3
|
+
ResponseReturnSuccessType,
|
|
4
|
+
ResponseReturnErrorType,
|
|
5
|
+
ProgressDataType,
|
|
6
|
+
ExtractAdapterOptionsType,
|
|
7
|
+
ExtractAdapterStatusType,
|
|
8
|
+
ExtractAdapterExtraType,
|
|
9
|
+
AdapterType,
|
|
10
|
+
AdapterInstance,
|
|
11
|
+
ResponseReturnType,
|
|
12
|
+
} from "adapter";
|
|
2
13
|
import { RequestInstance, getProgressData, AdapterProgressEventType } from "request";
|
|
3
|
-
import { ExtractResponseType, ExtractErrorType } from "types";
|
|
14
|
+
import { ExtractResponseType, ExtractErrorType, ExtractPayloadType } from "types";
|
|
15
|
+
import { mocker } from "mocker";
|
|
4
16
|
|
|
5
|
-
export const getAdapterBindings = async <
|
|
6
|
-
|
|
17
|
+
export const getAdapterBindings = async <T extends AdapterInstance = AdapterType>(
|
|
18
|
+
req: RequestInstance,
|
|
19
|
+
requestId: string,
|
|
20
|
+
systemErrorStatus: ExtractAdapterStatusType<T>,
|
|
21
|
+
systemErrorExtra: ExtractAdapterExtraType<T>,
|
|
22
|
+
) => {
|
|
23
|
+
const { url, requestManager, loggerManager, headerMapper, payloadMapper } = req.client;
|
|
7
24
|
|
|
8
25
|
const logger = loggerManager.init("Adapter");
|
|
9
26
|
|
|
27
|
+
let processingError = null;
|
|
28
|
+
|
|
10
29
|
let requestStartTimestamp: null | number = null;
|
|
11
30
|
let responseStartTimestamp: null | number = null;
|
|
12
|
-
let request =
|
|
31
|
+
let request = req;
|
|
13
32
|
|
|
14
33
|
// Progress
|
|
15
34
|
let requestTotal = 1;
|
|
@@ -20,10 +39,18 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
20
39
|
// Pre request modifications
|
|
21
40
|
logger.debug(`Starting request middleware callbacks`);
|
|
22
41
|
|
|
23
|
-
|
|
42
|
+
try {
|
|
43
|
+
request = await request.client.__modifyRequest(req);
|
|
44
|
+
|
|
45
|
+
if (request.auth) {
|
|
46
|
+
request = await request.client.__modifyAuth(req);
|
|
47
|
+
}
|
|
24
48
|
|
|
25
|
-
|
|
26
|
-
|
|
49
|
+
if (request.requestMapper) {
|
|
50
|
+
request = await request.requestMapper(request, requestId);
|
|
51
|
+
}
|
|
52
|
+
} catch (err) {
|
|
53
|
+
processingError = err;
|
|
27
54
|
}
|
|
28
55
|
|
|
29
56
|
// Request Setup
|
|
@@ -32,8 +59,20 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
32
59
|
const fullUrl = url + endpoint;
|
|
33
60
|
const effects = client.effects.filter((effect) => request.effectKey === effect.getEffectKey());
|
|
34
61
|
const headers = headerMapper(request);
|
|
35
|
-
|
|
36
|
-
|
|
62
|
+
let payload = data;
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
payload = payloadMapper(data);
|
|
66
|
+
if (request.dataMapper) {
|
|
67
|
+
payload = await request.dataMapper<ExtractPayloadType<RequestInstance>>(data);
|
|
68
|
+
}
|
|
69
|
+
} catch (err) {
|
|
70
|
+
processingError = err;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const config: ExtractAdapterOptionsType<T> = {
|
|
74
|
+
...request.options,
|
|
75
|
+
} as ExtractAdapterOptionsType<T>;
|
|
37
76
|
|
|
38
77
|
const getRequestStartTimestamp = () => {
|
|
39
78
|
return requestStartTimestamp;
|
|
@@ -178,12 +217,19 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
178
217
|
|
|
179
218
|
// Success
|
|
180
219
|
|
|
181
|
-
const onSuccess = async
|
|
182
|
-
responseData:
|
|
183
|
-
status:
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
220
|
+
const onSuccess = async (
|
|
221
|
+
responseData: any,
|
|
222
|
+
status: ExtractAdapterStatusType<T>,
|
|
223
|
+
extra: ExtractAdapterExtraType<T>,
|
|
224
|
+
resolve: (value: ResponseReturnErrorType<any, T>) => void,
|
|
225
|
+
): Promise<ResponseReturnSuccessType<ExtractResponseType<T>, T>> => {
|
|
226
|
+
let response = {
|
|
227
|
+
data: responseData,
|
|
228
|
+
error: null,
|
|
229
|
+
success: true,
|
|
230
|
+
status,
|
|
231
|
+
extra,
|
|
232
|
+
};
|
|
187
233
|
response = await request.client.__modifyResponse(response, request);
|
|
188
234
|
response = await request.client.__modifySuccessResponse(response, request);
|
|
189
235
|
|
|
@@ -197,12 +243,19 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
197
243
|
|
|
198
244
|
// Errors
|
|
199
245
|
|
|
200
|
-
const onError = async
|
|
201
|
-
error:
|
|
202
|
-
status:
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
246
|
+
const onError = async (
|
|
247
|
+
error: any,
|
|
248
|
+
status: ExtractAdapterStatusType<T>,
|
|
249
|
+
extra: ExtractAdapterExtraType<T>,
|
|
250
|
+
resolve: (value: ResponseReturnErrorType<any, T>) => void,
|
|
251
|
+
): Promise<ResponseReturnErrorType<any, T>> => {
|
|
252
|
+
let responseData = {
|
|
253
|
+
data: null,
|
|
254
|
+
status,
|
|
255
|
+
error,
|
|
256
|
+
success: false,
|
|
257
|
+
extra,
|
|
258
|
+
} as ResponseReturnErrorType<any, T>;
|
|
206
259
|
|
|
207
260
|
responseData = await request.client.__modifyResponse(responseData, request);
|
|
208
261
|
responseData = await request.client.__modifyErrorResponse(responseData, request);
|
|
@@ -215,25 +268,31 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
215
268
|
return responseData;
|
|
216
269
|
};
|
|
217
270
|
|
|
218
|
-
const onAbortError =
|
|
219
|
-
|
|
271
|
+
const onAbortError = (
|
|
272
|
+
status: ExtractAdapterStatusType<T>,
|
|
273
|
+
extra: ExtractAdapterExtraType<T>,
|
|
274
|
+
resolve: (value: ResponseReturnErrorType<ExtractErrorType<T>, T>) => void,
|
|
220
275
|
) => {
|
|
221
276
|
const error = getErrorMessage("abort");
|
|
222
|
-
return onError(error,
|
|
277
|
+
return onError(error, status, extra, resolve);
|
|
223
278
|
};
|
|
224
279
|
|
|
225
|
-
const onTimeoutError =
|
|
226
|
-
|
|
280
|
+
const onTimeoutError = (
|
|
281
|
+
status: ExtractAdapterStatusType<T>,
|
|
282
|
+
extra: ExtractAdapterExtraType<T>,
|
|
283
|
+
resolve: (value: ResponseReturnErrorType<ExtractErrorType<T>, T>) => void,
|
|
227
284
|
) => {
|
|
228
285
|
const error = getErrorMessage("timeout");
|
|
229
|
-
return onError(error,
|
|
286
|
+
return onError(error, status, extra, resolve);
|
|
230
287
|
};
|
|
231
288
|
|
|
232
|
-
const onUnexpectedError =
|
|
233
|
-
|
|
289
|
+
const onUnexpectedError = (
|
|
290
|
+
status: ExtractAdapterStatusType<T>,
|
|
291
|
+
extra: ExtractAdapterExtraType<T>,
|
|
292
|
+
resolve: (value: ResponseReturnErrorType<ExtractErrorType<T>, T>) => void,
|
|
234
293
|
) => {
|
|
235
294
|
const error = getErrorMessage();
|
|
236
|
-
return onError(error,
|
|
295
|
+
return onError(error, status, extra, resolve);
|
|
237
296
|
};
|
|
238
297
|
|
|
239
298
|
// Abort
|
|
@@ -242,9 +301,11 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
242
301
|
return requestManager.getAbortController(abortKey, requestId);
|
|
243
302
|
};
|
|
244
303
|
|
|
245
|
-
const createAbortListener =
|
|
304
|
+
const createAbortListener = (
|
|
305
|
+
status: ExtractAdapterStatusType<T>,
|
|
306
|
+
abortExtra: ExtractAdapterExtraType<T>,
|
|
246
307
|
callback: () => void,
|
|
247
|
-
resolve: (value:
|
|
308
|
+
resolve: (value: ResponseReturnErrorType<ExtractErrorType<T>, T>) => void,
|
|
248
309
|
) => {
|
|
249
310
|
const controller = getAbortController();
|
|
250
311
|
if (!controller) {
|
|
@@ -252,12 +313,12 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
252
313
|
}
|
|
253
314
|
|
|
254
315
|
const fn = () => {
|
|
255
|
-
onAbortError(resolve);
|
|
316
|
+
onAbortError(status, abortExtra, resolve);
|
|
256
317
|
callback();
|
|
257
318
|
requestManager.events.emitAbort(abortKey, requestId, request);
|
|
258
319
|
};
|
|
259
320
|
|
|
260
|
-
// Instant abort when we stack many requests triggered at once and we receive aborted controller
|
|
321
|
+
// Instant abort when we stack many requests triggered at once, and we receive aborted controller
|
|
261
322
|
if (controller.signal.aborted) {
|
|
262
323
|
fn();
|
|
263
324
|
}
|
|
@@ -268,8 +329,35 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
268
329
|
return () => controller.signal.removeEventListener("abort", fn);
|
|
269
330
|
};
|
|
270
331
|
|
|
332
|
+
const makeRequest = (
|
|
333
|
+
apiCall: (
|
|
334
|
+
resolve: (value: ResponseReturnType<any, any, T> | PromiseLike<ResponseReturnType<any, any, T>>) => void,
|
|
335
|
+
) => void,
|
|
336
|
+
): Promise<ResponseReturnType<any, any, T>> => {
|
|
337
|
+
if (processingError) {
|
|
338
|
+
return onError(processingError, systemErrorStatus, systemErrorExtra, () => null);
|
|
339
|
+
}
|
|
340
|
+
if (req.mock) {
|
|
341
|
+
return mocker(request, {
|
|
342
|
+
onError,
|
|
343
|
+
onResponseEnd,
|
|
344
|
+
onTimeoutError,
|
|
345
|
+
onRequestEnd,
|
|
346
|
+
createAbortListener,
|
|
347
|
+
onResponseProgress,
|
|
348
|
+
onRequestProgress,
|
|
349
|
+
onResponseStart,
|
|
350
|
+
onBeforeRequest,
|
|
351
|
+
onRequestStart,
|
|
352
|
+
onSuccess,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
return new Promise(apiCall);
|
|
356
|
+
};
|
|
357
|
+
|
|
271
358
|
return {
|
|
272
359
|
fullUrl,
|
|
360
|
+
data,
|
|
273
361
|
headers,
|
|
274
362
|
payload,
|
|
275
363
|
config,
|
|
@@ -289,5 +377,6 @@ export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance,
|
|
|
289
377
|
onTimeoutError,
|
|
290
378
|
onUnexpectedError,
|
|
291
379
|
onError,
|
|
380
|
+
makeRequest,
|
|
292
381
|
};
|
|
293
382
|
};
|
|
@@ -1,33 +1,37 @@
|
|
|
1
|
-
import { getAdapterBindings,
|
|
2
|
-
import {
|
|
1
|
+
import { getAdapterBindings, AdapterType, getResponseHeaders, parseResponse, parseErrorResponse } from "adapter";
|
|
2
|
+
import { xhrExtra } from "client";
|
|
3
|
+
import { defaultTimeout } from "./adapter.constants";
|
|
3
4
|
|
|
4
5
|
export const adapter: AdapterType = async (request, requestId) => {
|
|
5
6
|
const {
|
|
7
|
+
makeRequest,
|
|
6
8
|
fullUrl,
|
|
7
|
-
headers,
|
|
8
|
-
payload,
|
|
9
9
|
config,
|
|
10
|
+
payload,
|
|
11
|
+
headers,
|
|
12
|
+
onError,
|
|
13
|
+
onResponseEnd,
|
|
14
|
+
onTimeoutError,
|
|
15
|
+
onRequestEnd,
|
|
10
16
|
createAbortListener,
|
|
11
|
-
|
|
12
|
-
onRequestStart,
|
|
17
|
+
onResponseProgress,
|
|
13
18
|
onRequestProgress,
|
|
14
|
-
onRequestEnd,
|
|
15
19
|
onResponseStart,
|
|
16
|
-
|
|
20
|
+
onBeforeRequest,
|
|
21
|
+
onRequestStart,
|
|
17
22
|
onSuccess,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} = await getAdapterBindings(request, requestId);
|
|
23
|
+
} = await getAdapterBindings<AdapterType>(request, requestId, 0, {
|
|
24
|
+
headers: {},
|
|
25
|
+
});
|
|
22
26
|
|
|
23
|
-
const { method } = request;
|
|
27
|
+
const { method = "GET" } = request;
|
|
24
28
|
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
return makeRequest((resolve) => {
|
|
30
|
+
const xhr = new XMLHttpRequest();
|
|
31
|
+
xhr.timeout = defaultTimeout;
|
|
27
32
|
|
|
28
|
-
|
|
33
|
+
const abort = () => xhr.abort();
|
|
29
34
|
|
|
30
|
-
return new Promise<ResponseType<unknown, unknown>>((resolve) => {
|
|
31
35
|
// Inject xhr options
|
|
32
36
|
Object.entries(config).forEach(([name, value]) => {
|
|
33
37
|
xhr[name] = value;
|
|
@@ -37,10 +41,10 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
37
41
|
xhr.open(method, fullUrl, true);
|
|
38
42
|
|
|
39
43
|
// Set Headers
|
|
40
|
-
Object.entries(headers).forEach(([name, value]) => xhr.setRequestHeader(name, value));
|
|
44
|
+
Object.entries(headers).forEach(([name, value]) => xhr.setRequestHeader(name, value as string));
|
|
41
45
|
|
|
42
46
|
// Listen to abort signal
|
|
43
|
-
const unmountListener = createAbortListener(abort, resolve);
|
|
47
|
+
const unmountListener = createAbortListener(0, xhrExtra, abort, resolve);
|
|
44
48
|
|
|
45
49
|
// Request handlers
|
|
46
50
|
xhr.upload.onprogress = onRequestProgress;
|
|
@@ -58,7 +62,7 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
58
62
|
unmountListener();
|
|
59
63
|
};
|
|
60
64
|
|
|
61
|
-
xhr.ontimeout = () => onTimeoutError;
|
|
65
|
+
xhr.ontimeout = () => onTimeoutError(0, xhrExtra, resolve);
|
|
62
66
|
|
|
63
67
|
// Data handler
|
|
64
68
|
xhr.onreadystatechange = (e: Event) => {
|
|
@@ -67,15 +71,16 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
67
71
|
|
|
68
72
|
if (event.target && event.target.readyState === finishedState) {
|
|
69
73
|
const { status } = event.target;
|
|
70
|
-
const
|
|
74
|
+
const success = String(status).startsWith("2") || String(status).startsWith("3");
|
|
75
|
+
const responseHeaders = getResponseHeaders(xhr.getAllResponseHeaders());
|
|
71
76
|
|
|
72
|
-
if (
|
|
77
|
+
if (success) {
|
|
73
78
|
const data = parseResponse(event.target.response);
|
|
74
|
-
onSuccess(data, status, resolve);
|
|
79
|
+
onSuccess(data, status, { headers: responseHeaders }, resolve);
|
|
75
80
|
} else {
|
|
76
81
|
// delay to finish after onabort/ontimeout
|
|
77
82
|
const data = parseErrorResponse(event.target.response);
|
|
78
|
-
onError(data, status, resolve);
|
|
83
|
+
onError(data, status, { headers: responseHeaders }, resolve);
|
|
79
84
|
}
|
|
80
85
|
}
|
|
81
86
|
};
|
|
@@ -1,106 +1,106 @@
|
|
|
1
1
|
import http, { OutgoingHttpHeaders } from "http";
|
|
2
|
-
import https from "https";
|
|
3
2
|
import stream from "stream";
|
|
3
|
+
import https from "https";
|
|
4
4
|
|
|
5
|
-
import { getAdapterBindings
|
|
6
|
-
import {
|
|
5
|
+
import { getAdapterBindings } from "./adapter.bindings";
|
|
6
|
+
import { AdapterType } from "./adapter.types";
|
|
7
|
+
import { defaultTimeout } from "./adapter.constants";
|
|
8
|
+
import { getStreamPayload, getUploadSize, parseErrorResponse, parseResponse } from "./adapter.utils";
|
|
9
|
+
import { HttpMethodsEnum } from "../constants/http.constants";
|
|
10
|
+
import { xhrExtra } from "client";
|
|
7
11
|
|
|
8
12
|
export const adapter: AdapterType = async (request, requestId) => {
|
|
9
13
|
const {
|
|
14
|
+
makeRequest,
|
|
10
15
|
fullUrl,
|
|
16
|
+
config,
|
|
11
17
|
headers,
|
|
12
18
|
payload,
|
|
13
|
-
|
|
19
|
+
onError,
|
|
20
|
+
onResponseEnd,
|
|
21
|
+
onTimeoutError,
|
|
22
|
+
onRequestEnd,
|
|
14
23
|
createAbortListener,
|
|
15
|
-
|
|
16
|
-
onRequestStart,
|
|
24
|
+
onResponseProgress,
|
|
17
25
|
onRequestProgress,
|
|
18
|
-
onRequestEnd,
|
|
19
26
|
onResponseStart,
|
|
20
|
-
|
|
27
|
+
onBeforeRequest,
|
|
28
|
+
onRequestStart,
|
|
21
29
|
onSuccess,
|
|
22
|
-
|
|
23
|
-
onError,
|
|
24
|
-
onResponseEnd,
|
|
25
|
-
} = await getAdapterBindings<AdapterOptionsType>(request, requestId);
|
|
26
|
-
const { method, client } = request;
|
|
27
|
-
const httpClient = client.url.includes("https://") ? https : http;
|
|
30
|
+
} = await getAdapterBindings<AdapterType>(request, requestId, 0, xhrExtra);
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
});
|
|
32
|
+
const { method = HttpMethodsEnum.get, client } = request;
|
|
33
|
+
const httpClient = client.url.includes("https://") ? https : http;
|
|
34
|
+
const options = {
|
|
35
|
+
path: fullUrl,
|
|
36
|
+
method,
|
|
37
|
+
headers: headers as OutgoingHttpHeaders,
|
|
38
|
+
timeout: defaultTimeout,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
Object.entries(config).forEach(([name, value]) => {
|
|
42
|
+
options[name] = value;
|
|
43
|
+
});
|
|
42
44
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
let unmountListener = () => null;
|
|
46
|
+
onBeforeRequest();
|
|
45
47
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
const totalUploadBytes = payload ? Number(getUploadSize(payload)) : 0;
|
|
49
|
+
let uploadedBytes = 0;
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
const payloadChunks = await getStreamPayload(payload);
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
return makeRequest((resolve) => {
|
|
54
|
+
const httpRequest = httpClient.request(options, (response) => {
|
|
55
|
+
response.setEncoding("utf8");
|
|
56
|
+
unmountListener = createAbortListener(0, xhrExtra, response.destroy, resolve);
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
let chunks = "";
|
|
59
|
+
const totalDownloadBytes = Number(response.headers["content-length"]);
|
|
60
|
+
let downloadedBytes = 0;
|
|
57
61
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
response.on("data", (chunk) => {
|
|
63
|
+
if (!chunks) onResponseStart();
|
|
64
|
+
downloadedBytes += chunk.length;
|
|
65
|
+
chunks += chunk;
|
|
66
|
+
onResponseProgress({ total: totalDownloadBytes, loaded: downloadedBytes });
|
|
67
|
+
});
|
|
64
68
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
});
|
|
69
|
+
response.on("end", () => {
|
|
70
|
+
const { statusCode } = response;
|
|
71
|
+
const success = String(statusCode).startsWith("2") || String(statusCode).startsWith("3");
|
|
72
|
+
|
|
73
|
+
if (success) {
|
|
74
|
+
const data = parseResponse(chunks);
|
|
75
|
+
onSuccess(data, statusCode, { headers: response.headers as Record<string, string> }, resolve);
|
|
76
|
+
} else {
|
|
77
|
+
// delay to finish after onabort/ontimeout
|
|
78
|
+
const data = parseErrorResponse(chunks);
|
|
79
|
+
onError(data, statusCode, { headers: response.headers as Record<string, string> }, resolve);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
unmountListener();
|
|
83
|
+
onResponseEnd();
|
|
81
84
|
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
httpRequest.on("timeout", () => onTimeoutError(0, xhrExtra, resolve));
|
|
88
|
+
httpRequest.on("error", (error) => onError(error, 0, xhrExtra, resolve));
|
|
89
|
+
|
|
90
|
+
if (payloadChunks) {
|
|
91
|
+
const readableStream = stream.Readable.from(payloadChunks, { objectMode: false })
|
|
92
|
+
.on("data", (chunk) => {
|
|
93
|
+
if (!uploadedBytes) onRequestStart();
|
|
94
|
+
uploadedBytes += chunk.length;
|
|
95
|
+
onRequestProgress({ total: totalUploadBytes, loaded: uploadedBytes });
|
|
96
|
+
})
|
|
97
|
+
.on("end", () => {
|
|
98
|
+
onRequestEnd();
|
|
99
|
+
});
|
|
82
100
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
httpRequest.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (payloadChunks) {
|
|
89
|
-
const readableStream = stream.Readable.from(payloadChunks, { objectMode: false })
|
|
90
|
-
.on("data", (chunk) => {
|
|
91
|
-
if (!uploadedBytes) onRequestStart();
|
|
92
|
-
uploadedBytes += chunk.length;
|
|
93
|
-
onRequestProgress({ total: totalUploadBytes, loaded: uploadedBytes });
|
|
94
|
-
})
|
|
95
|
-
.on("end", () => {
|
|
96
|
-
onRequestEnd();
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
readableStream.pipe(httpRequest);
|
|
100
|
-
} else {
|
|
101
|
-
httpRequest.end();
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
execute();
|
|
101
|
+
readableStream.pipe(httpRequest);
|
|
102
|
+
} else {
|
|
103
|
+
httpRequest.end();
|
|
104
|
+
}
|
|
105
105
|
});
|
|
106
106
|
};
|
package/src/adapter/adapter.ts
CHANGED
|
@@ -7,10 +7,5 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
7
7
|
if (typeof XMLHttpRequest !== "undefined") {
|
|
8
8
|
return browser.adapter(request, requestId);
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
return server.adapter(request, requestId);
|
|
12
|
-
}
|
|
13
|
-
return (() => {
|
|
14
|
-
return [null, new Error("Cannot find the right environment for http adapter"), 0];
|
|
15
|
-
}) as unknown as ReturnType<AdapterType>;
|
|
10
|
+
return server.adapter(request, requestId);
|
|
16
11
|
};
|