@hyper-fetch/core 9.6.0 → 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,308 @@
|
|
|
1
|
+
import { createControlledClient, flushPromises, isPending } from "../../utils/controlled-adapter.utils";
|
|
2
|
+
|
|
3
|
+
describe("Mocker [ Outcomes ]", () => {
|
|
4
|
+
const fast = { requestTime: 0, responseTime: 0 };
|
|
5
|
+
|
|
6
|
+
const setup = () => {
|
|
7
|
+
const { client, calls } = createControlledClient();
|
|
8
|
+
const request = client.createRequest<{ response: any; error: any; payload: any }>()({
|
|
9
|
+
endpoint: "/mocked",
|
|
10
|
+
method: "POST",
|
|
11
|
+
});
|
|
12
|
+
/** Send and let the timers of the mocker pass */
|
|
13
|
+
const run = async <T>(sent: Promise<T>, time = 1000) => {
|
|
14
|
+
await vi.advanceTimersByTimeAsync(time);
|
|
15
|
+
return sent;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return { client, calls, request, run };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.useFakeTimers();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
vi.useRealTimers();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("When deciding if the mocked response is a success", () => {
|
|
30
|
+
it.each([
|
|
31
|
+
["data only", { data: { id: 1 }, status: 200 }, true],
|
|
32
|
+
["falsy data", { data: 0, status: 200 }, true],
|
|
33
|
+
["an empty string as data", { data: "", status: 200 }, true],
|
|
34
|
+
["null data without an error", { data: null, status: 204 }, true],
|
|
35
|
+
["an error only", { error: { message: "Bad" }, status: 400 }, false],
|
|
36
|
+
["null data with an error", { data: null, error: { message: "Bad" }, status: 500 }, false],
|
|
37
|
+
["data together with an error", { data: { id: 1 }, error: { message: "Partial" }, status: 200 }, true],
|
|
38
|
+
["data marked as a failure", { data: { id: 1 }, status: 400, success: false }, false],
|
|
39
|
+
["an error marked as a failure", { error: { message: "Bad" }, status: 400, success: false }, false],
|
|
40
|
+
["an error marked as a success", { error: { message: "Bad" }, status: 200, success: true }, true],
|
|
41
|
+
] as const)("should treat %s accordingly", async (_, mock, success) => {
|
|
42
|
+
const { request, run } = setup();
|
|
43
|
+
|
|
44
|
+
const response = await run(request.setMock(() => mock as never, fast).send());
|
|
45
|
+
|
|
46
|
+
expect(response.success).toBe(success);
|
|
47
|
+
expect(response.status).toBe(mock.status);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should give the error of a failed mock without the data", async () => {
|
|
51
|
+
const { request, run } = setup();
|
|
52
|
+
request.setMock(() => ({ error: { message: "Bad" }, status: 400 }), fast);
|
|
53
|
+
|
|
54
|
+
const response = await run(request.send());
|
|
55
|
+
|
|
56
|
+
expect(response).toMatchObject({ data: null, error: { message: "Bad" }, status: 400, success: false });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("should retry a mock which gives only an error", async () => {
|
|
60
|
+
const { request, run } = setup();
|
|
61
|
+
const mock = vi.fn(() => ({ error: { message: "Bad" }, status: 500 }));
|
|
62
|
+
|
|
63
|
+
await run(request.setRetry(2).setRetryTime(10).setMock(mock, fast).send());
|
|
64
|
+
|
|
65
|
+
expect(mock).toHaveBeenCalledTimes(3);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should give both the data and the error of a partial response", async () => {
|
|
69
|
+
const { request, run } = setup();
|
|
70
|
+
request.setMock(() => ({ data: { id: 1 }, error: { message: "Partial" }, status: 200 }), fast);
|
|
71
|
+
|
|
72
|
+
const response = await run(request.send());
|
|
73
|
+
|
|
74
|
+
expect(response).toMatchObject({ data: { id: 1 }, error: { message: "Partial" }, success: true });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should use the default extra of the adapter and the one given by the mock", async () => {
|
|
78
|
+
const { request, run } = setup();
|
|
79
|
+
|
|
80
|
+
const withDefault = await run(request.setMock(() => ({ data: 1, status: 200 }), fast).send());
|
|
81
|
+
const withOwn = await run(
|
|
82
|
+
request.setMock(() => ({ data: 1, status: 200, extra: { headers: { "x-mock": "1" } } }), fast).send(),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
expect(withDefault.extra).toEqual({ headers: {} });
|
|
86
|
+
expect(withOwn.extra).toEqual({ headers: { "x-mock": "1" } });
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("When the mock function is called", () => {
|
|
91
|
+
it("should receive the request with the values of the call", async () => {
|
|
92
|
+
const { client, run } = setup();
|
|
93
|
+
const request = client.createRequest<{ response: any; payload: any }>()({
|
|
94
|
+
endpoint: "/mocked/:id",
|
|
95
|
+
method: "POST",
|
|
96
|
+
});
|
|
97
|
+
const mock = vi.fn(() => ({ data: null, status: 200 }));
|
|
98
|
+
request.setMock(mock, fast);
|
|
99
|
+
|
|
100
|
+
await run(request.send({ params: { id: 7 }, payload: { name: "John" } }));
|
|
101
|
+
const [{ request: mocked, requestId }] = mock.mock.calls[0] as unknown as [{ request: any; requestId: string }];
|
|
102
|
+
|
|
103
|
+
expect(mocked.endpoint).toBe("/mocked/7");
|
|
104
|
+
expect(mocked.params).toEqual({ id: 7 });
|
|
105
|
+
expect(mocked.payload).toEqual({ name: "John" });
|
|
106
|
+
expect(requestId).toEqual(expect.any(String));
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("should wait for an async mock", async () => {
|
|
110
|
+
const { request, run } = setup();
|
|
111
|
+
request.setMock(async () => ({ data: "async", status: 200 }), fast);
|
|
112
|
+
|
|
113
|
+
await expect(run(request.send())).resolves.toHaveProperty("data", "async");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it.each([
|
|
117
|
+
[
|
|
118
|
+
"throws",
|
|
119
|
+
() => {
|
|
120
|
+
throw new Error("Broken mock");
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
[
|
|
124
|
+
"rejects",
|
|
125
|
+
async () => {
|
|
126
|
+
throw new Error("Broken mock");
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
])("should give the error when the mock %s", async (_, mock) => {
|
|
130
|
+
const { request, run, client } = setup();
|
|
131
|
+
|
|
132
|
+
const response = await run(request.setMock(mock as never, fast).send());
|
|
133
|
+
|
|
134
|
+
expect(response.success).toBe(false);
|
|
135
|
+
expect(response.status).toBe(0);
|
|
136
|
+
expect(response.error.message).toBe("Broken mock");
|
|
137
|
+
expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should not call the real adapter", async () => {
|
|
141
|
+
const { request, run, calls } = setup();
|
|
142
|
+
|
|
143
|
+
await run(request.setMock(() => ({ data: 1, status: 200 }), fast).send());
|
|
144
|
+
|
|
145
|
+
expect(calls).toHaveLength(0);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("should be used by exec too", async () => {
|
|
149
|
+
const { request, run } = setup();
|
|
150
|
+
|
|
151
|
+
const response = await run(request.setMock(() => ({ data: "exec", status: 200 }), fast).exec({}));
|
|
152
|
+
|
|
153
|
+
expect(response.data).toBe("exec");
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("When mocking is switched off", () => {
|
|
158
|
+
it("should use the real adapter for a request with the disabled mock", async () => {
|
|
159
|
+
const { request, calls } = setup();
|
|
160
|
+
request.setMock(() => ({ data: "mock", status: 200 }), fast).setMockingEnabled(false);
|
|
161
|
+
|
|
162
|
+
const sent = request.send();
|
|
163
|
+
await flushPromises();
|
|
164
|
+
await calls[0].succeed("real");
|
|
165
|
+
|
|
166
|
+
await expect(sent).resolves.toHaveProperty("data", "real");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("should use the mock again after enabling it", async () => {
|
|
170
|
+
const { request, run } = setup();
|
|
171
|
+
request.setMock(() => ({ data: "mock", status: 200 }), fast).setMockingEnabled(false);
|
|
172
|
+
request.setMockingEnabled(true);
|
|
173
|
+
|
|
174
|
+
await expect(run(request.send())).resolves.toHaveProperty("data", "mock");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should use the real adapter after clearing the mock", async () => {
|
|
178
|
+
const { request, calls } = setup();
|
|
179
|
+
request.setMock(() => ({ data: "mock", status: 200 }), fast).clearMock();
|
|
180
|
+
|
|
181
|
+
const sent = request.send();
|
|
182
|
+
await flushPromises();
|
|
183
|
+
await calls[0].succeed("real");
|
|
184
|
+
|
|
185
|
+
await expect(sent).resolves.toHaveProperty("data", "real");
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("should use the real adapter when mocking is disabled for the whole client", async () => {
|
|
189
|
+
const { client, request, calls } = setup();
|
|
190
|
+
request.setMock(() => ({ data: "mock", status: 200 }), fast);
|
|
191
|
+
client.setEnableGlobalMocking(false);
|
|
192
|
+
|
|
193
|
+
const sent = request.send();
|
|
194
|
+
await flushPromises();
|
|
195
|
+
await calls[0].succeed("real");
|
|
196
|
+
|
|
197
|
+
await expect(sent).resolves.toHaveProperty("data", "real");
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("When the mock takes time", () => {
|
|
202
|
+
it("should answer after the request and the response time", async () => {
|
|
203
|
+
const { request } = setup();
|
|
204
|
+
request.setMock(() => ({ data: 1, status: 200 }), { requestTime: 100, responseTime: 200 });
|
|
205
|
+
|
|
206
|
+
const sent = request.send();
|
|
207
|
+
await vi.advanceTimersByTimeAsync(299);
|
|
208
|
+
expect(await isPending(sent)).toBe(true);
|
|
209
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
210
|
+
|
|
211
|
+
await expect(sent).resolves.toHaveProperty("data", 1);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should report the whole upload and download", async () => {
|
|
215
|
+
const { request, run } = setup();
|
|
216
|
+
const upload: number[] = [];
|
|
217
|
+
const download: number[] = [];
|
|
218
|
+
request.setMock(() => ({ data: 1, status: 200 }), {
|
|
219
|
+
requestTime: 60,
|
|
220
|
+
responseTime: 60,
|
|
221
|
+
totalUploaded: 300,
|
|
222
|
+
totalDownloaded: 900,
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
await run(
|
|
226
|
+
request.send({
|
|
227
|
+
onUploadProgress: ({ loaded }) => upload.push(loaded),
|
|
228
|
+
onDownloadProgress: ({ loaded }) => download.push(loaded),
|
|
229
|
+
}),
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
expect(upload.at(-1)).toBe(300);
|
|
233
|
+
expect(download.at(-1)).toBe(900);
|
|
234
|
+
expect(upload).toEqual(upload.toSorted((a, b) => a - b));
|
|
235
|
+
expect(download).toEqual(download.toSorted((a, b) => a - b));
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("should call the lifecycle callbacks in order", async () => {
|
|
239
|
+
const { request, run } = setup();
|
|
240
|
+
const order: string[] = [];
|
|
241
|
+
request.setMock(() => ({ data: 1, status: 200 }), fast);
|
|
242
|
+
|
|
243
|
+
await run(
|
|
244
|
+
request.send({
|
|
245
|
+
onBeforeSent: () => order.push("onBeforeSent"),
|
|
246
|
+
onRequestStart: () => order.push("onRequestStart"),
|
|
247
|
+
onResponseStart: () => order.push("onResponseStart"),
|
|
248
|
+
onResponse: () => order.push("onResponse"),
|
|
249
|
+
}),
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
expect(order).toEqual(["onBeforeSent", "onRequestStart", "onResponseStart", "onResponse"]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("should give the abort error and stop reporting the progress after an abort", async () => {
|
|
256
|
+
const { request, run } = setup();
|
|
257
|
+
const onUploadProgress = vi.fn();
|
|
258
|
+
const onResponseStart = vi.fn();
|
|
259
|
+
request.setMock(() => ({ data: 1, status: 200 }), { requestTime: 1000, responseTime: 1000 });
|
|
260
|
+
|
|
261
|
+
const sent = request.send({ onUploadProgress, onResponseStart });
|
|
262
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
263
|
+
request.abort();
|
|
264
|
+
const callsBeforeAbort = onUploadProgress.mock.calls.length;
|
|
265
|
+
const response = await run(sent, 5000);
|
|
266
|
+
|
|
267
|
+
expect(response.error.message).toBe("Request aborted");
|
|
268
|
+
expect(onUploadProgress).toHaveBeenCalledTimes(callsBeforeAbort);
|
|
269
|
+
expect(onResponseStart).not.toHaveBeenCalled();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should give one response for an aborted mock", async () => {
|
|
273
|
+
const { client, request, run } = setup();
|
|
274
|
+
const onResponse = vi.fn();
|
|
275
|
+
client.requestManager.events.onResponse(onResponse);
|
|
276
|
+
request.setMock(() => ({ data: 1, status: 200 }), { requestTime: 100, responseTime: 100 });
|
|
277
|
+
|
|
278
|
+
const sent = request.send();
|
|
279
|
+
await vi.advanceTimersByTimeAsync(50);
|
|
280
|
+
request.abort();
|
|
281
|
+
await run(sent, 5000);
|
|
282
|
+
|
|
283
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("should give the timeout error when the mock is slower than its timeout", async () => {
|
|
287
|
+
const { request, run } = setup();
|
|
288
|
+
request.setMock(() => ({ data: 1, status: 200 }), { requestTime: 500, responseTime: 500, timeout: 300 });
|
|
289
|
+
|
|
290
|
+
const response = await run(request.send(), 5000);
|
|
291
|
+
|
|
292
|
+
expect(response.success).toBe(false);
|
|
293
|
+
expect(response.error.message).toBe("Request timeout");
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("should give the response when the mock is faster than its timeout", async () => {
|
|
297
|
+
const { client, request, run } = setup();
|
|
298
|
+
const onResponse = vi.fn();
|
|
299
|
+
client.requestManager.events.onResponse(onResponse);
|
|
300
|
+
request.setMock(() => ({ data: 1, status: 200 }), { requestTime: 20, responseTime: 20, timeout: 300 });
|
|
301
|
+
|
|
302
|
+
const response = await run(request.send(), 5000);
|
|
303
|
+
|
|
304
|
+
expect(response.data).toBe(1);
|
|
305
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
});
|