@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.
Files changed (270) 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 +44 -17
  7. package/__tests__/features/adapter/adapter.bindings.spec.ts +35 -33
  8. package/__tests__/features/adapter/adapter.browser.spec.ts +5 -20
  9. package/__tests__/features/adapter/adapter.server.spec.ts +229 -37
  10. package/__tests__/features/cache/cache.base.spec.ts +23 -15
  11. package/__tests__/features/cache/cache.events.spec.ts +9 -5
  12. package/__tests__/features/cache/cache.garbage-collector.spec.ts +9 -6
  13. package/__tests__/features/cache/cache.lazy-storage.spec.ts +11 -8
  14. package/__tests__/features/cache/cache.modes.spec.ts +145 -0
  15. package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
  16. package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
  17. package/__tests__/features/cache/cache.utils.spec.ts +10 -3
  18. package/__tests__/features/client/client.auth.spec.ts +5 -5
  19. package/__tests__/features/client/client.base.spec.ts +16 -4
  20. package/__tests__/features/client/client.from-json.spec.ts +182 -0
  21. package/__tests__/features/client/client.hydration.spec.ts +69 -4
  22. package/__tests__/features/client/client.interceptor.spec.ts +4 -4
  23. package/__tests__/features/client/client.methods.spec.ts +5 -4
  24. package/__tests__/features/client/client.middleware.spec.ts +4 -4
  25. package/__tests__/features/client/client.utils.spec.ts +3 -3
  26. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +8 -8
  27. package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
  28. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +17 -12
  29. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +31 -31
  30. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +15 -15
  31. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +22 -5
  32. package/__tests__/features/managers/app/app.manager.base.spec.ts +23 -23
  33. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +6 -3
  34. package/__tests__/features/managers/logger/logger.base.spec.ts +4 -2
  35. package/__tests__/features/managers/logger/logger.utils.spec.ts +10 -4
  36. package/__tests__/features/managers/request/request.manager.base.spec.ts +21 -19
  37. package/__tests__/features/managers/request/request.manager.events.spec.ts +137 -53
  38. package/__tests__/features/mocker/mocker.base.spec.ts +82 -10
  39. package/__tests__/features/plugin/plugin.base.spec.ts +19 -19
  40. package/__tests__/features/request/request.base.spec.ts +39 -13
  41. package/__tests__/features/request/request.cloning.spec.ts +1 -1
  42. package/__tests__/features/request/request.hooks.spec.ts +173 -0
  43. package/__tests__/features/request/request.optimistic.spec.ts +93 -0
  44. package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
  45. package/__tests__/features/request/request.sending.spec.ts +107 -15
  46. package/__tests__/features/request/request.setters.spec.ts +11 -4
  47. package/__tests__/features/request/request.typescript.spec.ts +1 -1
  48. package/__tests__/features/request/request.utils.spec.ts +2 -2
  49. package/__tests__/features/sdk/sdk.base.spec.ts +5 -3
  50. package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
  51. package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
  52. package/__tests__/shared/callbacks.shared.ts +3 -1
  53. package/__tests__/shared/progress.shared.ts +2 -1
  54. package/__tests__/tsconfig.json +14 -3
  55. package/__tests__/type-stress/generate-stress-test.ts +92 -0
  56. package/__tests__/type-stress/stress-10.ts +35 -0
  57. package/__tests__/type-stress/stress-just-create.ts +2107 -0
  58. package/__tests__/type-stress/stress-minimal.ts +23 -0
  59. package/__tests__/type-stress/stress-test-500.ts +2319 -0
  60. package/__tests__/type-stress/stress-test-sdk.ts +210 -0
  61. package/__tests__/type-stress/tsconfig.json +6 -0
  62. package/__tests__/utils/adapter.utils.ts +3 -2
  63. package/__tests__/utils/cache.utils.ts +3 -2
  64. package/__tests__/utils/callbacks.utils.ts +5 -4
  65. package/__tests__/utils/dispatcher.utils.ts +3 -2
  66. package/__tests__/vitest.setup.ts +15 -0
  67. package/dist/adapter/adapter.bindings.d.ts +71 -0
  68. package/dist/adapter/adapter.bindings.d.ts.map +1 -0
  69. package/dist/adapter/adapter.d.ts +127 -0
  70. package/dist/adapter/adapter.d.ts.map +1 -0
  71. package/dist/adapter/adapter.types.d.ts +108 -0
  72. package/dist/adapter/adapter.types.d.ts.map +1 -0
  73. package/dist/adapter/adapter.utils.d.ts +23 -0
  74. package/dist/adapter/adapter.utils.d.ts.map +1 -0
  75. package/dist/adapter/index.d.ts +5 -0
  76. package/dist/adapter/index.d.ts.map +1 -0
  77. package/dist/cache/cache.d.ts +95 -0
  78. package/dist/cache/cache.d.ts.map +1 -0
  79. package/dist/cache/cache.events.d.ts +51 -0
  80. package/dist/cache/cache.events.d.ts.map +1 -0
  81. package/dist/cache/cache.types.d.ts +44 -0
  82. package/dist/cache/cache.types.d.ts.map +1 -0
  83. package/dist/cache/cache.utils.d.ts +11 -0
  84. package/dist/cache/cache.utils.d.ts.map +1 -0
  85. package/dist/cache/index.d.ts +5 -0
  86. package/dist/cache/index.d.ts.map +1 -0
  87. package/dist/client/client.constants.d.ts +10 -0
  88. package/dist/client/client.constants.d.ts.map +1 -0
  89. package/dist/client/client.create.d.ts +8 -0
  90. package/dist/client/client.create.d.ts.map +1 -0
  91. package/dist/client/client.d.ts +195 -0
  92. package/dist/client/client.d.ts.map +1 -0
  93. package/dist/client/client.types.d.ts +71 -0
  94. package/dist/client/client.types.d.ts.map +1 -0
  95. package/dist/client/client.utils.d.ts +11 -0
  96. package/dist/client/client.utils.d.ts.map +1 -0
  97. package/dist/client/index.d.ts +6 -0
  98. package/dist/client/index.d.ts.map +1 -0
  99. package/dist/constants/http.constants.d.ts +8 -0
  100. package/dist/constants/http.constants.d.ts.map +1 -0
  101. package/dist/constants/time.constants.d.ts +12 -0
  102. package/dist/constants/time.constants.d.ts.map +1 -0
  103. package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
  104. package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
  105. package/dist/dispatcher/dispatcher.d.ts +168 -0
  106. package/dist/dispatcher/dispatcher.d.ts.map +1 -0
  107. package/dist/dispatcher/dispatcher.events.d.ts +49 -0
  108. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
  109. package/dist/dispatcher/dispatcher.types.d.ts +44 -0
  110. package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
  111. package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
  112. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
  113. package/dist/dispatcher/index.d.ts +6 -0
  114. package/dist/dispatcher/index.d.ts.map +1 -0
  115. package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
  116. package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
  117. package/dist/http-adapter/http-adapter.d.ts +3 -0
  118. package/dist/http-adapter/http-adapter.d.ts.map +1 -0
  119. package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
  120. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
  121. package/dist/http-adapter/http-adapter.types.d.ts +73 -0
  122. package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
  123. package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
  124. package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
  125. package/dist/http-adapter/index.d.ts +6 -0
  126. package/dist/http-adapter/index.d.ts.map +1 -0
  127. package/dist/index.d.ts +15 -2083
  128. package/dist/index.d.ts.map +1 -0
  129. package/dist/index.mjs +4084 -0
  130. package/dist/index.mjs.map +1 -0
  131. package/dist/managers/app/app.manager.constants.d.ts +10 -0
  132. package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
  133. package/dist/managers/app/app.manager.d.ts +35 -0
  134. package/dist/managers/app/app.manager.d.ts.map +1 -0
  135. package/dist/managers/app/app.manager.events.d.ts +12 -0
  136. package/dist/managers/app/app.manager.events.d.ts.map +1 -0
  137. package/dist/managers/app/app.manager.types.d.ts +7 -0
  138. package/dist/managers/app/app.manager.types.d.ts.map +1 -0
  139. package/dist/managers/app/app.manager.utils.d.ts +5 -0
  140. package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
  141. package/dist/managers/app/index.d.ts +6 -0
  142. package/dist/managers/app/index.d.ts.map +1 -0
  143. package/dist/managers/index.d.ts +4 -0
  144. package/dist/managers/index.d.ts.map +1 -0
  145. package/dist/managers/logger/index.d.ts +5 -0
  146. package/dist/managers/logger/index.d.ts.map +1 -0
  147. package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
  148. package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
  149. package/dist/managers/logger/logger.manager.d.ts +22 -0
  150. package/dist/managers/logger/logger.manager.d.ts.map +1 -0
  151. package/dist/managers/logger/logger.manager.types.d.ts +43 -0
  152. package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
  153. package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
  154. package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
  155. package/dist/managers/request/index.d.ts +5 -0
  156. package/dist/managers/request/index.d.ts.map +1 -0
  157. package/dist/managers/request/request.manager.d.ts +60 -0
  158. package/dist/managers/request/request.manager.d.ts.map +1 -0
  159. package/dist/managers/request/request.manager.events.d.ts +55 -0
  160. package/dist/managers/request/request.manager.events.d.ts.map +1 -0
  161. package/dist/managers/request/request.manager.types.d.ts +48 -0
  162. package/dist/managers/request/request.manager.types.d.ts.map +1 -0
  163. package/dist/managers/request/request.manager.utils.d.ts +30 -0
  164. package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
  165. package/dist/mocker/index.d.ts +3 -0
  166. package/dist/mocker/index.d.ts.map +1 -0
  167. package/dist/mocker/mocker.d.ts +3 -0
  168. package/dist/mocker/mocker.d.ts.map +1 -0
  169. package/dist/mocker/mocker.types.d.ts +25 -0
  170. package/dist/mocker/mocker.types.d.ts.map +1 -0
  171. package/dist/plugin/index.d.ts +3 -0
  172. package/dist/plugin/index.d.ts.map +1 -0
  173. package/dist/plugin/plugin.d.ts +55 -0
  174. package/dist/plugin/plugin.d.ts.map +1 -0
  175. package/dist/plugin/plugin.types.d.ts +122 -0
  176. package/dist/plugin/plugin.types.d.ts.map +1 -0
  177. package/dist/request/index.d.ts +5 -0
  178. package/dist/request/index.d.ts.map +1 -0
  179. package/dist/request/request.d.ts +195 -0
  180. package/dist/request/request.d.ts.map +1 -0
  181. package/dist/request/request.hooks.d.ts +19 -0
  182. package/dist/request/request.hooks.d.ts.map +1 -0
  183. package/dist/request/request.types.d.ts +240 -0
  184. package/dist/request/request.types.d.ts.map +1 -0
  185. package/dist/request/request.utils.d.ts +25 -0
  186. package/dist/request/request.utils.d.ts.map +1 -0
  187. package/dist/sdk/index.d.ts +2 -0
  188. package/dist/sdk/index.d.ts.map +1 -0
  189. package/dist/sdk/sdk.d.ts +69 -0
  190. package/dist/sdk/sdk.d.ts.map +1 -0
  191. package/dist/types/adapter.types.d.ts +22 -0
  192. package/dist/types/adapter.types.d.ts.map +1 -0
  193. package/dist/types/client.types.d.ts +4 -0
  194. package/dist/types/client.types.d.ts.map +1 -0
  195. package/dist/types/helpers.types.d.ts +17 -0
  196. package/dist/types/helpers.types.d.ts.map +1 -0
  197. package/dist/types/http.types.d.ts +3 -0
  198. package/dist/types/http.types.d.ts.map +1 -0
  199. package/dist/types/hydration.types.d.ts +15 -0
  200. package/dist/types/hydration.types.d.ts.map +1 -0
  201. package/dist/types/index.d.ts +7 -0
  202. package/dist/types/index.d.ts.map +1 -0
  203. package/dist/types/request.types.d.ts +34 -0
  204. package/dist/types/request.types.d.ts.map +1 -0
  205. package/dist/utils/emitter.utils.d.ts +13 -0
  206. package/dist/utils/emitter.utils.d.ts.map +1 -0
  207. package/dist/utils/index.d.ts +3 -0
  208. package/dist/utils/index.d.ts.map +1 -0
  209. package/dist/utils/uuid.utils.d.ts +3 -0
  210. package/dist/utils/uuid.utils.d.ts.map +1 -0
  211. package/package.json +10 -25
  212. package/src/adapter/adapter.bindings.ts +8 -12
  213. package/src/adapter/adapter.ts +8 -7
  214. package/src/adapter/adapter.types.ts +10 -6
  215. package/src/adapter/adapter.utils.ts +1 -1
  216. package/src/cache/cache.events.ts +4 -11
  217. package/src/cache/cache.ts +28 -22
  218. package/src/cache/cache.types.ts +8 -4
  219. package/src/cache/cache.utils.ts +3 -3
  220. package/src/client/client.create.ts +4 -3
  221. package/src/client/client.ts +44 -12
  222. package/src/client/client.types.ts +25 -3
  223. package/src/client/client.utils.ts +24 -4
  224. package/src/dispatcher/dispatcher.events.ts +3 -3
  225. package/src/dispatcher/dispatcher.ts +59 -28
  226. package/src/dispatcher/dispatcher.types.ts +19 -2
  227. package/src/dispatcher/dispatcher.utils.ts +5 -4
  228. package/src/http-adapter/http-adapter.constants.ts +1 -1
  229. package/src/http-adapter/http-adapter.fetch.ts +174 -0
  230. package/src/http-adapter/http-adapter.ts +3 -8
  231. package/src/http-adapter/http-adapter.types.ts +17 -9
  232. package/src/http-adapter/http-adapter.utils.ts +6 -9
  233. package/src/http-adapter/index.ts +1 -0
  234. package/src/managers/app/app.manager.constants.ts +2 -2
  235. package/src/managers/app/app.manager.events.ts +1 -1
  236. package/src/managers/app/app.manager.ts +2 -1
  237. package/src/managers/logger/logger.manager.constants.ts +1 -1
  238. package/src/managers/logger/logger.manager.ts +2 -1
  239. package/src/managers/logger/logger.manager.types.ts +2 -2
  240. package/src/managers/logger/logger.manager.utils.ts +2 -1
  241. package/src/managers/request/request.manager.events.ts +13 -11
  242. package/src/managers/request/request.manager.types.ts +6 -3
  243. package/src/mocker/mocker.ts +3 -2
  244. package/src/mocker/mocker.types.ts +1 -1
  245. package/src/plugin/plugin.ts +2 -2
  246. package/src/plugin/plugin.types.ts +9 -9
  247. package/src/request/index.ts +1 -0
  248. package/src/request/request.hooks.ts +87 -0
  249. package/src/request/request.ts +155 -77
  250. package/src/request/request.types.ts +54 -22
  251. package/src/request/request.utils.ts +86 -23
  252. package/src/sdk/sdk.ts +153 -8
  253. package/src/types/adapter.types.ts +1 -1
  254. package/src/types/client.types.ts +1 -1
  255. package/src/types/hydration.types.ts +3 -3
  256. package/src/types/request.types.ts +30 -20
  257. package/src/utils/uuid.utils.ts +1 -1
  258. package/tsconfig.base.json +1 -6
  259. package/tsconfig.json +1 -4
  260. package/vite.config.ts +45 -0
  261. package/.eslintrc.json +0 -11
  262. package/__tests__/global.d.ts +0 -1
  263. package/__tests__/jest.setup.ts +0 -13
  264. package/dist/browser/index.js +0 -3751
  265. package/dist/browser/index.js.map +0 -7
  266. package/dist/index.js +0 -4002
  267. package/dist/index.js.map +0 -7
  268. package/jest.config.ts +0 -6
  269. package/src/http-adapter/http-adapter.browser.ts +0 -102
  270. package/src/http-adapter/http-adapter.server.ts +0 -149
@@ -1,11 +1,13 @@
1
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
2
2
 
3
3
  import { Plugin } from "plugin";
4
- import { getAdapterBindings, ResponseType, getErrorMessage, ResponseErrorType } from "adapter";
4
+ import type { ResponseType, ResponseErrorType } from "adapter";
5
+ import { getAdapterBindings, getErrorMessage } from "adapter";
5
6
  import { testProgressSpy } from "../../shared";
6
7
  import { Client } from "client";
7
- import { RequestInstance } from "request";
8
- import { HttpAdapterOptionsType, HttpAdapterType, xhrExtra } from "http-adapter";
8
+ import type { RequestInstance } from "request";
9
+ import type { FetchAdapterOptionsType, HttpAdapterType } from "http-adapter";
10
+ import { xhrExtra } from "http-adapter";
9
11
 
10
12
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
11
13
 
@@ -35,13 +37,13 @@ describe("Adapter [ Bindings ]", () => {
35
37
  status: 400,
36
38
  extra: xhrExtra,
37
39
  };
38
- const requestConfig: HttpAdapterOptionsType = { responseType: "arraybuffer", timeout: 1000 };
40
+ const requestConfig: FetchAdapterOptionsType = { timeout: 1000 };
39
41
 
40
- const onTriggerSpy = jest.fn();
41
- const onErrorSpy = jest.fn();
42
- const onFinishedSpy = jest.fn();
43
- const onStartSpy = jest.fn();
44
- const onSuccessSpy = jest.fn();
42
+ const onTriggerSpy = vi.fn();
43
+ const onErrorSpy = vi.fn();
44
+ const onFinishedSpy = vi.fn();
45
+ const onStartSpy = vi.fn();
46
+ const onSuccessSpy = vi.fn();
45
47
 
46
48
  const initializeSetup = () => {
47
49
  const effect = new Plugin({
@@ -77,7 +79,7 @@ describe("Adapter [ Bindings ]", () => {
77
79
  beforeEach(() => {
78
80
  setup = initializeSetup();
79
81
  resetMocks();
80
- jest.resetAllMocks();
82
+ vi.resetAllMocks();
81
83
  });
82
84
 
83
85
  afterAll(() => {
@@ -140,7 +142,7 @@ describe("Adapter [ Bindings ]", () => {
140
142
  describe("given bindings abort methods being used", () => {
141
143
  describe("when using AbortController methods", () => {
142
144
  it("should create listener using createAbortListener", async () => {
143
- const resolveSpy = jest.fn();
145
+ const resolveSpy = vi.fn();
144
146
  const { createAbortListener, getAbortController } = await getAdapterBindings({
145
147
  request,
146
148
  requestId,
@@ -148,7 +150,7 @@ describe("Adapter [ Bindings ]", () => {
148
150
  onStartTime: () => null,
149
151
  internalErrorMapping: (error) => error,
150
152
  });
151
- const spy = jest.fn();
153
+ const spy = vi.fn();
152
154
  request.client.requestManager.addAbortController(request.abortKey, requestId);
153
155
  const controller = getAbortController();
154
156
  const unmount = createAbortListener({ status: 0, extra: xhrExtra, onAbort: spy });
@@ -170,8 +172,8 @@ describe("Adapter [ Bindings ]", () => {
170
172
  onStartTime: () => null,
171
173
  internalErrorMapping: (error) => error,
172
174
  });
173
- const spy = jest.fn();
174
- const resolveSpy = jest.fn();
175
+ const spy = vi.fn();
176
+ const resolveSpy = vi.fn();
175
177
  const controller = getAbortController();
176
178
  const unmount = createAbortListener({ status: 0, extra: xhrExtra, onAbort: spy });
177
179
  expect(spy).toHaveBeenCalledTimes(0);
@@ -199,7 +201,7 @@ describe("Adapter [ Bindings ]", () => {
199
201
  const abortController = request.client.requestManager.getAbortController(request.abortKey, requestId);
200
202
 
201
203
  abortController?.abort();
202
- const spy = jest.fn();
204
+ const spy = vi.fn();
203
205
 
204
206
  const { createAbortListener } = await getAdapterBindings({
205
207
  request,
@@ -224,7 +226,7 @@ describe("Adapter [ Bindings ]", () => {
224
226
  internalErrorMapping: (error) => error,
225
227
  });
226
228
 
227
- const resolveSpy = jest.fn();
229
+ const resolveSpy = vi.fn();
228
230
  const controller = getAbortController();
229
231
  const unmount = createAbortListener({ status: 0, extra: xhrExtra }); // No onAbort provided
230
232
 
@@ -265,7 +267,7 @@ describe("Adapter [ Bindings ]", () => {
265
267
  }>()({
266
268
  endpoint: "shared-endpoint/",
267
269
  });
268
- const spy = jest.fn();
270
+ const spy = vi.fn();
269
271
  const newData = {
270
272
  userId: 11,
271
273
  role: "ADMIN",
@@ -298,7 +300,7 @@ describe("Adapter [ Bindings ]", () => {
298
300
  }>()({
299
301
  endpoint: "shared-endpoint/",
300
302
  });
301
- const spy = jest.fn();
303
+ const spy = vi.fn();
302
304
  const newData = {
303
305
  userId: 11,
304
306
  role: "ADMIN",
@@ -342,7 +344,7 @@ describe("Adapter [ Bindings ]", () => {
342
344
  onStartTime: () => null,
343
345
  internalErrorMapping: (error) => error,
344
346
  });
345
- const spy = jest.fn();
347
+ const spy = vi.fn();
346
348
  const unmount = client.requestManager.events.onRequestStartByQueue(request.queryKey, spy);
347
349
  onRequestStart();
348
350
  unmount();
@@ -356,7 +358,7 @@ describe("Adapter [ Bindings ]", () => {
356
358
  onStartTime: () => null,
357
359
  internalErrorMapping: (error) => error,
358
360
  });
359
- const spy = jest.fn();
361
+ const spy = vi.fn();
360
362
  const unmount = client.requestManager.events.onUploadProgressByQueue(request.queryKey, spy);
361
363
  const startTimestamp = onRequestStart();
362
364
  unmount();
@@ -375,7 +377,7 @@ describe("Adapter [ Bindings ]", () => {
375
377
  loaded: 0,
376
378
  };
377
379
 
378
- const spy = jest.fn();
380
+ const spy = vi.fn();
379
381
  const unmount = client.requestManager.events.onUploadProgressByQueue(request.queryKey, spy);
380
382
  const startTimestamp = onRequestStart(progress);
381
383
  unmount();
@@ -396,7 +398,7 @@ describe("Adapter [ Bindings ]", () => {
396
398
  onStartTime: () => null,
397
399
  internalErrorMapping: (error) => error,
398
400
  });
399
- const spy = jest.fn();
401
+ const spy = vi.fn();
400
402
  const startTimestamp = onRequestStart();
401
403
  await sleep(30);
402
404
  const unmount = client.requestManager.events.onUploadProgressByQueue(request.queryKey, spy);
@@ -466,7 +468,7 @@ describe("Adapter [ Bindings ]", () => {
466
468
  onStartTime: () => null,
467
469
  internalErrorMapping: (error) => error,
468
470
  });
469
- const spy = jest.fn();
471
+ const spy = vi.fn();
470
472
  const startTimestamp = onRequestStart({ total: progress.total, loaded: 0 });
471
473
  await sleep(30);
472
474
  const unmount = client.requestManager.events.onUploadProgressByQueue(request.queryKey, spy);
@@ -491,7 +493,7 @@ describe("Adapter [ Bindings ]", () => {
491
493
  describe("when handling payload mapping", () => {
492
494
  it("should use adapter's unstable_payloadMapper if no request mapper exists", async () => {
493
495
  const mappedPayload = "mapped_by_adapter";
494
- const spy = jest.fn().mockReturnValue(mappedPayload);
496
+ const spy = vi.fn().mockReturnValue(mappedPayload);
495
497
 
496
498
  client.adapter.unstable_payloadMapper = spy;
497
499
 
@@ -511,8 +513,8 @@ describe("Adapter [ Bindings ]", () => {
511
513
  const adapterMappedPayload = "mapped_by_adapter";
512
514
  const requestMappedPayload = "mapped_by_request";
513
515
 
514
- const adapterSpy = jest.fn().mockReturnValue(adapterMappedPayload);
515
- const requestSpy = jest.fn().mockReturnValue(requestMappedPayload);
516
+ const adapterSpy = vi.fn().mockReturnValue(adapterMappedPayload);
517
+ const requestSpy = vi.fn().mockReturnValue(requestMappedPayload);
516
518
 
517
519
  client.adapter.unstable_payloadMapper = adapterSpy;
518
520
  request.unstable_payloadMapper = requestSpy;
@@ -575,7 +577,7 @@ describe("Adapter [ Bindings ]", () => {
575
577
  onStartTime: () => null,
576
578
  internalErrorMapping: (error) => error,
577
579
  });
578
- const spy = jest.fn();
580
+ const spy = vi.fn();
579
581
  const unmount = client.requestManager.events.onResponseStartByQueue(request.queryKey, spy);
580
582
  onResponseStart();
581
583
  unmount();
@@ -589,7 +591,7 @@ describe("Adapter [ Bindings ]", () => {
589
591
  onStartTime: () => null,
590
592
  internalErrorMapping: (error) => error,
591
593
  });
592
- const spy = jest.fn();
594
+ const spy = vi.fn();
593
595
  const unmount = client.requestManager.events.onDownloadProgressByQueue(request.queryKey, spy);
594
596
  const startTimestamp = onResponseStart();
595
597
  unmount();
@@ -608,7 +610,7 @@ describe("Adapter [ Bindings ]", () => {
608
610
  loaded: 0,
609
611
  };
610
612
 
611
- const spy = jest.fn();
613
+ const spy = vi.fn();
612
614
  const unmount = client.requestManager.events.onDownloadProgressByQueue(request.queryKey, spy);
613
615
  const startTimestamp = onResponseStart(progress);
614
616
  unmount();
@@ -652,7 +654,7 @@ describe("Adapter [ Bindings ]", () => {
652
654
  onStartTime: () => null,
653
655
  internalErrorMapping: (error) => error,
654
656
  });
655
- const spy = jest.fn();
657
+ const spy = vi.fn();
656
658
  const startTimestamp = onResponseStart();
657
659
  await sleep(30);
658
660
  const unmount = client.requestManager.events.onDownloadProgressByQueue(request.queryKey, spy);
@@ -689,7 +691,7 @@ describe("Adapter [ Bindings ]", () => {
689
691
  onStartTime: () => null,
690
692
  internalErrorMapping: (error) => error,
691
693
  });
692
- const spy = jest.fn();
694
+ const spy = vi.fn();
693
695
  const startTimestamp = onResponseStart({ total: progress.total, loaded: 0 });
694
696
  await sleep(30);
695
697
  const unmount = client.requestManager.events.onDownloadProgressByQueue(request.queryKey, spy);
@@ -981,7 +983,7 @@ describe("Adapter [ Bindings ]", () => {
981
983
  internalErrorMapping: (error) => error,
982
984
  });
983
985
 
984
- const spy = jest.fn();
986
+ const spy = vi.fn();
985
987
  const unmount = client.requestManager.events.onUploadProgressByQueue(request.queryKey, spy);
986
988
 
987
989
  // First call to set previousRequestTotal to 100
@@ -1005,7 +1007,7 @@ describe("Adapter [ Bindings ]", () => {
1005
1007
  internalErrorMapping: (error) => error,
1006
1008
  });
1007
1009
 
1008
- const spy = jest.fn();
1010
+ const spy = vi.fn();
1009
1011
  const unmount = client.requestManager.events.onDownloadProgressByQueue(request.queryKey, spy);
1010
1012
 
1011
1013
  // First call to set previousResponseTotal to 100
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @jest-environment jsdom
2
+ * @vitest-environment jsdom
3
3
  */
4
4
  import { createHttpMockingServer } from "@hyper-fetch/testing";
5
5
 
@@ -25,7 +25,7 @@ describe("Http Adapter [ Browser ]", () => {
25
25
 
26
26
  request.client.requestManager.addAbortController(request.abortKey, requestId);
27
27
  resetMocks();
28
- jest.resetAllMocks();
28
+ vi.resetAllMocks();
29
29
  });
30
30
 
31
31
  afterAll(() => {
@@ -77,27 +77,12 @@ describe("Http Adapter [ Browser ]", () => {
77
77
  expect(error.message).toEqual(getErrorMessage("timeout").message);
78
78
  });
79
79
 
80
- it("should use GET as default method when no method is specified", async () => {
81
- const mockSpy = jest.spyOn(XMLHttpRequest.prototype, "open");
80
+ it("should use GET as default method and return data", async () => {
82
81
  const data = mockRequest(request, { data: { response: [] } });
83
82
 
84
- const { data: response } = await HttpAdapter().initialize(client).fetch(request, requestId);
83
+ const { data: response, status } = await HttpAdapter().initialize(client).fetch(request, requestId);
85
84
 
86
- expect(mockSpy).toHaveBeenCalledWith("GET", "https://shared-base-url/shared-endpoint", true);
87
85
  expect(response).toStrictEqual(data);
86
+ expect(status).toBe(200);
88
87
  });
89
-
90
- // it("should not throw when XMLHttpRequest is not available on window", async () => {
91
- // const data = mockRequest(request, { delay: 20 });
92
- // const xml = window.XMLHttpRequest;
93
- // window.XMLHttpRequest = undefined as any;
94
-
95
- // const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
96
-
97
- // expect(response).toStrictEqual(data);
98
- // expect(status).toBe(200);
99
- // expect(error).toBe(null);
100
- // expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
101
- // window.XMLHttpRequest = xml;
102
- // });
103
88
  });
@@ -1,7 +1,7 @@
1
1
  /**
2
- * @jest-environment node
2
+ * @vitest-environment node
3
3
  */
4
- import http from "http";
4
+ import type { Mock } from "vitest";
5
5
  import { createHttpMockingServer } from "@hyper-fetch/testing";
6
6
 
7
7
  import { getErrorMessage } from "adapter";
@@ -10,15 +10,12 @@ import { HttpAdapter } from "http-adapter";
10
10
 
11
11
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
12
12
 
13
- describe("Http Adapter [ Server ]", () => {
13
+ describe("Http Adapter [ Fetch ]", () => {
14
14
  const requestId = "test";
15
15
  const abortKey = "abort-key";
16
- const requestCopy = http.request;
17
16
 
18
17
  let client = new Client({ url: "http://shared-base-url" });
19
- let clientHttps = new Client({ url: "https://shared-base-url" });
20
18
  let request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint", abortKey });
21
- let requestHttps = clientHttps.createRequest()({ endpoint: "/shared-endpoint", abortKey });
22
19
 
23
20
  beforeAll(() => {
24
21
  startServer();
@@ -26,20 +23,16 @@ describe("Http Adapter [ Server ]", () => {
26
23
 
27
24
  beforeEach(() => {
28
25
  client = new Client({ url: "http://shared-base-url" });
29
- clientHttps = new Client({ url: "https://shared-base-url" });
30
26
  client.appManager.isBrowser = false;
31
- clientHttps.appManager.isBrowser = false;
32
27
 
33
28
  request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint", abortKey });
34
- requestHttps = clientHttps.createRequest()({ endpoint: "/shared-endpoint", abortKey });
35
29
 
36
30
  client.requestManager.addAbortController(abortKey, requestId);
37
- clientHttps.requestManager.addAbortController(abortKey, requestId);
38
31
 
39
32
  resetMocks();
40
- jest.resetAllMocks();
41
- jest.clearAllMocks();
42
- jest.restoreAllMocks();
33
+ vi.resetAllMocks();
34
+ vi.clearAllMocks();
35
+ vi.restoreAllMocks();
43
36
  });
44
37
 
45
38
  afterAll(() => {
@@ -52,11 +45,6 @@ describe("Http Adapter [ Server ]", () => {
52
45
  await expect(() => HttpAdapter().initialize(client).fetch(request, requestId)).not.toThrow();
53
46
  });
54
47
 
55
- it("should pick https module", async () => {
56
- mockRequest(requestHttps);
57
- await expect(() => HttpAdapter().initialize(client).fetch(requestHttps, requestId)).not.toThrow();
58
- });
59
-
60
48
  it("should make a request and return success data with status", async () => {
61
49
  const data = mockRequest(request, { data: { response: [] } });
62
50
 
@@ -130,48 +118,252 @@ describe("Http Adapter [ Server ]", () => {
130
118
  expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
131
119
  });
132
120
 
133
- it("should allow to calculate payload size", async () => {
134
- let receivedOptions: any;
121
+ it("should send payload with object data", async () => {
135
122
  const mutation = client.createRequest<{ response: any; payload: any }>()({
136
123
  endpoint: "/shared-endpoint",
137
124
  method: "POST",
138
125
  });
139
-
140
- jest.spyOn(http, "request").mockImplementation((_, options, callback) => {
141
- receivedOptions = options;
142
- return requestCopy(options, callback);
143
- });
126
+ client.requestManager.addAbortController(mutation.abortKey, requestId);
144
127
  mockRequest(mutation);
145
128
 
146
- await mutation.send({
129
+ const { error, status } = await mutation.send({
147
130
  payload: {
148
131
  username: "Kacper",
149
132
  password: "Kacper1234",
150
133
  },
151
134
  });
152
135
 
153
- expect(receivedOptions.headers["Content-Length"]).toBeGreaterThan(0);
136
+ expect(error).toBeNull();
137
+ expect(status).toBe(200);
154
138
  });
155
139
 
156
- it("should allow to calculate Buffer size", async () => {
157
- let receivedOptions: any;
140
+ it("should send payload with string data", async () => {
158
141
  const mutation = client.createRequest<{ response: any; payload: any }>()({
159
142
  endpoint: "/shared-endpoint",
160
143
  method: "POST",
161
144
  });
145
+ client.requestManager.addAbortController(mutation.abortKey, requestId);
146
+ mockRequest(mutation);
162
147
 
163
- jest.spyOn(http, "request").mockImplementation((_, options, callback) => {
164
- receivedOptions = options;
165
- return requestCopy(options, callback);
148
+ const { error, status } = await mutation.send({
149
+ payload: "raw-string-payload",
166
150
  });
167
- mockRequest(mutation);
168
151
 
169
- const buffer = Buffer.from("test");
152
+ expect(error).toBeNull();
153
+ expect(status).toBe(200);
154
+ });
155
+
156
+ describe("When streaming mode is enabled", () => {
157
+ it("should return response.body for successful streaming response", async () => {
158
+ mockRequest(request, { data: { response: "stream" } });
159
+
160
+ const streamRequest = request.setOptions({ streaming: true });
170
161
 
171
- await mutation.send({
172
- payload: buffer as any,
162
+ const result = await HttpAdapter().initialize(client).fetch(streamRequest, requestId);
163
+
164
+ expect(result.status).toBe(200);
165
+ expect(result.data).toBeDefined();
166
+ });
167
+
168
+ it("should return error for failed streaming response", async () => {
169
+ mockRequest(request, { status: 400, data: { error: "stream-fail" } });
170
+
171
+ const streamRequest = request.setOptions({ streaming: true });
172
+
173
+ const result = await HttpAdapter().initialize(client).fetch(streamRequest, requestId);
174
+
175
+ expect(result.status).toBe(400);
176
+ expect(result.data).toBe(null);
177
+ expect(result.error).toBeDefined();
173
178
  });
179
+ });
180
+
181
+ describe("When response.body has no getReader", () => {
182
+ it("should fall back to response.text()", async () => {
183
+ const originalFetch = globalThis.fetch;
184
+ globalThis.fetch = vi.fn().mockResolvedValue({
185
+ ok: true,
186
+ status: 200,
187
+ headers: new Headers({ "content-type": "application/json" }),
188
+ body: null,
189
+ text: vi.fn().mockResolvedValue(JSON.stringify({ fallback: true })),
190
+ });
191
+
192
+ try {
193
+ const adapter = HttpAdapter().initialize(client);
194
+ const result = await adapter.fetch(request, requestId);
195
+
196
+ expect(result.status).toBe(200);
197
+ expect(result.data).toStrictEqual({ fallback: true });
198
+ } finally {
199
+ globalThis.fetch = originalFetch;
200
+ }
201
+ });
202
+ });
174
203
 
175
- expect(receivedOptions.headers["Content-Length"]).toBeGreaterThan(0);
204
+ describe("When non-timeout, non-abort error occurs", () => {
205
+ it("should call onError with the error", async () => {
206
+ const originalFetch = globalThis.fetch;
207
+ const testError = new Error("Network failure");
208
+ globalThis.fetch = vi.fn().mockRejectedValue(testError);
209
+
210
+ try {
211
+ const adapter = HttpAdapter().initialize(client);
212
+ const result = await adapter.fetch(request, requestId);
213
+
214
+ expect(result.data).toBe(null);
215
+ expect(result.error).toBe(testError);
216
+ expect(result.status).toBe(0);
217
+ } finally {
218
+ globalThis.fetch = originalFetch;
219
+ }
220
+ });
221
+ });
222
+
223
+ describe("When sending non-string payload with POST method", () => {
224
+ it("should handle non-string body without triggering string upload progress", async () => {
225
+ const originalFetch = globalThis.fetch;
226
+ globalThis.fetch = vi.fn().mockResolvedValue({
227
+ ok: true,
228
+ status: 200,
229
+ headers: new Headers({ "content-type": "application/octet-stream" }),
230
+ body: null,
231
+ text: vi.fn().mockResolvedValue("ok"),
232
+ });
233
+
234
+ try {
235
+ const mutation = client.createRequest<{ response: any; payload: any }>()({
236
+ endpoint: "/shared-endpoint",
237
+ method: "POST",
238
+ abortKey,
239
+ });
240
+
241
+ const bufPayload = Buffer.from("binary data");
242
+ client.adapter.setPayloadMapper(() => bufPayload as any);
243
+ const req = mutation.setPayload("ignored");
244
+
245
+ const result = await client.adapter.fetch(req, requestId);
246
+
247
+ const fetchCall = (globalThis.fetch as unknown as Mock).mock.calls[0][1];
248
+ expect(fetchCall.body).toBe(bufPayload);
249
+ expect(result.status).toBe(200);
250
+ } finally {
251
+ globalThis.fetch = originalFetch;
252
+ }
253
+ });
254
+ });
255
+
256
+ describe("When response has content-length header in streaming reader path", () => {
257
+ it("should use content-length for progress total when available", async () => {
258
+ const originalFetch = globalThis.fetch;
259
+ const bodyContent = JSON.stringify({ data: "test" });
260
+ const encoder = new TextEncoder();
261
+ const encoded = encoder.encode(bodyContent);
262
+
263
+ const mockReader = {
264
+ read: vi
265
+ .fn()
266
+ .mockResolvedValueOnce({ done: false, value: encoded })
267
+ .mockResolvedValueOnce({ done: true, value: undefined }),
268
+ };
269
+
270
+ globalThis.fetch = vi.fn().mockResolvedValue({
271
+ ok: true,
272
+ status: 200,
273
+ headers: new Headers({
274
+ "content-type": "application/json",
275
+ "content-length": String(encoded.length),
276
+ }),
277
+ body: { getReader: () => mockReader },
278
+ });
279
+
280
+ try {
281
+ const adapter = HttpAdapter().initialize(client);
282
+ const result = await adapter.fetch(request, requestId);
283
+
284
+ expect(result.status).toBe(200);
285
+ expect(result.data).toStrictEqual({ data: "test" });
286
+ } finally {
287
+ globalThis.fetch = originalFetch;
288
+ }
289
+ });
290
+
291
+ it("should use receivedLength as total when content-length is missing", async () => {
292
+ const originalFetch = globalThis.fetch;
293
+ const bodyContent = JSON.stringify({ noLength: true });
294
+ const encoder = new TextEncoder();
295
+ const encoded = encoder.encode(bodyContent);
296
+
297
+ const mockReader = {
298
+ read: vi
299
+ .fn()
300
+ .mockResolvedValueOnce({ done: false, value: encoded })
301
+ .mockResolvedValueOnce({ done: true, value: undefined }),
302
+ };
303
+
304
+ globalThis.fetch = vi.fn().mockResolvedValue({
305
+ ok: true,
306
+ status: 200,
307
+ headers: new Headers({ "content-type": "application/json" }),
308
+ body: { getReader: () => mockReader },
309
+ });
310
+
311
+ try {
312
+ const adapter = HttpAdapter().initialize(client);
313
+ const result = await adapter.fetch(request, requestId);
314
+
315
+ expect(result.status).toBe(200);
316
+ expect(result.data).toStrictEqual({ noLength: true });
317
+ } finally {
318
+ globalThis.fetch = originalFetch;
319
+ }
320
+ });
321
+ });
322
+
323
+ describe("When error occurs without timeout set", () => {
324
+ it("should handle catch block when timeoutId is not set", async () => {
325
+ const originalFetch = globalThis.fetch;
326
+ const testError = new Error("Connection refused");
327
+ globalThis.fetch = vi.fn().mockRejectedValue(testError);
328
+
329
+ try {
330
+ const noTimeoutRequest = request.setOptions({ timeout: 0 });
331
+
332
+ const adapter = HttpAdapter().initialize(client);
333
+ const result = await adapter.fetch(noTimeoutRequest, requestId);
334
+
335
+ expect(result.data).toBe(null);
336
+ expect(result.error).toBe(testError);
337
+ expect(result.status).toBe(0);
338
+ } finally {
339
+ globalThis.fetch = originalFetch;
340
+ }
341
+ });
342
+ });
343
+
344
+ describe("When timeoutMs is 0 or negative", () => {
345
+ it("should skip timeout setup when timeout is 0 and still succeed", async () => {
346
+ const data = mockRequest(request, { data: { response: [] } });
347
+
348
+ const zeroTimeoutRequest = request.setOptions({ timeout: 0 });
349
+
350
+ const result = await zeroTimeoutRequest.send();
351
+
352
+ expect(result.status).toBe(200);
353
+ expect(result.error).toBe(null);
354
+ expect(result.data).toStrictEqual(data);
355
+ });
356
+
357
+ it("should skip timeout setup when timeout is negative and still succeed", async () => {
358
+ const data = mockRequest(request, { data: { response: [] } });
359
+
360
+ const negativeTimeoutRequest = request.setOptions({ timeout: -1 });
361
+
362
+ const result = await negativeTimeoutRequest.send();
363
+
364
+ expect(result.status).toBe(200);
365
+ expect(result.error).toBe(null);
366
+ expect(result.data).toStrictEqual(data);
367
+ });
176
368
  });
177
369
  });