@hyper-fetch/core 9.6.1 → 9.7.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 (134) hide show
  1. package/__tests__/features/adapter/adapter.bindings.progress.spec.ts +120 -0
  2. package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
  3. package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
  4. package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
  5. package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
  6. package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
  7. package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
  8. package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
  9. package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
  10. package/__tests__/features/cache/cache.timing.spec.ts +448 -0
  11. package/__tests__/features/client/client.clear.spec.ts +313 -0
  12. package/__tests__/features/client/client.configuration.spec.ts +416 -0
  13. package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
  14. package/__tests__/features/client/client.interceptor.spec.ts +54 -45
  15. package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
  16. package/__tests__/features/client/client.user-errors.spec.ts +220 -0
  17. package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
  18. package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
  19. package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
  20. package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
  21. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
  22. package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
  23. package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
  24. package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
  25. package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
  26. package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
  27. package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
  28. package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
  29. package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
  30. package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
  31. package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
  32. package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
  33. package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
  34. package/__tests__/features/optimistic/optimistic.concurrency.spec.ts +278 -0
  35. package/__tests__/features/optimistic/optimistic.helpers.spec.ts +307 -0
  36. package/__tests__/features/optimistic/optimistic.matrix.spec.ts +315 -0
  37. package/__tests__/features/optimistic/optimistic.races.spec.ts +270 -0
  38. package/__tests__/features/optimistic/optimistic.random.spec.ts +191 -0
  39. package/__tests__/features/optimistic/optimistic.rollback.spec.ts +1001 -0
  40. package/__tests__/features/optimistic/optimistic.typescript.spec.ts +196 -0
  41. package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
  42. package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
  43. package/__tests__/features/request/request.immutability.spec.ts +259 -0
  44. package/__tests__/features/request/request.inference.spec.ts +269 -0
  45. package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
  46. package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
  47. package/__tests__/features/request/request.optimistic.spec.ts +12 -1
  48. package/__tests__/features/request/request.values.spec.ts +408 -0
  49. package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
  50. package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
  51. package/__tests__/utils/controlled-adapter.utils.ts +119 -0
  52. package/__tests__/utils/fixtures.utils.ts +193 -0
  53. package/__tests__/utils/optimistic.utils.ts +124 -0
  54. package/dist/adapter/adapter.bindings.d.ts.map +1 -1
  55. package/dist/adapter/adapter.utils.d.ts.map +1 -1
  56. package/dist/cache/cache.d.ts +12 -1
  57. package/dist/cache/cache.d.ts.map +1 -1
  58. package/dist/cache/cache.events.d.ts +20 -6
  59. package/dist/cache/cache.events.d.ts.map +1 -1
  60. package/dist/cache/cache.utils.d.ts +1 -0
  61. package/dist/cache/cache.utils.d.ts.map +1 -1
  62. package/dist/client/client.d.ts +3 -3
  63. package/dist/client/client.d.ts.map +1 -1
  64. package/dist/client/client.utils.d.ts.map +1 -1
  65. package/dist/dispatcher/dispatcher.d.ts +26 -5
  66. package/dist/dispatcher/dispatcher.d.ts.map +1 -1
  67. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
  68. package/dist/index.d.ts +1 -0
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.mjs +2787 -1893
  71. package/dist/index.mjs.map +1 -1
  72. package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
  73. package/dist/managers/app/app.manager.d.ts +9 -0
  74. package/dist/managers/app/app.manager.d.ts.map +1 -1
  75. package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
  76. package/dist/managers/logger/logger.manager.d.ts.map +1 -1
  77. package/dist/managers/request/request.manager.d.ts.map +1 -1
  78. package/dist/managers/request/request.manager.types.d.ts +2 -0
  79. package/dist/managers/request/request.manager.types.d.ts.map +1 -1
  80. package/dist/mocker/mocker.d.ts +1 -1
  81. package/dist/mocker/mocker.d.ts.map +1 -1
  82. package/dist/optimistic/index.d.ts +5 -0
  83. package/dist/optimistic/index.d.ts.map +1 -0
  84. package/dist/optimistic/optimistic.d.ts +67 -0
  85. package/dist/optimistic/optimistic.d.ts.map +1 -0
  86. package/dist/optimistic/optimistic.settlement.d.ts +60 -0
  87. package/dist/optimistic/optimistic.settlement.d.ts.map +1 -0
  88. package/dist/optimistic/optimistic.types.d.ts +109 -0
  89. package/dist/optimistic/optimistic.types.d.ts.map +1 -0
  90. package/dist/optimistic/optimistic.utils.d.ts +29 -0
  91. package/dist/optimistic/optimistic.utils.d.ts.map +1 -0
  92. package/dist/plugin/plugin.d.ts +2 -0
  93. package/dist/plugin/plugin.d.ts.map +1 -1
  94. package/dist/request/request.d.ts +14 -6
  95. package/dist/request/request.d.ts.map +1 -1
  96. package/dist/request/request.types.d.ts +40 -1
  97. package/dist/request/request.types.d.ts.map +1 -1
  98. package/dist/request/request.utils.d.ts.map +1 -1
  99. package/dist/utils/emitter.utils.d.ts.map +1 -1
  100. package/dist/utils/error.utils.d.ts +17 -0
  101. package/dist/utils/error.utils.d.ts.map +1 -0
  102. package/dist/utils/index.d.ts +1 -0
  103. package/dist/utils/index.d.ts.map +1 -1
  104. package/package.json +1 -1
  105. package/src/adapter/adapter.bindings.ts +46 -18
  106. package/src/adapter/adapter.utils.ts +47 -7
  107. package/src/cache/cache.events.ts +24 -8
  108. package/src/cache/cache.ts +110 -21
  109. package/src/cache/cache.utils.ts +33 -10
  110. package/src/client/client.ts +101 -7
  111. package/src/client/client.utils.ts +10 -4
  112. package/src/dispatcher/dispatcher.ts +285 -132
  113. package/src/http-adapter/http-adapter.fetch.ts +15 -3
  114. package/src/index.ts +4 -0
  115. package/src/managers/app/app.manager.constants.ts +12 -1
  116. package/src/managers/app/app.manager.ts +43 -23
  117. package/src/managers/app/app.manager.utils.ts +8 -4
  118. package/src/managers/logger/logger.manager.ts +21 -8
  119. package/src/managers/request/request.manager.ts +7 -1
  120. package/src/managers/request/request.manager.types.ts +2 -0
  121. package/src/mocker/mocker.ts +14 -11
  122. package/src/optimistic/index.ts +4 -0
  123. package/src/optimistic/optimistic.settlement.ts +406 -0
  124. package/src/optimistic/optimistic.ts +162 -0
  125. package/src/optimistic/optimistic.types.ts +175 -0
  126. package/src/optimistic/optimistic.utils.ts +112 -0
  127. package/src/plugin/plugin.ts +6 -0
  128. package/src/request/request.ts +50 -28
  129. package/src/request/request.types.ts +41 -1
  130. package/src/request/request.utils.ts +51 -66
  131. package/src/utils/emitter.utils.ts +9 -2
  132. package/src/utils/error.utils.ts +29 -0
  133. package/src/utils/index.ts +1 -0
  134. package/tsconfig.json +2 -0
@@ -0,0 +1,313 @@
1
+ import { getErrorMessage } from "adapter";
2
+ import { Client } from "client";
3
+ import { AppManager } from "managers";
4
+ import { Plugin } from "plugin";
5
+ import type { RequestInstance } from "request";
6
+
7
+ import { createCacheResponse, createDeferred } from "../../utils/fixtures.utils";
8
+
9
+ type Data = { id: number };
10
+
11
+ /**
12
+ * Request with the mock which starts and then waits for the gate - the request stays in progress as long as we need.
13
+ */
14
+ const createPendingRequest = (client: Client, options: { endpoint?: string; method?: "GET" | "POST" } = {}) => {
15
+ const started = createDeferred();
16
+ const gate = createDeferred();
17
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users", ...options });
18
+
19
+ request.setMock(async () => {
20
+ started.resolve();
21
+ await gate.promise;
22
+ return { data: { id: 1 }, status: 200 };
23
+ });
24
+
25
+ return { request, started, gate };
26
+ };
27
+
28
+ const countListeners = (emitter: { eventNames: () => (string | symbol)[]; listenerCount: (name: string) => number }) =>
29
+ emitter.eventNames().reduce((sum, name) => sum + emitter.listenerCount(String(name)), 0);
30
+
31
+ describe("Client [ Clear ]", () => {
32
+ let client = new Client({ url: "shared-base-url" });
33
+
34
+ beforeEach(() => {
35
+ client = new Client({ url: "shared-base-url" });
36
+ });
37
+
38
+ afterEach(() => {
39
+ client.clear();
40
+ });
41
+
42
+ describe("When requests are in progress", () => {
43
+ it("should resolve the pending request instead of leaving it forever", async () => {
44
+ const { request, started, gate } = createPendingRequest(client);
45
+ const pending = request.send();
46
+ await started.promise;
47
+
48
+ client.clear();
49
+ const response = await pending;
50
+ gate.resolve();
51
+
52
+ expect(response.success).toBeFalse();
53
+ expect(response.data).toBeNull();
54
+ expect(response.error?.message).toBe(getErrorMessage("deleted").message);
55
+ });
56
+
57
+ it("should abort the request which is in progress", async () => {
58
+ const { request, started, gate } = createPendingRequest(client);
59
+ const pending = request.send();
60
+ await started.promise;
61
+ const controllers = [...client.requestManager.abortControllers.values()].flatMap((group) => [...group.values()]);
62
+
63
+ client.clear();
64
+ await pending;
65
+ gate.resolve();
66
+
67
+ expect(controllers).toHaveLength(1);
68
+ expect(controllers[0].signal.aborted).toBeTrue();
69
+ });
70
+
71
+ it("should resolve the requests waiting in the queue", async () => {
72
+ const { request, started, gate } = createPendingRequest(client);
73
+ const queued = request.setQueued(true);
74
+ const pending = [queued.send(), queued.send(), queued.send()];
75
+ await started.promise;
76
+
77
+ client.clear();
78
+ const responses = await Promise.all(pending);
79
+ gate.resolve();
80
+
81
+ expect(responses.map((response) => response.success)).toStrictEqual([false, false, false]);
82
+ });
83
+
84
+ it("should resolve the deduplicated requests", async () => {
85
+ const { request, started, gate } = createPendingRequest(client);
86
+ const pending = [request.send(), request.send()];
87
+ await started.promise;
88
+
89
+ client.clear();
90
+ const responses = await Promise.all(pending);
91
+ gate.resolve();
92
+
93
+ expect(responses.map((response) => response.success)).toStrictEqual([false, false]);
94
+ });
95
+
96
+ it("should resolve the pending requests of the submit dispatcher", async () => {
97
+ const { request, started, gate } = createPendingRequest(client, { method: "POST" });
98
+ const pending = request.send();
99
+ await started.promise;
100
+ expect(client.submitDispatcher.getAllRunningRequests()).toHaveLength(1);
101
+
102
+ client.clear();
103
+ const response = await pending;
104
+ gate.resolve();
105
+
106
+ expect(response.success).toBeFalse();
107
+ });
108
+
109
+ it("should call the removal callback of the pending request", async () => {
110
+ const { request, started, gate } = createPendingRequest(client);
111
+ const onRemove = vi.fn();
112
+ const onResponse = vi.fn();
113
+ const pending = request.send({ onRemove, onResponse });
114
+ await started.promise;
115
+
116
+ client.clear();
117
+ await pending;
118
+ gate.resolve();
119
+
120
+ expect(onRemove).toHaveBeenCalledTimes(1);
121
+ expect(onResponse).not.toHaveBeenCalled();
122
+ });
123
+
124
+ it("should not save the response of the removed request in the new cache", async () => {
125
+ const { request, started, gate } = createPendingRequest(client);
126
+ const pending = request.send();
127
+ await started.promise;
128
+
129
+ client.clear();
130
+ gate.resolve();
131
+ await pending;
132
+ const { request: nextRequest, gate: nextGate } = createPendingRequest(client, { endpoint: "/next" });
133
+ nextGate.resolve();
134
+ await nextRequest.send();
135
+
136
+ expect(request.read()).toBeUndefined();
137
+ expect(client.cache.keys()).toStrictEqual([nextRequest.cacheKey]);
138
+ });
139
+
140
+ it("should leave no running requests nor queues behind", async () => {
141
+ const { request, started, gate } = createPendingRequest(client);
142
+ const previousDispatcher = client.fetchDispatcher;
143
+ const pending = request.send();
144
+ await started.promise;
145
+
146
+ client.clear();
147
+ await pending;
148
+ gate.resolve();
149
+
150
+ expect(previousDispatcher.getAllRunningRequests()).toHaveLength(0);
151
+ expect(previousDispatcher.getQueuesKeys()).toHaveLength(0);
152
+ expect(client.fetchDispatcher.getAllRunningRequests()).toHaveLength(0);
153
+ expect(client.requestManager.abortControllers.size).toBe(0);
154
+ });
155
+ });
156
+
157
+ describe("When listeners are attached", () => {
158
+ it("should remove the listeners of the previous modules", () => {
159
+ const previous = {
160
+ cache: client.cache,
161
+ fetchDispatcher: client.fetchDispatcher,
162
+ submitDispatcher: client.submitDispatcher,
163
+ appManager: client.appManager,
164
+ };
165
+ client.cache.events.onData(() => {});
166
+ client.cache.events.onDelete(() => {});
167
+ client.requestManager.events.onResponse(() => {});
168
+ client.appManager.events.onFocus(() => {});
169
+ client.appManager.events.onOnline(() => {});
170
+
171
+ client.clear();
172
+
173
+ expect(countListeners(previous.cache.emitter)).toBe(0);
174
+ expect(countListeners(previous.fetchDispatcher.emitter)).toBe(0);
175
+ expect(countListeners(previous.submitDispatcher.emitter)).toBe(0);
176
+ expect(countListeners(previous.appManager.emitter)).toBe(0);
177
+ expect(client.requestManager.emitter.listenerCount("response")).toBe(0);
178
+ });
179
+
180
+ it("should not notify the listeners of the previous cache", () => {
181
+ const spy = vi.fn();
182
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
183
+ client.cache.events.onData(spy);
184
+
185
+ client.clear();
186
+ client.cache.set(request, createCacheResponse({ id: 1 }));
187
+
188
+ expect(spy).not.toHaveBeenCalled();
189
+ });
190
+
191
+ it("should stop reacting to the browser events with the previous app manager", () => {
192
+ const previousAppManager = client.appManager;
193
+ const spy = vi.fn();
194
+ previousAppManager.events.onBlur(spy);
195
+
196
+ client.clear();
197
+ window.dispatchEvent(new Event("blur"));
198
+ window.dispatchEvent(new Event("focus"));
199
+
200
+ expect(spy).not.toHaveBeenCalled();
201
+ expect(previousAppManager.isFocused).toBeTrue();
202
+ });
203
+
204
+ it("should not pile up the listeners on the app manager shared between the clears", () => {
205
+ const appManager = new AppManager();
206
+ const sharedClient = new Client({ url: "shared-base-url", appManager: () => appManager });
207
+ const initialListeners = appManager.emitter.listenerCount("online");
208
+
209
+ sharedClient.clear();
210
+ sharedClient.clear();
211
+ sharedClient.clear();
212
+
213
+ expect(appManager.emitter.listenerCount("online")).toBe(initialListeners);
214
+ });
215
+
216
+ it("should keep the modules listening to the shared app manager after the clear", () => {
217
+ vi.useFakeTimers();
218
+ const appManager = new AppManager();
219
+ const sharedClient = new Client({ url: "shared-base-url", appManager: () => appManager });
220
+ const request = sharedClient.createRequest<{ response: Data }>()({ endpoint: "/users", cacheTime: 100 });
221
+ sharedClient.clear();
222
+ sharedClient.cache.set(request, createCacheResponse({ id: 1 }));
223
+ appManager.setOnline(false);
224
+ vi.advanceTimersByTime(1000);
225
+ expect(request.read()).toBeDefined();
226
+
227
+ appManager.setOnline(true);
228
+
229
+ expect(request.read()).toBeUndefined();
230
+ sharedClient.clear();
231
+ vi.useRealTimers();
232
+ });
233
+ });
234
+
235
+ describe("When the client keeps its configuration", () => {
236
+ it("should empty the cache and create the new modules", () => {
237
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
238
+ const previous = {
239
+ cache: client.cache,
240
+ fetchDispatcher: client.fetchDispatcher,
241
+ submitDispatcher: client.submitDispatcher,
242
+ appManager: client.appManager,
243
+ };
244
+ client.cache.set(request, createCacheResponse({ id: 1 }));
245
+
246
+ client.clear();
247
+
248
+ expect(request.read()).toBeUndefined();
249
+ expect(previous.cache.keys()).toHaveLength(0);
250
+ expect(client.cache).not.toBe(previous.cache);
251
+ expect(client.fetchDispatcher).not.toBe(previous.fetchDispatcher);
252
+ expect(client.submitDispatcher).not.toBe(previous.submitDispatcher);
253
+ expect(client.appManager).not.toBe(previous.appManager);
254
+ });
255
+
256
+ it("should keep the plugins without unmounting them", () => {
257
+ const onUnmount = vi.fn();
258
+ const onRequestCreate = vi.fn();
259
+ const plugin = new Plugin({ name: "kept" }).onUnmount(onUnmount).onRequestCreate(onRequestCreate);
260
+ client.addPlugin(plugin);
261
+
262
+ client.clear();
263
+ client.createRequest()({ endpoint: "/users" });
264
+
265
+ expect(client.plugins).toStrictEqual([plugin]);
266
+ expect(onUnmount).not.toHaveBeenCalled();
267
+ expect(onRequestCreate).toHaveBeenCalledTimes(1);
268
+ });
269
+
270
+ it("should trigger the plugins with the new cache", () => {
271
+ const onCacheItemChange = vi.fn();
272
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
273
+ client.addPlugin(new Plugin({ name: "cache" }).onCacheItemChange(onCacheItemChange));
274
+
275
+ client.clear();
276
+ client.cache.set(request, createCacheResponse({ id: 1 }));
277
+
278
+ expect(onCacheItemChange).toHaveBeenCalledTimes(1);
279
+ expect(onCacheItemChange.mock.calls[0][0].cache).toBe(client.cache);
280
+ });
281
+
282
+ it("should keep the interceptors and the settings", () => {
283
+ const interceptor = (request: RequestInstance) => request;
284
+ client.onRequest(interceptor).onAuth(interceptor).setDebug(true);
285
+
286
+ client.clear();
287
+
288
+ expect(client.unstable_onRequestCallbacks).toStrictEqual([interceptor]);
289
+ expect(client.unstable_onAuthCallbacks).toStrictEqual([interceptor]);
290
+ expect(client.debug).toBeTrue();
291
+ expect(client.url).toBe("shared-base-url");
292
+ });
293
+
294
+ it("should send and cache the requests after the clear", async () => {
295
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
296
+ request.setMock(() => ({ data: { id: 1 }, status: 200 }));
297
+
298
+ client.clear();
299
+ const response = await request.send();
300
+
301
+ expect(response.data).toStrictEqual({ id: 1 });
302
+ expect(request.read()?.data).toStrictEqual({ id: 1 });
303
+ });
304
+
305
+ it("should be safe to clear the client many times", () => {
306
+ expect(() => {
307
+ client.clear();
308
+ client.clear();
309
+ client.clear();
310
+ }).not.toThrow();
311
+ });
312
+ });
313
+ });