@hyper-fetch/core 8.2.0 → 8.2.3

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 (158) hide show
  1. package/README.md +4 -4
  2. package/__tests__/e2e/e2e.advanced.spec.ts +7 -7
  3. package/__tests__/features/adapter/adapter.base.spec.ts +2 -1
  4. package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -4
  5. package/__tests__/features/adapter/adapter.browser.spec.ts +0 -1
  6. package/__tests__/features/adapter/adapter.server.spec.ts +4 -5
  7. package/__tests__/features/adapter/adapter.utils.spec.ts +6 -7
  8. package/__tests__/features/cache/cache.base.spec.ts +7 -7
  9. package/__tests__/features/cache/cache.events.spec.ts +15 -15
  10. package/__tests__/features/cache/cache.garbage-collector.spec.ts +18 -18
  11. package/__tests__/features/cache/cache.lazy-storage.spec.ts +8 -8
  12. package/__tests__/features/cache/cache.modes.spec.ts +7 -6
  13. package/__tests__/features/cache/cache.scoping.spec.ts +12 -8
  14. package/__tests__/features/cache/cache.typescript.spec.ts +2 -2
  15. package/__tests__/features/cache/cache.utils.spec.ts +14 -14
  16. package/__tests__/features/client/client.auth.spec.ts +0 -1
  17. package/__tests__/features/client/client.base.spec.ts +2 -3
  18. package/__tests__/features/client/client.from-json.spec.ts +0 -1
  19. package/__tests__/features/client/client.hydration.spec.ts +5 -5
  20. package/__tests__/features/client/client.interceptor.spec.ts +3 -3
  21. package/__tests__/features/client/client.methods.spec.ts +5 -5
  22. package/__tests__/features/client/client.middleware.spec.ts +2 -2
  23. package/__tests__/features/client/client.utils.spec.ts +16 -15
  24. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +3 -3
  25. package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +4 -5
  26. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +4 -4
  27. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +13 -13
  28. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +10 -10
  29. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +35 -8
  30. package/__tests__/features/managers/app/app.manager.base.spec.ts +2 -3
  31. package/__tests__/features/managers/logger/logger.base.spec.ts +1 -2
  32. package/__tests__/features/managers/request/request.manager.base.spec.ts +8 -9
  33. package/__tests__/features/managers/request/request.manager.events.spec.ts +1 -2
  34. package/__tests__/features/mocker/mocker.base.spec.ts +6 -6
  35. package/__tests__/features/plugin/plugin.base.spec.ts +1 -2
  36. package/__tests__/features/request/request.cloning.spec.ts +0 -1
  37. package/__tests__/features/request/request.deduplicate-defaults.spec.ts +79 -0
  38. package/__tests__/features/request/request.retry-on-error.spec.ts +1 -2
  39. package/__tests__/features/request/request.sending.spec.ts +73 -24
  40. package/__tests__/features/request/request.setters.spec.ts +4 -3
  41. package/__tests__/features/request/request.typescript.spec.ts +0 -1
  42. package/__tests__/features/request/request.utils.spec.ts +1 -0
  43. package/__tests__/features/sdk/sdk.base.spec.ts +0 -1
  44. package/__tests__/features/sdk/sdk.request-fields.spec.ts +80 -0
  45. package/__tests__/features/sdk/sdk.types.spec.ts +3 -4
  46. package/__tests__/shared/callbacks.shared.ts +1 -1
  47. package/__tests__/tsconfig.json +4 -17
  48. package/__tests__/utils/adapter.utils.ts +1 -2
  49. package/__tests__/utils/callbacks.utils.ts +1 -2
  50. package/__tests__/utils/dispatcher.utils.ts +1 -1
  51. package/__tests__/vitest.setup.ts +1 -1
  52. package/dist/adapter/adapter.d.ts +4 -4
  53. package/dist/adapter/adapter.d.ts.map +1 -1
  54. package/dist/adapter/adapter.types.d.ts +1 -1
  55. package/dist/adapter/adapter.types.d.ts.map +1 -1
  56. package/dist/adapter/adapter.utils.d.ts.map +1 -1
  57. package/dist/cache/cache.d.ts +3 -3
  58. package/dist/cache/cache.d.ts.map +1 -1
  59. package/dist/cache/cache.events.d.ts +2 -2
  60. package/dist/cache/cache.events.d.ts.map +1 -1
  61. package/dist/client/client.create.d.ts +1 -1
  62. package/dist/client/client.create.d.ts.map +1 -1
  63. package/dist/client/client.d.ts +3 -3
  64. package/dist/client/client.d.ts.map +1 -1
  65. package/dist/client/client.types.d.ts +1 -1
  66. package/dist/client/client.types.d.ts.map +1 -1
  67. package/dist/client/client.utils.d.ts.map +1 -1
  68. package/dist/constants/http.constants.d.ts +5 -1
  69. package/dist/constants/http.constants.d.ts.map +1 -1
  70. package/dist/constants/time.constants.d.ts.map +1 -1
  71. package/dist/dispatcher/dispatcher.d.ts +3 -3
  72. package/dist/dispatcher/dispatcher.d.ts.map +1 -1
  73. package/dist/dispatcher/dispatcher.events.d.ts +1 -1
  74. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -1
  75. package/dist/dispatcher/dispatcher.utils.d.ts +1 -1
  76. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -1
  77. package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -1
  78. package/dist/http-adapter/http-adapter.d.ts.map +1 -1
  79. package/dist/http-adapter/http-adapter.fetch.d.ts +2 -2
  80. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
  81. package/dist/http-adapter/http-adapter.types.d.ts +2 -2
  82. package/dist/http-adapter/http-adapter.types.d.ts.map +1 -1
  83. package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -1
  84. package/dist/index.mjs +2363 -2347
  85. package/dist/index.mjs.map +1 -1
  86. package/dist/managers/app/app.manager.constants.d.ts +1 -1
  87. package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
  88. package/dist/managers/app/app.manager.d.ts +1 -1
  89. package/dist/managers/app/app.manager.d.ts.map +1 -1
  90. package/dist/managers/app/app.manager.events.d.ts.map +1 -1
  91. package/dist/managers/logger/logger.manager.d.ts +1 -1
  92. package/dist/managers/logger/logger.manager.d.ts.map +1 -1
  93. package/dist/managers/request/request.manager.d.ts.map +1 -1
  94. package/dist/managers/request/request.manager.events.d.ts +2 -2
  95. package/dist/managers/request/request.manager.events.d.ts.map +1 -1
  96. package/dist/plugin/plugin.d.ts +1 -1
  97. package/dist/plugin/plugin.d.ts.map +1 -1
  98. package/dist/plugin/plugin.types.d.ts +4 -4
  99. package/dist/plugin/plugin.types.d.ts.map +1 -1
  100. package/dist/request/request.d.ts +5 -4
  101. package/dist/request/request.d.ts.map +1 -1
  102. package/dist/request/request.hooks.d.ts +2 -2
  103. package/dist/request/request.hooks.d.ts.map +1 -1
  104. package/dist/request/request.types.d.ts +3 -3
  105. package/dist/request/request.types.d.ts.map +1 -1
  106. package/dist/request/request.utils.d.ts.map +1 -1
  107. package/dist/sdk/sdk.d.ts +1 -1
  108. package/dist/sdk/sdk.d.ts.map +1 -1
  109. package/dist/types/http.types.d.ts +1 -1
  110. package/dist/types/http.types.d.ts.map +1 -1
  111. package/dist/types/request.types.d.ts +2 -2
  112. package/dist/types/request.types.d.ts.map +1 -1
  113. package/dist/utils/emitter.utils.d.ts +1 -1
  114. package/dist/utils/emitter.utils.d.ts.map +1 -1
  115. package/package.json +8 -8
  116. package/src/adapter/adapter.bindings.ts +14 -14
  117. package/src/adapter/adapter.ts +10 -9
  118. package/src/adapter/adapter.types.ts +2 -1
  119. package/src/adapter/adapter.utils.ts +5 -5
  120. package/src/cache/cache.events.ts +2 -3
  121. package/src/cache/cache.ts +11 -11
  122. package/src/client/client.create.ts +1 -1
  123. package/src/client/client.ts +9 -8
  124. package/src/client/client.types.ts +2 -2
  125. package/src/client/client.utils.ts +3 -2
  126. package/src/constants/http.constants.ts +5 -0
  127. package/src/constants/time.constants.ts +1 -1
  128. package/src/dispatcher/dispatcher.events.ts +1 -2
  129. package/src/dispatcher/dispatcher.ts +12 -12
  130. package/src/dispatcher/dispatcher.utils.ts +10 -4
  131. package/src/http-adapter/http-adapter.constants.ts +1 -0
  132. package/src/http-adapter/http-adapter.fetch.ts +19 -9
  133. package/src/http-adapter/http-adapter.ts +2 -1
  134. package/src/http-adapter/http-adapter.types.ts +3 -2
  135. package/src/http-adapter/http-adapter.utils.ts +15 -11
  136. package/src/managers/app/app.manager.constants.ts +2 -1
  137. package/src/managers/app/app.manager.events.ts +0 -1
  138. package/src/managers/app/app.manager.ts +1 -1
  139. package/src/managers/app/app.manager.utils.ts +2 -2
  140. package/src/managers/logger/logger.manager.ts +1 -2
  141. package/src/managers/request/request.manager.events.ts +2 -3
  142. package/src/managers/request/request.manager.ts +4 -4
  143. package/src/mocker/mocker.ts +2 -2
  144. package/src/plugin/plugin.ts +1 -1
  145. package/src/plugin/plugin.types.ts +4 -4
  146. package/src/request/request.hooks.ts +6 -5
  147. package/src/request/request.ts +49 -34
  148. package/src/request/request.types.ts +4 -3
  149. package/src/request/request.utils.ts +17 -16
  150. package/src/sdk/sdk.ts +26 -19
  151. package/src/types/http.types.ts +1 -1
  152. package/src/types/request.types.ts +3 -2
  153. package/src/utils/emitter.utils.ts +1 -1
  154. package/src/utils/uuid.utils.ts +1 -1
  155. package/tsconfig.json +30 -2
  156. package/vite.config.ts +14 -20
  157. package/.eslintrc.cjs +0 -12
  158. package/tsconfig.base.json +0 -7
@@ -1,5 +1,4 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
2
  import { Client, interceptRequest, interceptResponse, stringifyDefaultOptions } from "client";
4
3
  import { stringifyQueryParams } from "http-adapter";
5
4
 
@@ -152,7 +151,7 @@ describe("Client [ Utils ]", () => {
152
151
  describe("When using headerMapper", () => {
153
152
  it("should assign default headers with headers mapper", async () => {
154
153
  const defaultHeaders = {};
155
- const headers = client.adapter.unstable_headerMapper(request, undefined);
154
+ const headers = client.adapter.unstable_headerMapper(request);
156
155
 
157
156
  expect(headers).toEqual(defaultHeaders);
158
157
  });
@@ -160,14 +159,14 @@ describe("Client [ Utils ]", () => {
160
159
  const defaultHeaders = {};
161
160
  const data = new FormData();
162
161
  const formDataRequest = request.setPayload(data);
163
- const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
162
+ const headers = client.adapter.unstable_headerMapper(formDataRequest);
164
163
 
165
164
  expect(headers).toEqual(defaultHeaders);
166
165
  });
167
166
  it("should not re-assign form data headers", async () => {
168
167
  const defaultHeaders = { "Content-Type": "some-custom-format" };
169
168
  const formDataRequest = request.setHeaders(defaultHeaders);
170
- const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
169
+ const headers = client.adapter.unstable_headerMapper(formDataRequest);
171
170
 
172
171
  expect(headers).toEqual(defaultHeaders);
173
172
  });
@@ -175,7 +174,7 @@ describe("Client [ Utils ]", () => {
175
174
  const defaultHeaders = { "Content-Type": "application/json" };
176
175
  const data = { data: [] } as any;
177
176
  const formDataRequest = request.setPayload(data);
178
- const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
177
+ const headers = client.adapter.unstable_headerMapper(formDataRequest);
179
178
 
180
179
  expect(headers).toEqual(defaultHeaders);
181
180
  });
@@ -184,29 +183,30 @@ describe("Client [ Utils ]", () => {
184
183
  describe("When using payloadMapper", () => {
185
184
  it("should allow to stringify payload", async () => {
186
185
  const data = { data: [] };
187
- const payload = client.adapter.unstable_payloadMapper(data, undefined);
186
+ const payload = client.adapter.unstable_payloadMapper(data);
188
187
 
189
188
  expect(payload).toEqual(JSON.stringify(data));
190
189
  });
191
190
  it("should not stringify FormData payload", async () => {
192
191
  const data = new FormData();
193
- const payload = client.adapter.unstable_payloadMapper(data, undefined);
192
+ const payload = client.adapter.unstable_payloadMapper(data);
194
193
 
195
194
  expect(payload).toEqual(data);
196
195
  });
197
196
  it("should stringify null payload", async () => {
198
197
  const data = null;
199
- const payload = client.adapter.unstable_payloadMapper(data, undefined);
198
+ const payload = client.adapter.unstable_payloadMapper(data);
200
199
 
201
200
  expect(payload).toEqual(JSON.stringify(data));
202
201
  });
203
202
  it("should not stringify undefined payload", async () => {
204
- const payload = client.adapter.unstable_payloadMapper(undefined, undefined);
203
+ // oxlint-disable-next-line unicorn/no-useless-undefined
204
+ const payload = client.adapter.unstable_payloadMapper(undefined);
205
205
 
206
206
  expect(payload).toBeUndefined();
207
207
  });
208
208
  it("should not stringify invalid payload", async () => {
209
- const payload = client.adapter.unstable_payloadMapper(() => null, undefined);
209
+ const payload = client.adapter.unstable_payloadMapper(() => null);
210
210
  expect(payload).toBeUndefined();
211
211
  });
212
212
  });
@@ -214,24 +214,25 @@ describe("Client [ Utils ]", () => {
214
214
  describe("When using stringifyValue util", () => {
215
215
  it("should allow to stringify payload", async () => {
216
216
  const data = { data: [] };
217
- const payload = client.adapter.unstable_payloadMapper(data, undefined);
217
+ const payload = client.adapter.unstable_payloadMapper(data);
218
218
 
219
219
  expect(payload).toEqual(JSON.stringify(data));
220
220
  });
221
221
  it("should not stringify FormData payload", async () => {
222
222
  const data = new FormData();
223
- const payload = client.adapter.unstable_payloadMapper(data, undefined);
223
+ const payload = client.adapter.unstable_payloadMapper(data);
224
224
 
225
225
  expect(payload).toEqual(data);
226
226
  });
227
227
  it("should stringify null payload", async () => {
228
228
  const data = null;
229
- const payload = client.adapter.unstable_payloadMapper(data, undefined);
229
+ const payload = client.adapter.unstable_payloadMapper(data);
230
230
 
231
231
  expect(payload).toEqual(JSON.stringify(data));
232
232
  });
233
233
  it("should not stringify undefined payload", async () => {
234
- const payload = client.adapter.unstable_payloadMapper(undefined, undefined);
234
+ // oxlint-disable-next-line unicorn/no-useless-undefined
235
+ const payload = client.adapter.unstable_payloadMapper(undefined);
235
236
 
236
237
  expect(payload).toBeUndefined();
237
238
  });
@@ -239,7 +240,7 @@ describe("Client [ Utils ]", () => {
239
240
  const data: Record<string, unknown> = {};
240
241
  data.a = { b: data };
241
242
 
242
- const payload = client.adapter.unstable_payloadMapper(data, undefined);
243
+ const payload = client.adapter.unstable_payloadMapper(data);
243
244
  expect(payload).toBe("");
244
245
  });
245
246
  });
@@ -1,10 +1,10 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
- import type { QueueDataType } from "dispatcher";
4
- import { createDispatcher } from "../../utils";
5
2
  import { Client } from "client";
3
+ import type { QueueDataType } from "dispatcher";
6
4
  import { Plugin } from "plugin";
7
5
 
6
+ import { createDispatcher } from "../../utils";
7
+
8
8
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
9
9
 
10
10
  describe("Dispatcher [ Basic ]", () => {
@@ -1,9 +1,8 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
3
-
2
+ import { waitFor } from "@testing-library/dom";
3
+ import { Client } from "client";
4
4
  import type { QueueDataType } from "dispatcher";
5
5
  import { Dispatcher } from "dispatcher";
6
- import { Client } from "client";
7
6
 
8
7
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
9
8
 
@@ -26,7 +25,7 @@ const createSerializingStorage = () => {
26
25
  const value = store.get(key);
27
26
  return value ? JSON.parse(value) : undefined;
28
27
  },
29
- keys: () => Array.from(store.keys()),
28
+ keys: () => [...store.keys()],
30
29
  entries: () => {
31
30
  return Array.from(
32
31
  store.entries(),
@@ -93,7 +92,7 @@ describe("Dispatcher [ Custom Serializing Storage - Issue #125 ]", () => {
93
92
  });
94
93
 
95
94
  it("should handle multiple concurrent requests with serializing storage", async () => {
96
- const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
95
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
97
96
  mockRequest(request);
98
97
 
99
98
  const fetchSpy = vi.spyOn(client.adapter, "fetch");
@@ -1,12 +1,12 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
3
-
2
+ import { waitFor } from "@testing-library/dom";
4
3
  import type { AdapterInstance, ResponseType } from "adapter";
5
4
  import { getErrorMessage } from "adapter";
6
- import type { ResponseDetailsType } from "managers";
7
- import { createDispatcher, createAdapter } from "../../utils";
8
5
  import { Client } from "client";
9
6
  import { xhrExtra } from "http-adapter";
7
+ import type { ResponseDetailsType } from "managers";
8
+
9
+ import { createDispatcher, createAdapter } from "../../utils";
10
10
 
11
11
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
12
12
 
@@ -1,11 +1,11 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
3
-
4
- import { Dispatcher } from "dispatcher";
5
- import { createDispatcher, createAdapter } from "../../utils";
2
+ import { waitFor } from "@testing-library/dom";
6
3
  import { Client } from "client";
4
+ import { Dispatcher } from "dispatcher";
7
5
  import { Plugin } from "plugin";
8
6
 
7
+ import { createDispatcher, createAdapter } from "../../utils";
8
+
9
9
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
10
10
 
11
11
  describe("Dispatcher [ Queue ]", () => {
@@ -87,7 +87,7 @@ describe("Dispatcher [ Queue ]", () => {
87
87
  expect(dispatcher.getAllRunningRequests()).toHaveLength(1);
88
88
  });
89
89
  it("should send all concurrent requests", async () => {
90
- const request = client.createRequest()({ endpoint: "shared-base-endpoint", queued: false });
90
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", queued: false, deduplicate: false });
91
91
  mockRequest(request);
92
92
 
93
93
  const spy = vi.spyOn(dispatcher, "performRequest");
@@ -99,7 +99,7 @@ describe("Dispatcher [ Queue ]", () => {
99
99
  expect(dispatcher.getAllRunningRequests()).toHaveLength(2);
100
100
  });
101
101
  it("should send all concurrent requests without any duplicates", async () => {
102
- const request = client.createRequest()({ endpoint: "shared-base-endpoint", queued: false });
102
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", queued: false, deduplicate: false });
103
103
  mockRequest(request, { delay: 40 });
104
104
 
105
105
  const spy1 = vi.spyOn(dispatcher, "performRequest");
@@ -235,8 +235,8 @@ describe("Dispatcher [ Queue ]", () => {
235
235
  });
236
236
  describe("When flushing requests", () => {
237
237
  it("should flush all queues request", async () => {
238
- const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1" });
239
- const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "2" });
238
+ const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1", deduplicate: false });
239
+ const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "2", deduplicate: false });
240
240
  mockRequest(firstRequest);
241
241
  mockRequest(secondRequest);
242
242
 
@@ -307,7 +307,7 @@ describe("Dispatcher [ Queue ]", () => {
307
307
  expect(spy).toHaveBeenCalledTimes(0);
308
308
  });
309
309
  it("should not duplicate ongoing requests using flushQueue", async () => {
310
- const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
310
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
311
311
  mockRequest(request, { delay: 30 });
312
312
 
313
313
  const spy = vi.spyOn(dispatcher, "performRequest");
@@ -334,7 +334,7 @@ describe("Dispatcher [ Queue ]", () => {
334
334
  });
335
335
  describe("When starting and stopping queue", () => {
336
336
  it("should stop queue from being send", async () => {
337
- const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1" });
337
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1", deduplicate: false });
338
338
  mockRequest(request);
339
339
 
340
340
  const spy = vi.spyOn(client.adapter, "fetch");
@@ -348,7 +348,7 @@ describe("Dispatcher [ Queue ]", () => {
348
348
  });
349
349
  });
350
350
  it("should stop queue and cancel ongoing requests", async () => {
351
- const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1" });
351
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1", deduplicate: false });
352
352
  mockRequest(request);
353
353
 
354
354
  const spy = vi.spyOn(client.adapter, "fetch");
@@ -372,7 +372,7 @@ describe("Dispatcher [ Queue ]", () => {
372
372
  });
373
373
  });
374
374
  it("should start previously stopped queue", async () => {
375
- const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1" });
375
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1", deduplicate: false });
376
376
  mockRequest(request);
377
377
 
378
378
  const spy = vi.spyOn(client.adapter, "fetch");
@@ -387,7 +387,7 @@ describe("Dispatcher [ Queue ]", () => {
387
387
  });
388
388
  });
389
389
  it("should pause queue and finish ongoing requests", async () => {
390
- const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1" });
390
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1", deduplicate: false });
391
391
  mockRequest(request);
392
392
 
393
393
  const spy = vi.spyOn(client.adapter, "fetch");
@@ -1,8 +1,8 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
2
+ import { waitFor } from "@testing-library/dom";
3
+ import { Client } from "client";
3
4
 
4
5
  import { createDispatcher, createAdapter } from "../../utils";
5
- import { Client } from "client";
6
6
 
7
7
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
8
8
 
@@ -90,8 +90,8 @@ describe("Dispatcher [ Requests ]", () => {
90
90
  const firstSpy = vi.fn();
91
91
  const secondSpy = vi.fn();
92
92
  const thirdSpy = vi.fn();
93
- const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
94
- const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
93
+ const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
94
+ const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
95
95
  mockRequest(firstRequest, { delay: 5 });
96
96
  mockRequest(secondRequest, { delay: 5 });
97
97
 
@@ -114,8 +114,8 @@ describe("Dispatcher [ Requests ]", () => {
114
114
  it("should allow to cancel single running requests", async () => {
115
115
  const firstSpy = vi.fn();
116
116
  const secondSpy = vi.fn();
117
- const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
118
- const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
117
+ const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
118
+ const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
119
119
  mockRequest(firstRequest, { delay: 5 });
120
120
  mockRequest(secondRequest, { delay: 5 });
121
121
 
@@ -136,8 +136,8 @@ describe("Dispatcher [ Requests ]", () => {
136
136
  const firstSpy = vi.fn();
137
137
  const secondSpy = vi.fn();
138
138
  const thirdSpy = vi.fn();
139
- const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
140
- const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
139
+ const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
140
+ const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
141
141
  mockRequest(firstRequest, { delay: 5 });
142
142
  mockRequest(secondRequest, { delay: 5 });
143
143
 
@@ -161,8 +161,8 @@ describe("Dispatcher [ Requests ]", () => {
161
161
  const firstSpy = vi.fn();
162
162
  const secondSpy = vi.fn();
163
163
  const thirdSpy = vi.fn();
164
- const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
165
- const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint" });
164
+ const firstRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
165
+ const secondRequest = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: false });
166
166
  mockRequest(firstRequest, { delay: 5 });
167
167
  mockRequest(secondRequest, { delay: 5 });
168
168
 
@@ -1,5 +1,5 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
2
+ import { Client } from "client";
3
3
  import type { ResolvedQueueItemType } from "dispatcher";
4
4
  import {
5
5
  canRetryRequest,
@@ -10,8 +10,8 @@ import {
10
10
  getIsEqualTimestamp,
11
11
  getRequestType,
12
12
  } from "dispatcher";
13
+
13
14
  import { createDispatcher, createAdapter } from "../../utils";
14
- import { Client } from "client";
15
15
 
16
16
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
17
17
 
@@ -80,13 +80,13 @@ describe("Dispatcher [ Utils ]", () => {
80
80
  });
81
81
  describe("When using getIsEqualTimestamp util", () => {
82
82
  it("should return true when timestamps are equal", async () => {
83
- expect(getIsEqualTimestamp(+new Date(), 0, +new Date())).toBeTrue();
84
- expect(getIsEqualTimestamp(+new Date(), 10, +new Date() + 5)).toBeTrue();
83
+ expect(getIsEqualTimestamp(Date.now(), 0, Date.now())).toBeTrue();
84
+ expect(getIsEqualTimestamp(Date.now(), 10, Date.now() + 5)).toBeTrue();
85
85
  });
86
86
  it("should return false when timestamps aren't equal", async () => {
87
- expect(getIsEqualTimestamp(+new Date(), 0, +new Date() + 5)).toBeFalse();
88
- expect(getIsEqualTimestamp(+new Date(), 10, +new Date() + 15)).toBeFalse();
89
- expect(getIsEqualTimestamp(+new Date(), 10)).toBeFalse();
87
+ expect(getIsEqualTimestamp(Date.now(), 0, Date.now() + 5)).toBeFalse();
88
+ expect(getIsEqualTimestamp(Date.now(), 10, Date.now() + 15)).toBeFalse();
89
+ expect(getIsEqualTimestamp(Date.now(), 10)).toBeFalse();
90
90
  });
91
91
  });
92
92
  describe("When using canRetryRequest util", () => {
@@ -134,7 +134,34 @@ describe("Dispatcher [ Utils ]", () => {
134
134
  .createRequest()({ endpoint: "shared-base-endpoint", deduplicate: true })
135
135
  .setDeduplicateTime(1);
136
136
  const duplicated: ResolvedQueueItemType<typeof request> = dispatcher.createStorageItem(request);
137
- duplicated.timestamp = +new Date() - 1000;
137
+ duplicated.timestamp = Date.now() - 1000;
138
+ const type = getRequestType(request, duplicated);
139
+ expect(type).toBe(DispatcherMode.ALL_AT_ONCE);
140
+ });
141
+ it("should not deduplicate onto a resolved queue item", async () => {
142
+ // A resolved item already emitted its response, so a late joiner would wait forever
143
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: true });
144
+ const duplicated: ResolvedQueueItemType<typeof request> = dispatcher.createStorageItem(request);
145
+ duplicated.resolved = true;
146
+ const type = getRequestType(request, duplicated);
147
+ expect(type).toBe(DispatcherMode.ALL_AT_ONCE);
148
+ });
149
+ it("should not deduplicate onto a stopped queue item", async () => {
150
+ // A stopped item may never fire, so a joiner would never receive a response
151
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: true });
152
+ const duplicated: ResolvedQueueItemType<typeof request> = dispatcher.createStorageItem(request);
153
+ duplicated.stopped = true;
154
+ const type = getRequestType(request, duplicated);
155
+ expect(type).toBe(DispatcherMode.ALL_AT_ONCE);
156
+ });
157
+ it("should not deduplicate a request marked as used", async () => {
158
+ // setUsed(true) marks intentional resends, like the auth interceptor retrying after a
159
+ // token refresh. Deduplicating them against the original in-flight request would
160
+ // deadlock: the retry would await a response blocked on its own interceptor.
161
+ const request = client
162
+ .createRequest()({ endpoint: "shared-base-endpoint", deduplicate: true })
163
+ .setUsed(true);
164
+ const duplicated: ResolvedQueueItemType<typeof request> = dispatcher.createStorageItem(request);
138
165
  const type = getRequestType(request, duplicated);
139
166
  expect(type).toBe(DispatcherMode.ALL_AT_ONCE);
140
167
  });
@@ -1,8 +1,7 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
3
-
4
- import { AppManager, appManagerInitialOptions, hasDocument, hasWindow } from "managers";
2
+ import { waitFor } from "@testing-library/dom";
5
3
  import { Client } from "client";
4
+ import { AppManager, appManagerInitialOptions, hasDocument, hasWindow } from "managers";
6
5
 
7
6
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
8
7
 
@@ -1,7 +1,6 @@
1
- import type { Mock } from "vitest";
2
-
3
1
  import { Client } from "client";
4
2
  import { LoggerManager } from "managers";
3
+ import type { Mock } from "vitest";
5
4
 
6
5
  describe("Logger [ Base ]", () => {
7
6
  let client = new Client({ url: "shared-base-url" }).setDebug(true);
@@ -1,9 +1,8 @@
1
- import EventEmitter from "events";
2
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
3
-
2
+ import { Client } from "client";
3
+ import EventEmitter from "events";
4
4
  import type { RequestProgressEventType } from "managers";
5
5
  import { getRequestManagerEvents } from "managers";
6
- import { Client } from "client";
7
6
  import type { RequestInstance } from "request";
8
7
 
9
8
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
@@ -176,18 +175,18 @@ describe("RequestManager [ Base ]", () => {
176
175
  status: 200,
177
176
  success: true,
178
177
  extra: null,
179
- requestTimestamp: +new Date(),
180
- responseTimestamp: +new Date(),
178
+ requestTimestamp: Date.now(),
179
+ responseTimestamp: Date.now(),
181
180
  },
182
181
  details: {
183
182
  retries: 0,
184
183
  isCanceled: false,
185
184
  isOffline: false,
186
185
  willRetry: false,
187
- requestTimestamp: +new Date(),
188
- responseTimestamp: +new Date(),
189
- triggerTimestamp: +new Date(),
190
- addedTimestamp: +new Date(),
186
+ requestTimestamp: Date.now(),
187
+ responseTimestamp: Date.now(),
188
+ triggerTimestamp: Date.now(),
189
+ addedTimestamp: Date.now(),
191
190
  },
192
191
  });
193
192
 
@@ -1,6 +1,5 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
3
-
2
+ import { waitFor } from "@testing-library/dom";
4
3
  import { Client } from "client";
5
4
 
6
5
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
@@ -1,7 +1,6 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
2
+ import { waitFor } from "@testing-library/dom";
3
3
 
4
- import { createAdapter, createDispatcher } from "../../utils";
5
4
  import type {
6
5
  HttpAdapterType,
7
6
  RequestInstance,
@@ -10,6 +9,7 @@ import type {
10
9
  ResponseType,
11
10
  } from "../../../src";
12
11
  import { Client, getErrorMessage, mocker } from "../../../src";
12
+ import { createAdapter, createDispatcher } from "../../utils";
13
13
 
14
14
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
15
15
 
@@ -92,7 +92,7 @@ describe("Mocker [ Base ]", () => {
92
92
  const firstSpy = vi.fn();
93
93
  const secondSpy = vi.fn();
94
94
  const firstRequest = client
95
- .createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" })
95
+ .createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint", deduplicate: false })
96
96
  .setMock(
97
97
  () => ({
98
98
  data: fixture,
@@ -103,7 +103,7 @@ describe("Mocker [ Base ]", () => {
103
103
  },
104
104
  );
105
105
  const secondRequest = client
106
- .createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" })
106
+ .createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint", deduplicate: false })
107
107
  .setMock(
108
108
  () => ({
109
109
  data: fixture,
@@ -361,9 +361,9 @@ describe("Mocker [ Base ]", () => {
361
361
  }),
362
362
  { requestTime: 100, responseTime: 100 },
363
363
  );
364
- const startDate = +new Date();
364
+ const startDate = Date.now();
365
365
  const response = await mockedRequest.send();
366
- const endDate = +new Date();
366
+ const endDate = Date.now();
367
367
  expect(endDate - startDate).toBeGreaterThanOrEqual(200);
368
368
  expect(response).toStrictEqual({
369
369
  data: fixture,
@@ -1,6 +1,5 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
3
-
2
+ import { waitFor } from "@testing-library/dom";
4
3
  import { Client } from "client";
5
4
  import { Plugin } from "plugin";
6
5
 
@@ -1,5 +1,4 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
2
  import { Client } from "client";
4
3
 
5
4
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
@@ -0,0 +1,79 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+ import type { ClientInstance } from "client";
3
+ import { createClient } from "client";
4
+ import { HttpMethods } from "constants/http.constants";
5
+ import { getRequestDispatcher } from "request";
6
+
7
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
8
+
9
+ /**
10
+ * Safe reads (GET, QUERY) deduplicate by default on the HTTP adapter: concurrent
11
+ * identical requests collapse into a single network call. Mutations keep the old
12
+ * behavior, and an explicit `deduplicate` always wins over the default.
13
+ */
14
+ describe("Request [ Deduplicate defaults ]", () => {
15
+ let client: ClientInstance;
16
+
17
+ beforeAll(() => {
18
+ startServer();
19
+ });
20
+
21
+ beforeEach(() => {
22
+ resetMocks();
23
+ client = createClient({ url: "http://localhost:3000" });
24
+ });
25
+
26
+ afterAll(() => {
27
+ stopServer();
28
+ });
29
+
30
+ it("should deduplicate safe reads by default and leave mutations off", () => {
31
+ expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.GET }).deduplicate).toBe(true);
32
+ expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.QUERY }).deduplicate).toBe(true);
33
+ // No method falls back to the adapter default (GET)
34
+ expect(client.createRequest()({ endpoint: "/users" }).deduplicate).toBe(true);
35
+
36
+ expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.POST }).deduplicate).toBe(false);
37
+ expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.PUT }).deduplicate).toBe(false);
38
+ expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.PATCH }).deduplicate).toBe(false);
39
+ expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.DELETE }).deduplicate).toBe(false);
40
+ });
41
+
42
+ it("should let explicit deduplicate settings win over the default", () => {
43
+ const optedOut = client.createRequest()({ endpoint: "/users", method: HttpMethods.GET, deduplicate: false });
44
+ expect(optedOut.deduplicate).toBe(false);
45
+
46
+ const optedIn = client.createRequest()({ endpoint: "/users", method: HttpMethods.POST, deduplicate: true });
47
+ expect(optedIn.deduplicate).toBe(true);
48
+
49
+ const setterWins = client.createRequest()({ endpoint: "/users", method: HttpMethods.GET }).setDeduplicate(false);
50
+ expect(setterWins.deduplicate).toBe(false);
51
+ });
52
+
53
+ it("should route QUERY to the fetch dispatcher like other safe reads", () => {
54
+ const query = client.createRequest()({ endpoint: "/search", method: HttpMethods.QUERY });
55
+ const get = client.createRequest()({ endpoint: "/search", method: HttpMethods.GET });
56
+ const post = client.createRequest()({ endpoint: "/search", method: HttpMethods.POST });
57
+
58
+ expect(getRequestDispatcher(query)[1]).toBe(true);
59
+ expect(getRequestDispatcher(get)[1]).toBe(true);
60
+ expect(getRequestDispatcher(post)[1]).toBe(false);
61
+ });
62
+
63
+ it("should collapse concurrent identical GET requests into one call", async () => {
64
+ const request = client.createRequest<{ response: { ok: boolean } }>()({
65
+ endpoint: "/users",
66
+ method: HttpMethods.GET,
67
+ });
68
+ mockRequest(request, { data: { ok: true } });
69
+
70
+ const deduplicatedSpy = vi.fn();
71
+ client.requestManager.events.onDeduplicated(deduplicatedSpy);
72
+
73
+ const [first, second] = await Promise.all([request.send(), request.send()]);
74
+
75
+ expect(first.data).toEqual({ ok: true });
76
+ expect(second.data).toEqual({ ok: true });
77
+ expect(deduplicatedSpy).toHaveBeenCalledTimes(1);
78
+ });
79
+ });
@@ -1,6 +1,5 @@
1
- import { waitFor } from "@testing-library/dom";
2
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
3
-
2
+ import { waitFor } from "@testing-library/dom";
4
3
  import { Client } from "client";
5
4
 
6
5
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();