@hyper-fetch/react 8.1.1 → 8.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -52
- package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +1 -1
- package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +1 -1
- package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +3 -3
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +55 -55
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +8 -9
- package/__tests__/features/provider/provider.hydration.spec.tsx +3 -4
- package/__tests__/features/provider/use-config-provider.base.spec.tsx +1 -2
- package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +1 -1
- package/__tests__/features/use-cache/use-cache.base.spec.ts +6 -6
- package/__tests__/features/use-cache/use-cache.typescript.spec.ts +1 -2
- package/__tests__/features/use-emitter/use-emitter.base.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.base.spec.ts +13 -9
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +2 -2
- package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +2 -2
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +3 -2
- package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +3 -3
- package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +47 -17
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +4 -4
- package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +1 -2
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +2 -2
- package/__tests__/features/use-fetch/use-fetch.window.spec.ts +1 -1
- package/__tests__/features/use-listener/use-listener.base.spec.ts +1 -1
- package/__tests__/features/use-queue/use-queue.actions.spec.ts +3 -3
- package/__tests__/features/use-queue/use-queue.base.spec.ts +13 -13
- package/__tests__/features/use-queue/use-queue.resolve.spec.ts +6 -6
- package/__tests__/features/use-stream/use-stream.base.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.base.spec.ts +2 -2
- package/__tests__/features/use-submit/use-submit.cancel.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +15 -15
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.queue.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +2 -2
- package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.submit.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.typescript.spec.ts +2 -3
- package/__tests__/shared/isolation.tests.ts +1 -1
- package/__tests__/shared/response.tests.ts +2 -2
- package/__tests__/tsconfig.json +11 -17
- package/__tests__/utils/cache.utils.ts +6 -6
- package/__tests__/utils/use-app-manager.utils.ts +0 -1
- package/__tests__/utils/use-cache.utils.ts +0 -1
- package/__tests__/utils/use-emitter.utils.ts +1 -2
- package/__tests__/utils/use-fetch.utils.ts +0 -1
- package/__tests__/utils/use-listener.utils.ts +1 -2
- package/__tests__/utils/use-queue.utils.ts +4 -5
- package/__tests__/utils/use-request-events.utils.ts +0 -1
- package/__tests__/utils/use-socket-state.utils.ts +1 -2
- package/__tests__/utils/use-submit.utils.ts +0 -1
- package/__tests__/utils/use-tracked-state.utils.ts +0 -1
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts.map +1 -1
- package/dist/hooks/use-cache/use-cache.hooks.d.ts +1 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
- package/dist/hooks/use-cache/use-cache.types.d.ts +2 -2
- package/dist/hooks/use-cache/use-cache.types.d.ts.map +1 -1
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +1 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts.map +1 -1
- package/dist/hooks/use-emitter/use-emitter.types.d.ts +1 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +2 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.types.d.ts.map +1 -1
- package/dist/hooks/use-listener/use-listener.hooks.d.ts +1 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
- package/dist/hooks/use-listener/use-listener.types.d.ts +1 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts.map +1 -1
- package/dist/hooks/use-queue/use-queue.constants.d.ts.map +1 -1
- package/dist/hooks/use-queue/use-queue.hooks.d.ts +2 -1
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
- package/dist/hooks/use-queue/use-queue.types.d.ts +6 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.hooks.d.ts +2 -1
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.types.d.ts +1 -1
- package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -1
- package/dist/index.mjs +30 -22
- package/dist/index.mjs.map +1 -1
- package/dist/provider/provider.types.d.ts +4 -4
- package/dist/provider/provider.types.d.ts.map +1 -1
- package/dist/utils/bounce.utils.d.ts.map +1 -1
- package/dist/utils/deep-equal.utils.d.ts +2 -2
- package/dist/utils/deep-equal.utils.d.ts.map +1 -1
- package/dist/utils/tracked-proxy.utils.d.ts +1 -1
- package/dist/utils/tracked-proxy.utils.d.ts.map +1 -1
- package/package.json +8 -13
- package/src/helpers/use-request-events/use-request-events.hooks.ts +4 -5
- package/src/helpers/use-request-events/use-request-events.types.ts +0 -1
- package/src/helpers/use-socket-state/use-socket-state.hooks.ts +5 -6
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +11 -11
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +0 -1
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +12 -13
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
- package/src/hooks/use-cache/use-cache.hooks.ts +4 -4
- package/src/hooks/use-cache/use-cache.types.ts +2 -3
- package/src/hooks/use-emitter/use-emitter.hooks.ts +4 -4
- package/src/hooks/use-emitter/use-emitter.types.ts +1 -0
- package/src/hooks/use-fetch/use-fetch.constants.ts +0 -1
- package/src/hooks/use-fetch/use-fetch.hooks.ts +14 -10
- package/src/hooks/use-fetch/use-fetch.types.ts +0 -1
- package/src/hooks/use-listener/use-listener.hooks.ts +3 -3
- package/src/hooks/use-listener/use-listener.types.ts +1 -0
- package/src/hooks/use-queue/use-queue.constants.ts +0 -1
- package/src/hooks/use-queue/use-queue.hooks.ts +3 -3
- package/src/hooks/use-queue/use-queue.types.ts +6 -0
- package/src/hooks/use-stream/use-stream.hooks.ts +4 -4
- package/src/hooks/use-submit/use-submit.hooks.ts +7 -7
- package/src/hooks/use-submit/use-submit.types.ts +1 -2
- package/src/provider/provider.types.ts +4 -5
- package/src/utils/bounce.utils.ts +1 -0
- package/src/utils/deep-equal.utils.ts +28 -12
- package/src/utils/tracked-proxy.utils.ts +1 -1
- package/tsconfig.json +11 -2
- package/vite.config.ts +14 -20
- package/.eslintrc.cjs +0 -12
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
# ⚛️
|
|
1
|
+
# ⚛️ Hyper Fetch React
|
|
2
2
|
|
|
3
|
-
<p>
|
|
3
|
+
<p align="center">
|
|
4
|
+
<b>React hooks for HyperFetch. Automatic fetching, caching, and real-time updates with full type safety.</b>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
4
8
|
<a href="https://bettertyped.com/">
|
|
5
9
|
<img src="https://custom-icon-badges.demolab.com/static/v1?label=&message=BetterTyped&color=333&logo=BT" />
|
|
6
10
|
</a>
|
|
@@ -13,60 +17,206 @@
|
|
|
13
17
|
<a href="https://www.npmjs.com/package/@hyper-fetch/react">
|
|
14
18
|
<img src="https://custom-icon-badges.demolab.com/npm/v/@hyper-fetch/react.svg?logo=npm&color=e76f51" />
|
|
15
19
|
</a>
|
|
20
|
+
<a href="https://www.npmjs.com/package/@hyper-fetch/react">
|
|
21
|
+
<img src="https://custom-icon-badges.demolab.com/npm/dm/@hyper-fetch/react?logoColor=fff&logo=trending-up" />
|
|
22
|
+
</a>
|
|
16
23
|
<a href="https://github.com/BetterTyped/hyper-fetch">
|
|
17
24
|
<img src="https://custom-icon-badges.demolab.com/badge/typescript-%23007ACC.svg?logo=typescript&logoColor=white" />
|
|
18
25
|
</a>
|
|
19
|
-
<a href="https://
|
|
26
|
+
<a href="https://bundlephobia.com/package/@hyper-fetch/react">
|
|
20
27
|
<img src="https://custom-icon-badges.demolab.com/bundlephobia/minzip/@hyper-fetch/react?color=64BC4B&logo=package" />
|
|
21
28
|
</a>
|
|
22
29
|
</p>
|
|
23
30
|
|
|
24
|
-
## About
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
## Key
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
-
|
|
72
|
-
-
|
|
31
|
+
## 📖 About
|
|
32
|
+
|
|
33
|
+
`@hyper-fetch/react` connects HyperFetch's typed request system to React's component lifecycle. `useFetch` triggers
|
|
34
|
+
requests on mount and re-fetches when dependencies change. `useSubmit` gives you a `submit` function for mutations
|
|
35
|
+
triggered by user actions. Both hooks return fully typed `data`, `error`, and loading states with built-in callbacks.
|
|
36
|
+
|
|
37
|
+
## 🎯 Key Capabilities
|
|
38
|
+
|
|
39
|
+
- ⚡ **Data on mount, zero boilerplate** — `useFetch` fetches, caches, and types your data automatically
|
|
40
|
+
- 🎯 **Mutations that just work** — `useSubmit` handles loading, errors, and success states for form submissions
|
|
41
|
+
- 🔄 **Always fresh data** — Auto-refetch on window focus, polling, and stale-time revalidation built in
|
|
42
|
+
- ✨ **Optimistic updates with rollback** — Update the UI instantly and auto-revert if the request fails
|
|
43
|
+
- 🔮 **Full type safety** — Every hook, callback, response, and error is typed end-to-end with zero `any`
|
|
44
|
+
- 🚀 **Instant page loads** — Prefetch data before components mount so users never wait
|
|
45
|
+
- 🔗 **No more useEffect spaghetti** — Dependent requests via `disabled` option, no manual orchestration
|
|
46
|
+
- 📡 **SSR out of the box** — Works with Next.js, Remix, and any server-rendered framework
|
|
47
|
+
- 💎 **Skip the state library** — No Redux or Zustand needed for server state — it's all handled
|
|
48
|
+
|
|
49
|
+
## 🚀 Quick Start
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install @hyper-fetch/core @hyper-fetch/react
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import { useFetch } from "@hyper-fetch/react";
|
|
57
|
+
import { getUsers } from "./api";
|
|
58
|
+
|
|
59
|
+
const UserList = () => {
|
|
60
|
+
// Fetches on mount, returns typed data — no useEffect needed
|
|
61
|
+
const { data, loading, error } = useFetch(getUsers);
|
|
62
|
+
|
|
63
|
+
if (loading) return <p>Loading...</p>;
|
|
64
|
+
if (error) return <p>Something went wrong</p>;
|
|
65
|
+
return (
|
|
66
|
+
<ul>
|
|
67
|
+
{data?.map((user) => (
|
|
68
|
+
<li key={user.id}>{user.name}</li>
|
|
69
|
+
))}
|
|
70
|
+
</ul>
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 📚 Documentation
|
|
76
|
+
|
|
77
|
+
- [Getting Started](https://hyperfetch.bettertyped.com/docs/getting-started/quick-start)
|
|
78
|
+
- [React Overview](https://hyperfetch.bettertyped.com/docs/react/overview)
|
|
79
|
+
- [API Reference](https://hyperfetch.bettertyped.com/api/)
|
|
80
|
+
|
|
81
|
+
## 💡 Examples
|
|
82
|
+
|
|
83
|
+
### Fetch with callbacks
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { useFetch } from "@hyper-fetch/react";
|
|
87
|
+
|
|
88
|
+
const UserProfile = ({ userId }: { userId: number }) => {
|
|
89
|
+
// setParams makes the request dynamic — re-fetches when userId changes
|
|
90
|
+
const { data, loading, error, onSuccess, onError } = useFetch(getUser.setParams({ userId }));
|
|
91
|
+
|
|
92
|
+
// Typed callbacks — response shape matches your request definition
|
|
93
|
+
onSuccess(({ response }) => {
|
|
94
|
+
console.log("Loaded user:", response.data.name);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
onError(({ response }) => {
|
|
98
|
+
console.error("Failed to load user:", response.error);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (loading) return <p>Loading...</p>;
|
|
102
|
+
if (error) return <p>Error: {error.message}</p>;
|
|
103
|
+
return <h1>{data.name}</h1>;
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Mutations with useSubmit
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
import { useSubmit } from "@hyper-fetch/react";
|
|
111
|
+
|
|
112
|
+
const CreateUserForm = () => {
|
|
113
|
+
// useSubmit doesn't fire on mount — it waits for you to call submit()
|
|
114
|
+
const { submit, submitting, onSubmitSuccess, onSubmitError } = useSubmit(createUser);
|
|
115
|
+
|
|
116
|
+
// Callbacks fire after each submission with typed response data
|
|
117
|
+
onSubmitSuccess(({ response }) => {
|
|
118
|
+
alert(`User ${response.data.name} created!`);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
onSubmitError(({ response }) => {
|
|
122
|
+
alert(`Error: ${response.error.message}`);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
126
|
+
e.preventDefault();
|
|
127
|
+
const form = new FormData(e.currentTarget);
|
|
128
|
+
// Pass typed payload — TypeScript ensures the shape matches your request definition
|
|
129
|
+
submit({
|
|
130
|
+
data: { name: form.get("name") as string, email: form.get("email") as string },
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<form onSubmit={handleSubmit}>
|
|
136
|
+
<input name="name" placeholder="Name" required />
|
|
137
|
+
<input name="email" placeholder="Email" required />
|
|
138
|
+
<button type="submit" disabled={submitting}>
|
|
139
|
+
{submitting ? "Creating..." : "Create User"}
|
|
140
|
+
</button>
|
|
141
|
+
</form>
|
|
142
|
+
);
|
|
143
|
+
};
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Dependent requests
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
const UserPosts = ({ userId }: { userId: number }) => {
|
|
150
|
+
// First request fetches the user
|
|
151
|
+
const { data: user } = useFetch(getUser.setParams({ userId }));
|
|
152
|
+
// Second request waits until user is loaded — no useEffect chains needed
|
|
153
|
+
const { data: posts } = useFetch(getUserPosts.setParams({ userId }), {
|
|
154
|
+
disabled: !user,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<div>
|
|
159
|
+
<h1>{user?.name}</h1>
|
|
160
|
+
{posts?.map((post) => (
|
|
161
|
+
<article key={post.id}>{post.title}</article>
|
|
162
|
+
))}
|
|
163
|
+
</div>
|
|
164
|
+
);
|
|
165
|
+
};
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Optimistic updates with automatic rollback
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
import { useSubmit } from "@hyper-fetch/react";
|
|
172
|
+
|
|
173
|
+
const AddComment = ({ postId }: { postId: number }) => {
|
|
174
|
+
// setOptimistic runs BEFORE the request — update the list instantly
|
|
175
|
+
const optimisticAdd = addComment.setParams({ postId }).setOptimistic((request, client) => {
|
|
176
|
+
const cacheKey = getComments.setParams({ postId });
|
|
177
|
+
const previous = client.cache.get(cacheKey);
|
|
178
|
+
|
|
179
|
+
// Immediately insert the new comment into the cached list
|
|
180
|
+
const optimisticComment = { id: `temp-${Date.now()}`, text: request.data.text, author: "You" };
|
|
181
|
+
client.cache.set(cacheKey, {
|
|
182
|
+
...previous,
|
|
183
|
+
data: [...(previous?.data || []), optimisticComment],
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
context: { previous },
|
|
188
|
+
// If the request fails, put the old list back
|
|
189
|
+
rollback: () => client.cache.set(cacheKey, previous),
|
|
190
|
+
// On success, refetch to get the real server data
|
|
191
|
+
invalidate: [cacheKey],
|
|
192
|
+
};
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const { submit, submitting, onSubmitError } = useSubmit(optimisticAdd);
|
|
196
|
+
|
|
197
|
+
// mutationContext is fully typed from your setOptimistic return
|
|
198
|
+
onSubmitError(({ mutationContext }) => {
|
|
199
|
+
console.log("Rolled back — restored", mutationContext?.previous?.data?.length, "comments");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
203
|
+
e.preventDefault();
|
|
204
|
+
const text = new FormData(e.currentTarget).get("text") as string;
|
|
205
|
+
submit({ data: { text } });
|
|
206
|
+
e.currentTarget.reset();
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<form onSubmit={handleSubmit}>
|
|
211
|
+
<input name="text" placeholder="Write a comment..." required />
|
|
212
|
+
<button type="submit" disabled={submitting}>
|
|
213
|
+
Post
|
|
214
|
+
</button>
|
|
215
|
+
</form>
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
[MIT](https://github.com/BetterTyped/hyper-fetch/blob/main/License.md)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { act } from "@testing-library/react";
|
|
2
1
|
import { createClient } from "@hyper-fetch/core";
|
|
3
2
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
import { act } from "@testing-library/react";
|
|
4
4
|
|
|
5
5
|
import { renderUseRequestEvents } from "../../../utils";
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { act } from "@testing-library/react";
|
|
2
1
|
import { Socket } from "@hyper-fetch/sockets";
|
|
3
2
|
import { createWebsocketMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
import { act } from "@testing-library/react";
|
|
4
4
|
|
|
5
5
|
import { renderUseSocketState } from "../../../utils/use-socket-state.utils";
|
|
6
6
|
|
|
@@ -191,13 +191,13 @@ describe("useSocketState [ Base ]", () => {
|
|
|
191
191
|
actions.setExtra({ test: true } as any);
|
|
192
192
|
actions.setConnected(true);
|
|
193
193
|
actions.setConnecting(true);
|
|
194
|
-
actions.setTimestamp(
|
|
194
|
+
actions.setTimestamp(12_345);
|
|
195
195
|
});
|
|
196
196
|
|
|
197
197
|
expect(view.result.current[0].data).toBe("some-data");
|
|
198
198
|
expect(view.result.current[0].connected).toBe(true);
|
|
199
199
|
expect(view.result.current[0].connecting).toBe(true);
|
|
200
|
-
expect(view.result.current[0].timestamp).toBe(
|
|
200
|
+
expect(view.result.current[0].timestamp).toBe(12_345);
|
|
201
201
|
|
|
202
202
|
act(() => {
|
|
203
203
|
view.result.current[1].clearState();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createClient } from "@hyper-fetch/core";
|
|
2
|
-
import { act, waitFor } from "@testing-library/react";
|
|
3
2
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
import { act, waitFor } from "@testing-library/react";
|
|
4
4
|
|
|
5
5
|
import { renderUseTrackedState } from "../../../utils";
|
|
6
6
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createClient, xhrExtra } from "@hyper-fetch/core";
|
|
2
|
-
import { act, waitFor } from "@testing-library/react";
|
|
3
2
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
4
|
-
|
|
3
|
+
import { act, waitFor } from "@testing-library/react";
|
|
5
4
|
import { initialState } from "helpers";
|
|
5
|
+
|
|
6
6
|
import { renderUseTrackedState } from "../../../utils";
|
|
7
7
|
|
|
8
8
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
@@ -68,10 +68,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
68
68
|
success: true,
|
|
69
69
|
extra: xhrExtra,
|
|
70
70
|
retries: 0,
|
|
71
|
-
requestTimestamp:
|
|
72
|
-
responseTimestamp:
|
|
73
|
-
addedTimestamp:
|
|
74
|
-
triggerTimestamp:
|
|
71
|
+
requestTimestamp: Date.now(),
|
|
72
|
+
responseTimestamp: Date.now(),
|
|
73
|
+
addedTimestamp: Date.now(),
|
|
74
|
+
triggerTimestamp: Date.now(),
|
|
75
75
|
cacheKey: request.cacheKey,
|
|
76
76
|
scope: request.scope,
|
|
77
77
|
isCanceled: false,
|
|
@@ -95,10 +95,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
95
95
|
success: true,
|
|
96
96
|
extra: xhrExtra,
|
|
97
97
|
retries: 0,
|
|
98
|
-
requestTimestamp:
|
|
99
|
-
responseTimestamp:
|
|
100
|
-
addedTimestamp:
|
|
101
|
-
triggerTimestamp:
|
|
98
|
+
requestTimestamp: Date.now(),
|
|
99
|
+
responseTimestamp: Date.now(),
|
|
100
|
+
addedTimestamp: Date.now(),
|
|
101
|
+
triggerTimestamp: Date.now(),
|
|
102
102
|
cacheKey: request.cacheKey,
|
|
103
103
|
scope: request.scope,
|
|
104
104
|
isCanceled: false,
|
|
@@ -131,10 +131,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
131
131
|
success: true,
|
|
132
132
|
extra: xhrExtra,
|
|
133
133
|
retries: 0,
|
|
134
|
-
requestTimestamp:
|
|
135
|
-
responseTimestamp:
|
|
136
|
-
addedTimestamp:
|
|
137
|
-
triggerTimestamp:
|
|
134
|
+
requestTimestamp: Date.now(),
|
|
135
|
+
responseTimestamp: Date.now(),
|
|
136
|
+
addedTimestamp: Date.now(),
|
|
137
|
+
triggerTimestamp: Date.now(),
|
|
138
138
|
cacheKey: request.cacheKey,
|
|
139
139
|
scope: request.scope,
|
|
140
140
|
isCanceled: false,
|
|
@@ -168,10 +168,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
168
168
|
success: true,
|
|
169
169
|
extra: xhrExtra,
|
|
170
170
|
retries: 0,
|
|
171
|
-
requestTimestamp:
|
|
172
|
-
responseTimestamp:
|
|
173
|
-
addedTimestamp:
|
|
174
|
-
triggerTimestamp:
|
|
171
|
+
requestTimestamp: Date.now(),
|
|
172
|
+
responseTimestamp: Date.now(),
|
|
173
|
+
addedTimestamp: Date.now(),
|
|
174
|
+
triggerTimestamp: Date.now(),
|
|
175
175
|
cacheKey: request.cacheKey,
|
|
176
176
|
scope: request.scope,
|
|
177
177
|
isCanceled: false,
|
|
@@ -204,10 +204,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
204
204
|
success: true,
|
|
205
205
|
extra: xhrExtra,
|
|
206
206
|
retries: 0,
|
|
207
|
-
requestTimestamp:
|
|
208
|
-
responseTimestamp:
|
|
209
|
-
addedTimestamp:
|
|
210
|
-
triggerTimestamp:
|
|
207
|
+
requestTimestamp: Date.now(),
|
|
208
|
+
responseTimestamp: Date.now(),
|
|
209
|
+
addedTimestamp: Date.now(),
|
|
210
|
+
triggerTimestamp: Date.now(),
|
|
211
211
|
cacheKey: request.cacheKey,
|
|
212
212
|
scope: request.scope,
|
|
213
213
|
isCanceled: false,
|
|
@@ -243,10 +243,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
243
243
|
success: true,
|
|
244
244
|
extra: xhrExtra,
|
|
245
245
|
retries: 0,
|
|
246
|
-
requestTimestamp:
|
|
247
|
-
responseTimestamp:
|
|
248
|
-
addedTimestamp:
|
|
249
|
-
triggerTimestamp:
|
|
246
|
+
requestTimestamp: Date.now(),
|
|
247
|
+
responseTimestamp: Date.now(),
|
|
248
|
+
addedTimestamp: Date.now(),
|
|
249
|
+
triggerTimestamp: Date.now(),
|
|
250
250
|
cacheKey: request.cacheKey,
|
|
251
251
|
scope: request.scope,
|
|
252
252
|
isCanceled: false,
|
|
@@ -287,7 +287,7 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
287
287
|
it("should map the async data", async () => {
|
|
288
288
|
const { result } = renderUseTrackedState(
|
|
289
289
|
request.setResponseMapper(async (response) => {
|
|
290
|
-
return
|
|
290
|
+
return { ...response, data: "new-data" } as typeof response;
|
|
291
291
|
}),
|
|
292
292
|
);
|
|
293
293
|
|
|
@@ -300,10 +300,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
300
300
|
success: true,
|
|
301
301
|
extra: xhrExtra,
|
|
302
302
|
retries: 0,
|
|
303
|
-
requestTimestamp:
|
|
304
|
-
responseTimestamp:
|
|
305
|
-
addedTimestamp:
|
|
306
|
-
triggerTimestamp:
|
|
303
|
+
requestTimestamp: Date.now(),
|
|
304
|
+
responseTimestamp: Date.now(),
|
|
305
|
+
addedTimestamp: Date.now(),
|
|
306
|
+
triggerTimestamp: Date.now(),
|
|
307
307
|
cacheKey: request.cacheKey,
|
|
308
308
|
scope: request.scope,
|
|
309
309
|
isCanceled: false,
|
|
@@ -338,10 +338,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
338
338
|
success: true,
|
|
339
339
|
extra: xhrExtra,
|
|
340
340
|
retries: 0,
|
|
341
|
-
requestTimestamp:
|
|
342
|
-
responseTimestamp:
|
|
343
|
-
addedTimestamp:
|
|
344
|
-
triggerTimestamp:
|
|
341
|
+
requestTimestamp: Date.now(),
|
|
342
|
+
responseTimestamp: Date.now(),
|
|
343
|
+
addedTimestamp: Date.now(),
|
|
344
|
+
triggerTimestamp: Date.now(),
|
|
345
345
|
cacheKey: request.cacheKey,
|
|
346
346
|
scope: request.scope,
|
|
347
347
|
isCanceled: false,
|
|
@@ -375,10 +375,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
375
375
|
success: true,
|
|
376
376
|
extra: xhrExtra,
|
|
377
377
|
retries: 0,
|
|
378
|
-
requestTimestamp:
|
|
379
|
-
responseTimestamp:
|
|
380
|
-
addedTimestamp:
|
|
381
|
-
triggerTimestamp:
|
|
378
|
+
requestTimestamp: Date.now(),
|
|
379
|
+
responseTimestamp: Date.now(),
|
|
380
|
+
addedTimestamp: Date.now(),
|
|
381
|
+
triggerTimestamp: Date.now(),
|
|
382
382
|
cacheKey: request.cacheKey,
|
|
383
383
|
scope: request.scope,
|
|
384
384
|
isCanceled: false,
|
|
@@ -434,10 +434,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
434
434
|
success: true,
|
|
435
435
|
extra: xhrExtra,
|
|
436
436
|
retries: 0,
|
|
437
|
-
requestTimestamp:
|
|
438
|
-
responseTimestamp:
|
|
439
|
-
addedTimestamp:
|
|
440
|
-
triggerTimestamp:
|
|
437
|
+
requestTimestamp: Date.now(),
|
|
438
|
+
responseTimestamp: Date.now(),
|
|
439
|
+
addedTimestamp: Date.now(),
|
|
440
|
+
triggerTimestamp: Date.now(),
|
|
441
441
|
cacheKey: request.cacheKey,
|
|
442
442
|
scope: request.scope,
|
|
443
443
|
isCanceled: false,
|
|
@@ -459,10 +459,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
459
459
|
success: true,
|
|
460
460
|
extra: xhrExtra,
|
|
461
461
|
retries: 0,
|
|
462
|
-
requestTimestamp:
|
|
463
|
-
responseTimestamp:
|
|
464
|
-
addedTimestamp:
|
|
465
|
-
triggerTimestamp:
|
|
462
|
+
requestTimestamp: Date.now(),
|
|
463
|
+
responseTimestamp: Date.now(),
|
|
464
|
+
addedTimestamp: Date.now(),
|
|
465
|
+
triggerTimestamp: Date.now(),
|
|
466
466
|
cacheKey: request.cacheKey,
|
|
467
467
|
scope: request.scope,
|
|
468
468
|
isCanceled: false,
|
|
@@ -510,10 +510,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
510
510
|
success: true,
|
|
511
511
|
extra: xhrExtra,
|
|
512
512
|
retries: 0,
|
|
513
|
-
requestTimestamp:
|
|
514
|
-
responseTimestamp:
|
|
515
|
-
addedTimestamp:
|
|
516
|
-
triggerTimestamp:
|
|
513
|
+
requestTimestamp: Date.now(),
|
|
514
|
+
responseTimestamp: Date.now(),
|
|
515
|
+
addedTimestamp: Date.now(),
|
|
516
|
+
triggerTimestamp: Date.now(),
|
|
517
517
|
cacheKey: request.cacheKey,
|
|
518
518
|
scope: request.scope,
|
|
519
519
|
isCanceled: false,
|
|
@@ -543,10 +543,10 @@ describe("useTrackingState [ Events ]", () => {
|
|
|
543
543
|
success: true,
|
|
544
544
|
extra: xhrExtra,
|
|
545
545
|
retries: 0,
|
|
546
|
-
requestTimestamp:
|
|
547
|
-
responseTimestamp:
|
|
548
|
-
addedTimestamp:
|
|
549
|
-
triggerTimestamp:
|
|
546
|
+
requestTimestamp: Date.now(),
|
|
547
|
+
responseTimestamp: Date.now(),
|
|
548
|
+
addedTimestamp: Date.now(),
|
|
549
|
+
triggerTimestamp: Date.now(),
|
|
550
550
|
cacheKey: request.cacheKey,
|
|
551
551
|
scope: request.scope,
|
|
552
552
|
isCanceled: false,
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createClient } from "@hyper-fetch/core";
|
|
2
|
-
|
|
3
2
|
import { getValidCacheData, getInitialState, initialState } from "helpers";
|
|
4
3
|
import { isEmpty, isEqual } from "utils";
|
|
5
4
|
|
|
@@ -16,7 +15,7 @@ describe("useTrackedState [ Utils ]", () => {
|
|
|
16
15
|
expect(isEmpty(NaN)).toBe(false);
|
|
17
16
|
expect(isEmpty("")).toBe(false);
|
|
18
17
|
expect(isEmpty(false)).toBe(false);
|
|
19
|
-
expect(isEmpty(
|
|
18
|
+
expect(isEmpty()).toBe(false);
|
|
20
19
|
expect(isEmpty(0)).toBe(false);
|
|
21
20
|
expect(isEmpty(true)).toBe(false);
|
|
22
21
|
expect(isEmpty(123)).toBe(false);
|
|
@@ -70,7 +69,7 @@ describe("useTrackedState [ Utils ]", () => {
|
|
|
70
69
|
|
|
71
70
|
expect(isEqual(true, true)).toBe(true);
|
|
72
71
|
expect(isEqual(false, false)).toBe(true);
|
|
73
|
-
expect(isEqual(
|
|
72
|
+
expect(isEqual()).toBe(true);
|
|
74
73
|
expect(isEqual(null, null)).toBe(true);
|
|
75
74
|
expect(isEqual(date, date)).toBe(true);
|
|
76
75
|
expect(isEqual(date, date)).toBe(true);
|
|
@@ -135,8 +134,8 @@ describe("useTrackedState [ Utils ]", () => {
|
|
|
135
134
|
data: "test-data",
|
|
136
135
|
status: 200,
|
|
137
136
|
success: true,
|
|
138
|
-
requestTimestamp:
|
|
139
|
-
responseTimestamp:
|
|
137
|
+
requestTimestamp: Date.now(),
|
|
138
|
+
responseTimestamp: Date.now(),
|
|
140
139
|
};
|
|
141
140
|
|
|
142
141
|
const result = getValidCacheData(mockRequest, initialResponse, null);
|
|
@@ -162,7 +161,7 @@ describe("useTrackedState [ Utils ]", () => {
|
|
|
162
161
|
});
|
|
163
162
|
|
|
164
163
|
it("should return cache data when not stale", () => {
|
|
165
|
-
const currentTime =
|
|
164
|
+
const currentTime = Date.now();
|
|
166
165
|
const cacheData = {
|
|
167
166
|
data: "cached-data",
|
|
168
167
|
error: null,
|
|
@@ -177,7 +176,7 @@ describe("useTrackedState [ Utils ]", () => {
|
|
|
177
176
|
});
|
|
178
177
|
|
|
179
178
|
it("should return stale cache data for stale-while-revalidate", () => {
|
|
180
|
-
const staleTimestamp =
|
|
179
|
+
const staleTimestamp = Date.now() - 5000; // Well beyond the 1000ms staleTime
|
|
181
180
|
const cacheData = {
|
|
182
181
|
data: "stale-data",
|
|
183
182
|
error: null,
|
|
@@ -223,7 +222,7 @@ describe("useTrackedState [ Utils ]", () => {
|
|
|
223
222
|
|
|
224
223
|
describe("when getInitialState handles sync responseMapper", () => {
|
|
225
224
|
it("should apply sync responseMapper to cache data", () => {
|
|
226
|
-
const currentTime =
|
|
225
|
+
const currentTime = Date.now();
|
|
227
226
|
const cacheData = {
|
|
228
227
|
data: "original-data",
|
|
229
228
|
error: null,
|
|
@@ -242,7 +241,7 @@ describe("useTrackedState [ Utils ]", () => {
|
|
|
242
241
|
});
|
|
243
242
|
|
|
244
243
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
245
|
-
// @ts-
|
|
244
|
+
// @ts-expect-error
|
|
246
245
|
client.cache = {
|
|
247
246
|
get: vi.fn().mockReturnValue(cacheData),
|
|
248
247
|
version: "1",
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
/* eslint-disable react/jsx-fragments */
|
|
2
|
-
/* eslint-disable react/jsx-no-useless-fragment */
|
|
3
|
-
import { Fragment } from "react";
|
|
4
1
|
import type { HttpAdapterExtraType } from "@hyper-fetch/core";
|
|
5
2
|
import { Client } from "@hyper-fetch/core";
|
|
6
3
|
import { render, waitFor } from "@testing-library/react";
|
|
7
|
-
|
|
8
4
|
import type { UseFetchReturnType } from "hooks/use-fetch";
|
|
9
5
|
import { useFetch } from "hooks/use-fetch";
|
|
10
6
|
import type { UseSubmitReturnType } from "hooks/use-submit";
|
|
11
7
|
import { useSubmit } from "hooks/use-submit";
|
|
12
8
|
import { Provider } from "provider";
|
|
9
|
+
/* eslint-disable react/jsx-fragments */
|
|
10
|
+
/* eslint-disable react/jsx-no-useless-fragment */
|
|
11
|
+
import { Fragment } from "react";
|
|
13
12
|
|
|
14
13
|
describe("Provider [ Hydration ]", () => {
|
|
15
14
|
const data = [1, 2, 3, 4, 5];
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
2
|
-
|
|
3
2
|
import type { ProviderValueType } from "provider";
|
|
4
3
|
import { Provider, useProvider } from "provider";
|
|
5
4
|
|
|
@@ -32,7 +31,7 @@ describe("useProvider [ Base ]", () => {
|
|
|
32
31
|
});
|
|
33
32
|
});
|
|
34
33
|
it("should allow to modify received values", async () => {
|
|
35
|
-
const customConfig = { useFetchConfig: { bounceTime:
|
|
34
|
+
const customConfig = { useFetchConfig: { bounceTime: 99_999 } };
|
|
36
35
|
render(<App />);
|
|
37
36
|
|
|
38
37
|
act(() => {
|