@hyper-fetch/core 7.5.2 → 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.
Files changed (271) hide show
  1. package/.eslintrc.cjs +12 -0
  2. package/__tests__/e2e/e2e.abort-timeout.spec.ts +101 -0
  3. package/__tests__/e2e/e2e.advanced.spec.ts +423 -0
  4. package/__tests__/e2e/e2e.methods.spec.ts +243 -0
  5. package/__tests__/e2e/e2e.payloads.spec.ts +154 -0
  6. package/__tests__/features/adapter/adapter.base.spec.ts +414 -0
  7. package/__tests__/features/adapter/adapter.bindings.spec.ts +1025 -0
  8. package/__tests__/features/adapter/adapter.browser.spec.ts +88 -0
  9. package/__tests__/features/adapter/adapter.server.spec.ts +369 -0
  10. package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
  11. package/__tests__/features/cache/cache.base.spec.ts +366 -0
  12. package/__tests__/features/cache/cache.events.spec.ts +219 -0
  13. package/__tests__/features/cache/cache.garbage-collector.spec.ts +175 -0
  14. package/__tests__/features/cache/cache.lazy-storage.spec.ts +132 -0
  15. package/__tests__/features/cache/cache.modes.spec.ts +145 -0
  16. package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
  17. package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
  18. package/__tests__/features/cache/cache.utils.spec.ts +126 -0
  19. package/__tests__/features/client/client.auth.spec.ts +125 -0
  20. package/__tests__/features/client/client.base.spec.ts +161 -0
  21. package/__tests__/features/client/client.from-json.spec.ts +182 -0
  22. package/__tests__/features/client/client.hydration.spec.ts +236 -0
  23. package/__tests__/features/client/client.interceptor.spec.ts +225 -0
  24. package/__tests__/features/client/client.methods.spec.ts +189 -0
  25. package/__tests__/features/client/client.middleware.spec.ts +118 -0
  26. package/__tests__/features/client/client.utils.spec.ts +292 -0
  27. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
  28. package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
  29. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +309 -0
  30. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
  31. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
  32. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +142 -0
  33. package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
  34. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +43 -0
  35. package/__tests__/features/managers/logger/logger.base.spec.ts +109 -0
  36. package/__tests__/features/managers/logger/logger.utils.spec.ts +106 -0
  37. package/__tests__/features/managers/request/request.manager.base.spec.ts +224 -0
  38. package/__tests__/features/managers/request/request.manager.events.spec.ts +364 -0
  39. package/__tests__/features/mocker/mocker.base.spec.ts +639 -0
  40. package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
  41. package/__tests__/features/request/request.base.spec.ts +225 -0
  42. package/__tests__/features/request/request.cloning.spec.ts +47 -0
  43. package/__tests__/features/request/request.hooks.spec.ts +173 -0
  44. package/__tests__/features/request/request.optimistic.spec.ts +93 -0
  45. package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
  46. package/__tests__/features/request/request.sending.spec.ts +364 -0
  47. package/__tests__/features/request/request.setters.spec.ts +164 -0
  48. package/__tests__/features/request/request.typescript.spec.ts +423 -0
  49. package/__tests__/features/request/request.utils.spec.ts +77 -0
  50. package/__tests__/features/sdk/sdk.base.spec.ts +132 -0
  51. package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
  52. package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
  53. package/__tests__/shared/callbacks.shared.ts +11 -0
  54. package/__tests__/shared/index.ts +2 -0
  55. package/__tests__/shared/progress.shared.ts +36 -0
  56. package/__tests__/tsconfig.json +24 -0
  57. package/__tests__/type-stress/generate-stress-test.ts +92 -0
  58. package/__tests__/type-stress/stress-10.ts +35 -0
  59. package/__tests__/type-stress/stress-just-create.ts +2107 -0
  60. package/__tests__/type-stress/stress-minimal.ts +23 -0
  61. package/__tests__/type-stress/stress-test-500.ts +2319 -0
  62. package/__tests__/type-stress/stress-test-sdk.ts +210 -0
  63. package/__tests__/type-stress/tsconfig.json +6 -0
  64. package/__tests__/utils/adapter.utils.ts +39 -0
  65. package/__tests__/utils/cache.utils.ts +20 -0
  66. package/__tests__/utils/callbacks.utils.ts +48 -0
  67. package/__tests__/utils/dispatcher.utils.ts +7 -0
  68. package/__tests__/utils/index.ts +4 -0
  69. package/__tests__/vitest.setup.ts +15 -0
  70. package/dist/adapter/adapter.bindings.d.ts +71 -0
  71. package/dist/adapter/adapter.bindings.d.ts.map +1 -0
  72. package/dist/adapter/adapter.d.ts +127 -0
  73. package/dist/adapter/adapter.d.ts.map +1 -0
  74. package/dist/adapter/adapter.types.d.ts +108 -0
  75. package/dist/adapter/adapter.types.d.ts.map +1 -0
  76. package/dist/adapter/adapter.utils.d.ts +23 -0
  77. package/dist/adapter/adapter.utils.d.ts.map +1 -0
  78. package/dist/adapter/index.d.ts +5 -0
  79. package/dist/adapter/index.d.ts.map +1 -0
  80. package/dist/cache/cache.d.ts +95 -0
  81. package/dist/cache/cache.d.ts.map +1 -0
  82. package/dist/cache/cache.events.d.ts +51 -0
  83. package/dist/cache/cache.events.d.ts.map +1 -0
  84. package/dist/cache/cache.types.d.ts +44 -0
  85. package/dist/cache/cache.types.d.ts.map +1 -0
  86. package/dist/cache/cache.utils.d.ts +11 -0
  87. package/dist/cache/cache.utils.d.ts.map +1 -0
  88. package/dist/cache/index.d.ts +5 -0
  89. package/dist/cache/index.d.ts.map +1 -0
  90. package/dist/client/client.constants.d.ts +10 -0
  91. package/dist/client/client.constants.d.ts.map +1 -0
  92. package/dist/client/client.create.d.ts +8 -0
  93. package/dist/client/client.create.d.ts.map +1 -0
  94. package/dist/client/client.d.ts +195 -0
  95. package/dist/client/client.d.ts.map +1 -0
  96. package/dist/client/client.types.d.ts +71 -0
  97. package/dist/client/client.types.d.ts.map +1 -0
  98. package/dist/client/client.utils.d.ts +11 -0
  99. package/dist/client/client.utils.d.ts.map +1 -0
  100. package/dist/client/index.d.ts +6 -0
  101. package/dist/client/index.d.ts.map +1 -0
  102. package/dist/constants/http.constants.d.ts +8 -0
  103. package/dist/constants/http.constants.d.ts.map +1 -0
  104. package/dist/constants/time.constants.d.ts +12 -0
  105. package/dist/constants/time.constants.d.ts.map +1 -0
  106. package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
  107. package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
  108. package/dist/dispatcher/dispatcher.d.ts +168 -0
  109. package/dist/dispatcher/dispatcher.d.ts.map +1 -0
  110. package/dist/dispatcher/dispatcher.events.d.ts +49 -0
  111. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
  112. package/dist/dispatcher/dispatcher.types.d.ts +44 -0
  113. package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
  114. package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
  115. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
  116. package/dist/dispatcher/index.d.ts +6 -0
  117. package/dist/dispatcher/index.d.ts.map +1 -0
  118. package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
  119. package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
  120. package/dist/http-adapter/http-adapter.d.ts +3 -0
  121. package/dist/http-adapter/http-adapter.d.ts.map +1 -0
  122. package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
  123. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
  124. package/dist/http-adapter/http-adapter.types.d.ts +73 -0
  125. package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
  126. package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
  127. package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
  128. package/dist/http-adapter/index.d.ts +6 -0
  129. package/dist/http-adapter/index.d.ts.map +1 -0
  130. package/dist/index.d.ts +15 -2083
  131. package/dist/index.d.ts.map +1 -0
  132. package/dist/index.mjs +4084 -0
  133. package/dist/index.mjs.map +1 -0
  134. package/dist/managers/app/app.manager.constants.d.ts +10 -0
  135. package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
  136. package/dist/managers/app/app.manager.d.ts +35 -0
  137. package/dist/managers/app/app.manager.d.ts.map +1 -0
  138. package/dist/managers/app/app.manager.events.d.ts +12 -0
  139. package/dist/managers/app/app.manager.events.d.ts.map +1 -0
  140. package/dist/managers/app/app.manager.types.d.ts +7 -0
  141. package/dist/managers/app/app.manager.types.d.ts.map +1 -0
  142. package/dist/managers/app/app.manager.utils.d.ts +5 -0
  143. package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
  144. package/dist/managers/app/index.d.ts +6 -0
  145. package/dist/managers/app/index.d.ts.map +1 -0
  146. package/dist/managers/index.d.ts +4 -0
  147. package/dist/managers/index.d.ts.map +1 -0
  148. package/dist/managers/logger/index.d.ts +5 -0
  149. package/dist/managers/logger/index.d.ts.map +1 -0
  150. package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
  151. package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
  152. package/dist/managers/logger/logger.manager.d.ts +22 -0
  153. package/dist/managers/logger/logger.manager.d.ts.map +1 -0
  154. package/dist/managers/logger/logger.manager.types.d.ts +43 -0
  155. package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
  156. package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
  157. package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
  158. package/dist/managers/request/index.d.ts +5 -0
  159. package/dist/managers/request/index.d.ts.map +1 -0
  160. package/dist/managers/request/request.manager.d.ts +60 -0
  161. package/dist/managers/request/request.manager.d.ts.map +1 -0
  162. package/dist/managers/request/request.manager.events.d.ts +55 -0
  163. package/dist/managers/request/request.manager.events.d.ts.map +1 -0
  164. package/dist/managers/request/request.manager.types.d.ts +48 -0
  165. package/dist/managers/request/request.manager.types.d.ts.map +1 -0
  166. package/dist/managers/request/request.manager.utils.d.ts +30 -0
  167. package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
  168. package/dist/mocker/index.d.ts +3 -0
  169. package/dist/mocker/index.d.ts.map +1 -0
  170. package/dist/mocker/mocker.d.ts +3 -0
  171. package/dist/mocker/mocker.d.ts.map +1 -0
  172. package/dist/mocker/mocker.types.d.ts +25 -0
  173. package/dist/mocker/mocker.types.d.ts.map +1 -0
  174. package/dist/plugin/index.d.ts +3 -0
  175. package/dist/plugin/index.d.ts.map +1 -0
  176. package/dist/plugin/plugin.d.ts +55 -0
  177. package/dist/plugin/plugin.d.ts.map +1 -0
  178. package/dist/plugin/plugin.types.d.ts +122 -0
  179. package/dist/plugin/plugin.types.d.ts.map +1 -0
  180. package/dist/request/index.d.ts +5 -0
  181. package/dist/request/index.d.ts.map +1 -0
  182. package/dist/request/request.d.ts +195 -0
  183. package/dist/request/request.d.ts.map +1 -0
  184. package/dist/request/request.hooks.d.ts +19 -0
  185. package/dist/request/request.hooks.d.ts.map +1 -0
  186. package/dist/request/request.types.d.ts +240 -0
  187. package/dist/request/request.types.d.ts.map +1 -0
  188. package/dist/request/request.utils.d.ts +25 -0
  189. package/dist/request/request.utils.d.ts.map +1 -0
  190. package/dist/sdk/index.d.ts +2 -0
  191. package/dist/sdk/index.d.ts.map +1 -0
  192. package/dist/sdk/sdk.d.ts +69 -0
  193. package/dist/sdk/sdk.d.ts.map +1 -0
  194. package/dist/types/adapter.types.d.ts +22 -0
  195. package/dist/types/adapter.types.d.ts.map +1 -0
  196. package/dist/types/client.types.d.ts +4 -0
  197. package/dist/types/client.types.d.ts.map +1 -0
  198. package/dist/types/helpers.types.d.ts +17 -0
  199. package/dist/types/helpers.types.d.ts.map +1 -0
  200. package/dist/types/http.types.d.ts +3 -0
  201. package/dist/types/http.types.d.ts.map +1 -0
  202. package/dist/types/hydration.types.d.ts +15 -0
  203. package/dist/types/hydration.types.d.ts.map +1 -0
  204. package/dist/types/index.d.ts +7 -0
  205. package/dist/types/index.d.ts.map +1 -0
  206. package/dist/types/request.types.d.ts +34 -0
  207. package/dist/types/request.types.d.ts.map +1 -0
  208. package/dist/utils/emitter.utils.d.ts +13 -0
  209. package/dist/utils/emitter.utils.d.ts.map +1 -0
  210. package/dist/utils/index.d.ts +3 -0
  211. package/dist/utils/index.d.ts.map +1 -0
  212. package/dist/utils/uuid.utils.d.ts +3 -0
  213. package/dist/utils/uuid.utils.d.ts.map +1 -0
  214. package/package.json +10 -25
  215. package/project.json +4 -0
  216. package/src/adapter/adapter.bindings.ts +8 -12
  217. package/src/adapter/adapter.ts +8 -7
  218. package/src/adapter/adapter.types.ts +10 -6
  219. package/src/adapter/adapter.utils.ts +1 -1
  220. package/src/cache/cache.events.ts +4 -11
  221. package/src/cache/cache.ts +28 -22
  222. package/src/cache/cache.types.ts +8 -4
  223. package/src/cache/cache.utils.ts +3 -3
  224. package/src/client/client.create.ts +4 -3
  225. package/src/client/client.ts +44 -12
  226. package/src/client/client.types.ts +25 -3
  227. package/src/client/client.utils.ts +24 -4
  228. package/src/dispatcher/dispatcher.events.ts +3 -3
  229. package/src/dispatcher/dispatcher.ts +59 -28
  230. package/src/dispatcher/dispatcher.types.ts +19 -2
  231. package/src/dispatcher/dispatcher.utils.ts +5 -4
  232. package/src/http-adapter/http-adapter.constants.ts +1 -1
  233. package/src/http-adapter/http-adapter.fetch.ts +174 -0
  234. package/src/http-adapter/http-adapter.ts +3 -8
  235. package/src/http-adapter/http-adapter.types.ts +17 -9
  236. package/src/http-adapter/http-adapter.utils.ts +6 -9
  237. package/src/http-adapter/index.ts +1 -0
  238. package/src/managers/app/app.manager.constants.ts +2 -2
  239. package/src/managers/app/app.manager.events.ts +1 -1
  240. package/src/managers/app/app.manager.ts +2 -1
  241. package/src/managers/logger/logger.manager.constants.ts +1 -1
  242. package/src/managers/logger/logger.manager.ts +2 -1
  243. package/src/managers/logger/logger.manager.types.ts +2 -2
  244. package/src/managers/logger/logger.manager.utils.ts +2 -1
  245. package/src/managers/request/request.manager.events.ts +13 -11
  246. package/src/managers/request/request.manager.types.ts +6 -3
  247. package/src/mocker/mocker.ts +3 -2
  248. package/src/mocker/mocker.types.ts +1 -1
  249. package/src/plugin/plugin.ts +2 -2
  250. package/src/plugin/plugin.types.ts +9 -9
  251. package/src/request/index.ts +1 -0
  252. package/src/request/request.hooks.ts +87 -0
  253. package/src/request/request.ts +155 -77
  254. package/src/request/request.types.ts +54 -22
  255. package/src/request/request.utils.ts +86 -23
  256. package/src/sdk/sdk.ts +153 -8
  257. package/src/types/adapter.types.ts +1 -1
  258. package/src/types/client.types.ts +1 -1
  259. package/src/types/hydration.types.ts +3 -3
  260. package/src/types/request.types.ts +30 -20
  261. package/src/utils/uuid.utils.ts +1 -1
  262. package/tsconfig.base.json +1 -6
  263. package/tsconfig.json +1 -4
  264. package/vite.config.ts +45 -0
  265. package/.eslintrc.json +0 -11
  266. package/dist/browser/index.js +0 -3751
  267. package/dist/browser/index.js.map +0 -7
  268. package/dist/index.js +0 -3991
  269. package/dist/index.js.map +0 -7
  270. package/src/http-adapter/http-adapter.browser.ts +0 -102
  271. package/src/http-adapter/http-adapter.server.ts +0 -135
@@ -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 * as browser from "@browser-adapter";
2
- import * as server from "@server-adapter";
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
- if (typeof XMLHttpRequest !== "undefined") {
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: HttpAdapterOptionsType;
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 interface HttpAdapterRequest extends Omit<XMLHttpRequest, "responseType"> {
22
- responseType: XMLHttpRequestResponseType | "stream";
23
- }
24
- export type HttpAdapterOptionsType = Partial<HttpAdapterRequest>;
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 { getErrorMessage, QueryParamsType, QueryParamType, QueryParamValuesType } from "adapter";
2
- import { RequestInstance } from "request";
3
- import { ExtractErrorType, EmptyTypes } from "types";
4
- import { HttpAdapterRequest, BufferEncoding, QueryStringifyOptionsType } from "./http-adapter.types";
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":
@@ -2,3 +2,4 @@ export * from "./http-adapter";
2
2
  export * from "./http-adapter.types";
3
3
  export * from "./http-adapter.utils";
4
4
  export * from "./http-adapter.constants";
5
+ export * from "./http-adapter.fetch";
@@ -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,4 +1,4 @@
1
- import EventEmitter from "events";
1
+ import type EventEmitter from "events";
2
2
 
3
3
  import { AppEvents } from "managers";
4
4
 
@@ -1,5 +1,6 @@
1
1
  import { EventEmitter } from "utils";
2
- import { appManagerInitialOptions, AppManagerOptionsType, getAppManagerEvents, hasDocument } from "managers";
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,4 +1,4 @@
1
- import { LogLevel } from "./logger.manager.types";
1
+ import type { LogLevel } from "./logger.manager.types";
2
2
 
3
3
  export const logLevelOrder: LogLevel[] = ["error", "warning", "info", "debug"];
4
4
 
@@ -1,7 +1,8 @@
1
1
  import EventEmitter from "events";
2
2
 
3
3
  import type { ClientInstance } from "client";
4
- import { logger, LogLevel, LoggerOptionsType, logLevelOrder, LoggerType, LoggerMethods } from "managers";
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,5 +1,6 @@
1
1
  /* eslint-disable no-console */
2
- import { loggerColors, loggerStyles, LoggerType } from "managers";
2
+ import type { LoggerType } from "managers";
3
+ import { loggerColors, loggerStyles } from "managers";
3
4
 
4
5
  export const getTime = () => {
5
6
  const d = new Date();
@@ -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 */
@@ -1,5 +1,6 @@
1
- import { getAdapterBindings, AdapterInstance, RequestProcessingError } from "adapter";
2
- import { ExtractAdapterExtraType, ExtractAdapterStatusType } from "types";
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,
@@ -1,4 +1,4 @@
1
- import { AdapterInstance, ResponseType } from "adapter";
1
+ import type { AdapterInstance, ResponseType } from "adapter";
2
2
 
3
3
  type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
4
4
 
@@ -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, QueueItemType } from "dispatcher";
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: QueueItemType<
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: QueueItemType<
128
+ queueItem: ResolvedQueueItemType<
129
129
  ExtendRequest<
130
130
  RequestInstance,
131
131
  {
@@ -1,3 +1,4 @@
1
1
  export * from "./request";
2
2
  export * from "./request.types";
3
3
  export * from "./request.utils";
4
+ export * from "./request.hooks";
@@ -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
+ }