@hyper-fetch/react 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.
Files changed (102) hide show
  1. package/__tests__/features/helpers/use-request-events/use-request-events.async-mapper.spec.ts +243 -0
  2. package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
  3. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.keys.spec.ts +294 -0
  4. package/__tests__/features/helpers/utils.spec.ts +127 -0
  5. package/__tests__/features/provider/provider.config.spec.tsx +172 -0
  6. package/__tests__/features/use-app-manager/use-app-manager.events.spec.tsx +497 -0
  7. package/__tests__/features/use-cache/use-cache.state.spec.tsx +326 -0
  8. package/__tests__/features/use-emitter/use-emitter.lifecycle.spec.tsx +417 -0
  9. package/__tests__/features/use-fetch/use-fetch.bounce-timers.spec.ts +199 -0
  10. package/__tests__/features/use-fetch/use-fetch.cache-events.spec.ts +212 -0
  11. package/__tests__/features/use-fetch/use-fetch.callbacks.spec.tsx +359 -0
  12. package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +6 -3
  13. package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +72 -80
  14. package/__tests__/features/use-fetch/use-fetch.hydration.spec.tsx +126 -0
  15. package/__tests__/features/use-fetch/use-fetch.infinite.spec.ts +146 -0
  16. package/__tests__/features/use-fetch/use-fetch.initial-state.spec.ts +340 -0
  17. package/__tests__/features/use-fetch/use-fetch.keys.spec.tsx +559 -0
  18. package/__tests__/features/use-fetch/use-fetch.lifecycle.spec.ts +348 -0
  19. package/__tests__/features/use-fetch/use-fetch.mounting.spec.tsx +411 -0
  20. package/__tests__/features/use-fetch/use-fetch.pagination.spec.ts +177 -0
  21. package/__tests__/features/use-fetch/use-fetch.refresh-interval.spec.ts +355 -0
  22. package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +36 -13
  23. package/__tests__/features/use-fetch/use-fetch.response-order.spec.ts +105 -0
  24. package/__tests__/features/use-fetch/use-fetch.retry.spec.ts +235 -0
  25. package/__tests__/features/use-fetch/use-fetch.return-types.spec.ts +186 -0
  26. package/__tests__/features/use-fetch/use-fetch.scope.spec.ts +333 -0
  27. package/__tests__/features/use-fetch/use-fetch.ssr.spec.tsx +195 -0
  28. package/__tests__/features/use-fetch/use-fetch.suspense-edge.spec.tsx +391 -0
  29. package/__tests__/features/use-fetch/use-fetch.throwing-callbacks.spec.tsx +112 -0
  30. package/__tests__/features/use-fetch/use-fetch.tracking.spec.tsx +364 -0
  31. package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +11 -2
  32. package/__tests__/features/use-fetch/use-fetch.window-events.spec.ts +300 -0
  33. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.parity.spec.tsx +1027 -0
  34. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.resubscribe.spec.tsx +267 -0
  35. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.typescript.spec.ts +64 -1
  36. package/__tests__/features/use-is-fetching/use-is-fetching.base.spec.tsx +19 -9
  37. package/__tests__/features/use-is-fetching/use-is-fetching.lifecycle.spec.tsx +301 -0
  38. package/__tests__/features/use-listener/use-listener.lifecycle.spec.tsx +507 -0
  39. package/__tests__/features/use-listener/use-listener.ssr.spec.tsx +89 -0
  40. package/__tests__/features/use-queue/use-queue.controls.spec.tsx +593 -0
  41. package/__tests__/features/use-queue/use-queue.list.spec.tsx +560 -0
  42. package/__tests__/features/use-queue/use-queue.progress.spec.tsx +650 -0
  43. package/__tests__/features/use-submit/use-submit.abort.spec.tsx +270 -0
  44. package/__tests__/features/use-submit/use-submit.actions.spec.tsx +446 -0
  45. package/__tests__/features/use-submit/use-submit.arguments.spec.tsx +732 -0
  46. package/__tests__/features/use-submit/use-submit.bounce.spec.tsx +644 -0
  47. package/__tests__/features/use-submit/use-submit.callbacks.spec.tsx +860 -0
  48. package/__tests__/features/use-submit/use-submit.chaining.spec.tsx +167 -0
  49. package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +2 -1
  50. package/__tests__/features/use-submit/use-submit.debounce.spec.ts +100 -124
  51. package/__tests__/features/use-submit/use-submit.double-submit.spec.tsx +552 -0
  52. package/__tests__/features/use-submit/use-submit.offline.spec.tsx +285 -0
  53. package/__tests__/features/use-submit/use-submit.optimistic-cache.spec.tsx +619 -0
  54. package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +8 -1
  55. package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
  56. package/__tests__/features/use-submit/use-submit.retry.spec.ts +51 -35
  57. package/__tests__/features/use-submit/use-submit.state.spec.tsx +1025 -0
  58. package/__tests__/features/use-submit/use-submit.submit-types.spec.ts +194 -0
  59. package/__tests__/features/use-submit/use-submit.submitting.spec.tsx +180 -0
  60. package/__tests__/features/use-submit/use-submit.unmount.spec.ts +2 -1
  61. package/__tests__/utils/gate.utils.ts +103 -0
  62. package/__tests__/utils/gated-client.utils.ts +158 -0
  63. package/__tests__/utils/helpers.utils.ts +19 -0
  64. package/__tests__/utils/use-queue.utils.ts +8 -4
  65. package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
  66. package/dist/helpers/use-request-events/use-request-events.types.d.ts +1 -1
  67. package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
  68. package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
  69. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +8 -0
  70. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
  71. package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
  72. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
  73. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.hooks.d.ts.map +1 -1
  74. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts +3 -0
  75. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts.map +1 -1
  76. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts +5 -3
  77. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts.map +1 -1
  78. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts +11 -1
  79. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts.map +1 -1
  80. package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
  81. package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
  82. package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
  83. package/dist/index.mjs +633 -347
  84. package/dist/index.mjs.map +1 -1
  85. package/dist/provider/provider.d.ts.map +1 -1
  86. package/package.json +1 -1
  87. package/src/helpers/use-request-events/use-request-events.hooks.ts +147 -32
  88. package/src/helpers/use-request-events/use-request-events.types.ts +1 -1
  89. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +148 -101
  90. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +26 -5
  91. package/src/hooks/use-cache/use-cache.hooks.ts +20 -5
  92. package/src/hooks/use-fetch/use-fetch.hooks.ts +194 -113
  93. package/src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts +55 -26
  94. package/src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts +35 -29
  95. package/src/hooks/use-infinite-fetch/use-infinite-fetch.types.ts +5 -3
  96. package/src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts +25 -1
  97. package/src/hooks/use-listener/use-listener.hooks.ts +2 -1
  98. package/src/hooks/use-queue/use-queue.hooks.ts +85 -60
  99. package/src/hooks/use-submit/use-submit.hooks.ts +163 -53
  100. package/src/provider/provider.tsx +9 -1
  101. package/__tests__/features/use-fetch/use-fetch.pagination.ts +0 -36
  102. package/__tests__/features/use-fetch/use-fetch.retry.ts +0 -38
@@ -0,0 +1,235 @@
1
+ import { act, renderHook } from "@testing-library/react";
2
+ import { useFetch } from "hooks/use-fetch";
3
+ import { useIsFetching } from "hooks/use-is-fetching";
4
+
5
+ import { client, createRequest } from "../../utils";
6
+ import { advance, gateRequest, settle } from "../../utils/gate.utils";
7
+
8
+ type Data = { value: number };
9
+
10
+ const retryTime = 5000;
11
+
12
+ describe("useFetch [ Retry ]", () => {
13
+ let calls = gateRequest(createRequest<Data>());
14
+
15
+ const renderRetry = (retry: number) => {
16
+ const request = createRequest<Data>({ retry, retryTime });
17
+ calls = gateRequest(request);
18
+ const spies = { onError: vi.fn(), onSuccess: vi.fn(), onFinished: vi.fn() };
19
+ const view = renderHook(() => {
20
+ const state = useFetch(request);
21
+ state.onError(spies.onError);
22
+ state.onSuccess(spies.onSuccess);
23
+ state.onFinished(spies.onFinished);
24
+ const fetching = useIsFetching(client);
25
+ return {
26
+ data: state.data,
27
+ error: state.error?.message ?? null,
28
+ loading: state.loading,
29
+ retries: state.retries,
30
+ fetching,
31
+ };
32
+ });
33
+ return { ...view, spies, request };
34
+ };
35
+
36
+ const failAttempt = async (index: number) => {
37
+ calls[index].fail(new Error(`Attempt ${index + 1} failed`));
38
+ await settle();
39
+ };
40
+
41
+ beforeEach(() => {
42
+ vi.useFakeTimers();
43
+ client.clear();
44
+ });
45
+
46
+ afterEach(() => {
47
+ vi.useRealTimers();
48
+ });
49
+
50
+ describe("when request retry attribute is set to false", () => {
51
+ it("should not retry request on failure", async () => {
52
+ const { result } = renderRetry(0);
53
+ await advance();
54
+ await failAttempt(0);
55
+ await advance(retryTime * 3);
56
+
57
+ expect(calls).toHaveLength(1);
58
+ expect(result.current).toStrictEqual({
59
+ data: null,
60
+ error: "Attempt 1 failed",
61
+ loading: false,
62
+ retries: 0,
63
+ fetching: 0,
64
+ });
65
+ });
66
+ it("should call the error callback once", async () => {
67
+ const { spies } = renderRetry(0);
68
+ await advance();
69
+ await failAttempt(0);
70
+ await advance(retryTime * 3);
71
+
72
+ expect(spies.onError).toHaveBeenCalledOnce();
73
+ expect(spies.onError.mock.calls[0][0].details.willRetry).toBeFalse();
74
+ });
75
+ });
76
+
77
+ describe("when request retry attribute is set to true", () => {
78
+ it("should retry request once", async () => {
79
+ renderRetry(1);
80
+ await advance();
81
+ await failAttempt(0);
82
+ await advance(retryTime);
83
+ await failAttempt(1);
84
+ await advance(retryTime * 3);
85
+
86
+ expect(calls).toHaveLength(2);
87
+ });
88
+ it("should retry request twice", async () => {
89
+ renderRetry(2);
90
+ await advance();
91
+ await failAttempt(0);
92
+ await advance(retryTime);
93
+ await failAttempt(1);
94
+ await advance(retryTime);
95
+ await failAttempt(2);
96
+ await advance(retryTime * 3);
97
+
98
+ expect(calls).toHaveLength(3);
99
+ });
100
+ it("should trigger retries with the config interval", async () => {
101
+ renderRetry(1);
102
+ await advance();
103
+ await failAttempt(0);
104
+
105
+ await advance(retryTime - 1000);
106
+ expect(calls).toHaveLength(1);
107
+
108
+ await advance(1000);
109
+ expect(calls).toHaveLength(2);
110
+ });
111
+ it("should stop retrying after the successful attempt", async () => {
112
+ const { result } = renderRetry(3);
113
+ await advance();
114
+ await failAttempt(0);
115
+ await advance(retryTime);
116
+ calls[1].succeed({ value: 1 });
117
+ await settle();
118
+ await advance(retryTime * 3);
119
+
120
+ expect(calls).toHaveLength(2);
121
+ expect(result.current).toStrictEqual({
122
+ data: { value: 1 },
123
+ error: null,
124
+ loading: false,
125
+ retries: 1,
126
+ fetching: 0,
127
+ });
128
+ });
129
+ it("should expose the number of the used retries", async () => {
130
+ const { result } = renderRetry(2);
131
+ await advance();
132
+ expect(result.current.retries).toBe(0);
133
+
134
+ await failAttempt(0);
135
+ await advance(retryTime);
136
+ await failAttempt(1);
137
+ expect(result.current.retries).toBe(1);
138
+
139
+ await advance(retryTime);
140
+ await failAttempt(2);
141
+ expect(result.current.retries).toBe(2);
142
+ });
143
+ it("should be loading during every attempt", async () => {
144
+ const { result } = renderRetry(1);
145
+ await advance();
146
+ expect(result.current.loading).toBeTrue();
147
+ expect(result.current.fetching).toBe(1);
148
+
149
+ await failAttempt(0);
150
+ await advance(retryTime);
151
+
152
+ expect(result.current.loading).toBeTrue();
153
+ expect(result.current.fetching).toBe(1);
154
+ });
155
+ it("should end with the last error", async () => {
156
+ const { result } = renderRetry(1);
157
+ await advance();
158
+ await failAttempt(0);
159
+ await advance(retryTime);
160
+ await failAttempt(1);
161
+
162
+ expect(result.current).toStrictEqual({
163
+ data: null,
164
+ error: "Attempt 2 failed",
165
+ loading: false,
166
+ retries: 1,
167
+ fetching: 0,
168
+ });
169
+ });
170
+ it("should call the callbacks for every attempt", async () => {
171
+ const { spies } = renderRetry(2);
172
+ await advance();
173
+ await failAttempt(0);
174
+ await advance(retryTime);
175
+ await failAttempt(1);
176
+ await advance(retryTime);
177
+ calls[2].succeed({ value: 1 });
178
+ await settle();
179
+
180
+ expect(spies.onError).toHaveBeenCalledTimes(2);
181
+ expect(spies.onError.mock.calls.map(([params]) => params.details.willRetry)).toStrictEqual([true, true]);
182
+ expect(spies.onSuccess).toHaveBeenCalledOnce();
183
+ expect(spies.onFinished).toHaveBeenCalledTimes(3);
184
+ });
185
+ it("should mark only the last failed attempt as final", async () => {
186
+ const { spies } = renderRetry(1);
187
+ await advance();
188
+ await failAttempt(0);
189
+ await advance(retryTime);
190
+ await failAttempt(1);
191
+
192
+ expect(spies.onError.mock.calls.map(([params]) => params.details.willRetry)).toStrictEqual([true, false]);
193
+ });
194
+ it("should start the retries from scratch on refetch", async () => {
195
+ const request = createRequest<Data>({ retry: 1, retryTime });
196
+ calls = gateRequest(request);
197
+ const { result } = renderHook(() => {
198
+ const { retries, refetch } = useFetch(request);
199
+ return { retries, refetch };
200
+ });
201
+ await advance();
202
+ await failAttempt(0);
203
+ await advance(retryTime);
204
+ await failAttempt(1);
205
+
206
+ act(() => {
207
+ result.current.refetch();
208
+ });
209
+ await advance();
210
+ calls[2].succeed({ value: 1 });
211
+ await settle();
212
+
213
+ expect(result.current.retries).toBe(0);
214
+ });
215
+ it("should not retry the request removed with abort", async () => {
216
+ const request = createRequest<Data>({ retry: 3, retryTime });
217
+ calls = gateRequest(request);
218
+ const { result } = renderHook(() => {
219
+ const { error, abort } = useFetch(request);
220
+ return { error, abort };
221
+ });
222
+ await advance();
223
+ await failAttempt(0);
224
+ await advance(retryTime);
225
+ expect(calls).toHaveLength(2);
226
+
227
+ act(() => {
228
+ result.current.abort();
229
+ });
230
+ await advance(retryTime * 3);
231
+
232
+ expect(calls).toHaveLength(2);
233
+ });
234
+ });
235
+ });
@@ -0,0 +1,186 @@
1
+ import type { Client, ExtractAdapterExtraType, ExtractAdapterType } from "@hyper-fetch/core";
2
+ import type { UseCacheReturnType } from "hooks/use-cache";
3
+ import type { UseFetchOptionsType, UseFetchReturnType } from "hooks/use-fetch";
4
+ import { useFetch } from "hooks/use-fetch";
5
+ import { expectTypeOf } from "vitest";
6
+
7
+ import { client } from "../../utils";
8
+
9
+ type User = { id: number; name: string; tags: string[] };
10
+ type ApiError = { code: string; message: string };
11
+
12
+ const getUser = client.createRequest<{ response: User; error: ApiError }>()({ endpoint: "/users/:userId" });
13
+ const getUsers = client.createRequest<{ response: User[]; queryParams: { page: number } }>()({ endpoint: "/users" });
14
+ const getPlain = client.createRequest<{ response: string }>()({ endpoint: "/plain" });
15
+
16
+ const userRequest = getUser.setParams({ userId: 1 });
17
+
18
+ type UserRequest = typeof userRequest;
19
+ type UserState = UseFetchReturnType<UserRequest>;
20
+
21
+ /**
22
+ * Type level tests - nothing is rendered, hooks are referenced only inside of never called functions
23
+ */
24
+ describe("useFetch [ Return Types ]", () => {
25
+ describe("state", () => {
26
+ it("should type data as the response or null", () => {
27
+ expectTypeOf<UserState["data"]>().toEqualTypeOf<User | null>();
28
+ expectTypeOf<UseFetchReturnType<typeof getUsers>["data"]>().toEqualTypeOf<User[] | null>();
29
+ expectTypeOf<UseFetchReturnType<typeof getPlain>["data"]>().toEqualTypeOf<string | null>();
30
+ });
31
+ it("should type error as the request error union or null", () => {
32
+ expectTypeOf<UserState["error"]>().toEqualTypeOf<ApiError | Error | null>();
33
+ expectTypeOf<UseFetchReturnType<typeof getPlain>["error"]>().toEqualTypeOf<Error | null>();
34
+ });
35
+ it("should type the flags, counters and timestamps", () => {
36
+ expectTypeOf<UserState["loading"]>().toEqualTypeOf<boolean>();
37
+ expectTypeOf<UserState["success"]>().toEqualTypeOf<boolean>();
38
+ expectTypeOf<UserState["retries"]>().toEqualTypeOf<number>();
39
+ expectTypeOf<UserState["responseTimestamp"]>().toEqualTypeOf<Date | null>();
40
+ expectTypeOf<UserState["requestTimestamp"]>().toEqualTypeOf<Date | null>();
41
+ });
42
+ it("should type status and extra with the adapter types", () => {
43
+ expectTypeOf<UserState["status"]>().toEqualTypeOf<number | null>();
44
+ expectTypeOf<UserState["extra"]>().toEqualTypeOf<ExtractAdapterExtraType<
45
+ ExtractAdapterType<UserRequest>
46
+ > | null>();
47
+ });
48
+ it("should not allow to read unknown fields", () => {
49
+ expectTypeOf<UserState>().not.toHaveProperty("isLoading");
50
+ expectTypeOf<UserState>().not.toHaveProperty("submit");
51
+ expectTypeOf<UserState>().not.toHaveProperty("invalidate");
52
+ });
53
+ });
54
+
55
+ describe("actions", () => {
56
+ it("should type the functions without arguments", () => {
57
+ expectTypeOf<UserState["refetch"]>().toEqualTypeOf<() => void>();
58
+ expectTypeOf<UserState["abort"]>().toEqualTypeOf<() => void>();
59
+ expectTypeOf<UserState["clearState"]>().toEqualTypeOf<() => void>();
60
+ });
61
+ it("should type setData with the response, null and the updater", () => {
62
+ expectTypeOf<UserState["setData"]>().toBeCallableWith({ id: 1, name: "name", tags: [] });
63
+ expectTypeOf<UserState["setData"]>().toBeCallableWith(null);
64
+ expectTypeOf<UserState["setData"]>().toBeCallableWith((previous: User | null) => previous);
65
+ expectTypeOf<UserState["setData"]>().parameter(0).not.toEqualTypeOf<string>();
66
+ });
67
+ it("should type setError with the request error", () => {
68
+ expectTypeOf<UserState["setError"]>().toBeCallableWith({ code: "E", message: "m" });
69
+ expectTypeOf<UserState["setError"]>().toBeCallableWith(new Error("error"));
70
+ expectTypeOf<UserState["setError"]>().toBeCallableWith(null);
71
+ });
72
+ it("should type the bounce state", () => {
73
+ expectTypeOf<UserState["bounce"]>().toEqualTypeOf<{ active: boolean; reset: () => void }>();
74
+ });
75
+ });
76
+
77
+ describe("callbacks", () => {
78
+ it("should pass the success response to onSuccess", () => {
79
+ type Params = Parameters<Parameters<UserState["onSuccess"]>[0]>[0];
80
+
81
+ expectTypeOf<Params["response"]["data"]>().toEqualTypeOf<User>();
82
+ expectTypeOf<Params["response"]["error"]>().toEqualTypeOf<null>();
83
+ expectTypeOf<Params["response"]["success"]>().toEqualTypeOf<true>();
84
+ expectTypeOf<Params["requestId"]>().toEqualTypeOf<string>();
85
+ });
86
+ it("should pass the error response to onError", () => {
87
+ type Params = Parameters<Parameters<UserState["onError"]>[0]>[0];
88
+
89
+ expectTypeOf<Params["response"]["data"]>().toEqualTypeOf<null>();
90
+ expectTypeOf<Params["response"]["error"]>().toEqualTypeOf<ApiError | Error>();
91
+ expectTypeOf<Params["response"]["success"]>().toEqualTypeOf<false>();
92
+ });
93
+ it("should pass any response to onFinished", () => {
94
+ type Params = Parameters<Parameters<UserState["onFinished"]>[0]>[0];
95
+
96
+ expectTypeOf<Params["response"]["data"]>().toEqualTypeOf<User | null>();
97
+ expectTypeOf<Params["response"]["error"]>().toEqualTypeOf<ApiError | Error | null>();
98
+ expectTypeOf<Params["details"]["retries"]>().toEqualTypeOf<number>();
99
+ expectTypeOf<Params["details"]["willRetry"]>().toEqualTypeOf<boolean>();
100
+ });
101
+ });
102
+
103
+ describe("select", () => {
104
+ it("should type data with the selector result", () => {
105
+ const useName = () => useFetch(getUser.setParams({ userId: 1 }), { select: (user) => user.name });
106
+ const useTags = () => useFetch(getUser.setParams({ userId: 1 }), { select: (user) => user.tags.length });
107
+
108
+ expectTypeOf<ReturnType<typeof useName>["data"]>().toEqualTypeOf<string | null>();
109
+ expectTypeOf<ReturnType<typeof useTags>["data"]>().toEqualTypeOf<number | null>();
110
+ });
111
+ it("should type setData with the selected data", () => {
112
+ const useName = () => useFetch(getUser.setParams({ userId: 1 }), { select: (user) => user.name });
113
+
114
+ expectTypeOf<ReturnType<typeof useName>["setData"]>().toBeCallableWith("name");
115
+ expectTypeOf<ReturnType<typeof useName>["setData"]>().parameter(0).not.toEqualTypeOf<User | null>();
116
+ });
117
+ it("should keep the full response in the callbacks", () => {
118
+ const useName = () => useFetch(getUser.setParams({ userId: 1 }), { select: (user) => user.name });
119
+ type Params = Parameters<Parameters<ReturnType<typeof useName>["onSuccess"]>[0]>[0];
120
+
121
+ expectTypeOf<Params["response"]["data"]>().toEqualTypeOf<User>();
122
+ });
123
+ it("should pass the response to the selector", () => {
124
+ type Select = NonNullable<UseFetchOptionsType<UserRequest, string>["select"]>;
125
+
126
+ expectTypeOf<Parameters<Select>[0]>().toEqualTypeOf<User>();
127
+ expectTypeOf<ReturnType<Select>>().toEqualTypeOf<string>();
128
+ });
129
+ });
130
+
131
+ describe("options", () => {
132
+ it("should type the initial response with the request types", () => {
133
+ type InitialResponse = NonNullable<UseFetchOptionsType<UserRequest>["initialResponse"]>;
134
+
135
+ expectTypeOf<InitialResponse["data"]>().toEqualTypeOf<User | null | undefined>();
136
+ expectTypeOf<InitialResponse["error"]>().toEqualTypeOf<ApiError | Error | null | undefined>();
137
+ });
138
+ it("should type the modes and flags", () => {
139
+ type Options = UseFetchOptionsType<UserRequest>;
140
+
141
+ expectTypeOf<Options["keepPreviousData"]>().toEqualTypeOf<"auto" | "preserve" | "clean" | undefined>();
142
+ expectTypeOf<Options["disabled"]>().toEqualTypeOf<boolean | undefined>();
143
+ expectTypeOf<Options["suspense"]>().toEqualTypeOf<boolean | undefined>();
144
+ expectTypeOf<Options["refreshTime"]>().toEqualTypeOf<number | undefined>();
145
+ expectTypeOf<Options["bounceType"]>().toEqualTypeOf<"debounce" | "throttle" | undefined>();
146
+ });
147
+ it("should allow the bounce timeout only in the throttle mode", () => {
148
+ expectTypeOf<{ bounceType: "throttle"; bounceTimeout: number }>().toExtend<UseFetchOptionsType<UserRequest>>();
149
+ expectTypeOf<{ bounceType: "debounce"; bounceTimeout: number }>().not.toExtend<
150
+ UseFetchOptionsType<UserRequest>
151
+ >();
152
+ });
153
+ it("should type the throwOnError predicate with the request error", () => {
154
+ type ThrowOnError = Exclude<UseFetchOptionsType<UserRequest>["throwOnError"], boolean | undefined>;
155
+
156
+ expectTypeOf<Parameters<ThrowOnError>[0]>().toEqualTypeOf<ApiError | Error>();
157
+ });
158
+ });
159
+
160
+ describe("request", () => {
161
+ it("should accept the request with the params set", () => {
162
+ const useUser = () => useFetch(getUser.setParams({ userId: 1 }));
163
+
164
+ expectTypeOf<ReturnType<typeof useUser>["data"]>().toEqualTypeOf<User | null>();
165
+ });
166
+ it("should require the declared params", () => {
167
+ const withParams = () => getUser.setParams({ userId: 1 });
168
+ // @ts-expect-error - params of the endpoint have to be used
169
+ const withWrongParams = () => getUser.setParams({ id: 1 });
170
+
171
+ expectTypeOf(withParams).returns.toHaveProperty("params");
172
+ expectTypeOf(withWrongParams).toBeFunction();
173
+ });
174
+ it("should require the declared query params", () => {
175
+ expectTypeOf(getUsers.setQueryParams).parameter(0).toEqualTypeOf<{ page: number }>();
176
+ });
177
+ it("should share the state types with useCache", () => {
178
+ expectTypeOf<UseCacheReturnType<UserRequest>["data"]>().toEqualTypeOf<UserState["data"]>();
179
+ expectTypeOf<UseCacheReturnType<UserRequest>["error"]>().toEqualTypeOf<UserState["error"]>();
180
+ expectTypeOf<UseCacheReturnType<UserRequest>>().not.toHaveProperty("refetch");
181
+ });
182
+ it("should use the client of the request", () => {
183
+ expectTypeOf(getUser.client).toExtend<Client>();
184
+ });
185
+ });
186
+ });