@hyper-fetch/core 9.6.1 → 9.6.2
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.
- package/__tests__/features/adapter/adapter.bindings.progress.spec.ts +120 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
- package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
- package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
- package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
- package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
- package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
- package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
- package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
- package/__tests__/features/cache/cache.timing.spec.ts +448 -0
- package/__tests__/features/client/client.clear.spec.ts +313 -0
- package/__tests__/features/client/client.configuration.spec.ts +416 -0
- package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
- package/__tests__/features/client/client.interceptor.spec.ts +54 -45
- package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
- package/__tests__/features/client/client.user-errors.spec.ts +220 -0
- package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
- package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
- package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
- package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
- package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
- package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
- package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
- package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
- package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
- package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
- package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
- package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
- package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
- package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
- package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
- package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
- package/__tests__/features/request/request.immutability.spec.ts +259 -0
- package/__tests__/features/request/request.inference.spec.ts +269 -0
- package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
- package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
- package/__tests__/features/request/request.values.spec.ts +408 -0
- package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
- package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
- package/__tests__/utils/controlled-adapter.utils.ts +119 -0
- package/__tests__/utils/fixtures.utils.ts +193 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/cache/cache.d.ts +12 -1
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.events.d.ts +20 -6
- package/dist/cache/cache.events.d.ts.map +1 -1
- package/dist/cache/cache.utils.d.ts +1 -0
- package/dist/cache/cache.utils.d.ts.map +1 -1
- package/dist/client/client.d.ts +3 -3
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.utils.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +26 -5
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.mjs +2127 -1741
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
- package/dist/managers/app/app.manager.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
- package/dist/managers/logger/logger.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.types.d.ts +2 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -1
- package/dist/mocker/mocker.d.ts +1 -1
- package/dist/mocker/mocker.d.ts.map +1 -1
- package/dist/plugin/plugin.d.ts +2 -0
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/request/request.d.ts +1 -1
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/utils/emitter.utils.d.ts.map +1 -1
- package/dist/utils/error.utils.d.ts +17 -0
- package/dist/utils/error.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +46 -18
- package/src/adapter/adapter.utils.ts +47 -7
- package/src/cache/cache.events.ts +24 -8
- package/src/cache/cache.ts +109 -21
- package/src/cache/cache.utils.ts +33 -10
- package/src/client/client.ts +99 -7
- package/src/client/client.utils.ts +10 -4
- package/src/dispatcher/dispatcher.ts +273 -133
- package/src/http-adapter/http-adapter.fetch.ts +15 -3
- package/src/managers/app/app.manager.constants.ts +12 -1
- package/src/managers/app/app.manager.ts +43 -23
- package/src/managers/app/app.manager.utils.ts +8 -4
- package/src/managers/logger/logger.manager.ts +21 -8
- package/src/managers/request/request.manager.ts +7 -1
- package/src/managers/request/request.manager.types.ts +2 -0
- package/src/mocker/mocker.ts +14 -11
- package/src/plugin/plugin.ts +6 -0
- package/src/request/request.ts +31 -22
- package/src/request/request.utils.ts +23 -15
- package/src/utils/emitter.utils.ts +9 -2
- package/src/utils/error.utils.ts +29 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
import { createControlledClient, flushPromises, isPending } from "../../utils/controlled-adapter.utils";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mappers and callbacks are the code of the user running inside the request lifecycle. Whatever they do - return,
|
|
5
|
+
* resolve later, throw or reject - `send()` and `exec()` have to settle and the queue behind them has to move on.
|
|
6
|
+
*/
|
|
7
|
+
describe("Request [ Lifecycle ]", () => {
|
|
8
|
+
const setup = () => {
|
|
9
|
+
const { client, calls } = createControlledClient();
|
|
10
|
+
const request = client.createRequest<{ response: any; payload: any; error: any }>()({
|
|
11
|
+
endpoint: "/lifecycle",
|
|
12
|
+
method: "POST",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return { client, calls, request, dispatcher: client.submitDispatcher };
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.useFakeTimers();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
vi.useRealTimers();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe.each(["send", "exec"] as const)("When the response mapper is used with %s", (method) => {
|
|
27
|
+
it("should give the mapped response", async () => {
|
|
28
|
+
const { request, calls } = setup();
|
|
29
|
+
const mapped = request.setResponseMapper((response): any => ({
|
|
30
|
+
...response,
|
|
31
|
+
data: { name: response.data.NAME },
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
const sent = mapped[method]({});
|
|
35
|
+
await flushPromises();
|
|
36
|
+
await calls[0].succeed({ NAME: "John" });
|
|
37
|
+
|
|
38
|
+
await expect(sent).resolves.toHaveProperty("data", { name: "John" });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should wait for an async mapper", async () => {
|
|
42
|
+
const { request, calls } = setup();
|
|
43
|
+
const mapped = request.setResponseMapper(async (response): Promise<any> => ({ ...response, data: "async" }));
|
|
44
|
+
|
|
45
|
+
const sent = mapped[method]({});
|
|
46
|
+
await flushPromises();
|
|
47
|
+
await calls[0].succeed("raw");
|
|
48
|
+
|
|
49
|
+
await expect(sent).resolves.toHaveProperty("data", "async");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should map the error responses too", async () => {
|
|
53
|
+
const { request, calls } = setup();
|
|
54
|
+
const mapped = request.setResponseMapper((response): any => ({ ...response, error: { code: response.status } }));
|
|
55
|
+
|
|
56
|
+
const sent = mapped[method]({});
|
|
57
|
+
await flushPromises();
|
|
58
|
+
await calls[0].fail({ message: "Failed" }, 422);
|
|
59
|
+
|
|
60
|
+
await expect(sent).resolves.toHaveProperty("error", { code: 422 });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should let the mapper turn a success into an error", async () => {
|
|
64
|
+
const { request, calls } = setup();
|
|
65
|
+
const mapped = request.setResponseMapper((response): any => ({
|
|
66
|
+
...response,
|
|
67
|
+
data: null,
|
|
68
|
+
error: { message: "Invalid" },
|
|
69
|
+
success: false as const,
|
|
70
|
+
}));
|
|
71
|
+
|
|
72
|
+
const sent = mapped[method]({});
|
|
73
|
+
await flushPromises();
|
|
74
|
+
await calls[0].succeed({ invalid: true });
|
|
75
|
+
const response = await sent;
|
|
76
|
+
|
|
77
|
+
expect(response.success).toBe(false);
|
|
78
|
+
expect(response.error).toEqual({ message: "Invalid" });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should give the thrown error instead of hanging", async () => {
|
|
82
|
+
const { request, calls } = setup();
|
|
83
|
+
const error = new Error("Invalid response");
|
|
84
|
+
const mapped = request.setResponseMapper((): any => {
|
|
85
|
+
throw error;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const sent = mapped[method]({});
|
|
89
|
+
await flushPromises();
|
|
90
|
+
await calls[0].succeed({ invalid: true });
|
|
91
|
+
const response = await sent;
|
|
92
|
+
|
|
93
|
+
expect(response.error).toBe(error);
|
|
94
|
+
expect(response.data).toBeNull();
|
|
95
|
+
expect(response.success).toBe(false);
|
|
96
|
+
expect(response.status).toBe(200);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should give the rejection of an async mapper instead of hanging", async () => {
|
|
100
|
+
const { request, calls } = setup();
|
|
101
|
+
const error = new Error("Invalid response");
|
|
102
|
+
const mapped = request.setResponseMapper(async (): Promise<any> => {
|
|
103
|
+
throw error;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const sent = mapped[method]({});
|
|
107
|
+
await flushPromises();
|
|
108
|
+
await calls[0].succeed({ invalid: true });
|
|
109
|
+
const response = await sent;
|
|
110
|
+
|
|
111
|
+
expect(response.error).toBe(error);
|
|
112
|
+
expect(response.success).toBe(false);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("should use the response as it is when the mapper returns nothing", async () => {
|
|
116
|
+
const { request, calls } = setup();
|
|
117
|
+
const mapped = request.setResponseMapper((() => {}) as never);
|
|
118
|
+
|
|
119
|
+
const sent = mapped[method]({});
|
|
120
|
+
await flushPromises();
|
|
121
|
+
await calls[0].succeed("raw");
|
|
122
|
+
|
|
123
|
+
await expect(sent).resolves.toHaveProperty("data", "raw");
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("When the response mapper throws for a request sent with the dispatcher", () => {
|
|
128
|
+
it("should remove the request from the queue", async () => {
|
|
129
|
+
const { client, request, calls, dispatcher } = setup();
|
|
130
|
+
const mapped = request.setResponseMapper((): any => {
|
|
131
|
+
throw new Error("Invalid response");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const sent = mapped.send();
|
|
135
|
+
await flushPromises();
|
|
136
|
+
await calls[0].succeed();
|
|
137
|
+
await sent;
|
|
138
|
+
|
|
139
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
140
|
+
expect(dispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
141
|
+
expect(client.requestManager.abortControllers.size).toBe(0);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("should not block the queued requests behind it", async () => {
|
|
145
|
+
const { request, calls } = setup();
|
|
146
|
+
const queued = request.setQueued(true);
|
|
147
|
+
const broken = queued.setResponseMapper((): any => {
|
|
148
|
+
throw new Error("Invalid response");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const first = broken.send();
|
|
152
|
+
const second = queued.send();
|
|
153
|
+
await flushPromises();
|
|
154
|
+
await calls[0].succeed("first");
|
|
155
|
+
await flushPromises();
|
|
156
|
+
await calls[1].succeed("second");
|
|
157
|
+
|
|
158
|
+
await expect(first).resolves.toHaveProperty("success", false);
|
|
159
|
+
await expect(second).resolves.toHaveProperty("data", "second");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("should call the onResponse callback once", async () => {
|
|
163
|
+
const { request, calls } = setup();
|
|
164
|
+
const onResponse = vi.fn();
|
|
165
|
+
const mapped = request.setResponseMapper(async (): Promise<any> => {
|
|
166
|
+
throw new Error("Invalid response");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const sent = mapped.send({ onResponse });
|
|
170
|
+
await flushPromises();
|
|
171
|
+
await calls[0].succeed();
|
|
172
|
+
await sent;
|
|
173
|
+
|
|
174
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should give the thrown error for a response read from the cache", async () => {
|
|
178
|
+
const { request, calls } = setup();
|
|
179
|
+
let shouldThrow = false;
|
|
180
|
+
const mapped = request.setResponseMapper((response): any => {
|
|
181
|
+
if (shouldThrow) {
|
|
182
|
+
throw new Error("Invalid cache");
|
|
183
|
+
}
|
|
184
|
+
return response;
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const sent = mapped.send();
|
|
188
|
+
await flushPromises();
|
|
189
|
+
await calls[0].succeed("cached");
|
|
190
|
+
await sent;
|
|
191
|
+
shouldThrow = true;
|
|
192
|
+
const response = await mapped.send({ cachePolicy: "cache-first" });
|
|
193
|
+
|
|
194
|
+
expect(response.success).toBe(false);
|
|
195
|
+
expect(response.error.message).toBe("Invalid cache");
|
|
196
|
+
expect(calls).toHaveLength(1);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe.each(["send", "exec"] as const)("When the request mapper is used with %s", (method) => {
|
|
201
|
+
it("should send the mapped request", async () => {
|
|
202
|
+
const { request, calls } = setup();
|
|
203
|
+
const mapped = request.setRequestMapper((current) => current.setHeaders({ "X-Mapped": "1" }));
|
|
204
|
+
|
|
205
|
+
const sent = mapped[method]({});
|
|
206
|
+
await flushPromises();
|
|
207
|
+
await calls[0].succeed();
|
|
208
|
+
await sent;
|
|
209
|
+
|
|
210
|
+
expect(calls[0].request.headers).toEqual({ "X-Mapped": "1" });
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("should wait for an async mapper", async () => {
|
|
214
|
+
const { request, calls } = setup();
|
|
215
|
+
const mapped = request.setRequestMapper(async (current) => current.setPayload({ mapped: true }));
|
|
216
|
+
|
|
217
|
+
const sent = mapped[method]({});
|
|
218
|
+
await flushPromises();
|
|
219
|
+
await calls[0].succeed();
|
|
220
|
+
await sent;
|
|
221
|
+
|
|
222
|
+
expect(calls[0].request.payload).toEqual({ mapped: true });
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("should receive the request with the values given to the call", async () => {
|
|
226
|
+
const { request, calls } = setup();
|
|
227
|
+
const payloads: unknown[] = [];
|
|
228
|
+
const mapped = request.setRequestMapper((current) => {
|
|
229
|
+
payloads.push(current.payload);
|
|
230
|
+
return current;
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const sent = mapped[method]({ payload: { name: "John" } });
|
|
234
|
+
await flushPromises();
|
|
235
|
+
await calls[0].succeed();
|
|
236
|
+
await sent;
|
|
237
|
+
|
|
238
|
+
expect(payloads).toEqual([{ name: "John" }]);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it.each([
|
|
242
|
+
[
|
|
243
|
+
"throws",
|
|
244
|
+
() => () => {
|
|
245
|
+
throw new Error("Invalid request");
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
[
|
|
249
|
+
"rejects",
|
|
250
|
+
() => async () => {
|
|
251
|
+
throw new Error("Invalid request");
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
])("should give the error without sending anything when the mapper %s", async (_, createMapper) => {
|
|
255
|
+
const { request, calls } = setup();
|
|
256
|
+
const mapped = request.setRequestMapper(createMapper() as never);
|
|
257
|
+
|
|
258
|
+
const response = await mapped[method]({});
|
|
259
|
+
|
|
260
|
+
expect(response.error.message).toBe("Invalid request");
|
|
261
|
+
expect(response.success).toBe(false);
|
|
262
|
+
expect(response.status).toBe(0);
|
|
263
|
+
expect(calls).toHaveLength(0);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe("When the payload mapper is used", () => {
|
|
268
|
+
it("should send the mapped payload and keep the original one on the request", async () => {
|
|
269
|
+
const { client, request } = setup();
|
|
270
|
+
const payloads: unknown[] = [];
|
|
271
|
+
client.adapter.setFetcher(async ({ payload, onSuccess }) => {
|
|
272
|
+
payloads.push(payload);
|
|
273
|
+
await onSuccess({ data: null, status: 200, extra: { headers: {} } });
|
|
274
|
+
});
|
|
275
|
+
const mapped = request.setPayloadMapper((payload: { name: string }) => ({ name: payload.name.toUpperCase() }));
|
|
276
|
+
|
|
277
|
+
await mapped.send({ payload: { name: "john" } });
|
|
278
|
+
|
|
279
|
+
expect(payloads).toEqual([{ name: "JOHN" }]);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it.each([
|
|
283
|
+
[
|
|
284
|
+
"throws",
|
|
285
|
+
() => {
|
|
286
|
+
throw new Error("Invalid payload");
|
|
287
|
+
},
|
|
288
|
+
],
|
|
289
|
+
[
|
|
290
|
+
"rejects",
|
|
291
|
+
async () => {
|
|
292
|
+
throw new Error("Invalid payload");
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
])("should give the error when the mapper %s", async (_, mapper) => {
|
|
296
|
+
const { request, calls } = setup();
|
|
297
|
+
|
|
298
|
+
const response = await request.setPayloadMapper(mapper).send({ payload: {} });
|
|
299
|
+
|
|
300
|
+
expect(response.error.message).toBe("Invalid payload");
|
|
301
|
+
expect(response.success).toBe(false);
|
|
302
|
+
expect(calls).toHaveLength(0);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
describe("When send is compared with exec", () => {
|
|
307
|
+
it("should put only the request made with send into the dispatcher queue", async () => {
|
|
308
|
+
const { request, dispatcher } = setup();
|
|
309
|
+
const onQueueChange = vi.fn();
|
|
310
|
+
dispatcher.events.onQueueChange(onQueueChange);
|
|
311
|
+
|
|
312
|
+
request.exec({});
|
|
313
|
+
await flushPromises();
|
|
314
|
+
expect(onQueueChange).not.toHaveBeenCalled();
|
|
315
|
+
request.send();
|
|
316
|
+
await flushPromises();
|
|
317
|
+
|
|
318
|
+
expect(onQueueChange).toHaveBeenCalled();
|
|
319
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("should save only the response of send in the cache", async () => {
|
|
323
|
+
const { request, calls } = setup();
|
|
324
|
+
|
|
325
|
+
const executed = request.exec({});
|
|
326
|
+
await flushPromises();
|
|
327
|
+
await calls[0].succeed("exec");
|
|
328
|
+
await executed;
|
|
329
|
+
expect(request.read()).toBeUndefined();
|
|
330
|
+
|
|
331
|
+
const sent = request.send();
|
|
332
|
+
await flushPromises();
|
|
333
|
+
await calls[1].succeed("send");
|
|
334
|
+
await sent;
|
|
335
|
+
|
|
336
|
+
expect(request.read()?.data).toBe("send");
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("should not retry the request made with exec", async () => {
|
|
340
|
+
const { request, calls } = setup();
|
|
341
|
+
|
|
342
|
+
const executed = request.setRetry(3).setRetryTime(10).exec({});
|
|
343
|
+
await flushPromises();
|
|
344
|
+
await calls[0].fail();
|
|
345
|
+
const response = await executed;
|
|
346
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
347
|
+
|
|
348
|
+
expect(response.success).toBe(false);
|
|
349
|
+
expect(calls).toHaveLength(1);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("should not deduplicate the requests made with exec", async () => {
|
|
353
|
+
const { request, calls } = setup();
|
|
354
|
+
const deduplicated = request.setDeduplicate(true);
|
|
355
|
+
|
|
356
|
+
deduplicated.exec({});
|
|
357
|
+
deduplicated.exec({});
|
|
358
|
+
await flushPromises();
|
|
359
|
+
|
|
360
|
+
expect(calls).toHaveLength(2);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("should send the request made with exec even when the queue is stopped", async () => {
|
|
364
|
+
const { request, calls, dispatcher } = setup();
|
|
365
|
+
dispatcher.stop(request.queryKey);
|
|
366
|
+
|
|
367
|
+
request.exec({});
|
|
368
|
+
request.send();
|
|
369
|
+
await flushPromises();
|
|
370
|
+
|
|
371
|
+
expect(calls).toHaveLength(1);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("should abort the request made with exec", async () => {
|
|
375
|
+
const { request, calls } = setup();
|
|
376
|
+
|
|
377
|
+
const executed = request.exec({});
|
|
378
|
+
await flushPromises();
|
|
379
|
+
request.abort();
|
|
380
|
+
const response = await executed;
|
|
381
|
+
|
|
382
|
+
expect(calls[0].isAborted()).toBe(true);
|
|
383
|
+
expect(response.error.message).toBe("Request aborted");
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it("should use the values given to exec", async () => {
|
|
387
|
+
const { client, calls } = setup();
|
|
388
|
+
const request = client.createRequest<{ payload: any; queryParams: any }>()({
|
|
389
|
+
endpoint: "/users/:userId",
|
|
390
|
+
method: "POST",
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
request.exec({ params: { userId: 3 }, payload: { a: 1 }, queryParams: { page: 2 } });
|
|
394
|
+
await flushPromises();
|
|
395
|
+
|
|
396
|
+
expect(calls[0].request.endpoint).toBe("/users/3");
|
|
397
|
+
expect(calls[0].request.payload).toEqual({ a: 1 });
|
|
398
|
+
expect(calls[0].request.queryParams).toEqual({ page: 2 });
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
describe("When callbacks are passed to send", () => {
|
|
403
|
+
it("should call them in the order of the request lifecycle", async () => {
|
|
404
|
+
const { request, calls } = setup();
|
|
405
|
+
const order: string[] = [];
|
|
406
|
+
|
|
407
|
+
const sent = request.send({
|
|
408
|
+
onBeforeSent: () => order.push("onBeforeSent"),
|
|
409
|
+
onRequestStart: () => order.push("onRequestStart"),
|
|
410
|
+
onUploadProgress: () => order.push("onUploadProgress"),
|
|
411
|
+
onResponseStart: () => order.push("onResponseStart"),
|
|
412
|
+
onDownloadProgress: () => order.push("onDownloadProgress"),
|
|
413
|
+
onResponse: () => order.push("onResponse"),
|
|
414
|
+
onRemove: () => order.push("onRemove"),
|
|
415
|
+
});
|
|
416
|
+
order.push("send returned");
|
|
417
|
+
await flushPromises();
|
|
418
|
+
await calls[0].succeed();
|
|
419
|
+
await sent;
|
|
420
|
+
order.push("promise resolved");
|
|
421
|
+
|
|
422
|
+
expect(order).toEqual([
|
|
423
|
+
"onBeforeSent",
|
|
424
|
+
"send returned",
|
|
425
|
+
"onUploadProgress",
|
|
426
|
+
"onRequestStart",
|
|
427
|
+
"onUploadProgress",
|
|
428
|
+
"onDownloadProgress",
|
|
429
|
+
"onResponseStart",
|
|
430
|
+
"onDownloadProgress",
|
|
431
|
+
"onResponse",
|
|
432
|
+
"promise resolved",
|
|
433
|
+
]);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it("should give the same requestId to every callback", async () => {
|
|
437
|
+
const { request, calls } = setup();
|
|
438
|
+
const ids: string[] = [];
|
|
439
|
+
const collect = ({ requestId }: { requestId: string }) => ids.push(requestId);
|
|
440
|
+
|
|
441
|
+
const sent = request.send({
|
|
442
|
+
onBeforeSent: collect,
|
|
443
|
+
onRequestStart: collect,
|
|
444
|
+
onResponseStart: collect,
|
|
445
|
+
onResponse: collect,
|
|
446
|
+
});
|
|
447
|
+
await flushPromises();
|
|
448
|
+
await calls[0].succeed();
|
|
449
|
+
await sent;
|
|
450
|
+
|
|
451
|
+
expect(ids).toHaveLength(4);
|
|
452
|
+
expect(new Set(ids)).toEqual(new Set([calls[0].requestId]));
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("should call the hooks of the request after the callbacks of the call", async () => {
|
|
456
|
+
const { request, calls } = setup();
|
|
457
|
+
const order: string[] = [];
|
|
458
|
+
request.$hooks.onBeforeSent(() => order.push("hook onBeforeSent"));
|
|
459
|
+
request.$hooks.onResponse(() => order.push("hook onResponse"));
|
|
460
|
+
|
|
461
|
+
const sent = request.send({
|
|
462
|
+
onBeforeSent: () => order.push("onBeforeSent"),
|
|
463
|
+
onResponse: () => order.push("onResponse"),
|
|
464
|
+
});
|
|
465
|
+
await flushPromises();
|
|
466
|
+
await calls[0].succeed();
|
|
467
|
+
await sent;
|
|
468
|
+
|
|
469
|
+
expect(order).toEqual(["onBeforeSent", "hook onBeforeSent", "onResponse", "hook onResponse"]);
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it("should call onResponse once for a retried request - with the final response", async () => {
|
|
473
|
+
const { request, calls } = setup();
|
|
474
|
+
const onResponse = vi.fn();
|
|
475
|
+
const onRequestStart = vi.fn();
|
|
476
|
+
|
|
477
|
+
const sent = request.setRetry(1).setRetryTime(100).send({ onResponse, onRequestStart });
|
|
478
|
+
await flushPromises();
|
|
479
|
+
await calls[0].fail();
|
|
480
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
481
|
+
await calls[1].succeed("retried");
|
|
482
|
+
await sent;
|
|
483
|
+
|
|
484
|
+
expect(onRequestStart).toHaveBeenCalledTimes(2);
|
|
485
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
486
|
+
expect(onResponse.mock.calls[0][0].response.data).toBe("retried");
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it("should call onRemove instead of onResponse for a removed request", async () => {
|
|
490
|
+
const { client, request, dispatcher } = setup();
|
|
491
|
+
const onResponse = vi.fn();
|
|
492
|
+
const onRemove = vi.fn();
|
|
493
|
+
client.appManager.setOnline(false);
|
|
494
|
+
|
|
495
|
+
const sent = request.send({ onResponse, onRemove });
|
|
496
|
+
await flushPromises();
|
|
497
|
+
dispatcher.clearQueue(request.queryKey);
|
|
498
|
+
await sent;
|
|
499
|
+
|
|
500
|
+
expect(onRemove).toHaveBeenCalledTimes(1);
|
|
501
|
+
expect(onResponse).not.toHaveBeenCalled();
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it("should not call the callbacks of a finished request again", async () => {
|
|
505
|
+
const { request, calls } = setup();
|
|
506
|
+
const onResponse = vi.fn();
|
|
507
|
+
|
|
508
|
+
const first = request.send({ onResponse });
|
|
509
|
+
await flushPromises();
|
|
510
|
+
await calls[0].succeed();
|
|
511
|
+
await first;
|
|
512
|
+
const second = request.send();
|
|
513
|
+
await flushPromises();
|
|
514
|
+
await calls[1].succeed();
|
|
515
|
+
await second;
|
|
516
|
+
|
|
517
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
describe("When a callback throws", () => {
|
|
522
|
+
const callbackError = new Error("Broken callback");
|
|
523
|
+
const throwing = () => {
|
|
524
|
+
throw callbackError;
|
|
525
|
+
};
|
|
526
|
+
// The error of a callback is not swallowed - it is thrown from a timer, outside of the request lifecycle
|
|
527
|
+
const reported: unknown[] = [];
|
|
528
|
+
const flush = async () => {
|
|
529
|
+
try {
|
|
530
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
reported.push(error);
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
const getReported = () => {
|
|
536
|
+
try {
|
|
537
|
+
vi.runAllTimers();
|
|
538
|
+
} catch (error) {
|
|
539
|
+
reported.push(error);
|
|
540
|
+
}
|
|
541
|
+
return reported;
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
beforeEach(() => {
|
|
545
|
+
reported.length = 0;
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
it.each(["onBeforeSent", "onRequestStart", "onResponseStart", "onUploadProgress", "onDownloadProgress"] as const)(
|
|
549
|
+
"should still give the response when %s throws",
|
|
550
|
+
async (name) => {
|
|
551
|
+
const { request, calls } = setup();
|
|
552
|
+
|
|
553
|
+
const sent = request.send({ [name]: throwing });
|
|
554
|
+
await flush();
|
|
555
|
+
await calls[0].succeed("done");
|
|
556
|
+
const response = await sent;
|
|
557
|
+
|
|
558
|
+
expect(response.data).toBe("done");
|
|
559
|
+
expect(getReported()).toContain(callbackError);
|
|
560
|
+
},
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
it("should still resolve when onResponse throws", async () => {
|
|
564
|
+
const { client, request, calls, dispatcher } = setup();
|
|
565
|
+
|
|
566
|
+
const sent = request.send({ onResponse: throwing });
|
|
567
|
+
await flush();
|
|
568
|
+
await calls[0].succeed("done");
|
|
569
|
+
const response = await sent;
|
|
570
|
+
|
|
571
|
+
expect(response.data).toBe("done");
|
|
572
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
573
|
+
expect(client.requestManager.abortControllers.size).toBe(0);
|
|
574
|
+
expect(getReported()).toContain(callbackError);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("should still resolve when onRemove throws", async () => {
|
|
578
|
+
const { client, request, dispatcher } = setup();
|
|
579
|
+
client.appManager.setOnline(false);
|
|
580
|
+
|
|
581
|
+
const sent = request.send({ onRemove: throwing });
|
|
582
|
+
await flush();
|
|
583
|
+
dispatcher.clearQueue(request.queryKey);
|
|
584
|
+
const response = await sent;
|
|
585
|
+
|
|
586
|
+
expect(response.error.message).toBe("Request deleted");
|
|
587
|
+
expect(getReported()).toContain(callbackError);
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
it("should still resolve when a hook of the request throws", async () => {
|
|
591
|
+
const { request, calls } = setup();
|
|
592
|
+
const onResponse = vi.fn();
|
|
593
|
+
request.$hooks.onResponse(throwing);
|
|
594
|
+
|
|
595
|
+
const sent = request.send({ onResponse });
|
|
596
|
+
await flush();
|
|
597
|
+
await calls[0].succeed("done");
|
|
598
|
+
const response = await sent;
|
|
599
|
+
|
|
600
|
+
expect(response.data).toBe("done");
|
|
601
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
602
|
+
expect(getReported()).toContain(callbackError);
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
it("should not block the queued requests behind it", async () => {
|
|
606
|
+
const { request, calls } = setup();
|
|
607
|
+
const queued = request.setQueued(true);
|
|
608
|
+
|
|
609
|
+
const first = queued.send({ onResponse: throwing });
|
|
610
|
+
const second = queued.send();
|
|
611
|
+
await flush();
|
|
612
|
+
await calls[0].succeed("first");
|
|
613
|
+
await flush();
|
|
614
|
+
expect(calls).toHaveLength(2);
|
|
615
|
+
await calls[1].succeed("second");
|
|
616
|
+
|
|
617
|
+
await expect(first).resolves.toHaveProperty("data", "first");
|
|
618
|
+
await expect(second).resolves.toHaveProperty("data", "second");
|
|
619
|
+
expect(getReported()).toContain(callbackError);
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it("should not leave the request in the queue when a listener of the response event throws", async () => {
|
|
623
|
+
const { client, request, calls, dispatcher } = setup();
|
|
624
|
+
const onLoading = vi.fn();
|
|
625
|
+
client.requestManager.events.onResponse(throwing);
|
|
626
|
+
client.requestManager.events.onLoading(onLoading);
|
|
627
|
+
|
|
628
|
+
request.send();
|
|
629
|
+
await flush();
|
|
630
|
+
await calls[0].succeed("done");
|
|
631
|
+
await flush();
|
|
632
|
+
|
|
633
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
634
|
+
expect(onLoading).toHaveBeenLastCalledWith(expect.objectContaining({ loading: false }));
|
|
635
|
+
expect(request.read()?.data).toBe("done");
|
|
636
|
+
expect(getReported()).toContain(callbackError);
|
|
637
|
+
});
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
describe("When the optimistic callback is set", () => {
|
|
641
|
+
it("should pass its context to the callbacks", async () => {
|
|
642
|
+
const { request, calls } = setup();
|
|
643
|
+
const contexts: unknown[] = [];
|
|
644
|
+
const optimistic = request.setOptimistic(() => ({ context: { snapshot: 1 } }));
|
|
645
|
+
|
|
646
|
+
const sent = optimistic.send({
|
|
647
|
+
onBeforeSent: ({ mutationContext }) => contexts.push(mutationContext?.context),
|
|
648
|
+
onResponse: ({ mutationContext }) => contexts.push(mutationContext?.context),
|
|
649
|
+
});
|
|
650
|
+
await flushPromises();
|
|
651
|
+
await calls[0].succeed();
|
|
652
|
+
await sent;
|
|
653
|
+
|
|
654
|
+
expect(contexts).toEqual([{ snapshot: 1 }, { snapshot: 1 }]);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
it("should give the error without sending when the callback throws", async () => {
|
|
658
|
+
const { request, calls, dispatcher } = setup();
|
|
659
|
+
const optimistic = request.setOptimistic(() => {
|
|
660
|
+
throw new Error("No snapshot");
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
const response = await optimistic.send();
|
|
664
|
+
|
|
665
|
+
expect(response.success).toBe(false);
|
|
666
|
+
expect(response.error.message).toBe("Optimistic callback failed: No snapshot");
|
|
667
|
+
expect(calls).toHaveLength(0);
|
|
668
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
describe("When nothing answers", () => {
|
|
673
|
+
it("should keep the promise pending without resolving on its own", async () => {
|
|
674
|
+
const { request } = setup();
|
|
675
|
+
|
|
676
|
+
const sent = request.send();
|
|
677
|
+
await vi.advanceTimersByTimeAsync(600_000);
|
|
678
|
+
|
|
679
|
+
expect(await isPending(sent)).toBe(true);
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
});
|