qortex-react 0.1.5 → 0.1.7

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 CHANGED
@@ -21,291 +21,11 @@
21
21
  pnpm add qortex-react qortex-core
22
22
  ```
23
23
 
24
- ```tsx
25
- import { queryManager, useQuery, useQueryData } from "qortex-react";
24
+ ## 📚 Documentation
26
25
 
27
- // Set global defaults for all queries
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
- // Register a fetcher
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/index.js CHANGED
@@ -33,10 +33,10 @@ var import_qortex_core = require("qortex-core");
33
33
  function useQuery(key, opts) {
34
34
  const serializedKey = (0, import_qortex_core.serializeKey)(key);
35
35
  const getSnapshot = (0, import_react.useCallback)(() => {
36
- return import_qortex_core.queryManager.getQueryState(key, opts);
36
+ return (0, import_qortex_core.getQueryState)(key, opts);
37
37
  }, [serializedKey]);
38
38
  const subscribe = (0, import_react.useCallback)((callback) => {
39
- return import_qortex_core.queryManager.subscribeQuery(key, callback, opts);
39
+ return (0, import_qortex_core.subscribeQuery)(key, callback, opts);
40
40
  }, [serializedKey]);
41
41
  const state = (0, import_react.useSyncExternalStore)(
42
42
  subscribe,
@@ -51,10 +51,10 @@ var import_qortex_core2 = require("qortex-core");
51
51
  function useQueryData(key, opts) {
52
52
  const serializedKey = (0, import_qortex_core2.serializeKey)(key);
53
53
  const getSnapshot = (0, import_react2.useCallback)(() => {
54
- return import_qortex_core2.queryManager.getQueryData(key, opts);
54
+ return (0, import_qortex_core2.getQueryData)(key, opts);
55
55
  }, [serializedKey]);
56
56
  const subscribe = (0, import_react2.useCallback)((callback) => {
57
- return import_qortex_core2.queryManager.subscribeQuery(key, callback, opts);
57
+ return (0, import_qortex_core2.subscribeQuery)(key, callback, opts);
58
58
  }, [serializedKey]);
59
59
  const data = (0, import_react2.useSyncExternalStore)(
60
60
  subscribe,
package/index.mjs CHANGED
@@ -3,14 +3,14 @@ export * from "qortex-core";
3
3
 
4
4
  // src/useQuery.tsx
5
5
  import { useSyncExternalStore, useCallback } from "react";
6
- import { queryManager, serializeKey } from "qortex-core";
6
+ import { subscribeQuery, getQueryState, serializeKey } from "qortex-core";
7
7
  function useQuery(key, opts) {
8
8
  const serializedKey = serializeKey(key);
9
9
  const getSnapshot = useCallback(() => {
10
- return queryManager.getQueryState(key, opts);
10
+ return getQueryState(key, opts);
11
11
  }, [serializedKey]);
12
12
  const subscribe = useCallback((callback) => {
13
- return queryManager.subscribeQuery(key, callback, opts);
13
+ return subscribeQuery(key, callback, opts);
14
14
  }, [serializedKey]);
15
15
  const state = useSyncExternalStore(
16
16
  subscribe,
@@ -21,14 +21,14 @@ function useQuery(key, opts) {
21
21
 
22
22
  // src/useQueryData.tsx
23
23
  import { useSyncExternalStore as useSyncExternalStore2, useCallback as useCallback2 } from "react";
24
- import { queryManager as queryManager2, serializeKey as serializeKey2 } from "qortex-core";
24
+ import { getQueryData, subscribeQuery as subscribeQuery2, serializeKey as serializeKey2 } from "qortex-core";
25
25
  function useQueryData(key, opts) {
26
26
  const serializedKey = serializeKey2(key);
27
27
  const getSnapshot = useCallback2(() => {
28
- return queryManager2.getQueryData(key, opts);
28
+ return getQueryData(key, opts);
29
29
  }, [serializedKey]);
30
30
  const subscribe = useCallback2((callback) => {
31
- return queryManager2.subscribeQuery(key, callback, opts);
31
+ return subscribeQuery2(key, callback, opts);
32
32
  }, [serializedKey]);
33
33
  const data = useSyncExternalStore2(
34
34
  subscribe,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qortex-react",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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.5"
45
+ "qortex-core": "0.1.7"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "react": ">=18"