qortex-react 0.1.5 → 0.1.6
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 +6 -284
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,291 +21,11 @@
|
|
|
21
21
|
pnpm add qortex-react qortex-core
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
import { queryManager, useQuery, useQueryData } from "qortex-react";
|
|
24
|
+
## 📚 Documentation
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
queryManager.setDefaultConfig({
|
|
29
|
-
staleTime: 5 * 60 * 1000, // 5 minutes default
|
|
30
|
-
throttleTime: 100, // 100ms throttle
|
|
31
|
-
usePreviousDataOnError: true
|
|
32
|
-
});
|
|
26
|
+
**Complete documentation, examples, and API reference available at:**
|
|
33
27
|
|
|
34
|
-
|
|
35
|
-
queryManager.registerFetcher(["todos"], {
|
|
36
|
-
fetcher: async () => {
|
|
37
|
-
const response = await fetch("/api/todos");
|
|
38
|
-
return response.json();
|
|
39
|
-
},
|
|
40
|
-
placeholderData: [] // Uses global staleTime: 5 minutes
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
// Use in React component - full query state
|
|
44
|
-
function TodosList() {
|
|
45
|
-
const { data, isLoading, error, refetch } = useQuery(["todos"]);
|
|
46
|
-
|
|
47
|
-
if (isLoading) return <div>Loading...</div>;
|
|
48
|
-
if (error) return <div>Error: {error.message}</div>;
|
|
49
|
-
|
|
50
|
-
return (
|
|
51
|
-
<div>
|
|
52
|
-
<button onClick={() => refetch()}>Refresh</button>
|
|
53
|
-
<ul>
|
|
54
|
-
{data?.map(todo => (
|
|
55
|
-
<li key={todo.id}>{todo.title}</li>
|
|
56
|
-
))}
|
|
57
|
-
</ul>
|
|
58
|
-
</div>
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Or just get the data - perfect for simple data access
|
|
63
|
-
function TodoCount() {
|
|
64
|
-
const todos = useQueryData(["todos"]);
|
|
65
|
-
return <div>Total todos: {todos?.length || 0}</div>;
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
## 🔐 Perfect for Authentication
|
|
70
|
-
|
|
71
|
-
```tsx
|
|
72
|
-
// Auth service - update from anywhere
|
|
73
|
-
class AuthService {
|
|
74
|
-
async login(email: string, password: string) {
|
|
75
|
-
const { user, token } = await fetch("/api/auth/login", {
|
|
76
|
-
method: "POST",
|
|
77
|
-
body: JSON.stringify({ email, password })
|
|
78
|
-
}).then(r => r.json());
|
|
79
|
-
|
|
80
|
-
// 🎯 Update auth state - all components update automatically!
|
|
81
|
-
queryManager.setQueryData(["auth", "user"], user);
|
|
82
|
-
queryManager.setQueryData(["auth", "isAuthenticated"], true);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
logout() {
|
|
86
|
-
// 🎯 Clear auth state from anywhere
|
|
87
|
-
queryManager.setQueryData(["auth", "user"], null);
|
|
88
|
-
queryManager.setQueryData(["auth", "isAuthenticated"], false);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// React components automatically reflect changes
|
|
93
|
-
function UserProfile() {
|
|
94
|
-
const { data: user } = useQuery(["auth", "user"]);
|
|
95
|
-
const { data: isAuthenticated } = useQuery(["auth", "isAuthenticated"]);
|
|
96
|
-
|
|
97
|
-
if (!isAuthenticated) return <div>Please log in</div>;
|
|
98
|
-
|
|
99
|
-
return (
|
|
100
|
-
<div>
|
|
101
|
-
<h2>{user?.name}</h2>
|
|
102
|
-
<button onClick={() => AuthService.logout()}>Logout</button>
|
|
103
|
-
</div>
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Or use useQueryData for simpler data access
|
|
108
|
-
function UserAvatar() {
|
|
109
|
-
const user = useQueryData(["auth", "user"]);
|
|
110
|
-
const isAuthenticated = useQueryData(["auth", "isAuthenticated"]);
|
|
111
|
-
|
|
112
|
-
if (!isAuthenticated) return null;
|
|
113
|
-
|
|
114
|
-
return <img src={user?.avatar} alt={user?.name} />;
|
|
115
|
-
}
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
## 🎨 API Reference
|
|
119
|
-
|
|
120
|
-
### `useQuery(key, options?)`
|
|
121
|
-
|
|
122
|
-
Returns the full query state with loading, error, and refetch capabilities:
|
|
123
|
-
|
|
124
|
-
```tsx
|
|
125
|
-
const {
|
|
126
|
-
data,
|
|
127
|
-
isLoading,
|
|
128
|
-
isFetching,
|
|
129
|
-
isSuccess,
|
|
130
|
-
isError,
|
|
131
|
-
error,
|
|
132
|
-
refetch,
|
|
133
|
-
status,
|
|
134
|
-
isStale,
|
|
135
|
-
updatedAt
|
|
136
|
-
} = useQuery(key, {
|
|
137
|
-
refetchOnSubscribe?: "stale" | "always" | false, // Default: "stale"
|
|
138
|
-
enabled?: boolean, // Default: true
|
|
139
|
-
fetcher?: Fetcher<T>,
|
|
140
|
-
staleTime?: number,
|
|
141
|
-
placeholderData?: T
|
|
142
|
-
});
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### `useQueryData(key, options?)`
|
|
146
|
-
|
|
147
|
-
Returns just the data - perfect for simple data access without loading states:
|
|
148
|
-
|
|
149
|
-
```tsx
|
|
150
|
-
const data = useQueryData(key, {
|
|
151
|
-
refetchOnSubscribe?: "stale" | "always" | false, // Default: "stale"
|
|
152
|
-
enabled?: boolean, // Default: true
|
|
153
|
-
fetcher?: Fetcher<T>,
|
|
154
|
-
staleTime?: number,
|
|
155
|
-
placeholderData?: T
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
// Returns T | undefined
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
**When to use which:**
|
|
162
|
-
- **`useQuery`** - When you need loading states, error handling, or refetch capabilities
|
|
163
|
-
- **`useQueryData`** - When you just need the data and want a simpler API
|
|
164
|
-
|
|
165
|
-
### `queryManager.setQueryData(key, data)`
|
|
166
|
-
|
|
167
|
-
Update data from anywhere:
|
|
168
|
-
|
|
169
|
-
```tsx
|
|
170
|
-
// Direct update
|
|
171
|
-
queryManager.setQueryData(["todos"], newTodos);
|
|
172
|
-
|
|
173
|
-
// Functional update
|
|
174
|
-
queryManager.setQueryData(["todos"], (oldData) => [
|
|
175
|
-
...(oldData || []),
|
|
176
|
-
newTodo
|
|
177
|
-
]);
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
### `queryManager.getQueryData(key)`
|
|
181
|
-
|
|
182
|
-
Read data from anywhere:
|
|
183
|
-
|
|
184
|
-
```tsx
|
|
185
|
-
const user = queryManager.getQueryData(["auth", "user"]);
|
|
186
|
-
const isAuthenticated = queryManager.getQueryData(["auth", "isAuthenticated"]);
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
### `queryManager.setDefaultConfig(config)`
|
|
190
|
-
|
|
191
|
-
Set global default configuration for all queries:
|
|
192
|
-
|
|
193
|
-
```tsx
|
|
194
|
-
queryManager.setDefaultConfig({
|
|
195
|
-
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
196
|
-
refetchOnSubscribe: "stale",
|
|
197
|
-
throttleTime: 100, // 100ms throttle
|
|
198
|
-
usePreviousDataOnError: true,
|
|
199
|
-
equalityFn: shallowEqual
|
|
200
|
-
});
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
**Available options:**
|
|
204
|
-
- `enabled?: boolean` - Whether queries are enabled by default
|
|
205
|
-
- `refetchOnSubscribe?: "stale" | "always" | false` - Default refetch behavior
|
|
206
|
-
- `staleTime?: number` - Default time before data is considered stale
|
|
207
|
-
- `usePreviousDataOnError?: boolean` - Keep previous data on error
|
|
208
|
-
- `usePlaceholderOnError?: boolean` - Use placeholder data on error
|
|
209
|
-
- `equalityFn?: EqualityFn<any>` - Default equality function
|
|
210
|
-
- `throttleTime?: number` - Default throttle time for duplicate request prevention
|
|
211
|
-
|
|
212
|
-
## 🎯 More Examples
|
|
213
|
-
|
|
214
|
-
### WebSocket Updates
|
|
215
|
-
|
|
216
|
-
```tsx
|
|
217
|
-
// Update data from WebSocket
|
|
218
|
-
const ws = new WebSocket("ws://localhost:8080");
|
|
219
|
-
ws.onmessage = (event) => {
|
|
220
|
-
const data = JSON.parse(event.data);
|
|
221
|
-
queryManager.setQueryData(["live-stats"], data);
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// React component automatically updates
|
|
225
|
-
function LiveStats() {
|
|
226
|
-
const { data: stats } = useQuery(["live-stats"]);
|
|
227
|
-
return <div>Users online: {stats?.users}</div>;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Or use useQueryData for simpler access
|
|
231
|
-
function UserCount() {
|
|
232
|
-
const stats = useQueryData(["live-stats"]);
|
|
233
|
-
return <span>{stats?.users || 0}</span>;
|
|
234
|
-
}
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### Cross-Component Communication
|
|
238
|
-
|
|
239
|
-
```tsx
|
|
240
|
-
// Component A updates data
|
|
241
|
-
function UserActions({ userId }) {
|
|
242
|
-
const updateStatus = (status) => {
|
|
243
|
-
queryManager.setQueryData(["user", userId], (user) => ({
|
|
244
|
-
...user,
|
|
245
|
-
status
|
|
246
|
-
}));
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
return (
|
|
250
|
-
<div>
|
|
251
|
-
<button onClick={() => updateStatus("online")}>Set Online</button>
|
|
252
|
-
</div>
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Component B automatically reflects changes
|
|
257
|
-
function UserStatus({ userId }) {
|
|
258
|
-
const { data: user } = useQuery(["user", userId]);
|
|
259
|
-
return <div>Status: {user?.status}</div>;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// Or use useQueryData for simpler data access
|
|
263
|
-
function UserName({ userId }) {
|
|
264
|
-
const user = useQueryData(["user", userId]);
|
|
265
|
-
return <span>{user?.name}</span>;
|
|
266
|
-
}
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
## 🎭 TypeScript Support
|
|
270
|
-
|
|
271
|
-
```tsx
|
|
272
|
-
interface User {
|
|
273
|
-
id: string;
|
|
274
|
-
name: string;
|
|
275
|
-
email: string;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// Type-safe usage
|
|
279
|
-
queryManager.registerFetcher<User[]>(["users"], {
|
|
280
|
-
fetcher: async (): Promise<User[]> => {
|
|
281
|
-
const response = await fetch("/api/users");
|
|
282
|
-
return response.json();
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
function UsersList() {
|
|
287
|
-
const { data: users, isLoading, isSuccess, isError, error } = useQuery<User[]>(["users"]);
|
|
288
|
-
|
|
289
|
-
if (isLoading) return <div>Loading...</div>;
|
|
290
|
-
if (isError) return <div>Error: {error?.message}</div>;
|
|
291
|
-
if (isSuccess && users) {
|
|
292
|
-
return (
|
|
293
|
-
<ul>
|
|
294
|
-
{users.map(user => (
|
|
295
|
-
<li key={user.id}>{user.name} - {user.email}</li>
|
|
296
|
-
))}
|
|
297
|
-
</ul>
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
return <div>No users found</div>;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// Or use useQueryData for simpler typed access
|
|
304
|
-
function UserCount() {
|
|
305
|
-
const users = useQueryData<User[]>(["users"]);
|
|
306
|
-
return <div>Total users: {users?.length || 0}</div>;
|
|
307
|
-
}
|
|
308
|
-
```
|
|
28
|
+
### 🌐 [qortex.darshannaik.com](https://qortex.darshannaik.com)
|
|
309
29
|
|
|
310
30
|
## 📄 License
|
|
311
31
|
|
|
@@ -313,10 +33,12 @@ MIT License - feel free to use this in your projects! 🎉
|
|
|
313
33
|
|
|
314
34
|
## 🎯 Support
|
|
315
35
|
|
|
316
|
-
Need help? Have questions?
|
|
36
|
+
Need help? Have questions? Want to chat about data fetching strategies?
|
|
317
37
|
|
|
38
|
+
- 📚 **Documentation**: [qortex.darshannaik.com](https://qortex.darshannaik.com)
|
|
318
39
|
- 📧 **Email**: [darshannaik.com](https://darshannaik.com)
|
|
319
40
|
- 🐛 **Issues**: [GitHub Issues](https://github.com/Darshan-Naik/qortex/issues)
|
|
41
|
+
- 💬 **Discussions**: [GitHub Discussions](https://github.com/Darshan-Naik/qortex/discussions)
|
|
320
42
|
- 🌟 **Repository**: [https://github.com/Darshan-Naik/qortex](https://github.com/Darshan-Naik/qortex)
|
|
321
43
|
|
|
322
44
|
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qortex-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "React hook bridge for qortex runtime",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"author": "Darshan Naik",
|
|
43
43
|
"license": "MIT",
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"qortex-core": "0.1.
|
|
45
|
+
"qortex-core": "0.1.6"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"react": ">=18"
|