@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,220 @@
|
|
|
1
|
+
import { Client } from "client";
|
|
2
|
+
import { Plugin } from "plugin";
|
|
3
|
+
import type { RequestInstance, RequestSendOptionsType } from "request";
|
|
4
|
+
|
|
5
|
+
import { getThrownErrors } from "../../utils/fixtures.utils";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Callbacks, listeners, plugins and optimistic handlers are the code of the user running inside of the request
|
|
9
|
+
* lifecycle. All of them are handled the same way: the lifecycle goes on, and the error is thrown outside of it,
|
|
10
|
+
* so it reaches the error handlers of the environment instead of getting lost.
|
|
11
|
+
*/
|
|
12
|
+
describe("Client [ Errors of the user code ]", () => {
|
|
13
|
+
const failure = new Error("User code failed");
|
|
14
|
+
const throwing = () => {
|
|
15
|
+
throw failure;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const setup = (outcome: "success" | "error" = "success") => {
|
|
19
|
+
const client = new Client({ url: "http://user-errors" });
|
|
20
|
+
const list = client.createRequest<{ response: string[] }>()({ endpoint: "/list" });
|
|
21
|
+
const request = client
|
|
22
|
+
.createRequest<{ response: { id: number }; error: { message: string } }>()({ endpoint: "/users", method: "POST" })
|
|
23
|
+
.setMock(
|
|
24
|
+
() =>
|
|
25
|
+
outcome === "success" ? { data: { id: 1 }, status: 200 } : { error: { message: "Failed" }, status: 400 },
|
|
26
|
+
{ requestTime: 0, responseTime: 0 },
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return { client, request, list };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type Setup = ReturnType<typeof setup>;
|
|
33
|
+
type Scenario = {
|
|
34
|
+
name: string;
|
|
35
|
+
outcome?: "success" | "error";
|
|
36
|
+
/** Puts the throwing code in place, returns the request to send and the options for it */
|
|
37
|
+
arrange: (
|
|
38
|
+
context: Setup,
|
|
39
|
+
) => { request?: RequestInstance; options?: RequestSendOptionsType<RequestInstance> } | void;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const scenarios: Scenario[] = [
|
|
43
|
+
{ name: "the onBeforeSent callback", arrange: () => ({ options: { onBeforeSent: throwing } }) },
|
|
44
|
+
{ name: "the onRequestStart callback", arrange: () => ({ options: { onRequestStart: throwing } }) },
|
|
45
|
+
{ name: "the onResponseStart callback", arrange: () => ({ options: { onResponseStart: throwing } }) },
|
|
46
|
+
{ name: "the onResponse callback", arrange: () => ({ options: { onResponse: throwing } }) },
|
|
47
|
+
{
|
|
48
|
+
name: "the onResponse callback of a failed request",
|
|
49
|
+
outcome: "error",
|
|
50
|
+
arrange: () => ({ options: { onResponse: throwing } }),
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "a hook of the request",
|
|
54
|
+
arrange: ({ request }) => {
|
|
55
|
+
request.$hooks.onResponse(throwing);
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "a listener of the response event",
|
|
60
|
+
arrange: ({ client }) => {
|
|
61
|
+
client.requestManager.events.onResponse(throwing);
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "a listener of the loading event",
|
|
66
|
+
arrange: ({ client }) => {
|
|
67
|
+
client.requestManager.events.onLoading(({ loading }) => {
|
|
68
|
+
if (!loading) {
|
|
69
|
+
throwing();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: "a listener of the cache data",
|
|
76
|
+
arrange: ({ client, request }) => {
|
|
77
|
+
client.cache.events.onDataByKey(request.cacheKey, throwing);
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
...(["onRequestTrigger", "onRequestStart", "onRequestSuccess", "onRequestFinished"] as const).map(
|
|
81
|
+
(hook): Scenario => ({
|
|
82
|
+
name: `the ${hook} hook of a plugin`,
|
|
83
|
+
arrange: ({ client }) => {
|
|
84
|
+
client.addPlugin(new Plugin({ name: "failing" })[hook](throwing));
|
|
85
|
+
},
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
{
|
|
89
|
+
name: "the onRequestError hook of a plugin",
|
|
90
|
+
outcome: "error",
|
|
91
|
+
arrange: ({ client }) => {
|
|
92
|
+
client.addPlugin(new Plugin({ name: "failing" }).onRequestError(throwing));
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "the onCacheItemChange hook of a plugin",
|
|
97
|
+
arrange: ({ client }) => {
|
|
98
|
+
client.addPlugin(new Plugin({ name: "failing" }).onCacheItemChange(throwing));
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: "the onDispatcherItemDeleted hook of a plugin",
|
|
103
|
+
arrange: ({ client }) => {
|
|
104
|
+
client.addPlugin(new Plugin({ name: "failing" }).onDispatcherItemDeleted(throwing));
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: "the optimistic rollback",
|
|
109
|
+
outcome: "error",
|
|
110
|
+
arrange: ({ request }) => ({ request: request.setOptimistic(() => ({ rollback: throwing })) }),
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: "the async optimistic rollback",
|
|
114
|
+
outcome: "error",
|
|
115
|
+
arrange: ({ request }) => ({
|
|
116
|
+
request: request.setOptimistic(() => ({
|
|
117
|
+
rollback: async () => {
|
|
118
|
+
throw failure;
|
|
119
|
+
},
|
|
120
|
+
})),
|
|
121
|
+
}),
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: "a listener of the optimistic invalidation",
|
|
125
|
+
arrange: ({ client, request, list }) => {
|
|
126
|
+
client.cache.events.onInvalidateByKey(list.cacheKey, throwing);
|
|
127
|
+
return { request: request.setOptimistic(() => ({ invalidate: [list] })) };
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
describe.each(scenarios)("When $name throws", ({ arrange, outcome = "success" }) => {
|
|
133
|
+
const send = async () => {
|
|
134
|
+
const context = setup(outcome);
|
|
135
|
+
const arranged = arrange(context) || {};
|
|
136
|
+
const sent = arranged.request ?? context.request;
|
|
137
|
+
let response: Awaited<ReturnType<typeof sent.send>> | undefined;
|
|
138
|
+
|
|
139
|
+
const thrown = await getThrownErrors(async () => {
|
|
140
|
+
response = await sent.send(arranged.options);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return { ...context, response, thrown };
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
it("should still give the response", async () => {
|
|
147
|
+
const { response } = await send();
|
|
148
|
+
|
|
149
|
+
expect(response?.success).toBe(outcome === "success");
|
|
150
|
+
expect(response?.status).toBe(outcome === "success" ? 200 : 400);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should throw the error outside of the request lifecycle, once", async () => {
|
|
154
|
+
const { thrown } = await send();
|
|
155
|
+
|
|
156
|
+
expect(thrown).toStrictEqual([failure]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("should leave nothing in the queue", async () => {
|
|
160
|
+
const { client, request } = await send();
|
|
161
|
+
|
|
162
|
+
expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
163
|
+
expect(client.submitDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("When several pieces of the user code throw for one request", () => {
|
|
168
|
+
it("should throw every error", async () => {
|
|
169
|
+
const { client, request } = setup();
|
|
170
|
+
const pluginError = new Error("Plugin failed");
|
|
171
|
+
const listenerError = new Error("Listener failed");
|
|
172
|
+
const callbackError = new Error("Callback failed");
|
|
173
|
+
client.addPlugin(
|
|
174
|
+
new Plugin({ name: "failing" }).onRequestSuccess(() => {
|
|
175
|
+
throw pluginError;
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
client.requestManager.events.onResponse(() => {
|
|
179
|
+
throw listenerError;
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const thrown = await getThrownErrors(() =>
|
|
183
|
+
request.send({
|
|
184
|
+
onBeforeSent: () => {
|
|
185
|
+
throw callbackError;
|
|
186
|
+
},
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
expect(thrown).toHaveLength(3);
|
|
191
|
+
expect(thrown).toStrictEqual(expect.arrayContaining([pluginError, listenerError, callbackError]));
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("should still call the code registered after the failing one", async () => {
|
|
195
|
+
const { client, request } = setup();
|
|
196
|
+
const nextPlugin = vi.fn();
|
|
197
|
+
const onResponse = vi.fn();
|
|
198
|
+
client
|
|
199
|
+
.addPlugin(new Plugin({ name: "failing" }).onRequestSuccess(throwing))
|
|
200
|
+
.addPlugin(new Plugin({ name: "next" }).onRequestSuccess(nextPlugin));
|
|
201
|
+
request.$hooks.onResponse(throwing);
|
|
202
|
+
|
|
203
|
+
await getThrownErrors(() => request.send({ onResponse }));
|
|
204
|
+
|
|
205
|
+
expect(nextPlugin).toHaveBeenCalledTimes(1);
|
|
206
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe("When nothing throws", () => {
|
|
211
|
+
it("should not throw anything outside of the lifecycle", async () => {
|
|
212
|
+
const { client, request } = setup();
|
|
213
|
+
client.addPlugin(new Plugin({ name: "quiet" }).onRequestSuccess(() => {}));
|
|
214
|
+
|
|
215
|
+
const thrown = await getThrownErrors(() => request.send({ onResponse: () => {} }));
|
|
216
|
+
|
|
217
|
+
expect(thrown).toStrictEqual([]);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
});
|