@hyper-fetch/core 10.4.1 → 10.5.1
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/request/request.exec.spec.ts +129 -0
- package/__tests__/features/request/request.signal.spec.ts +243 -0
- package/__tests__/features/request/request.throw-on-error.spec.ts +219 -0
- package/__tests__/features/request/request.typescript.spec.ts +46 -0
- package/dist/index.mjs +1529 -1394
- package/dist/index.mjs.map +1 -1
- package/dist/infinite/infinite.d.ts.map +1 -1
- package/dist/request/request.d.ts +10 -2
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.types.d.ts +15 -0
- package/dist/request/request.types.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts +19 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/sdk/sdk.d.ts +1 -0
- package/dist/sdk/sdk.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/infinite/infinite.ts +2 -1
- package/src/request/request.ts +95 -26
- package/src/request/request.types.ts +15 -0
- package/src/request/request.utils.ts +148 -46
- package/src/sdk/sdk.ts +4 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
import { getErrorMessage } from "adapter";
|
|
3
|
+
import { Client } from "client";
|
|
4
|
+
|
|
5
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
6
|
+
|
|
7
|
+
describe("Request [ exec ]", () => {
|
|
8
|
+
const fixture = { id: 1 };
|
|
9
|
+
|
|
10
|
+
let client = new Client({ url: "shared-base-url" });
|
|
11
|
+
let request = client.createRequest<{ response: { id: number } }>()({
|
|
12
|
+
endpoint: "/users",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
beforeAll(() => {
|
|
16
|
+
startServer();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
client = new Client({ url: "shared-base-url" });
|
|
21
|
+
request = client.createRequest<{ response: { id: number } }>()({
|
|
22
|
+
endpoint: "/users",
|
|
23
|
+
});
|
|
24
|
+
resetMocks();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterAll(() => {
|
|
28
|
+
stopServer();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("when the request lifecycle runs", () => {
|
|
32
|
+
it("should call the lifecycle callbacks given to exec()", async () => {
|
|
33
|
+
mockRequest(request, { data: fixture });
|
|
34
|
+
const onBeforeSent = vi.fn();
|
|
35
|
+
const onRequestStart = vi.fn();
|
|
36
|
+
const onResponseStart = vi.fn();
|
|
37
|
+
const onResponse = vi.fn();
|
|
38
|
+
|
|
39
|
+
const response = await request.exec({
|
|
40
|
+
onBeforeSent,
|
|
41
|
+
onRequestStart,
|
|
42
|
+
onResponseStart,
|
|
43
|
+
onResponse,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(response.data).toStrictEqual(fixture);
|
|
47
|
+
expect(onBeforeSent).toHaveBeenCalledOnce();
|
|
48
|
+
expect(onRequestStart).toHaveBeenCalledOnce();
|
|
49
|
+
expect(onResponseStart).toHaveBeenCalledOnce();
|
|
50
|
+
expect(onResponse).toHaveBeenCalledOnce();
|
|
51
|
+
expect(onResponse).toHaveBeenCalledWith(
|
|
52
|
+
expect.objectContaining({
|
|
53
|
+
requestId: onBeforeSent.mock.calls[0][0].requestId,
|
|
54
|
+
response: expect.objectContaining({ data: fixture, success: true }),
|
|
55
|
+
details: expect.objectContaining({ isCanceled: false, retries: 0 }),
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should fire the $hooks of the request", async () => {
|
|
61
|
+
mockRequest(request, { data: fixture });
|
|
62
|
+
const onBeforeSent = vi.fn();
|
|
63
|
+
const onRequestStart = vi.fn();
|
|
64
|
+
const onResponseStart = vi.fn();
|
|
65
|
+
const onResponse = vi.fn();
|
|
66
|
+
request.$hooks.onBeforeSent(onBeforeSent);
|
|
67
|
+
request.$hooks.onRequestStart(onRequestStart);
|
|
68
|
+
request.$hooks.onResponseStart(onResponseStart);
|
|
69
|
+
request.$hooks.onResponse(onResponse);
|
|
70
|
+
|
|
71
|
+
await request.exec();
|
|
72
|
+
|
|
73
|
+
expect(onBeforeSent).toHaveBeenCalledOnce();
|
|
74
|
+
expect(onRequestStart).toHaveBeenCalledOnce();
|
|
75
|
+
expect(onResponseStart).toHaveBeenCalledOnce();
|
|
76
|
+
expect(onResponse).toHaveBeenCalledOnce();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should give onResponse the adapter response and resolve with the mapped one", async () => {
|
|
80
|
+
mockRequest(request, { data: fixture });
|
|
81
|
+
const onResponse = vi.fn();
|
|
82
|
+
const mapped = request.setResponseMapper((res) =>
|
|
83
|
+
res.success ? { ...res, data: { id: res.data.id + 1 } } : res,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const response = await mapped.exec({ onResponse });
|
|
87
|
+
|
|
88
|
+
expect(response.data).toStrictEqual({ id: 2 });
|
|
89
|
+
expect(onResponse).toHaveBeenCalledWith(
|
|
90
|
+
expect.objectContaining({
|
|
91
|
+
response: expect.objectContaining({ data: { id: 1 } }),
|
|
92
|
+
}),
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("when the call changes the keys", () => {
|
|
98
|
+
it("should send with the abort key given to the call", async () => {
|
|
99
|
+
mockRequest(request, { data: fixture });
|
|
100
|
+
|
|
101
|
+
const response = await request.exec({ abortKey: "custom-abort-key" });
|
|
102
|
+
|
|
103
|
+
expect(response.success).toBeTrue();
|
|
104
|
+
expect(response.data).toStrictEqual(fixture);
|
|
105
|
+
expect(client.requestManager.abortControllers.size).toBe(0);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should be aborted by the abort key given to the call", async () => {
|
|
109
|
+
let release = () => {};
|
|
110
|
+
mockRequest(request, {
|
|
111
|
+
data: fixture,
|
|
112
|
+
until: new Promise<void>((resolve) => {
|
|
113
|
+
release = resolve;
|
|
114
|
+
}),
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const executed = request.exec({ abortKey: "custom-abort-key" });
|
|
118
|
+
await new Promise((resolve) => {
|
|
119
|
+
setTimeout(resolve, 0);
|
|
120
|
+
});
|
|
121
|
+
client.requestManager.abortByKey("custom-abort-key");
|
|
122
|
+
release();
|
|
123
|
+
const response = await executed;
|
|
124
|
+
|
|
125
|
+
expect(response.success).toBeFalse();
|
|
126
|
+
expect(response.error).toStrictEqual(getErrorMessage("abort"));
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { getErrorMessage } from "adapter";
|
|
2
|
+
|
|
3
|
+
import { createControlledClient, flushPromises, isPending } from "../../utils/controlled-adapter.utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The `signal` of a call only triggers the abort controller HyperFetch keeps for that call, so the call ends the same
|
|
7
|
+
* way `request.abort()` ends it - only without touching the other calls of the same abort key.
|
|
8
|
+
*/
|
|
9
|
+
describe("Request [ Signal ]", () => {
|
|
10
|
+
const abortError = getErrorMessage("abort");
|
|
11
|
+
|
|
12
|
+
const setup = (options?: { method?: "GET" | "POST" }) => {
|
|
13
|
+
const { client, calls } = createControlledClient();
|
|
14
|
+
const request = client.createRequest<{
|
|
15
|
+
response: any;
|
|
16
|
+
payload: any;
|
|
17
|
+
error: any;
|
|
18
|
+
}>()({
|
|
19
|
+
endpoint: "/signal",
|
|
20
|
+
method: options?.method ?? "POST",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return { client, calls, request };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
vi.useFakeTimers();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
vi.useRealTimers();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe.each(["send", "exec"] as const)("When the signal is given to %s", (method) => {
|
|
35
|
+
it("should abort the running call and give the abort error", async () => {
|
|
36
|
+
const { request, calls } = setup();
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
const onResponse = vi.fn();
|
|
39
|
+
|
|
40
|
+
const sent = request[method]({ signal: controller.signal, onResponse });
|
|
41
|
+
await flushPromises();
|
|
42
|
+
controller.abort();
|
|
43
|
+
const response = await sent;
|
|
44
|
+
|
|
45
|
+
expect(calls[0].isAborted()).toBeTrue();
|
|
46
|
+
expect(response.success).toBeFalse();
|
|
47
|
+
expect(response.error).toStrictEqual(abortError);
|
|
48
|
+
expect(onResponse).toHaveBeenCalledWith(
|
|
49
|
+
expect.objectContaining({
|
|
50
|
+
details: expect.objectContaining({ isCanceled: true }),
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("should abort only its own call, not the others of the same abort key", async () => {
|
|
56
|
+
const { request, calls } = setup();
|
|
57
|
+
const controller = new AbortController();
|
|
58
|
+
|
|
59
|
+
const aborted = request[method]({ signal: controller.signal });
|
|
60
|
+
const other = request[method]();
|
|
61
|
+
await flushPromises();
|
|
62
|
+
expect(calls).toHaveLength(2);
|
|
63
|
+
controller.abort();
|
|
64
|
+
await flushPromises();
|
|
65
|
+
await calls[1].succeed({ id: 1 });
|
|
66
|
+
|
|
67
|
+
await expect(aborted).resolves.toHaveProperty("error", abortError);
|
|
68
|
+
await expect(other).resolves.toHaveProperty("data", { id: 1 });
|
|
69
|
+
expect(calls[1].isAborted()).toBeFalse();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should not send anything when the signal is aborted already", async () => {
|
|
73
|
+
const { client, request, calls } = setup();
|
|
74
|
+
const controller = new AbortController();
|
|
75
|
+
controller.abort();
|
|
76
|
+
|
|
77
|
+
const response = await request[method]({ signal: controller.signal });
|
|
78
|
+
await flushPromises();
|
|
79
|
+
|
|
80
|
+
expect(calls).toHaveLength(0);
|
|
81
|
+
expect(response.error).toStrictEqual(abortError);
|
|
82
|
+
// The same status as an abort of a running call
|
|
83
|
+
expect(response.status).toBe(client.adapter.systemErrorStatus);
|
|
84
|
+
expect(client.submitDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
85
|
+
expect(client.requestManager.abortControllers.size).toBe(0);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should stop listening to the signal once the call is settled", async () => {
|
|
89
|
+
const { request, calls } = setup();
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
const removeListener = vi.spyOn(controller.signal, "removeEventListener");
|
|
92
|
+
|
|
93
|
+
const sent = request[method]({ signal: controller.signal });
|
|
94
|
+
await flushPromises();
|
|
95
|
+
await calls[0].succeed({ id: 1 });
|
|
96
|
+
await sent;
|
|
97
|
+
|
|
98
|
+
expect(removeListener).toHaveBeenCalledWith("abort", expect.any(Function));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should keep the response when the signal aborts after it", async () => {
|
|
102
|
+
const { client, request, calls } = setup();
|
|
103
|
+
const controller = new AbortController();
|
|
104
|
+
|
|
105
|
+
const sent = request[method]({ signal: controller.signal });
|
|
106
|
+
await flushPromises();
|
|
107
|
+
await calls[0].succeed({ id: 1 });
|
|
108
|
+
const response = await sent;
|
|
109
|
+
controller.abort();
|
|
110
|
+
await flushPromises();
|
|
111
|
+
|
|
112
|
+
expect(response.data).toStrictEqual({ id: 1 });
|
|
113
|
+
expect(client.requestManager.abortControllers.size).toBe(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should still be aborted by request.abort()", async () => {
|
|
117
|
+
const { request } = setup();
|
|
118
|
+
const controller = new AbortController();
|
|
119
|
+
|
|
120
|
+
const sent = request[method]({ signal: controller.signal });
|
|
121
|
+
await flushPromises();
|
|
122
|
+
request.abort();
|
|
123
|
+
|
|
124
|
+
await expect(sent).resolves.toHaveProperty("error", abortError);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("When the signal is given to send", () => {
|
|
129
|
+
it("should take a queued call out of the queue without sending it", async () => {
|
|
130
|
+
const { client, request, calls } = setup();
|
|
131
|
+
const queued = request.setQueued(true);
|
|
132
|
+
const controller = new AbortController();
|
|
133
|
+
const onRemove = vi.fn();
|
|
134
|
+
|
|
135
|
+
const first = queued.send();
|
|
136
|
+
const second = queued.send({ signal: controller.signal, onRemove });
|
|
137
|
+
await flushPromises();
|
|
138
|
+
expect(calls).toHaveLength(1);
|
|
139
|
+
controller.abort();
|
|
140
|
+
const response = await second;
|
|
141
|
+
await calls[0].succeed({ id: 1 });
|
|
142
|
+
|
|
143
|
+
expect(response.error).toStrictEqual(abortError);
|
|
144
|
+
expect(response.status).toBe(client.adapter.systemErrorStatus);
|
|
145
|
+
expect(onRemove).toHaveBeenCalledOnce();
|
|
146
|
+
await expect(first).resolves.toHaveProperty("data", { id: 1 });
|
|
147
|
+
await flushPromises();
|
|
148
|
+
expect(calls).toHaveLength(1);
|
|
149
|
+
expect(client.submitDispatcher.getQueue(queued.queryKey).requests).toHaveLength(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("should not send the retry of a call that waits for it", async () => {
|
|
153
|
+
const { request, calls } = setup();
|
|
154
|
+
const controller = new AbortController();
|
|
155
|
+
const onRetry = vi.fn();
|
|
156
|
+
|
|
157
|
+
const sent = request.setRetry(1).setRetryTime(10_000).send({ signal: controller.signal, onRetry });
|
|
158
|
+
await flushPromises();
|
|
159
|
+
await calls[0].fail({ message: "Failed" });
|
|
160
|
+
expect(onRetry).toHaveBeenCalledOnce();
|
|
161
|
+
controller.abort();
|
|
162
|
+
const response = await sent;
|
|
163
|
+
await vi.advanceTimersByTimeAsync(10_000);
|
|
164
|
+
|
|
165
|
+
expect(response.error).toStrictEqual(abortError);
|
|
166
|
+
expect(calls).toHaveLength(1);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("should only stop waiting for a deduplicated call and keep it for the others", async () => {
|
|
170
|
+
const { request, calls } = setup({ method: "GET" });
|
|
171
|
+
const deduplicated = request.setDeduplicate(true);
|
|
172
|
+
const controller = new AbortController();
|
|
173
|
+
|
|
174
|
+
const first = deduplicated.send();
|
|
175
|
+
const joined = deduplicated.send({ signal: controller.signal });
|
|
176
|
+
await flushPromises();
|
|
177
|
+
expect(calls).toHaveLength(1);
|
|
178
|
+
controller.abort();
|
|
179
|
+
|
|
180
|
+
await expect(joined).resolves.toHaveProperty("error", abortError);
|
|
181
|
+
expect(await isPending(first)).toBeTrue();
|
|
182
|
+
expect(calls[0].isAborted()).toBeFalse();
|
|
183
|
+
await calls[0].succeed({ id: 1 });
|
|
184
|
+
await expect(first).resolves.toHaveProperty("data", { id: 1 });
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should abort the call it joined when the deduplicated call is the one with the signal", async () => {
|
|
188
|
+
const { request, calls } = setup({ method: "GET" });
|
|
189
|
+
const deduplicated = request.setDeduplicate(true);
|
|
190
|
+
const controller = new AbortController();
|
|
191
|
+
|
|
192
|
+
const first = deduplicated.send({ signal: controller.signal });
|
|
193
|
+
const joined = deduplicated.send();
|
|
194
|
+
await flushPromises();
|
|
195
|
+
controller.abort();
|
|
196
|
+
|
|
197
|
+
// The signal belongs to the call which started the request - the joined ones share its outcome
|
|
198
|
+
await expect(first).resolves.toHaveProperty("error", abortError);
|
|
199
|
+
await expect(joined).resolves.toHaveProperty("error", abortError);
|
|
200
|
+
expect(calls[0].isAborted()).toBeTrue();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("should roll back the optimistic change with the abort reason", async () => {
|
|
204
|
+
const { request, calls } = setup();
|
|
205
|
+
const queued = request.setQueued(true);
|
|
206
|
+
const controller = new AbortController();
|
|
207
|
+
const rollback = vi.fn();
|
|
208
|
+
|
|
209
|
+
const first = queued.send();
|
|
210
|
+
const second = queued.send({
|
|
211
|
+
signal: controller.signal,
|
|
212
|
+
optimistic: () => ({ rollback }),
|
|
213
|
+
});
|
|
214
|
+
await flushPromises();
|
|
215
|
+
controller.abort();
|
|
216
|
+
await second;
|
|
217
|
+
await calls[0].succeed();
|
|
218
|
+
await first;
|
|
219
|
+
|
|
220
|
+
expect(rollback).toHaveBeenCalledOnce();
|
|
221
|
+
expect(rollback).toHaveBeenCalledWith(
|
|
222
|
+
expect.objectContaining({
|
|
223
|
+
reason: "abort",
|
|
224
|
+
response: expect.objectContaining({ error: abortError }),
|
|
225
|
+
}),
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("should not run the optimistic callback when the signal is aborted already", async () => {
|
|
230
|
+
const { request } = setup();
|
|
231
|
+
const controller = new AbortController();
|
|
232
|
+
const optimisticCallback = vi.fn(() => ({}));
|
|
233
|
+
controller.abort();
|
|
234
|
+
|
|
235
|
+
await request.send({
|
|
236
|
+
signal: controller.signal,
|
|
237
|
+
optimistic: optimisticCallback,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
expect(optimisticCallback).not.toHaveBeenCalled();
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
});
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { getErrorMessage } from "adapter";
|
|
2
|
+
import { Client } from "client";
|
|
3
|
+
import { createSdk } from "sdk";
|
|
4
|
+
|
|
5
|
+
import { createControlledClient, flushPromises } from "../../utils/controlled-adapter.utils";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* With `throwOnError` the call rejects with the error of a failed response - the contract of React Query, SWR and
|
|
9
|
+
* other code which tells failures by a rejected promise. Without it requests resolve with every response.
|
|
10
|
+
*/
|
|
11
|
+
describe("Request [ throwOnError ]", () => {
|
|
12
|
+
const setup = (client = createControlledClient()) => {
|
|
13
|
+
const request = client.client.createRequest<{
|
|
14
|
+
response: { id: number };
|
|
15
|
+
error: { message: string };
|
|
16
|
+
}>()({
|
|
17
|
+
endpoint: "/throw",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return { ...client, request };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
vi.useFakeTimers();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
vi.useRealTimers();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe.each(["send", "exec"] as const)("When it is used with %s", (method) => {
|
|
32
|
+
it("should resolve with the error response by default", async () => {
|
|
33
|
+
const { request, calls } = setup();
|
|
34
|
+
|
|
35
|
+
const sent = request[method]();
|
|
36
|
+
await flushPromises();
|
|
37
|
+
await calls[0].fail({ message: "Failed" }, 400);
|
|
38
|
+
|
|
39
|
+
await expect(sent).resolves.toMatchObject({
|
|
40
|
+
success: false,
|
|
41
|
+
error: { message: "Failed" },
|
|
42
|
+
status: 400,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should reject with the error when given to the call", async () => {
|
|
47
|
+
const { request, calls } = setup();
|
|
48
|
+
|
|
49
|
+
const sent = request[method]({ throwOnError: true });
|
|
50
|
+
await flushPromises();
|
|
51
|
+
await calls[0].fail({ message: "Failed" }, 400);
|
|
52
|
+
|
|
53
|
+
await expect(sent).rejects.toStrictEqual({ message: "Failed" });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should resolve with the successful response the same way as without it", async () => {
|
|
57
|
+
const { request, calls } = setup();
|
|
58
|
+
|
|
59
|
+
const sent = request[method]({ throwOnError: true });
|
|
60
|
+
await flushPromises();
|
|
61
|
+
await calls[0].succeed({ id: 1 }, 200);
|
|
62
|
+
|
|
63
|
+
await expect(sent).resolves.toMatchObject({
|
|
64
|
+
success: true,
|
|
65
|
+
data: { id: 1 },
|
|
66
|
+
error: null,
|
|
67
|
+
status: 200,
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should reject when it is set on the request", async () => {
|
|
72
|
+
const { request, calls } = setup();
|
|
73
|
+
|
|
74
|
+
const sent = request.setThrowOnError(true)[method]();
|
|
75
|
+
await flushPromises();
|
|
76
|
+
await calls[0].fail({ message: "Failed" });
|
|
77
|
+
|
|
78
|
+
await expect(sent).rejects.toStrictEqual({ message: "Failed" });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should let the call override the value of the request", async () => {
|
|
82
|
+
const { request, calls } = setup();
|
|
83
|
+
|
|
84
|
+
const sent = request.setThrowOnError(true)[method]({ throwOnError: false });
|
|
85
|
+
await flushPromises();
|
|
86
|
+
await calls[0].fail({ message: "Failed" });
|
|
87
|
+
|
|
88
|
+
await expect(sent).resolves.toHaveProperty("error", {
|
|
89
|
+
message: "Failed",
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should reject when the client defaults of the adapter set it", async () => {
|
|
94
|
+
const controlled = createControlledClient();
|
|
95
|
+
controlled.client.adapter.setRequestDefaults(() => ({
|
|
96
|
+
throwOnError: true,
|
|
97
|
+
}));
|
|
98
|
+
const { request, calls } = setup(controlled);
|
|
99
|
+
|
|
100
|
+
const sent = request[method]();
|
|
101
|
+
await flushPromises();
|
|
102
|
+
await calls[0].fail({ message: "Failed" });
|
|
103
|
+
|
|
104
|
+
await expect(sent).rejects.toStrictEqual({ message: "Failed" });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("should reject with the abort error of an aborted call", async () => {
|
|
108
|
+
const { request } = setup();
|
|
109
|
+
const controller = new AbortController();
|
|
110
|
+
|
|
111
|
+
const sent = request[method]({
|
|
112
|
+
throwOnError: true,
|
|
113
|
+
signal: controller.signal,
|
|
114
|
+
});
|
|
115
|
+
await flushPromises();
|
|
116
|
+
controller.abort();
|
|
117
|
+
|
|
118
|
+
await expect(sent).rejects.toStrictEqual(getErrorMessage("abort"));
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should decide by the mapped response", async () => {
|
|
122
|
+
const { request, calls } = setup();
|
|
123
|
+
const mapped = request.setResponseMapper((response) => ({
|
|
124
|
+
...response,
|
|
125
|
+
data: null,
|
|
126
|
+
error: { message: "Mapped" },
|
|
127
|
+
success: false as const,
|
|
128
|
+
}));
|
|
129
|
+
|
|
130
|
+
const sent = mapped[method]({ throwOnError: true });
|
|
131
|
+
await flushPromises();
|
|
132
|
+
await calls[0].succeed({ id: 1 });
|
|
133
|
+
|
|
134
|
+
await expect(sent).rejects.toStrictEqual({ message: "Mapped" });
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("When it is used with send", () => {
|
|
139
|
+
it("should resolve with the cached response of cache-first", async () => {
|
|
140
|
+
const { request, calls } = setup();
|
|
141
|
+
const first = request.send();
|
|
142
|
+
await flushPromises();
|
|
143
|
+
await calls[0].succeed({ id: 1 });
|
|
144
|
+
await first;
|
|
145
|
+
|
|
146
|
+
const response = await request.send({
|
|
147
|
+
throwOnError: true,
|
|
148
|
+
cachePolicy: "cache-first",
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(response.data).toStrictEqual({ id: 1 });
|
|
152
|
+
expect(calls).toHaveLength(1);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("should not reject the background revalidation of a cached response", async () => {
|
|
156
|
+
const { request, calls } = setup();
|
|
157
|
+
const first = request.send();
|
|
158
|
+
await flushPromises();
|
|
159
|
+
await calls[0].succeed({ id: 1 });
|
|
160
|
+
await first;
|
|
161
|
+
const unhandled = vi.fn();
|
|
162
|
+
process.on("unhandledRejection", unhandled);
|
|
163
|
+
|
|
164
|
+
const response = await request.send({
|
|
165
|
+
throwOnError: true,
|
|
166
|
+
cachePolicy: "revalidate",
|
|
167
|
+
});
|
|
168
|
+
await flushPromises();
|
|
169
|
+
await calls[1].fail({ message: "Failed" });
|
|
170
|
+
await flushPromises();
|
|
171
|
+
process.off("unhandledRejection", unhandled);
|
|
172
|
+
|
|
173
|
+
expect(response.data).toStrictEqual({ id: 1 });
|
|
174
|
+
expect(unhandled).not.toHaveBeenCalled();
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("When the request is configured", () => {
|
|
179
|
+
it("should be false by default", () => {
|
|
180
|
+
const { request } = setup();
|
|
181
|
+
|
|
182
|
+
expect(request.throwOnError).toBeFalse();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("should take the value from createRequest", () => {
|
|
186
|
+
const client = new Client({ url: "http://localhost" });
|
|
187
|
+
const request = client.createRequest()({
|
|
188
|
+
endpoint: "/users",
|
|
189
|
+
throwOnError: true,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
expect(request.throwOnError).toBeTrue();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("should keep the value in clones and serialized requests", () => {
|
|
196
|
+
const { client, request } = setup();
|
|
197
|
+
const configured = request.setThrowOnError(true);
|
|
198
|
+
|
|
199
|
+
expect(configured.clone().throwOnError).toBeTrue();
|
|
200
|
+
expect(configured.toJSON().throwOnError).toBeTrue();
|
|
201
|
+
expect(client.createRequest()({ endpoint: "/other" }).throwOnError).toBeFalse();
|
|
202
|
+
expect(request.throwOnError).toBeFalse();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("should be set by the sdk configuration", () => {
|
|
206
|
+
const { client } = setup();
|
|
207
|
+
const sdk = createSdk<
|
|
208
|
+
typeof client,
|
|
209
|
+
{
|
|
210
|
+
users: {
|
|
211
|
+
$get: ReturnType<ReturnType<typeof client.createRequest<{ response: { id: number } }>>>;
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
>(client).$configure({ "users.$get": { throwOnError: true } });
|
|
215
|
+
|
|
216
|
+
expect(sdk.users.$get.throwOnError).toBeTrue();
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
});
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
expectNotType,
|
|
5
5
|
expectType,
|
|
6
6
|
} from "@hyper-fetch/testing";
|
|
7
|
+
import type { RequestResponseType } from "adapter";
|
|
7
8
|
import { Client } from "client";
|
|
8
9
|
import type { ExtractLocalErrorType } from "types";
|
|
9
10
|
|
|
@@ -407,6 +408,51 @@ describe("Request [Types]", () => {
|
|
|
407
408
|
expectType<Error>().assert<ExtractLocalErrorType<typeof getUser>>(new Error("message"));
|
|
408
409
|
expectNotType<string>().assert<ExtractLocalErrorType<typeof getUser>>(new Error("message"));
|
|
409
410
|
});
|
|
411
|
+
|
|
412
|
+
describe("when the call takes a signal and throwOnError", () => {
|
|
413
|
+
it("should accept them in send and exec", () => {
|
|
414
|
+
const { signal } = new AbortController();
|
|
415
|
+
|
|
416
|
+
expectTypeOf(getUsers.send).toBeCallableWith({
|
|
417
|
+
signal,
|
|
418
|
+
throwOnError: true,
|
|
419
|
+
});
|
|
420
|
+
expectTypeOf(getUsers.exec).toBeCallableWith({
|
|
421
|
+
signal,
|
|
422
|
+
throwOnError: false,
|
|
423
|
+
});
|
|
424
|
+
expectTypeOf(postUser.send).toBeCallableWith({
|
|
425
|
+
payload: { name: "" },
|
|
426
|
+
signal,
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("should reject wrong values", () => {
|
|
431
|
+
// @ts-expect-error the signal has to be an AbortSignal
|
|
432
|
+
expectTypeOf(getUsers.send).toBeCallableWith({ signal: "abort" });
|
|
433
|
+
// @ts-expect-error throwOnError is a boolean
|
|
434
|
+
expectTypeOf(getUsers.exec).toBeCallableWith({ throwOnError: "yes" });
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it("should keep the response type of the call", () => {
|
|
438
|
+
expectTypeOf(getUsers.send).returns.toEqualTypeOf<Promise<RequestResponseType<typeof getUsers>>>();
|
|
439
|
+
expectTypeOf(getUsers.exec).returns.toEqualTypeOf<Promise<RequestResponseType<typeof getUsers>>>();
|
|
440
|
+
expectTypeOf(getUser.setThrowOnError(true)).toEqualTypeOf<typeof getUser>();
|
|
441
|
+
expectTypeOf(getUsers.setThrowOnError(true).exec).returns.toEqualTypeOf<
|
|
442
|
+
Promise<RequestResponseType<typeof getUsers>>
|
|
443
|
+
>();
|
|
444
|
+
expectTypeOf(getUser.throwOnError).toEqualTypeOf<boolean>();
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it("should be a request option of createRequest", () => {
|
|
448
|
+
const request = client.createRequest<{ response: { id: string } }>()({
|
|
449
|
+
endpoint: "/users",
|
|
450
|
+
throwOnError: true,
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
expectTypeOf(request.throwOnError).toEqualTypeOf<boolean>();
|
|
454
|
+
});
|
|
455
|
+
});
|
|
410
456
|
});
|
|
411
457
|
|
|
412
458
|
// // OK
|