@hyper-fetch/react 7.5.2 → 8.0.0
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/.eslintrc.cjs +12 -0
- package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +115 -0
- package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +69 -0
- package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +213 -0
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +550 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +566 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +260 -0
- package/__tests__/features/provider/provider.hydration.spec.tsx +165 -0
- package/__tests__/features/provider/use-config-provider.base.spec.tsx +69 -0
- package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +85 -0
- package/__tests__/features/use-cache/use-cache.base.spec.ts +136 -0
- package/__tests__/features/use-cache/use-cache.helpers.ts +40 -0
- package/__tests__/features/use-cache/use-cache.typescript.spec.ts +111 -0
- package/__tests__/features/use-emitter/use-emitter.base.spec.ts +124 -0
- package/__tests__/features/use-fetch/use-fetch.base.spec.ts +372 -0
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +83 -0
- package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +82 -0
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +91 -0
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +153 -0
- package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +80 -0
- package/__tests__/features/use-fetch/use-fetch.infinite.ts +39 -0
- package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +172 -0
- package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +215 -0
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +233 -0
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +103 -0
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +36 -0
- package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +73 -0
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +187 -0
- package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +151 -0
- package/__tests__/features/use-fetch/use-fetch.retry.ts +38 -0
- package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +237 -0
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +505 -0
- package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +182 -0
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +26 -0
- package/__tests__/features/use-fetch/use-fetch.window.spec.ts +131 -0
- package/__tests__/features/use-listener/use-listener.base.spec.ts +57 -0
- package/__tests__/features/use-queue/use-queue.actions.spec.ts +238 -0
- package/__tests__/features/use-queue/use-queue.base.spec.ts +367 -0
- package/__tests__/features/use-queue/use-queue.resolve.spec.ts +173 -0
- package/__tests__/features/use-stream/use-stream.base.spec.ts +481 -0
- package/__tests__/features/use-submit/use-submit.base.spec.ts +76 -0
- package/__tests__/features/use-submit/use-submit.cancel.spec.ts +46 -0
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +62 -0
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +320 -0
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +276 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +548 -0
- package/__tests__/features/use-submit/use-submit.queue.spec.ts +84 -0
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +92 -0
- package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +99 -0
- package/__tests__/features/use-submit/use-submit.submit.spec.ts +234 -0
- package/__tests__/features/use-submit/use-submit.typescript.spec.ts +325 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/isolation.tests.ts +12 -0
- package/__tests__/shared/response.tests.ts +128 -0
- package/__tests__/tsconfig.json +27 -0
- package/__tests__/utils/cache.utils.ts +55 -0
- package/__tests__/utils/client.utils.ts +3 -0
- package/__tests__/utils/emitter.utils.ts +8 -0
- package/__tests__/utils/helpers.utils.ts +9 -0
- package/__tests__/utils/index.ts +14 -0
- package/__tests__/utils/listener.utils.ts +11 -0
- package/__tests__/utils/request.utils.ts +12 -0
- package/__tests__/utils/state.utils.ts +7 -0
- package/__tests__/utils/use-app-manager.utils.ts +10 -0
- package/__tests__/utils/use-cache.utils.ts +12 -0
- package/__tests__/utils/use-emitter.utils.ts +12 -0
- package/__tests__/utils/use-fetch.utils.ts +12 -0
- package/__tests__/utils/use-listener.utils.ts +12 -0
- package/__tests__/utils/use-queue.utils.ts +70 -0
- package/__tests__/utils/use-request-events.utils.ts +43 -0
- package/__tests__/utils/use-socket-state.utils.ts +12 -0
- package/__tests__/utils/use-submit.utils.ts +12 -0
- package/__tests__/utils/use-tracked-state.utils.ts +27 -0
- package/__tests__/vitest.setup.ts +8 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/index.d.ts +3 -0
- package/dist/helpers/use-request-events/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts +10 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts +78 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/index.d.ts +4 -0
- package/dist/helpers/use-socket-state/index.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts +4 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts +24 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts +12 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/index.d.ts +5 -0
- package/dist/helpers/use-tracked-state/index.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts +3 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +12 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +113 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +29 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/index.d.ts +3 -0
- package/dist/hooks/use-app-manager/index.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +4 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts +19 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts.map +1 -0
- package/dist/hooks/use-cache/index.d.ts +4 -0
- package/dist/hooks/use-cache/index.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts +6 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts +4 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts +24 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts.map +1 -0
- package/dist/hooks/use-emitter/index.d.ts +3 -0
- package/dist/hooks/use-emitter/index.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +26 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts +4 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/index.d.ts +5 -0
- package/dist/hooks/use-fetch/index.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts +8 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +10 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts +118 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts +2 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts.map +1 -0
- package/dist/hooks/use-listener/index.d.ts +3 -0
- package/dist/hooks/use-listener/index.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts +31 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts +4 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts.map +1 -0
- package/dist/hooks/use-queue/index.d.ts +4 -0
- package/dist/hooks/use-queue/index.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts +4 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts +10 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts +58 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts.map +1 -0
- package/dist/hooks/use-stream/index.d.ts +3 -0
- package/dist/hooks/use-stream/index.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts +11 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts +52 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts.map +1 -0
- package/dist/hooks/use-submit/index.d.ts +4 -0
- package/dist/hooks/use-submit/index.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts +8 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts +10 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts +115 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -0
- package/dist/index.d.ts +12 -733
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +1935 -0
- package/dist/index.mjs.map +1 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.d.ts.map +1 -0
- package/dist/provider/provider.d.ts +13 -0
- package/dist/provider/provider.d.ts.map +1 -0
- package/dist/provider/provider.types.d.ts +31 -0
- package/dist/provider/provider.types.d.ts.map +1 -0
- package/dist/utils/bounce.utils.d.ts +8 -0
- package/dist/utils/bounce.utils.d.ts.map +1 -0
- package/dist/utils/deep-equal.utils.d.ts +14 -0
- package/dist/utils/deep-equal.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/tracked-proxy.utils.d.ts +7 -0
- package/dist/utils/tracked-proxy.utils.d.ts.map +1 -0
- package/package.json +21 -13
- package/project.json +4 -0
- package/src/helpers/use-request-events/use-request-events.hooks.ts +60 -14
- package/src/helpers/use-request-events/use-request-events.types.ts +19 -12
- package/src/helpers/use-socket-state/use-socket-state.constants.ts +2 -2
- package/src/helpers/use-socket-state/use-socket-state.hooks.ts +47 -13
- package/src/helpers/use-socket-state/use-socket-state.types.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +74 -22
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +7 -2
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +39 -4
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
- package/src/hooks/use-cache/use-cache.hooks.ts +33 -41
- package/src/hooks/use-cache/use-cache.types.ts +3 -3
- package/src/hooks/use-emitter/use-emitter.hooks.ts +17 -16
- package/src/hooks/use-fetch/use-fetch.constants.ts +5 -2
- package/src/hooks/use-fetch/use-fetch.hooks.ts +109 -51
- package/src/hooks/use-fetch/use-fetch.types.ts +18 -3
- package/src/hooks/use-listener/use-listener.hooks.ts +19 -27
- package/src/hooks/use-queue/use-queue.constants.ts +2 -2
- package/src/hooks/use-queue/use-queue.hooks.ts +25 -6
- package/src/hooks/use-queue/use-queue.types.ts +8 -2
- package/src/hooks/use-stream/index.ts +2 -0
- package/src/hooks/use-stream/use-stream.hooks.ts +164 -0
- package/src/hooks/use-stream/use-stream.types.ts +53 -0
- package/src/hooks/use-submit/use-submit.constants.ts +2 -2
- package/src/hooks/use-submit/use-submit.hooks.ts +43 -48
- package/src/hooks/use-submit/use-submit.types.ts +9 -9
- package/src/index.ts +1 -0
- package/src/provider/provider.tsx +1 -1
- package/src/provider/provider.types.ts +8 -8
- package/src/utils/index.ts +1 -0
- package/src/utils/tracked-proxy.utils.ts +19 -0
- package/tsconfig.json +1 -1
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/dist/index.js +0 -1541
- package/dist/index.js.map +0 -7
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import {
|
|
2
|
+
expectType,
|
|
3
|
+
expectNotType,
|
|
4
|
+
expectFunctionParametersType,
|
|
5
|
+
expectNotFunctionParametersType,
|
|
6
|
+
} from "@hyper-fetch/testing";
|
|
7
|
+
import type { RequestSendType, ExtractMutationContextType } from "@hyper-fetch/core";
|
|
8
|
+
import { Client } from "@hyper-fetch/core";
|
|
9
|
+
|
|
10
|
+
import type { UseSubmitReturnType } from "hooks/use-submit";
|
|
11
|
+
|
|
12
|
+
const client = new Client({
|
|
13
|
+
url: "http://localhost:3000",
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// ---- Request definitions ----
|
|
17
|
+
|
|
18
|
+
const postUser = client.createRequest<{
|
|
19
|
+
response: { id: string; name: string };
|
|
20
|
+
payload: { name: string; email: string };
|
|
21
|
+
}>()({
|
|
22
|
+
method: "POST",
|
|
23
|
+
endpoint: "/users",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const patchUser = client.createRequest<{
|
|
27
|
+
response: { id: string; name: string };
|
|
28
|
+
payload: { name?: string };
|
|
29
|
+
}>()({
|
|
30
|
+
method: "PATCH",
|
|
31
|
+
endpoint: "/users/:userId",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const deleteUser = client.createRequest<{
|
|
35
|
+
response: void;
|
|
36
|
+
}>()({
|
|
37
|
+
method: "DELETE",
|
|
38
|
+
endpoint: "/users/:userId",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const postWithQuery = client.createRequest<{
|
|
42
|
+
response: { id: string };
|
|
43
|
+
payload: { data: string };
|
|
44
|
+
queryParams: { dryRun: boolean };
|
|
45
|
+
}>()({
|
|
46
|
+
method: "POST",
|
|
47
|
+
endpoint: "/resources",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const postWithCustomError = client.createRequest<{
|
|
51
|
+
response: { id: string };
|
|
52
|
+
payload: { value: number };
|
|
53
|
+
error: { code: string; message: string };
|
|
54
|
+
}>()({
|
|
55
|
+
method: "POST",
|
|
56
|
+
endpoint: "/items",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// ---- Type-level tests using return type directly ----
|
|
60
|
+
|
|
61
|
+
type PostUserReturn = UseSubmitReturnType<typeof postUser>;
|
|
62
|
+
type PostWithCustomErrorReturn = UseSubmitReturnType<typeof postWithCustomError>;
|
|
63
|
+
|
|
64
|
+
describe("useSubmit [Types]", () => {
|
|
65
|
+
describe("return type: data", () => {
|
|
66
|
+
it("should type data as response type or null", () => {
|
|
67
|
+
const data = null as unknown as PostUserReturn["data"];
|
|
68
|
+
expectType<{ id: string; name: string } | null>().assert(data);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should not type data as just the response type (missing null)", () => {
|
|
72
|
+
const data = null as unknown as PostUserReturn["data"];
|
|
73
|
+
expectNotType<{ id: string; name: string }>().assert(data);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should type void response correctly", () => {
|
|
77
|
+
type DeleteData = UseSubmitReturnType<typeof deleteUser>["data"];
|
|
78
|
+
const data = null as unknown as DeleteData;
|
|
79
|
+
expectType<void | null>().assert(data);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("return type: error", () => {
|
|
84
|
+
it("should type error as Error or null for default error type", () => {
|
|
85
|
+
const error = null as unknown as PostUserReturn["error"];
|
|
86
|
+
expectType<Error | null>().assert(error);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should type error with custom error type", () => {
|
|
90
|
+
const error = null as unknown as PostWithCustomErrorReturn["error"];
|
|
91
|
+
expectType<{ code: string; message: string } | Error | null>().assert(error);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("return type: submitting (not loading)", () => {
|
|
96
|
+
it("should have submitting as boolean", () => {
|
|
97
|
+
const submitting = false as PostUserReturn["submitting"];
|
|
98
|
+
expectType<boolean>().assert(submitting);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("return type: success", () => {
|
|
103
|
+
it("should type success as boolean", () => {
|
|
104
|
+
const success = false as PostUserReturn["success"];
|
|
105
|
+
expectType<boolean>().assert(success);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("submit: payload requirements", () => {
|
|
110
|
+
it("should require payload in submit for requests with payload", () => {
|
|
111
|
+
type SubmitType = RequestSendType<typeof postUser>;
|
|
112
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
113
|
+
expectFunctionParametersType(submit).assert([
|
|
114
|
+
{
|
|
115
|
+
payload: { name: "John", email: "john@test.com" },
|
|
116
|
+
},
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("should not allow submit without payload when payload is required", () => {
|
|
121
|
+
type SubmitType = RequestSendType<typeof postUser>;
|
|
122
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
123
|
+
expectNotFunctionParametersType(submit).assert([
|
|
124
|
+
{
|
|
125
|
+
payload: null,
|
|
126
|
+
},
|
|
127
|
+
]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should allow submit without explicit payload after setPayload", () => {
|
|
131
|
+
const withPayload = postUser.setPayload({ name: "John", email: "john@test.com" });
|
|
132
|
+
type SubmitType = RequestSendType<typeof withPayload>;
|
|
133
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
134
|
+
expectFunctionParametersType(submit).assert([undefined]);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("submit: params requirements", () => {
|
|
139
|
+
it("should require params in submit for requests with URL params", () => {
|
|
140
|
+
type SubmitType = RequestSendType<typeof patchUser>;
|
|
141
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
142
|
+
expectFunctionParametersType(submit).assert([
|
|
143
|
+
{
|
|
144
|
+
params: { userId: "1" },
|
|
145
|
+
payload: { name: "John" },
|
|
146
|
+
},
|
|
147
|
+
]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("should allow submit without params after setParams", () => {
|
|
151
|
+
const withParams = patchUser.setParams({ userId: "1" });
|
|
152
|
+
type SubmitType = RequestSendType<typeof withParams>;
|
|
153
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
154
|
+
expectFunctionParametersType(submit).assert([
|
|
155
|
+
{
|
|
156
|
+
payload: { name: "John" },
|
|
157
|
+
params: null,
|
|
158
|
+
},
|
|
159
|
+
]);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("submit: query params requirements", () => {
|
|
164
|
+
it("should require queryParams in submit when defined", () => {
|
|
165
|
+
type SubmitType = RequestSendType<typeof postWithQuery>;
|
|
166
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
167
|
+
expectFunctionParametersType(submit).assert([
|
|
168
|
+
{
|
|
169
|
+
payload: { data: "test" },
|
|
170
|
+
queryParams: { dryRun: true },
|
|
171
|
+
},
|
|
172
|
+
]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("should not allow submit without required queryParams", () => {
|
|
176
|
+
type SubmitType = RequestSendType<typeof postWithQuery>;
|
|
177
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
178
|
+
expectNotFunctionParametersType(submit).assert([
|
|
179
|
+
{
|
|
180
|
+
payload: { data: "test" },
|
|
181
|
+
queryParams: undefined,
|
|
182
|
+
},
|
|
183
|
+
]);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("submit: no payload/params required", () => {
|
|
188
|
+
it("should allow submit without arguments for DELETE with params set", () => {
|
|
189
|
+
const withParams = deleteUser.setParams({ userId: "1" });
|
|
190
|
+
type SubmitType = RequestSendType<typeof withParams>;
|
|
191
|
+
const submit = (() => null) as unknown as SubmitType;
|
|
192
|
+
expectFunctionParametersType(submit).assert([undefined]);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe("return type: event callbacks", () => {
|
|
197
|
+
it("should have onSubmitSuccess callback", () => {
|
|
198
|
+
const cb = (() => null) as unknown as PostUserReturn["onSubmitSuccess"];
|
|
199
|
+
expectType<(callback: any) => void>().assert(cb);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("should have onSubmitError callback", () => {
|
|
203
|
+
const cb = (() => null) as unknown as PostUserReturn["onSubmitError"];
|
|
204
|
+
expectType<(callback: any) => void>().assert(cb);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("should have onSubmitFinished callback", () => {
|
|
208
|
+
const cb = (() => null) as unknown as PostUserReturn["onSubmitFinished"];
|
|
209
|
+
expectType<(callback: any) => void>().assert(cb);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("should have abort function", () => {
|
|
213
|
+
const abort = (() => null) as unknown as PostUserReturn["abort"];
|
|
214
|
+
expectType<() => void>().assert(abort);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("should have onSubmitRequestStart callback", () => {
|
|
218
|
+
const cb = (() => null) as unknown as PostUserReturn["onSubmitRequestStart"];
|
|
219
|
+
expectType<(callback: any) => void>().assert(cb);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("should have onSubmitResponseStart callback", () => {
|
|
223
|
+
const cb = (() => null) as unknown as PostUserReturn["onSubmitResponseStart"];
|
|
224
|
+
expectType<(callback: any) => void>().assert(cb);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("return type: actions", () => {
|
|
229
|
+
it("should have setData accepting the response type or null", () => {
|
|
230
|
+
const setData = (() => null) as unknown as PostUserReturn["setData"];
|
|
231
|
+
expectFunctionParametersType(setData).assert([{ id: "1", name: "John" }]);
|
|
232
|
+
expectFunctionParametersType(setData).assert([null]);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("should have setError accepting the error type or null", () => {
|
|
236
|
+
const setError = (() => null) as unknown as PostWithCustomErrorReturn["setError"];
|
|
237
|
+
expectFunctionParametersType(setError).assert([{ code: "ERR", message: "fail" }]);
|
|
238
|
+
expectFunctionParametersType(setError).assert([null]);
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
describe("return type: bounce and refetch", () => {
|
|
243
|
+
it("should have bounce state with active and reset", () => {
|
|
244
|
+
const bounce = {} as PostUserReturn["bounce"];
|
|
245
|
+
expectType<boolean>().assert(bounce.active);
|
|
246
|
+
expectType<() => void>().assert(bounce.reset);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("should have refetch function", () => {
|
|
250
|
+
const refetch = (() => null) as unknown as PostUserReturn["refetch"];
|
|
251
|
+
expectType<() => void>().assert(refetch);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// ============================
|
|
256
|
+
// Optimistic mutations type inference
|
|
257
|
+
// ============================
|
|
258
|
+
|
|
259
|
+
describe("return type: optimistic mutations (mutationContext)", () => {
|
|
260
|
+
const patchUserOptimistic = patchUser.setOptimistic(() => {
|
|
261
|
+
const snapshot = { prevName: "old" };
|
|
262
|
+
return {
|
|
263
|
+
context: { snapshot },
|
|
264
|
+
rollback: () => {},
|
|
265
|
+
invalidate: [],
|
|
266
|
+
};
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
type OptimisticReturn = UseSubmitReturnType<typeof patchUserOptimistic>;
|
|
270
|
+
|
|
271
|
+
it("should auto-infer mutationContext type in onSubmitSuccess", () => {
|
|
272
|
+
type SuccessParam = Parameters<OptimisticReturn["onSubmitSuccess"]>[0];
|
|
273
|
+
type SuccessParamArg = Parameters<SuccessParam>[0];
|
|
274
|
+
expectType<{ snapshot: { prevName: string } } | undefined>().assert({} as SuccessParamArg["mutationContext"]);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("should auto-infer mutationContext type in onSubmitError", () => {
|
|
278
|
+
type ErrorParam = Parameters<OptimisticReturn["onSubmitError"]>[0];
|
|
279
|
+
type ErrorParamArg = Parameters<ErrorParam>[0];
|
|
280
|
+
expectType<{ snapshot: { prevName: string } } | undefined>().assert({} as ErrorParamArg["mutationContext"]);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("should auto-infer mutationContext type in onSubmitFinished", () => {
|
|
284
|
+
type FinishedParam = Parameters<OptimisticReturn["onSubmitFinished"]>[0];
|
|
285
|
+
type FinishedParamArg = Parameters<FinishedParam>[0];
|
|
286
|
+
expectType<{ snapshot: { prevName: string } } | undefined>().assert({} as FinishedParamArg["mutationContext"]);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("should auto-infer mutationContext type in onSubmitAbort", () => {
|
|
290
|
+
type AbortParam = Parameters<OptimisticReturn["onSubmitAbort"]>[0];
|
|
291
|
+
type AbortParamArg = Parameters<AbortParam>[0];
|
|
292
|
+
expectType<{ snapshot: { prevName: string } } | undefined>().assert({} as AbortParamArg["mutationContext"]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("should have undefined mutationContext when no setOptimistic is used", () => {
|
|
296
|
+
type NonOptReturn = UseSubmitReturnType<typeof postUser>;
|
|
297
|
+
type SuccessParam = Parameters<NonOptReturn["onSubmitSuccess"]>[0];
|
|
298
|
+
type SuccessParamArg = Parameters<SuccessParam>[0];
|
|
299
|
+
expectType<undefined>().assert({} as unknown as SuccessParamArg["mutationContext"]);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("should correctly extract mutation context with ExtractMutationContextType", () => {
|
|
303
|
+
type WithOptimistic = ExtractMutationContextType<typeof patchUserOptimistic>;
|
|
304
|
+
expectType<{ snapshot: { prevName: string } }>().assert({} as WithOptimistic);
|
|
305
|
+
|
|
306
|
+
type WithoutOptimistic = ExtractMutationContextType<typeof postUser>;
|
|
307
|
+
expectType<undefined>().assert({} as unknown as WithoutOptimistic);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("should preserve mutationContext type through chaining", () => {
|
|
311
|
+
const chained = patchUserOptimistic.setParams({ userId: "1" }).setRetry(3);
|
|
312
|
+
type ChainedCtx = ExtractMutationContextType<typeof chained>;
|
|
313
|
+
expectType<{ snapshot: { prevName: string } }>().assert({} as ChainedCtx);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("should correctly type payload in optimistic callback args", () => {
|
|
317
|
+
const withTypedPayload = patchUser.setOptimistic(({ payload }) => {
|
|
318
|
+
expectType<{ name?: string }>().assert(payload);
|
|
319
|
+
return { context: { saved: payload } };
|
|
320
|
+
});
|
|
321
|
+
type Ctx = ExtractMutationContextType<typeof withTypedPayload>;
|
|
322
|
+
expectType<{ saved: { name?: string | undefined } }>().assert({} as Ctx);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { waitFor } from "@testing-library/react";
|
|
2
|
+
import type { ClientInstance } from "@hyper-fetch/core";
|
|
3
|
+
|
|
4
|
+
export const testClientIsolation = async (client: ClientInstance) => {
|
|
5
|
+
await waitFor(() => {
|
|
6
|
+
expect(client.fetchDispatcher.getAllRunningRequests()).toStrictEqual([]);
|
|
7
|
+
expect(client.submitDispatcher.getAllRunningRequests()).toStrictEqual([]);
|
|
8
|
+
|
|
9
|
+
const cacheKeys = client.cache.keys();
|
|
10
|
+
expect(cacheKeys).toStrictEqual([]);
|
|
11
|
+
});
|
|
12
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { RenderHookResult } from "@testing-library/react";
|
|
2
|
+
import { waitFor } from "@testing-library/react";
|
|
3
|
+
import type { ResponseType, RequestInstance, AdapterInstance } from "@hyper-fetch/core";
|
|
4
|
+
|
|
5
|
+
import type { UseFetchReturnType } from "hooks/use-fetch";
|
|
6
|
+
import type { UseSubmitReturnType } from "hooks/use-submit";
|
|
7
|
+
import { getCurrentState } from "../utils";
|
|
8
|
+
|
|
9
|
+
export const testInitialState = <H extends RenderHookResult<any, any>>(render: H, expectedLoading = true) => {
|
|
10
|
+
const response = getCurrentState(render);
|
|
11
|
+
expect(response.data).toBe(null);
|
|
12
|
+
expect(response.status).toBe(null);
|
|
13
|
+
expect(response.error).toBe(null);
|
|
14
|
+
if (typeof response.submitting === "boolean") {
|
|
15
|
+
expect(response.submitting).toBe(false);
|
|
16
|
+
} else {
|
|
17
|
+
expect(response.loading).toBe(expectedLoading);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const testSuccessState = async <
|
|
22
|
+
T extends UseFetchReturnType<RequestInstance> | UseSubmitReturnType<RequestInstance>,
|
|
23
|
+
H extends RenderHookResult<any, any>,
|
|
24
|
+
>(
|
|
25
|
+
mock: T["data"],
|
|
26
|
+
render: H,
|
|
27
|
+
) => {
|
|
28
|
+
await waitFor(() => {
|
|
29
|
+
const response = getCurrentState(render);
|
|
30
|
+
expect(response.data).toStrictEqual(mock as Record<string, unknown>);
|
|
31
|
+
expect(response.data).toBeDefined();
|
|
32
|
+
expect(response.success).toBeTrue();
|
|
33
|
+
expect(response.status).toBe(200);
|
|
34
|
+
expect(response.extra).toHaveProperty("headers");
|
|
35
|
+
expect(response.retries).toBeNumber();
|
|
36
|
+
|
|
37
|
+
expect(response.responseTimestamp).toBeDate();
|
|
38
|
+
expect(response.requestTimestamp).toBeDate();
|
|
39
|
+
if (typeof response.submitting === "boolean") {
|
|
40
|
+
expect(response.submitting).toBe(false);
|
|
41
|
+
} else {
|
|
42
|
+
expect(response.loading).toBe(false);
|
|
43
|
+
}
|
|
44
|
+
expect(response.error).toBe(null);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const testErrorState = async <
|
|
49
|
+
T extends UseFetchReturnType<RequestInstance>,
|
|
50
|
+
H extends RenderHookResult<any, any>,
|
|
51
|
+
>(
|
|
52
|
+
mock: T["error"],
|
|
53
|
+
render: H,
|
|
54
|
+
data: any | null = null,
|
|
55
|
+
) => {
|
|
56
|
+
await waitFor(() => {
|
|
57
|
+
const response = getCurrentState(render);
|
|
58
|
+
const status = response.status || 0;
|
|
59
|
+
expect(response.error).toStrictEqual(mock);
|
|
60
|
+
expect(response.error).toBeDefined();
|
|
61
|
+
expect(response.retries).toBeNumber();
|
|
62
|
+
expect(response.responseTimestamp).toBeDate();
|
|
63
|
+
expect(response.requestTimestamp).toBeDate();
|
|
64
|
+
expect(response.success).toBeFalse();
|
|
65
|
+
expect(response.extra).toHaveProperty("headers");
|
|
66
|
+
expect(Object.keys(response.extra)).toHaveLength(1);
|
|
67
|
+
expect((status >= 400 && status < 600) || status === 0).toBeTruthy();
|
|
68
|
+
if (typeof response.submitting === "boolean") {
|
|
69
|
+
expect(response.submitting).toBe(false);
|
|
70
|
+
} else {
|
|
71
|
+
expect(response.loading).toBe(false);
|
|
72
|
+
}
|
|
73
|
+
expect(response.data).toStrictEqual(data);
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const testCacheState = async <
|
|
78
|
+
T extends ResponseType<any, any, AdapterInstance>,
|
|
79
|
+
H extends RenderHookResult<any, any>,
|
|
80
|
+
>(
|
|
81
|
+
mock: T,
|
|
82
|
+
render: H,
|
|
83
|
+
) => {
|
|
84
|
+
await waitFor(() => {
|
|
85
|
+
const response = getCurrentState(render);
|
|
86
|
+
expect(response.data).toStrictEqual(mock.data);
|
|
87
|
+
expect(response.error).toStrictEqual(mock.error);
|
|
88
|
+
expect(response.status).toBe(mock.status);
|
|
89
|
+
expect(response.extra).toStrictEqual(mock.extra);
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const testLoading = async <H extends RenderHookResult<any, any>>(mock: boolean, render: H) => {
|
|
94
|
+
await waitFor(() => {
|
|
95
|
+
const response = getCurrentState(render);
|
|
96
|
+
if (typeof response.submitting === "boolean") {
|
|
97
|
+
expect(response.submitting).toBe(mock);
|
|
98
|
+
} else {
|
|
99
|
+
expect(response.loading).toBe(mock);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const testData = async <
|
|
105
|
+
T extends UseFetchReturnType<RequestInstance> | UseSubmitReturnType<RequestInstance>,
|
|
106
|
+
H extends RenderHookResult<any, any>,
|
|
107
|
+
>(
|
|
108
|
+
mock: T["data"],
|
|
109
|
+
render: H,
|
|
110
|
+
) => {
|
|
111
|
+
await waitFor(() => {
|
|
112
|
+
const response = getCurrentState(render);
|
|
113
|
+
expect(response.data).toStrictEqual(mock as Record<string, unknown>);
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const testError = async <
|
|
118
|
+
T extends UseFetchReturnType<RequestInstance> | UseSubmitReturnType<RequestInstance>,
|
|
119
|
+
H extends RenderHookResult<any, any>,
|
|
120
|
+
>(
|
|
121
|
+
mock: T["error"],
|
|
122
|
+
render: H,
|
|
123
|
+
) => {
|
|
124
|
+
await waitFor(() => {
|
|
125
|
+
const response = getCurrentState(render);
|
|
126
|
+
expect(response.error).toStrictEqual(mock as Record<string, unknown>);
|
|
127
|
+
});
|
|
128
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "esnext",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["dom", "dom.iterable", "ESNext"],
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noEmit": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"isolatedModules": true,
|
|
15
|
+
"noUnusedLocals": false,
|
|
16
|
+
"noUnusedParameters": false,
|
|
17
|
+
"types": ["vitest/globals", "jest-extended", "@testing-library/jest-dom", "@types/node"],
|
|
18
|
+
"baseUrl": "../src",
|
|
19
|
+
"paths": {
|
|
20
|
+
"hooks/*": ["hooks/*"],
|
|
21
|
+
"helpers/*": ["helpers/*"],
|
|
22
|
+
"utils/*": ["utils/*"],
|
|
23
|
+
"provider": ["provider"]
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"include": ["**/*.ts", "**/*.tsx", "../../../global.d.ts", "../../../vitest.d.ts"]
|
|
27
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ResponseType,
|
|
3
|
+
ResponseDetailsType,
|
|
4
|
+
RequestInstance,
|
|
5
|
+
ExtractResponseType,
|
|
6
|
+
ExtractErrorType,
|
|
7
|
+
ExtractAdapterType,
|
|
8
|
+
HttpAdapterType,
|
|
9
|
+
ExtractAdapterStatusType,
|
|
10
|
+
ExtractAdapterExtraType,
|
|
11
|
+
} from "@hyper-fetch/core";
|
|
12
|
+
import { xhrExtra } from "@hyper-fetch/core";
|
|
13
|
+
|
|
14
|
+
export const createCacheData = <T extends RequestInstance>(
|
|
15
|
+
request: T,
|
|
16
|
+
response?: {
|
|
17
|
+
data?: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>;
|
|
18
|
+
details?: Partial<ResponseDetailsType>;
|
|
19
|
+
},
|
|
20
|
+
) => {
|
|
21
|
+
const dataValue: ResponseType<
|
|
22
|
+
ExtractResponseType<T>,
|
|
23
|
+
ExtractErrorType<T>,
|
|
24
|
+
ExtractAdapterType<T>
|
|
25
|
+
> = response?.data || {
|
|
26
|
+
data: { data: 1 } as ExtractResponseType<T>,
|
|
27
|
+
error: null,
|
|
28
|
+
status: 200 as unknown as ExtractAdapterStatusType<ExtractAdapterType<T>>,
|
|
29
|
+
success: true,
|
|
30
|
+
extra: xhrExtra as unknown as ExtractAdapterExtraType<ExtractAdapterType<T>>,
|
|
31
|
+
requestTimestamp: +new Date(),
|
|
32
|
+
responseTimestamp: +new Date(),
|
|
33
|
+
};
|
|
34
|
+
const detailsValue: ResponseDetailsType = {
|
|
35
|
+
retries: 0,
|
|
36
|
+
triggerTimestamp: +new Date(),
|
|
37
|
+
addedTimestamp: +new Date(),
|
|
38
|
+
requestTimestamp: +new Date(),
|
|
39
|
+
responseTimestamp: +new Date(),
|
|
40
|
+
isCanceled: false,
|
|
41
|
+
isOffline: false,
|
|
42
|
+
willRetry: false,
|
|
43
|
+
...response?.details,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
request.client.cache.storage.set<any, any, HttpAdapterType>(request.cacheKey, {
|
|
47
|
+
...(dataValue as any),
|
|
48
|
+
...detailsValue,
|
|
49
|
+
staleTime: 1000,
|
|
50
|
+
version: request.client.cache.version,
|
|
51
|
+
cacheTime: Infinity,
|
|
52
|
+
cached: true,
|
|
53
|
+
});
|
|
54
|
+
return [dataValue, detailsValue] as const;
|
|
55
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { EmitterOptionsType, WebsocketAdapterType } from "@hyper-fetch/sockets";
|
|
2
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
3
|
+
|
|
4
|
+
export const createEmitter = <ResponseType = { name: string; age: number }>(
|
|
5
|
+
options?: Partial<EmitterOptionsType<any, WebsocketAdapterType>>,
|
|
6
|
+
) => {
|
|
7
|
+
return new Socket({ url: "ws://localhost:1234" }).createEmitter<ResponseType>()({ topic: "some-event", ...options });
|
|
8
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { sleep } from "@hyper-fetch/testing";
|
|
2
|
+
import { act } from "@testing-library/react";
|
|
3
|
+
|
|
4
|
+
// Solves the issue: https://github.com/testing-library/react-testing-library/issues/1051
|
|
5
|
+
export const waitForRender = async (time = 10) => {
|
|
6
|
+
await act(async () => {
|
|
7
|
+
await sleep(time);
|
|
8
|
+
});
|
|
9
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./client.utils";
|
|
2
|
+
export * from "./cache.utils";
|
|
3
|
+
export * from "./request.utils";
|
|
4
|
+
export * from "./helpers.utils";
|
|
5
|
+
export * from "./state.utils";
|
|
6
|
+
|
|
7
|
+
export * from "./use-fetch.utils";
|
|
8
|
+
export * from "./use-submit.utils";
|
|
9
|
+
export * from "./use-app-manager.utils";
|
|
10
|
+
export * from "./use-queue.utils";
|
|
11
|
+
export * from "./use-request-events.utils";
|
|
12
|
+
export * from "./use-tracked-state.utils";
|
|
13
|
+
export * from "./use-listener.utils";
|
|
14
|
+
export * from "./use-emitter.utils";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ListenerOptionsType, WebsocketAdapterType } from "@hyper-fetch/sockets";
|
|
2
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
3
|
+
|
|
4
|
+
export const createListener = <ResponseType = { name: string; age: number }>(
|
|
5
|
+
options?: Partial<ListenerOptionsType<any, WebsocketAdapterType>>,
|
|
6
|
+
) => {
|
|
7
|
+
return new Socket({ url: "ws://localhost:1234" }).createListener<ResponseType>()({
|
|
8
|
+
...options,
|
|
9
|
+
topic: "some-event",
|
|
10
|
+
});
|
|
11
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RequestOptionsType, FetchAdapterOptionsType } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
import { client } from "./client.utils";
|
|
4
|
+
|
|
5
|
+
export const createRequest = <ResponseType = any, PayloadType = any, Endpoint extends string = "/shared-endpoint">(
|
|
6
|
+
options?: Partial<RequestOptionsType<Endpoint, FetchAdapterOptionsType>>,
|
|
7
|
+
) => {
|
|
8
|
+
return client.createRequest<{ response: ResponseType; payload: PayloadType; error: Error; queryParams: any }>()({
|
|
9
|
+
endpoint: "/shared-endpoint" as Endpoint,
|
|
10
|
+
...options,
|
|
11
|
+
} as RequestOptionsType<Endpoint, FetchAdapterOptionsType>);
|
|
12
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ClientInstance } from "@hyper-fetch/core";
|
|
2
|
+
import { renderHook } from "@testing-library/react";
|
|
3
|
+
|
|
4
|
+
import { useAppManager } from "hooks/use-app-manager";
|
|
5
|
+
|
|
6
|
+
export const renderUseAppManager = <B extends ClientInstance>(client: B) => {
|
|
7
|
+
return renderHook(() => {
|
|
8
|
+
return useAppManager(client);
|
|
9
|
+
});
|
|
10
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RequestInstance } from "@hyper-fetch/core";
|
|
2
|
+
import { renderHook } from "@testing-library/react";
|
|
3
|
+
|
|
4
|
+
import type { UseCacheOptionsType } from "hooks/use-cache";
|
|
5
|
+
import { useCache } from "hooks/use-cache";
|
|
6
|
+
|
|
7
|
+
export const renderUseCache = <T extends RequestInstance>(request: T, options?: UseCacheOptionsType<T>) => {
|
|
8
|
+
return renderHook((rerenderOptions: UseCacheOptionsType<RequestInstance> & { request?: RequestInstance }) => {
|
|
9
|
+
const { request: cmd, ...rest } = rerenderOptions || {};
|
|
10
|
+
return useCache(cmd || request, { dependencyTracking: false, ...options, ...rest });
|
|
11
|
+
});
|
|
12
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react";
|
|
2
|
+
import type { EmitterInstance } from "@hyper-fetch/sockets";
|
|
3
|
+
|
|
4
|
+
import type { UseEmitterOptionsType } from "hooks/use-emitter";
|
|
5
|
+
import { useEmitter } from "hooks/use-emitter";
|
|
6
|
+
|
|
7
|
+
export const renderUseEmitter = <T extends EmitterInstance>(Emitter: T, options?: UseEmitterOptionsType) => {
|
|
8
|
+
return renderHook((rerenderOptions: UseEmitterOptionsType & { emitter?: EmitterInstance }) => {
|
|
9
|
+
const { emitter: lst, ...rest } = rerenderOptions || {};
|
|
10
|
+
return useEmitter((lst || Emitter) as T, { dependencyTracking: false, ...options, ...rest });
|
|
11
|
+
});
|
|
12
|
+
};
|