@hyper-fetch/core 7.5.3 → 8.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/.eslintrc.cjs +12 -0
- package/__tests__/e2e/e2e.abort-timeout.spec.ts +101 -0
- package/__tests__/e2e/e2e.advanced.spec.ts +423 -0
- package/__tests__/e2e/e2e.methods.spec.ts +243 -0
- package/__tests__/e2e/e2e.payloads.spec.ts +154 -0
- package/__tests__/features/adapter/adapter.base.spec.ts +44 -17
- package/__tests__/features/adapter/adapter.bindings.spec.ts +35 -33
- package/__tests__/features/adapter/adapter.browser.spec.ts +5 -20
- package/__tests__/features/adapter/adapter.server.spec.ts +229 -37
- package/__tests__/features/cache/cache.base.spec.ts +23 -15
- package/__tests__/features/cache/cache.events.spec.ts +9 -5
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +9 -6
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +11 -8
- package/__tests__/features/cache/cache.modes.spec.ts +145 -0
- package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
- package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
- package/__tests__/features/cache/cache.utils.spec.ts +10 -3
- package/__tests__/features/client/client.auth.spec.ts +5 -5
- package/__tests__/features/client/client.base.spec.ts +16 -4
- package/__tests__/features/client/client.from-json.spec.ts +182 -0
- package/__tests__/features/client/client.hydration.spec.ts +69 -4
- package/__tests__/features/client/client.interceptor.spec.ts +4 -4
- package/__tests__/features/client/client.methods.spec.ts +5 -4
- package/__tests__/features/client/client.middleware.spec.ts +4 -4
- package/__tests__/features/client/client.utils.spec.ts +3 -3
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +8 -8
- package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +17 -12
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +31 -31
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +15 -15
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +22 -5
- package/__tests__/features/managers/app/app.manager.base.spec.ts +23 -23
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +6 -3
- package/__tests__/features/managers/logger/logger.base.spec.ts +4 -2
- package/__tests__/features/managers/logger/logger.utils.spec.ts +10 -4
- package/__tests__/features/managers/request/request.manager.base.spec.ts +21 -19
- package/__tests__/features/managers/request/request.manager.events.spec.ts +137 -53
- package/__tests__/features/mocker/mocker.base.spec.ts +82 -10
- package/__tests__/features/plugin/plugin.base.spec.ts +19 -19
- package/__tests__/features/request/request.base.spec.ts +39 -13
- package/__tests__/features/request/request.cloning.spec.ts +1 -1
- package/__tests__/features/request/request.hooks.spec.ts +173 -0
- package/__tests__/features/request/request.optimistic.spec.ts +93 -0
- package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
- package/__tests__/features/request/request.sending.spec.ts +107 -15
- package/__tests__/features/request/request.setters.spec.ts +11 -4
- package/__tests__/features/request/request.typescript.spec.ts +1 -1
- package/__tests__/features/request/request.utils.spec.ts +2 -2
- package/__tests__/features/sdk/sdk.base.spec.ts +5 -3
- package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
- package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
- package/__tests__/shared/callbacks.shared.ts +3 -1
- package/__tests__/shared/progress.shared.ts +2 -1
- package/__tests__/tsconfig.json +14 -3
- package/__tests__/type-stress/generate-stress-test.ts +92 -0
- package/__tests__/type-stress/stress-10.ts +35 -0
- package/__tests__/type-stress/stress-just-create.ts +2107 -0
- package/__tests__/type-stress/stress-minimal.ts +23 -0
- package/__tests__/type-stress/stress-test-500.ts +2319 -0
- package/__tests__/type-stress/stress-test-sdk.ts +210 -0
- package/__tests__/type-stress/tsconfig.json +6 -0
- package/__tests__/utils/adapter.utils.ts +3 -2
- package/__tests__/utils/cache.utils.ts +3 -2
- package/__tests__/utils/callbacks.utils.ts +5 -4
- package/__tests__/utils/dispatcher.utils.ts +3 -2
- package/__tests__/vitest.setup.ts +15 -0
- package/dist/adapter/adapter.bindings.d.ts +71 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -0
- package/dist/adapter/adapter.d.ts +127 -0
- package/dist/adapter/adapter.d.ts.map +1 -0
- package/dist/adapter/adapter.types.d.ts +108 -0
- package/dist/adapter/adapter.types.d.ts.map +1 -0
- package/dist/adapter/adapter.utils.d.ts +23 -0
- package/dist/adapter/adapter.utils.d.ts.map +1 -0
- package/dist/adapter/index.d.ts +5 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/cache/cache.d.ts +95 -0
- package/dist/cache/cache.d.ts.map +1 -0
- package/dist/cache/cache.events.d.ts +51 -0
- package/dist/cache/cache.events.d.ts.map +1 -0
- package/dist/cache/cache.types.d.ts +44 -0
- package/dist/cache/cache.types.d.ts.map +1 -0
- package/dist/cache/cache.utils.d.ts +11 -0
- package/dist/cache/cache.utils.d.ts.map +1 -0
- package/dist/cache/index.d.ts +5 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/client/client.constants.d.ts +10 -0
- package/dist/client/client.constants.d.ts.map +1 -0
- package/dist/client/client.create.d.ts +8 -0
- package/dist/client/client.create.d.ts.map +1 -0
- package/dist/client/client.d.ts +195 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.types.d.ts +71 -0
- package/dist/client/client.types.d.ts.map +1 -0
- package/dist/client/client.utils.d.ts +11 -0
- package/dist/client/client.utils.d.ts.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/constants/http.constants.d.ts +8 -0
- package/dist/constants/http.constants.d.ts.map +1 -0
- package/dist/constants/time.constants.d.ts +12 -0
- package/dist/constants/time.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
- package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.d.ts +168 -0
- package/dist/dispatcher/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.events.d.ts +49 -0
- package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.types.d.ts +44 -0
- package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
- package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
- package/dist/dispatcher/index.d.ts +6 -0
- package/dist/dispatcher/index.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
- package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.d.ts +3 -0
- package/dist/http-adapter/http-adapter.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.types.d.ts +73 -0
- package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
- package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
- package/dist/http-adapter/index.d.ts +6 -0
- package/dist/http-adapter/index.d.ts.map +1 -0
- package/dist/index.d.ts +15 -2083
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +4084 -0
- package/dist/index.mjs.map +1 -0
- package/dist/managers/app/app.manager.constants.d.ts +10 -0
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -0
- package/dist/managers/app/app.manager.events.d.ts +12 -0
- package/dist/managers/app/app.manager.events.d.ts.map +1 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.types.d.ts.map +1 -0
- package/dist/managers/app/app.manager.utils.d.ts +5 -0
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
- package/dist/managers/app/index.d.ts +6 -0
- package/dist/managers/app/index.d.ts.map +1 -0
- package/dist/managers/index.d.ts +4 -0
- package/dist/managers/index.d.ts.map +1 -0
- package/dist/managers/logger/index.d.ts +5 -0
- package/dist/managers/logger/index.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
- package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.d.ts +22 -0
- package/dist/managers/logger/logger.manager.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.types.d.ts +43 -0
- package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
- package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
- package/dist/managers/request/index.d.ts +5 -0
- package/dist/managers/request/index.d.ts.map +1 -0
- package/dist/managers/request/request.manager.d.ts +60 -0
- package/dist/managers/request/request.manager.d.ts.map +1 -0
- package/dist/managers/request/request.manager.events.d.ts +55 -0
- package/dist/managers/request/request.manager.events.d.ts.map +1 -0
- package/dist/managers/request/request.manager.types.d.ts +48 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -0
- package/dist/managers/request/request.manager.utils.d.ts +30 -0
- package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
- package/dist/mocker/index.d.ts +3 -0
- package/dist/mocker/index.d.ts.map +1 -0
- package/dist/mocker/mocker.d.ts +3 -0
- package/dist/mocker/mocker.d.ts.map +1 -0
- package/dist/mocker/mocker.types.d.ts +25 -0
- package/dist/mocker/mocker.types.d.ts.map +1 -0
- package/dist/plugin/index.d.ts +3 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/plugin.d.ts +55 -0
- package/dist/plugin/plugin.d.ts.map +1 -0
- package/dist/plugin/plugin.types.d.ts +122 -0
- package/dist/plugin/plugin.types.d.ts.map +1 -0
- package/dist/request/index.d.ts +5 -0
- package/dist/request/index.d.ts.map +1 -0
- package/dist/request/request.d.ts +195 -0
- package/dist/request/request.d.ts.map +1 -0
- package/dist/request/request.hooks.d.ts +19 -0
- package/dist/request/request.hooks.d.ts.map +1 -0
- package/dist/request/request.types.d.ts +240 -0
- package/dist/request/request.types.d.ts.map +1 -0
- package/dist/request/request.utils.d.ts +25 -0
- package/dist/request/request.utils.d.ts.map +1 -0
- package/dist/sdk/index.d.ts +2 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/sdk.d.ts +69 -0
- package/dist/sdk/sdk.d.ts.map +1 -0
- package/dist/types/adapter.types.d.ts +22 -0
- package/dist/types/adapter.types.d.ts.map +1 -0
- package/dist/types/client.types.d.ts +4 -0
- package/dist/types/client.types.d.ts.map +1 -0
- package/dist/types/helpers.types.d.ts +17 -0
- package/dist/types/helpers.types.d.ts.map +1 -0
- package/dist/types/http.types.d.ts +3 -0
- package/dist/types/http.types.d.ts.map +1 -0
- package/dist/types/hydration.types.d.ts +15 -0
- package/dist/types/hydration.types.d.ts.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/request.types.d.ts +34 -0
- package/dist/types/request.types.d.ts.map +1 -0
- package/dist/utils/emitter.utils.d.ts +13 -0
- package/dist/utils/emitter.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/uuid.utils.d.ts +3 -0
- package/dist/utils/uuid.utils.d.ts.map +1 -0
- package/package.json +7 -30
- package/src/adapter/adapter.bindings.ts +8 -12
- package/src/adapter/adapter.ts +8 -7
- package/src/adapter/adapter.types.ts +10 -6
- package/src/adapter/adapter.utils.ts +1 -1
- package/src/cache/cache.events.ts +4 -11
- package/src/cache/cache.ts +28 -22
- package/src/cache/cache.types.ts +8 -4
- package/src/cache/cache.utils.ts +3 -3
- package/src/client/client.create.ts +4 -3
- package/src/client/client.ts +44 -12
- package/src/client/client.types.ts +25 -3
- package/src/client/client.utils.ts +24 -4
- package/src/dispatcher/dispatcher.events.ts +3 -3
- package/src/dispatcher/dispatcher.ts +59 -28
- package/src/dispatcher/dispatcher.types.ts +19 -2
- package/src/dispatcher/dispatcher.utils.ts +5 -4
- package/src/http-adapter/http-adapter.constants.ts +1 -1
- package/src/http-adapter/http-adapter.fetch.ts +174 -0
- package/src/http-adapter/http-adapter.ts +3 -8
- package/src/http-adapter/http-adapter.types.ts +17 -9
- package/src/http-adapter/http-adapter.utils.ts +6 -9
- package/src/http-adapter/index.ts +1 -0
- package/src/managers/app/app.manager.constants.ts +2 -2
- package/src/managers/app/app.manager.events.ts +1 -1
- package/src/managers/app/app.manager.ts +2 -1
- package/src/managers/logger/logger.manager.constants.ts +1 -1
- package/src/managers/logger/logger.manager.ts +2 -1
- package/src/managers/logger/logger.manager.types.ts +2 -2
- package/src/managers/logger/logger.manager.utils.ts +2 -1
- package/src/managers/request/request.manager.events.ts +13 -11
- package/src/managers/request/request.manager.types.ts +6 -3
- package/src/mocker/mocker.ts +3 -2
- package/src/mocker/mocker.types.ts +1 -1
- package/src/plugin/plugin.ts +2 -2
- package/src/plugin/plugin.types.ts +9 -9
- package/src/request/index.ts +1 -0
- package/src/request/request.hooks.ts +87 -0
- package/src/request/request.ts +155 -77
- package/src/request/request.types.ts +54 -22
- package/src/request/request.utils.ts +86 -23
- package/src/sdk/sdk.ts +149 -8
- package/src/types/adapter.types.ts +1 -1
- package/src/types/client.types.ts +1 -1
- package/src/types/hydration.types.ts +3 -3
- package/src/types/request.types.ts +30 -20
- package/src/utils/uuid.utils.ts +1 -1
- package/tsconfig.base.json +1 -6
- package/tsconfig.json +1 -4
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/__tests__/global.d.ts +0 -1
- package/__tests__/jest.setup.ts +0 -13
- package/dist/browser/index.js +0 -3751
- package/dist/browser/index.js.map +0 -7
- package/dist/index.js +0 -4002
- package/dist/index.js.map +0 -7
- package/jest.config.ts +0 -6
- package/src/http-adapter/http-adapter.browser.ts +0 -102
- package/src/http-adapter/http-adapter.server.ts +0 -149
package/jest.config.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { getResponseHeaders, parseErrorResponse, parseResponse, stringifyQueryParams } from "./http-adapter.utils";
|
|
2
|
-
import { defaultTimeout, xhrExtra } from "./http-adapter.constants";
|
|
3
|
-
import { Adapter } from "../adapter/adapter";
|
|
4
|
-
import { HttpMethods } from "constants/http.constants";
|
|
5
|
-
import { HttpMethodsType, HttpStatusType } from "types";
|
|
6
|
-
import { HttpAdapterOptionsType, HttpAdapterExtraType } from "./http-adapter.types";
|
|
7
|
-
import { QueryParamsType } from "adapter";
|
|
8
|
-
|
|
9
|
-
export const getAdapter = () =>
|
|
10
|
-
new Adapter<
|
|
11
|
-
HttpAdapterOptionsType,
|
|
12
|
-
HttpMethodsType,
|
|
13
|
-
HttpStatusType,
|
|
14
|
-
HttpAdapterExtraType,
|
|
15
|
-
QueryParamsType | string | null,
|
|
16
|
-
undefined,
|
|
17
|
-
string
|
|
18
|
-
>({
|
|
19
|
-
name: "http-browser",
|
|
20
|
-
defaultMethod: HttpMethods.GET,
|
|
21
|
-
defaultExtra: xhrExtra,
|
|
22
|
-
systemErrorStatus: 0 as number,
|
|
23
|
-
systemErrorExtra: xhrExtra,
|
|
24
|
-
})
|
|
25
|
-
.setQueryParamsMapper(stringifyQueryParams)
|
|
26
|
-
.setFetcher(
|
|
27
|
-
async ({
|
|
28
|
-
request,
|
|
29
|
-
adapterOptions,
|
|
30
|
-
headers,
|
|
31
|
-
payload,
|
|
32
|
-
onError,
|
|
33
|
-
onResponseEnd,
|
|
34
|
-
onTimeoutError,
|
|
35
|
-
onRequestEnd,
|
|
36
|
-
createAbortListener,
|
|
37
|
-
onResponseProgress,
|
|
38
|
-
onResponseStart,
|
|
39
|
-
onBeforeRequest,
|
|
40
|
-
onRequestStart,
|
|
41
|
-
onRequestProgress,
|
|
42
|
-
onSuccess,
|
|
43
|
-
}) => {
|
|
44
|
-
const { method, client, endpoint, queryParams } = request;
|
|
45
|
-
const queryString = queryParams ? stringifyQueryParams(queryParams) : "";
|
|
46
|
-
const fullUrl = `${client.url}${endpoint}${queryString}`;
|
|
47
|
-
const xhr = new XMLHttpRequest();
|
|
48
|
-
xhr.timeout = defaultTimeout;
|
|
49
|
-
const onAbort = () => xhr.abort();
|
|
50
|
-
// Inject xhr options
|
|
51
|
-
if (adapterOptions) {
|
|
52
|
-
Object.entries(adapterOptions).forEach(([name, value]) => {
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
xhr[name] = value;
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
// Open connection
|
|
59
|
-
xhr.open(method, fullUrl, true);
|
|
60
|
-
// Set Headers
|
|
61
|
-
Object.entries(headers).forEach(([name, value]) => xhr.setRequestHeader(name, value as string));
|
|
62
|
-
// Listen to abort signal
|
|
63
|
-
const unmountListener = createAbortListener({ status: 0, extra: xhrExtra, onAbort });
|
|
64
|
-
// Request handlers
|
|
65
|
-
xhr.upload.onprogress = onRequestProgress;
|
|
66
|
-
// Response handlers
|
|
67
|
-
xhr.onloadstart = (): void => {
|
|
68
|
-
onRequestEnd();
|
|
69
|
-
onResponseStart();
|
|
70
|
-
};
|
|
71
|
-
xhr.onprogress = onResponseProgress;
|
|
72
|
-
xhr.onloadend = () => {
|
|
73
|
-
onResponseEnd();
|
|
74
|
-
unmountListener();
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
/* istanbul ignore next */
|
|
78
|
-
xhr.ontimeout = () => onTimeoutError({ status: 0, extra: xhrExtra });
|
|
79
|
-
// Data handler
|
|
80
|
-
xhr.onreadystatechange = (e: Event) => {
|
|
81
|
-
const event = e as unknown as ProgressEvent<XMLHttpRequest>;
|
|
82
|
-
const finishedState = 4;
|
|
83
|
-
if (event.target && event.target.readyState === finishedState) {
|
|
84
|
-
const { status } = event.target;
|
|
85
|
-
const success = String(status).startsWith("2") || String(status).startsWith("3");
|
|
86
|
-
const responseHeaders = getResponseHeaders(xhr.getAllResponseHeaders());
|
|
87
|
-
if (success) {
|
|
88
|
-
const data = parseResponse(event.target.response);
|
|
89
|
-
onSuccess({ data, status, extra: { headers: responseHeaders } });
|
|
90
|
-
} else {
|
|
91
|
-
// delay to finish after onabort/ontimeout
|
|
92
|
-
const error = parseErrorResponse(event.target.response);
|
|
93
|
-
onError({ error, status, extra: { headers: responseHeaders } });
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
// Start request
|
|
98
|
-
onBeforeRequest();
|
|
99
|
-
onRequestStart();
|
|
100
|
-
xhr.send(payload as XMLHttpRequestBodyInit);
|
|
101
|
-
},
|
|
102
|
-
);
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import http, { OutgoingHttpHeaders } from "http";
|
|
2
|
-
import https from "https";
|
|
3
|
-
|
|
4
|
-
import { Adapter } from "../adapter/adapter";
|
|
5
|
-
import { HttpAdapterOptionsType, HttpAdapterExtraType } from "./http-adapter.types";
|
|
6
|
-
import { HttpMethods } from "constants/http.constants";
|
|
7
|
-
import { HttpMethodsType, HttpStatusType } from "types";
|
|
8
|
-
import { parseErrorResponse, parseResponse, stringifyQueryParams } from "./http-adapter.utils";
|
|
9
|
-
import { QueryParamsType } from "adapter";
|
|
10
|
-
import { defaultTimeout, xhrExtra } from "./http-adapter.constants";
|
|
11
|
-
|
|
12
|
-
export const getAdapter = () =>
|
|
13
|
-
new Adapter<
|
|
14
|
-
HttpAdapterOptionsType,
|
|
15
|
-
HttpMethodsType,
|
|
16
|
-
HttpStatusType,
|
|
17
|
-
HttpAdapterExtraType,
|
|
18
|
-
QueryParamsType | string | null,
|
|
19
|
-
undefined,
|
|
20
|
-
string
|
|
21
|
-
>({
|
|
22
|
-
name: "http-server",
|
|
23
|
-
defaultMethod: HttpMethods.GET,
|
|
24
|
-
defaultExtra: xhrExtra,
|
|
25
|
-
systemErrorStatus: 0 as number,
|
|
26
|
-
systemErrorExtra: xhrExtra,
|
|
27
|
-
})
|
|
28
|
-
.setQueryParamsMapper(stringifyQueryParams)
|
|
29
|
-
.setFetcher(
|
|
30
|
-
async ({
|
|
31
|
-
request,
|
|
32
|
-
adapterOptions,
|
|
33
|
-
adapter,
|
|
34
|
-
headers,
|
|
35
|
-
payload,
|
|
36
|
-
onError,
|
|
37
|
-
onResponseEnd,
|
|
38
|
-
onTimeoutError,
|
|
39
|
-
onRequestEnd,
|
|
40
|
-
createAbortListener,
|
|
41
|
-
getAbortController,
|
|
42
|
-
onResponseProgress,
|
|
43
|
-
onResponseStart,
|
|
44
|
-
onBeforeRequest,
|
|
45
|
-
onRequestStart,
|
|
46
|
-
onSuccess,
|
|
47
|
-
}) => {
|
|
48
|
-
const { method, client, endpoint, queryParams } = request;
|
|
49
|
-
const { url } = client;
|
|
50
|
-
const httpClient = client.url.includes("https://") ? https : http;
|
|
51
|
-
const options = {
|
|
52
|
-
method,
|
|
53
|
-
headers: headers as OutgoingHttpHeaders,
|
|
54
|
-
timeout: defaultTimeout,
|
|
55
|
-
signal: getAbortController()?.signal,
|
|
56
|
-
} satisfies https.RequestOptions;
|
|
57
|
-
|
|
58
|
-
if (adapterOptions) {
|
|
59
|
-
Object.entries(adapterOptions).forEach(([name, value]) => {
|
|
60
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
options[name] = value;
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
onBeforeRequest();
|
|
67
|
-
|
|
68
|
-
if (payload) {
|
|
69
|
-
// Calculate Content-Length based on the exact bytes that will be sent.
|
|
70
|
-
// If payload is already a string or Buffer (as produced by getAdapterPayload),
|
|
71
|
-
// avoid double-stringifying which would lead to incorrect Content-Length.
|
|
72
|
-
const payloadAsAny = payload as unknown;
|
|
73
|
-
const isString = typeof payloadAsAny === "string";
|
|
74
|
-
const isBuffer = Buffer.isBuffer(payloadAsAny);
|
|
75
|
-
let contentLength: number;
|
|
76
|
-
if (isString) {
|
|
77
|
-
contentLength = Buffer.byteLength(payloadAsAny);
|
|
78
|
-
} else if (isBuffer) {
|
|
79
|
-
contentLength = payloadAsAny.length;
|
|
80
|
-
} else {
|
|
81
|
-
contentLength = Buffer.byteLength(JSON.stringify(payloadAsAny));
|
|
82
|
-
}
|
|
83
|
-
options.headers["Content-Length"] = contentLength;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const queryString = queryParams ? stringifyQueryParams(queryParams) : "";
|
|
87
|
-
const fullUrl = `${url}${endpoint}${queryString}`;
|
|
88
|
-
|
|
89
|
-
const unmountListener = createAbortListener({
|
|
90
|
-
status: 0,
|
|
91
|
-
extra: xhrExtra,
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const httpRequest = httpClient.request(fullUrl, options, (response) => {
|
|
95
|
-
// TODO - Change to settable from options
|
|
96
|
-
response.setEncoding("utf8");
|
|
97
|
-
onRequestStart();
|
|
98
|
-
|
|
99
|
-
let chunks = "";
|
|
100
|
-
const totalDownloadBytes = Number(response.headers["content-length"]);
|
|
101
|
-
let downloadedBytes = 0;
|
|
102
|
-
|
|
103
|
-
response.on("data", (chunk) => {
|
|
104
|
-
/* istanbul ignore next */
|
|
105
|
-
if (!chunks) {
|
|
106
|
-
onRequestEnd();
|
|
107
|
-
onResponseStart();
|
|
108
|
-
}
|
|
109
|
-
downloadedBytes += chunk.length;
|
|
110
|
-
chunks += chunk;
|
|
111
|
-
|
|
112
|
-
onResponseProgress({ total: totalDownloadBytes, loaded: downloadedBytes });
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
response.on("end", async () => {
|
|
116
|
-
/* istanbul ignore next */
|
|
117
|
-
const { statusCode = adapter.systemErrorStatus } = response;
|
|
118
|
-
const success = String(statusCode).startsWith("2") || String(statusCode).startsWith("3");
|
|
119
|
-
|
|
120
|
-
if (success) {
|
|
121
|
-
const data = parseResponse(chunks);
|
|
122
|
-
onSuccess({
|
|
123
|
-
data,
|
|
124
|
-
status: statusCode,
|
|
125
|
-
extra: { headers: response.headers as Record<string, string> },
|
|
126
|
-
});
|
|
127
|
-
} else {
|
|
128
|
-
// delay to finish after onabort/ontimeout
|
|
129
|
-
const data = parseErrorResponse(chunks);
|
|
130
|
-
onError({
|
|
131
|
-
error: data,
|
|
132
|
-
status: statusCode,
|
|
133
|
-
extra: { headers: response.headers as Record<string, string> },
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
unmountListener();
|
|
138
|
-
onResponseEnd();
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
httpRequest.on("timeout", () => onTimeoutError({ status: 0, extra: xhrExtra }));
|
|
143
|
-
httpRequest.on("error", (error) => onError({ error, status: 0, extra: xhrExtra }));
|
|
144
|
-
if (payload) {
|
|
145
|
-
httpRequest.write(payload);
|
|
146
|
-
}
|
|
147
|
-
httpRequest.end();
|
|
148
|
-
},
|
|
149
|
-
);
|