@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,88 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
5
+
6
+ import { getErrorMessage } from "adapter";
7
+ import { Client } from "client";
8
+ import { HttpAdapter } from "http-adapter";
9
+
10
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
11
+
12
+ describe("Http Adapter [ Browser ]", () => {
13
+ const requestId = "test";
14
+
15
+ let client = new Client({ url: "https://shared-base-url" });
16
+ let request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" });
17
+
18
+ beforeAll(() => {
19
+ startServer();
20
+ });
21
+
22
+ beforeEach(() => {
23
+ client = new Client({ url: "https://shared-base-url" });
24
+ request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" });
25
+
26
+ request.client.requestManager.addAbortController(request.abortKey, requestId);
27
+ resetMocks();
28
+ vi.resetAllMocks();
29
+ });
30
+
31
+ afterAll(() => {
32
+ stopServer();
33
+ });
34
+
35
+ it("should make a request and return success data with status", async () => {
36
+ const data = mockRequest(request, { data: { response: [] } });
37
+
38
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
39
+
40
+ expect(response).toStrictEqual(data);
41
+ expect(status).toBe(200);
42
+ expect(error).toBe(null);
43
+ expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "15" } });
44
+ });
45
+
46
+ it("should make a request and return error data with status", async () => {
47
+ const data = mockRequest(request, { status: 400 });
48
+
49
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
50
+
51
+ expect(response).toBe(null);
52
+ expect(status).toBe(400);
53
+ expect(error).toStrictEqual(data);
54
+ expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "19" } });
55
+ });
56
+
57
+ it("should allow to cancel request and return error", async () => {
58
+ mockRequest(request, { delay: 5 });
59
+
60
+ setTimeout(() => {
61
+ request.abort();
62
+ }, 2);
63
+
64
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(request, requestId);
65
+
66
+ expect(response).toBe(null);
67
+ expect(error.message).toEqual(getErrorMessage("abort").message);
68
+ });
69
+
70
+ it("should return timeout error when request takes too long", async () => {
71
+ const timeoutRequest = request.setOptions({ timeout: 5 });
72
+ mockRequest(timeoutRequest, { delay: 20 });
73
+
74
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(timeoutRequest, requestId);
75
+
76
+ expect(response).toBe(null);
77
+ expect(error.message).toEqual(getErrorMessage("timeout").message);
78
+ });
79
+
80
+ it("should use GET as default method and return data", async () => {
81
+ const data = mockRequest(request, { data: { response: [] } });
82
+
83
+ const { data: response, status } = await HttpAdapter().initialize(client).fetch(request, requestId);
84
+
85
+ expect(response).toStrictEqual(data);
86
+ expect(status).toBe(200);
87
+ });
88
+ });
@@ -0,0 +1,369 @@
1
+ /**
2
+ * @vitest-environment node
3
+ */
4
+ import type { Mock } from "vitest";
5
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
6
+
7
+ import { getErrorMessage } from "adapter";
8
+ import { Client } from "client";
9
+ import { HttpAdapter } from "http-adapter";
10
+
11
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
12
+
13
+ describe("Http Adapter [ Fetch ]", () => {
14
+ const requestId = "test";
15
+ const abortKey = "abort-key";
16
+
17
+ let client = new Client({ url: "http://shared-base-url" });
18
+ let request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint", abortKey });
19
+
20
+ beforeAll(() => {
21
+ startServer();
22
+ });
23
+
24
+ beforeEach(() => {
25
+ client = new Client({ url: "http://shared-base-url" });
26
+ client.appManager.isBrowser = false;
27
+
28
+ request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint", abortKey });
29
+
30
+ client.requestManager.addAbortController(abortKey, requestId);
31
+
32
+ resetMocks();
33
+ vi.resetAllMocks();
34
+ vi.clearAllMocks();
35
+ vi.restoreAllMocks();
36
+ });
37
+
38
+ afterAll(() => {
39
+ stopServer();
40
+ });
41
+
42
+ it("should pick correct adapter and not throw", async () => {
43
+ mockRequest(request);
44
+ client.appManager.isBrowser = true;
45
+ await expect(() => HttpAdapter().initialize(client).fetch(request, requestId)).not.toThrow();
46
+ });
47
+
48
+ it("should make a request and return success data with status", async () => {
49
+ const data = mockRequest(request, { data: { response: [] } });
50
+
51
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
52
+
53
+ expect(response).toStrictEqual(data);
54
+ expect(status).toBe(200);
55
+ expect(error).toBe(null);
56
+ expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "15" } });
57
+ });
58
+
59
+ it("should make a request and return error data with status", async () => {
60
+ const data = mockRequest(request, { status: 400 });
61
+
62
+ const { data: response, error, status, extra } = await HttpAdapter().initialize(client).fetch(request, requestId);
63
+
64
+ expect(response).toBe(null);
65
+ expect(status).toBe(400);
66
+ expect(error).toStrictEqual(data);
67
+ expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "19" } });
68
+ });
69
+
70
+ it("should allow to cancel request and return error", async () => {
71
+ mockRequest(request, { delay: 5 });
72
+
73
+ const id = setTimeout(() => {
74
+ request.abort();
75
+ }, 2);
76
+
77
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(request, requestId);
78
+
79
+ expect(response).toBe(null);
80
+ expect(error.message).toEqual(getErrorMessage("abort").message);
81
+
82
+ clearTimeout(id);
83
+ });
84
+
85
+ it("should return timeout error when request takes too long", async () => {
86
+ const timeoutRequest = request.setOptions({ timeout: 10 });
87
+ mockRequest(timeoutRequest, { delay: 20 });
88
+
89
+ const { data: response, error } = await HttpAdapter().initialize(client).fetch(timeoutRequest, requestId);
90
+
91
+ expect(response).toBe(null);
92
+ expect(error.message).toEqual(getErrorMessage("timeout").message);
93
+ });
94
+
95
+ it("should allow to make post request with json data", async () => {
96
+ const payload = {
97
+ testData: "123",
98
+ };
99
+ const postRequest = client
100
+ .createRequest<{
101
+ response: null;
102
+ payload: { testData: string };
103
+ }>()({ endpoint: "shared-endpoint", method: "POST" })
104
+ .setPayload(payload);
105
+ client.requestManager.addAbortController(postRequest.abortKey, requestId);
106
+ const mock = mockRequest(postRequest);
107
+
108
+ const {
109
+ data: response,
110
+ error,
111
+ status,
112
+ extra,
113
+ } = await HttpAdapter().initialize(client).fetch(postRequest, requestId);
114
+
115
+ expect(response).toEqual(mock);
116
+ expect(error).toBeNull();
117
+ expect(status).toEqual(200);
118
+ expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
119
+ });
120
+
121
+ it("should send payload with object data", async () => {
122
+ const mutation = client.createRequest<{ response: any; payload: any }>()({
123
+ endpoint: "/shared-endpoint",
124
+ method: "POST",
125
+ });
126
+ client.requestManager.addAbortController(mutation.abortKey, requestId);
127
+ mockRequest(mutation);
128
+
129
+ const { error, status } = await mutation.send({
130
+ payload: {
131
+ username: "Kacper",
132
+ password: "Kacper1234",
133
+ },
134
+ });
135
+
136
+ expect(error).toBeNull();
137
+ expect(status).toBe(200);
138
+ });
139
+
140
+ it("should send payload with string data", async () => {
141
+ const mutation = client.createRequest<{ response: any; payload: any }>()({
142
+ endpoint: "/shared-endpoint",
143
+ method: "POST",
144
+ });
145
+ client.requestManager.addAbortController(mutation.abortKey, requestId);
146
+ mockRequest(mutation);
147
+
148
+ const { error, status } = await mutation.send({
149
+ payload: "raw-string-payload",
150
+ });
151
+
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 });
161
+
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();
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
+ });
203
+
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
+ });
368
+ });
369
+ });
@@ -0,0 +1,165 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { createClient } from "client";
4
+ import { getAdapterHeaders, getAdapterPayload, getErrorMessage } from "adapter";
5
+ import { parseResponse, parseErrorResponse, handleResponse, getResponseHeaders } from "http-adapter";
6
+
7
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
8
+
9
+ describe("Adapter [ Utils ]", () => {
10
+ beforeAll(() => {
11
+ startServer();
12
+ });
13
+
14
+ beforeEach(() => {
15
+ resetMocks();
16
+ });
17
+
18
+ afterAll(() => {
19
+ stopServer();
20
+ });
21
+
22
+ describe("When using getAdapterHeaders util", () => {
23
+ it("should allow to stringify payload", async () => {
24
+ const request = createClient({ url: "" }).createRequest<{ response: any; payload: FormData }>()({
25
+ endpoint: "shared-base-endpoint",
26
+ });
27
+
28
+ expect(() => getAdapterHeaders(request)).not.toThrow();
29
+ });
30
+ });
31
+
32
+ describe("When using getAdapterPayload util", () => {
33
+ it("should allow to stringify payload", async () => {
34
+ const data = { data: [] };
35
+
36
+ expect(() => getAdapterPayload(data)).not.toThrow();
37
+ });
38
+ });
39
+ describe("When getErrorMessage util get triggered", () => {
40
+ it("should return abort error", async () => {
41
+ const error = getErrorMessage("abort");
42
+ expect(error.message).toBe("Request aborted");
43
+ });
44
+ it("should return timeout error", async () => {
45
+ const error = getErrorMessage("timeout");
46
+ expect(error.message).toBe("Request timeout");
47
+ });
48
+ it("should return unexpected error", async () => {
49
+ const error = getErrorMessage();
50
+ expect(error.message).toBe("Unexpected error");
51
+ const error2 = getErrorMessage("unexpected");
52
+ expect(error2.message).toBe("Unexpected error");
53
+ });
54
+ it("should return deleted error", async () => {
55
+ const error = getErrorMessage("deleted");
56
+ expect(error.message).toBe("Request deleted");
57
+ });
58
+ it("should return processing error", async () => {
59
+ const error = getErrorMessage("processing");
60
+ expect(error.message).toBe("Request processing error");
61
+ });
62
+ });
63
+
64
+ describe("When getResponseHeaders util get triggered", () => {
65
+ it("should return headers", async () => {
66
+ const headersString = "Content-Type: application/json\nAuthorization: Bearer token";
67
+ const headers = getResponseHeaders(headersString);
68
+ expect(headers).toEqual({
69
+ "Content-Type": "application/json",
70
+ Authorization: "Bearer token",
71
+ });
72
+ });
73
+
74
+ it("should return empty headers", async () => {
75
+ const headersString = "";
76
+ const headers = getResponseHeaders(headersString);
77
+ expect(headers).toEqual({});
78
+ });
79
+
80
+ it("should handle header values containing colons", async () => {
81
+ const headersString = "Content-Type: application/json\nData: key: value: with: colons";
82
+ const headers = getResponseHeaders(headersString);
83
+ expect(headers).toEqual({
84
+ "Content-Type": "application/json",
85
+ Data: "key: value: with: colons",
86
+ });
87
+ });
88
+
89
+ it("should ignore malformed header lines", async () => {
90
+ const headersString =
91
+ "Content-Type: application/json\nmalformed-line-without-separator\nAuthorization: Bearer token";
92
+ const headers = getResponseHeaders(headersString);
93
+ expect(headers).toEqual({
94
+ "Content-Type": "application/json",
95
+ Authorization: "Bearer token",
96
+ "malformed-line-without-separator": "",
97
+ });
98
+ });
99
+ });
100
+
101
+ describe("When parseResponse util get triggered", () => {
102
+ it("should return parsed response json", async () => {
103
+ const response = { something: 123 };
104
+ const parsed = parseResponse(JSON.stringify(response));
105
+ expect(parsed).toEqual(response);
106
+ });
107
+ it("should return invalid original on parsing error", async () => {
108
+ const invalidResponse = () => null;
109
+ const parsed = parseResponse(invalidResponse);
110
+ expect(parsed).toBe(invalidResponse);
111
+ });
112
+ });
113
+
114
+ describe("When parseErrorResponse util get triggered", () => {
115
+ it("should return parsed error json", async () => {
116
+ const response = { something: 123 };
117
+ const parsed = parseErrorResponse(JSON.stringify(response));
118
+ expect(parsed).toEqual(response);
119
+ });
120
+ it("should return unexpected error when no response is passed", async () => {
121
+ const parsed = parseErrorResponse(null);
122
+ expect(parsed?.message).toBe("Unexpected error");
123
+ });
124
+ });
125
+
126
+ describe("When handleResponse util gets triggered", () => {
127
+ it("should return Buffer for arraybuffer response type", () => {
128
+ const chunks = [Buffer.from("Hello"), Buffer.from("World")];
129
+ const result = handleResponse(chunks, "arraybuffer", "utf-8");
130
+
131
+ expect(Buffer.isBuffer(result)).toBe(true);
132
+ expect(result.toString()).toBe("HelloWorld");
133
+ });
134
+
135
+ it("should parse JSON response type", () => {
136
+ const data = { message: "test" };
137
+ const chunks = [Buffer.from(JSON.stringify(data))];
138
+ const result = handleResponse(chunks, "json", "utf-8");
139
+
140
+ expect(result).toEqual(data);
141
+ });
142
+
143
+ it("should return string for default response type", () => {
144
+ const chunks = [Buffer.from("Hello"), Buffer.from("World")];
145
+ const result = handleResponse(chunks, "text", "utf-8");
146
+
147
+ expect(typeof result).toBe("string");
148
+ expect(result).toBe("HelloWorld");
149
+ });
150
+
151
+ it("should handle empty chunks", () => {
152
+ const chunks: Buffer[] = [];
153
+ const result = handleResponse(chunks, "text", "utf-8");
154
+
155
+ expect(result).toBe("");
156
+ });
157
+
158
+ it("should handle invalid JSON for json response type", () => {
159
+ const chunks = [Buffer.from("invalid json")];
160
+ const result = handleResponse(chunks, "json", "utf-8");
161
+
162
+ expect(result).toBe("invalid json");
163
+ });
164
+ });
165
+ });