@hyper-fetch/core 7.5.3 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.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 +10 -25
- 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 +153 -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
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Adapter } from "../adapter/adapter";
|
|
2
|
+
import { HttpMethods } from "constants/http.constants";
|
|
3
|
+
import type { HttpMethodsType, HttpStatusType } from "types";
|
|
4
|
+
import type { QueryParamsType } from "adapter";
|
|
5
|
+
import type { HttpAdapterExtraType, FetchAdapterOptionsType } from "./http-adapter.types";
|
|
6
|
+
import { parseErrorResponse, parseResponse, stringifyQueryParams } from "./http-adapter.utils";
|
|
7
|
+
import { defaultTimeout, xhrExtra } from "./http-adapter.constants";
|
|
8
|
+
|
|
9
|
+
const defaultExtra = xhrExtra;
|
|
10
|
+
|
|
11
|
+
export const getAdapter = () =>
|
|
12
|
+
new Adapter<
|
|
13
|
+
FetchAdapterOptionsType,
|
|
14
|
+
HttpMethodsType,
|
|
15
|
+
HttpStatusType,
|
|
16
|
+
HttpAdapterExtraType,
|
|
17
|
+
QueryParamsType | string | null,
|
|
18
|
+
undefined,
|
|
19
|
+
string
|
|
20
|
+
>({
|
|
21
|
+
name: "http",
|
|
22
|
+
defaultMethod: HttpMethods.GET,
|
|
23
|
+
defaultExtra,
|
|
24
|
+
systemErrorStatus: 0 as number,
|
|
25
|
+
systemErrorExtra: defaultExtra,
|
|
26
|
+
})
|
|
27
|
+
.setQueryParamsMapper(stringifyQueryParams)
|
|
28
|
+
.setFetcher(
|
|
29
|
+
async ({
|
|
30
|
+
request,
|
|
31
|
+
adapterOptions,
|
|
32
|
+
headers,
|
|
33
|
+
payload,
|
|
34
|
+
onError,
|
|
35
|
+
onResponseEnd,
|
|
36
|
+
onTimeoutError,
|
|
37
|
+
onRequestEnd,
|
|
38
|
+
createAbortListener,
|
|
39
|
+
getAbortController,
|
|
40
|
+
onResponseProgress,
|
|
41
|
+
onResponseStart,
|
|
42
|
+
onBeforeRequest,
|
|
43
|
+
onRequestStart,
|
|
44
|
+
onRequestProgress,
|
|
45
|
+
onSuccess,
|
|
46
|
+
}) => {
|
|
47
|
+
const { method, client, endpoint, queryParams } = request;
|
|
48
|
+
const queryString = queryParams ? stringifyQueryParams(queryParams) : "";
|
|
49
|
+
const fullUrl = `${client.url}${endpoint}${queryString}`;
|
|
50
|
+
|
|
51
|
+
const controller = getAbortController();
|
|
52
|
+
const { timeout: timeoutMs = defaultTimeout, streaming = false, ...restOptions } = adapterOptions || {};
|
|
53
|
+
|
|
54
|
+
let timedOut = false;
|
|
55
|
+
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
56
|
+
|
|
57
|
+
// Abort listener handles user-initiated aborts (e.g. request.abort())
|
|
58
|
+
// For fetch, onAbort is a no-op since the signal is already passed to fetch()
|
|
59
|
+
const unmountListener = createAbortListener({
|
|
60
|
+
status: 0,
|
|
61
|
+
extra: defaultExtra,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (timeoutMs > 0 && controller) {
|
|
65
|
+
timeoutId = setTimeout(() => {
|
|
66
|
+
timedOut = true;
|
|
67
|
+
unmountListener();
|
|
68
|
+
controller.abort();
|
|
69
|
+
}, timeoutMs);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
onBeforeRequest();
|
|
73
|
+
onRequestStart();
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const init: RequestInit = {
|
|
77
|
+
method,
|
|
78
|
+
headers: headers as Record<string, string>,
|
|
79
|
+
signal: controller?.signal,
|
|
80
|
+
...restOptions,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
if (payload && method !== "GET" && method !== "HEAD") {
|
|
84
|
+
init.body = payload as BodyInit;
|
|
85
|
+
|
|
86
|
+
if (typeof payload === "string") {
|
|
87
|
+
const total = new Blob([payload]).size;
|
|
88
|
+
onRequestProgress({ total, loaded: total } as ProgressEvent);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
onRequestEnd();
|
|
93
|
+
|
|
94
|
+
const response = await fetch(fullUrl, init);
|
|
95
|
+
|
|
96
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
97
|
+
|
|
98
|
+
onResponseStart();
|
|
99
|
+
|
|
100
|
+
const responseHeaders: Record<string, string> = {};
|
|
101
|
+
response.headers.forEach((value, key) => {
|
|
102
|
+
responseHeaders[key] = value;
|
|
103
|
+
});
|
|
104
|
+
const extra: HttpAdapterExtraType = { headers: responseHeaders };
|
|
105
|
+
const { status } = response;
|
|
106
|
+
|
|
107
|
+
if (streaming && response.body) {
|
|
108
|
+
onResponseEnd();
|
|
109
|
+
unmountListener();
|
|
110
|
+
|
|
111
|
+
if (response.ok) {
|
|
112
|
+
onSuccess({ data: response.body as any, status, extra });
|
|
113
|
+
} else {
|
|
114
|
+
const errorBody = await response.text();
|
|
115
|
+
const error = parseErrorResponse(errorBody);
|
|
116
|
+
onError({ error, status, extra });
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
let body: string;
|
|
120
|
+
|
|
121
|
+
if (response.body && typeof response.body.getReader === "function") {
|
|
122
|
+
const reader = response.body.getReader();
|
|
123
|
+
const contentLength = parseInt(response.headers.get("content-length") || "0", 10);
|
|
124
|
+
let receivedLength = 0;
|
|
125
|
+
const chunks: Uint8Array[] = [];
|
|
126
|
+
|
|
127
|
+
// eslint-disable-next-line no-constant-condition
|
|
128
|
+
while (true) {
|
|
129
|
+
// eslint-disable-next-line no-await-in-loop
|
|
130
|
+
const { done, value } = await reader.read();
|
|
131
|
+
if (done) break;
|
|
132
|
+
chunks.push(value);
|
|
133
|
+
receivedLength += value.length;
|
|
134
|
+
onResponseProgress({ total: contentLength || receivedLength, loaded: receivedLength } as ProgressEvent);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const allChunks = new Uint8Array(receivedLength);
|
|
138
|
+
let position = 0;
|
|
139
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
140
|
+
for (const chunk of chunks) {
|
|
141
|
+
allChunks.set(chunk, position);
|
|
142
|
+
position += chunk.length;
|
|
143
|
+
}
|
|
144
|
+
body = new TextDecoder().decode(allChunks);
|
|
145
|
+
} else {
|
|
146
|
+
body = await response.text();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
onResponseEnd();
|
|
150
|
+
unmountListener();
|
|
151
|
+
|
|
152
|
+
if (response.ok) {
|
|
153
|
+
const data = parseResponse(body);
|
|
154
|
+
onSuccess({ data, status, extra });
|
|
155
|
+
} else {
|
|
156
|
+
const error = parseErrorResponse(body);
|
|
157
|
+
onError({ error, status, extra });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} catch (err: any) {
|
|
161
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
162
|
+
unmountListener();
|
|
163
|
+
|
|
164
|
+
if (controller?.signal?.aborted) {
|
|
165
|
+
if (timedOut) {
|
|
166
|
+
onTimeoutError({ status: 0, extra: defaultExtra });
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
onError({ error: err, status: 0, extra: defaultExtra });
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
);
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import { HttpAdapterType } from "http-adapter/http-adapter.types";
|
|
1
|
+
import { getAdapter } from "./http-adapter.fetch";
|
|
2
|
+
import type { HttpAdapterType } from "http-adapter/http-adapter.types";
|
|
5
3
|
|
|
6
4
|
export const HttpAdapter = (): HttpAdapterType => {
|
|
7
|
-
|
|
8
|
-
return browser.getAdapter();
|
|
9
|
-
}
|
|
10
|
-
return server.getAdapter();
|
|
5
|
+
return getAdapter();
|
|
11
6
|
};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { HttpMethodsType, HttpStatusType } from "../types";
|
|
2
|
-
import { DeclareAdapterType, QueryParamsType, QueryParamType } from "adapter";
|
|
3
|
-
import { stringifyQueryParams } from "./http-adapter.utils";
|
|
1
|
+
import type { HttpMethodsType, HttpStatusType } from "../types";
|
|
2
|
+
import type { DeclareAdapterType, QueryParamsType, QueryParamType } from "adapter";
|
|
3
|
+
import type { stringifyQueryParams } from "./http-adapter.utils";
|
|
4
4
|
/**
|
|
5
5
|
* Base Adapter
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
export type HttpAdapterType = DeclareAdapterType<{
|
|
9
|
-
adapterOptions:
|
|
9
|
+
adapterOptions: FetchAdapterOptionsType;
|
|
10
10
|
methodType: HttpMethodsType;
|
|
11
11
|
statusType: HttpStatusType;
|
|
12
12
|
extra: HttpAdapterExtraType;
|
|
@@ -16,12 +16,20 @@ export type HttpAdapterType = DeclareAdapterType<{
|
|
|
16
16
|
}>;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Options
|
|
19
|
+
* Options passed to the native fetch adapter. Extends RequestInit with a `timeout` option.
|
|
20
20
|
*/
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
export type FetchAdapterOptionsType = Omit<RequestInit, "method" | "headers" | "body" | "signal"> & {
|
|
22
|
+
/**
|
|
23
|
+
* Request timeout in milliseconds. Defaults to 5000ms.
|
|
24
|
+
*/
|
|
25
|
+
timeout?: number;
|
|
26
|
+
/**
|
|
27
|
+
* When true, the response `data` will be a `ReadableStream<Uint8Array>` instead of parsed JSON/text.
|
|
28
|
+
* This enables streaming consumption of the response body (e.g., for SSE, large file downloads,
|
|
29
|
+
* or real-time text generation from LLMs).
|
|
30
|
+
*/
|
|
31
|
+
streaming?: boolean;
|
|
32
|
+
};
|
|
25
33
|
|
|
26
34
|
export type HttpAdapterExtraType = {
|
|
27
35
|
headers: Record<string, string>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import type { QueryParamsType, QueryParamType, QueryParamValuesType } from "adapter";
|
|
2
|
+
import { getErrorMessage } from "adapter";
|
|
3
|
+
import type { RequestInstance } from "request";
|
|
4
|
+
import type { ExtractErrorType, EmptyTypes } from "types";
|
|
5
|
+
import type { BufferEncoding, QueryStringifyOptionsType } from "./http-adapter.types";
|
|
5
6
|
import { stringifyDefaultOptions } from "client";
|
|
6
7
|
|
|
7
8
|
// Utils
|
|
@@ -32,11 +33,7 @@ export const parseResponse = (response: string | unknown) => {
|
|
|
32
33
|
}
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
export const handleResponse = (
|
|
36
|
-
responseChunks: any[],
|
|
37
|
-
responseType: HttpAdapterRequest["responseType"],
|
|
38
|
-
responseEncoding: BufferEncoding,
|
|
39
|
-
) => {
|
|
36
|
+
export const handleResponse = (responseChunks: any[], responseType: string, responseEncoding: BufferEncoding) => {
|
|
40
37
|
const bufferedResponse = Buffer.concat(responseChunks);
|
|
41
38
|
switch (responseType) {
|
|
42
39
|
case "arraybuffer":
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RequiredKeys } from "types";
|
|
2
|
-
import { AppManagerOptionsType } from "managers";
|
|
1
|
+
import type { RequiredKeys } from "types";
|
|
2
|
+
import type { AppManagerOptionsType } from "managers";
|
|
3
3
|
import { onWindowEvent, onDocumentEvent } from "./app.manager.utils";
|
|
4
4
|
|
|
5
5
|
export enum AppEvents {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "utils";
|
|
2
|
-
import {
|
|
2
|
+
import type { AppManagerOptionsType } from "managers";
|
|
3
|
+
import { appManagerInitialOptions, getAppManagerEvents, hasDocument } from "managers";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* App manager handles main application states - focus and online. Those two values can answer questions:
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
3
|
import type { ClientInstance } from "client";
|
|
4
|
-
import {
|
|
4
|
+
import type { LogLevel, LoggerOptionsType, LoggerType, LoggerMethods } from "managers";
|
|
5
|
+
import { logger, logLevelOrder } from "managers";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* This class is used across the Hyper Fetch library to provide unified logging system with necessary setup per each client.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AdapterInstance, ResponseType } from "adapter";
|
|
2
|
-
import { RequestInstance } from "request";
|
|
1
|
+
import type { AdapterInstance, ResponseType } from "adapter";
|
|
2
|
+
import type { RequestInstance } from "request";
|
|
3
3
|
|
|
4
4
|
// Extra
|
|
5
5
|
export type LogLevel = "debug" | "info" | "warning" | "error";
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
/* eslint-disable max-params */
|
|
2
|
-
import EventEmitter from "events";
|
|
2
|
+
import type EventEmitter from "events";
|
|
3
3
|
|
|
4
|
+
import type {
|
|
5
|
+
RequestEventType,
|
|
6
|
+
RequestLoadingEventType,
|
|
7
|
+
RequestProgressEventType,
|
|
8
|
+
RequestResponseEventType,
|
|
9
|
+
RequestRemovedEventType,
|
|
10
|
+
RequestDeduplicatedEventType,
|
|
11
|
+
} from "managers";
|
|
4
12
|
import {
|
|
5
13
|
getRequestStarByQueryKey,
|
|
6
14
|
getResponseStartByQueryKey,
|
|
@@ -16,8 +24,6 @@ import {
|
|
|
16
24
|
getRequestStartByIdKey,
|
|
17
25
|
getLoadingByQueryKey,
|
|
18
26
|
getLoadingByIdKey,
|
|
19
|
-
RequestEventType,
|
|
20
|
-
RequestLoadingEventType,
|
|
21
27
|
getRemoveByQueryKey,
|
|
22
28
|
getRemoveByIdKey,
|
|
23
29
|
getLoadingKey,
|
|
@@ -28,20 +34,16 @@ import {
|
|
|
28
34
|
getAbortKey,
|
|
29
35
|
getRemoveKey,
|
|
30
36
|
getResponseStartKey,
|
|
31
|
-
RequestProgressEventType,
|
|
32
|
-
RequestResponseEventType,
|
|
33
|
-
RequestRemovedEventType,
|
|
34
37
|
getLoadingByCacheKey,
|
|
35
|
-
RequestDeduplicatedEventType,
|
|
36
38
|
getRequestDeduplicatedKey,
|
|
37
39
|
getRequestDeduplicatedByIdKey,
|
|
38
40
|
getRequestDeduplicatedByCacheKey,
|
|
39
41
|
getRequestDeduplicatedByQueryKey,
|
|
40
42
|
} from "managers";
|
|
41
|
-
import { AdapterInstance } from "adapter";
|
|
42
|
-
import { RequestInstance } from "request";
|
|
43
|
-
import { Client } from "client";
|
|
44
|
-
import { ExtendRequest } from "types";
|
|
43
|
+
import type { AdapterInstance } from "adapter";
|
|
44
|
+
import type { RequestInstance } from "request";
|
|
45
|
+
import type { Client } from "client";
|
|
46
|
+
import type { ExtendRequest } from "types";
|
|
45
47
|
|
|
46
48
|
export const getRequestManagerEvents = (emitter: EventEmitter) => ({
|
|
47
49
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ProgressType, ResponseType } from "adapter";
|
|
2
|
-
import { RequestInstance } from "request";
|
|
3
|
-
import { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from "types";
|
|
1
|
+
import type { ProgressType, ResponseType } from "adapter";
|
|
2
|
+
import type { OptimisticCallbackResult, RequestInstance } from "request";
|
|
3
|
+
import type { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from "types";
|
|
4
4
|
|
|
5
5
|
// Events
|
|
6
6
|
export type RequestLoadingEventType<T extends RequestInstance> = {
|
|
@@ -14,6 +14,7 @@ export type RequestProgressEventType<T extends RequestInstance> = ProgressType &
|
|
|
14
14
|
export type RequestEventType<T extends RequestInstance> = {
|
|
15
15
|
request: T;
|
|
16
16
|
requestId: string;
|
|
17
|
+
mutationContext?: OptimisticCallbackResult<any>;
|
|
17
18
|
};
|
|
18
19
|
|
|
19
20
|
export type RequestRemovedEventType<T extends RequestInstance> = {
|
|
@@ -41,6 +42,8 @@ export type ResponseDetailsType = {
|
|
|
41
42
|
isCanceled: boolean;
|
|
42
43
|
/** If error from offline status */
|
|
43
44
|
isOffline: boolean;
|
|
45
|
+
/** Whether the dispatcher will schedule another retry for this request */
|
|
46
|
+
willRetry: boolean;
|
|
44
47
|
/** When added to dispatcher's queue (pre-middleware which could take time) */
|
|
45
48
|
addedTimestamp: number;
|
|
46
49
|
/** When request is picked from queue and started to be sent */
|
package/src/mocker/mocker.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { getAdapterBindings, AdapterInstance
|
|
2
|
-
import {
|
|
1
|
+
import type { getAdapterBindings, AdapterInstance } from "adapter";
|
|
2
|
+
import { RequestProcessingError } from "adapter";
|
|
3
|
+
import type { ExtractAdapterExtraType, ExtractAdapterStatusType } from "types";
|
|
3
4
|
|
|
4
5
|
export const mocker = async <T extends AdapterInstance>({
|
|
5
6
|
request,
|
package/src/plugin/plugin.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PluginMethodParameters, PluginMethods, PluginOptionsType } from "plugin";
|
|
2
|
-
import { ClientInstance } from "client";
|
|
1
|
+
import type { PluginMethodParameters, PluginMethods, PluginOptionsType } from "plugin";
|
|
2
|
+
import type { ClientInstance } from "client";
|
|
3
3
|
|
|
4
4
|
export class Plugin<Client extends ClientInstance = ClientInstance, PluginData = void> {
|
|
5
5
|
public name: string;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { RequestInstance } from "request";
|
|
2
|
-
import { ResponseErrorType, ResponseType, ResponseSuccessType, AdapterInstance } from "adapter";
|
|
3
|
-
import { Plugin } from "plugin";
|
|
4
|
-
import {
|
|
1
|
+
import type { RequestInstance } from "request";
|
|
2
|
+
import type { ResponseErrorType, ResponseType, ResponseSuccessType, AdapterInstance } from "adapter";
|
|
3
|
+
import type { Plugin } from "plugin";
|
|
4
|
+
import type {
|
|
5
5
|
ExtractAdapterType,
|
|
6
6
|
ExtractErrorType,
|
|
7
7
|
ExtractResponseType,
|
|
8
8
|
ExtendRequest,
|
|
9
9
|
ExtractClientAdapterType,
|
|
10
10
|
} from "types";
|
|
11
|
-
import { ClientInstance } from "client";
|
|
12
|
-
import { Cache, CacheValueType } from "cache";
|
|
13
|
-
import { Dispatcher, QueueDataType,
|
|
11
|
+
import type { ClientInstance } from "client";
|
|
12
|
+
import type { Cache, CacheValueType } from "cache";
|
|
13
|
+
import type { Dispatcher, QueueDataType, ResolvedQueueItemType } from "dispatcher";
|
|
14
14
|
|
|
15
15
|
export type PluginLifecycle = "trigger" | "start" | "success" | "error" | "finished";
|
|
16
16
|
export type PluginInstance = Plugin<any, any>;
|
|
@@ -106,7 +106,7 @@ export type PluginMethods<Client extends ClientInstance> = {
|
|
|
106
106
|
}
|
|
107
107
|
>
|
|
108
108
|
>;
|
|
109
|
-
queueItem:
|
|
109
|
+
queueItem: ResolvedQueueItemType<
|
|
110
110
|
ExtendRequest<
|
|
111
111
|
RequestInstance,
|
|
112
112
|
{
|
|
@@ -125,7 +125,7 @@ export type PluginMethods<Client extends ClientInstance> = {
|
|
|
125
125
|
}
|
|
126
126
|
>
|
|
127
127
|
>;
|
|
128
|
-
queueItem:
|
|
128
|
+
queueItem: ResolvedQueueItemType<
|
|
129
129
|
ExtendRequest<
|
|
130
130
|
RequestInstance,
|
|
131
131
|
{
|
package/src/request/index.ts
CHANGED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { RequestEventType, RequestProgressEventType, RequestResponseEventType } from "managers";
|
|
2
|
+
import type { RequestInstance } from "./request.types";
|
|
3
|
+
|
|
4
|
+
type HookName =
|
|
5
|
+
| "onBeforeSent"
|
|
6
|
+
| "onRequestStart"
|
|
7
|
+
| "onResponseStart"
|
|
8
|
+
| "onUploadProgress"
|
|
9
|
+
| "onDownloadProgress"
|
|
10
|
+
| "onResponse"
|
|
11
|
+
| "onRemove";
|
|
12
|
+
|
|
13
|
+
export interface RequestHooks<R extends RequestInstance> {
|
|
14
|
+
onBeforeSent: (cb: (eventData: RequestEventType<R>) => void) => () => void;
|
|
15
|
+
onRequestStart: (cb: (eventData: RequestEventType<R>) => void) => () => void;
|
|
16
|
+
onResponseStart: (cb: (eventData: RequestEventType<R>) => void) => () => void;
|
|
17
|
+
onUploadProgress: (cb: (eventData: RequestProgressEventType<R>) => void) => () => void;
|
|
18
|
+
onDownloadProgress: (cb: (eventData: RequestProgressEventType<R>) => void) => () => void;
|
|
19
|
+
onResponse: (cb: (eventData: RequestResponseEventType<R>) => void) => () => void;
|
|
20
|
+
onRemove: (cb: (eventData: RequestEventType<R>) => void) => () => void;
|
|
21
|
+
/** @internal Invoke all registered listeners for a given hook */
|
|
22
|
+
__emit: (name: HookName, data: any) => void;
|
|
23
|
+
/** @internal Get a snapshot of all registered listeners (used by clone) */
|
|
24
|
+
__snapshot: () => Record<HookName, Array<(...args: any[]) => void>>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const HOOK_NAMES: HookName[] = [
|
|
28
|
+
"onBeforeSent",
|
|
29
|
+
"onRequestStart",
|
|
30
|
+
"onResponseStart",
|
|
31
|
+
"onUploadProgress",
|
|
32
|
+
"onDownloadProgress",
|
|
33
|
+
"onResponse",
|
|
34
|
+
"onRemove",
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export function createRequestHooks<R extends RequestInstance>(
|
|
38
|
+
initial?: Record<HookName, Array<(...args: any[]) => void>>,
|
|
39
|
+
): RequestHooks<R> {
|
|
40
|
+
const listeners: Record<HookName, Array<(...args: any[]) => void>> = {
|
|
41
|
+
onBeforeSent: [],
|
|
42
|
+
onRequestStart: [],
|
|
43
|
+
onResponseStart: [],
|
|
44
|
+
onUploadProgress: [],
|
|
45
|
+
onDownloadProgress: [],
|
|
46
|
+
onResponse: [],
|
|
47
|
+
onRemove: [],
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (initial) {
|
|
51
|
+
HOOK_NAMES.forEach((name) => {
|
|
52
|
+
listeners[name] = [...initial[name]];
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const subscribe = (name: HookName) => {
|
|
57
|
+
return (cb: (...args: any[]) => void) => {
|
|
58
|
+
listeners[name].push(cb);
|
|
59
|
+
return () => {
|
|
60
|
+
const idx = listeners[name].indexOf(cb);
|
|
61
|
+
if (idx !== -1) listeners[name].splice(idx, 1);
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
onBeforeSent: subscribe("onBeforeSent"),
|
|
68
|
+
onRequestStart: subscribe("onRequestStart"),
|
|
69
|
+
onResponseStart: subscribe("onResponseStart"),
|
|
70
|
+
onUploadProgress: subscribe("onUploadProgress"),
|
|
71
|
+
onDownloadProgress: subscribe("onDownloadProgress"),
|
|
72
|
+
onResponse: subscribe("onResponse"),
|
|
73
|
+
onRemove: subscribe("onRemove"),
|
|
74
|
+
__emit(name: HookName, data: any) {
|
|
75
|
+
listeners[name].forEach((cb) => {
|
|
76
|
+
cb(data);
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
__snapshot() {
|
|
80
|
+
const snap = {} as Record<HookName, Array<(...args: any[]) => void>>;
|
|
81
|
+
HOOK_NAMES.forEach((name) => {
|
|
82
|
+
snap[name] = [...listeners[name]];
|
|
83
|
+
});
|
|
84
|
+
return snap;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|