qortex-core 0.1.4 → 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 +7 -249
- package/index.d.ts +98 -5
- package/index.js +85 -15
- package/index.mjs +74 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,255 +20,11 @@
|
|
|
20
20
|
pnpm add qortex-core
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
import { queryManager } from "qortex-core";
|
|
23
|
+
## 📚 Documentation
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
queryManager.setDefaultConfig({
|
|
28
|
-
staleTime: 5 * 60 * 1000, // 5 minutes default
|
|
29
|
-
throttleTime: 100, // 100ms throttle
|
|
30
|
-
usePreviousDataOnError: true
|
|
31
|
-
});
|
|
25
|
+
**Complete documentation, examples, and API reference available at:**
|
|
32
26
|
|
|
33
|
-
|
|
34
|
-
queryManager.registerFetcher(["todos"], {
|
|
35
|
-
fetcher: async () => {
|
|
36
|
-
const response = await fetch("/api/todos");
|
|
37
|
-
return response.json();
|
|
38
|
-
},
|
|
39
|
-
placeholderData: [] // Uses global staleTime: 5 minutes
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// Fetch data
|
|
43
|
-
const todos = await queryManager.fetchQuery(["todos"]);
|
|
44
|
-
console.log("Todos loaded:", todos);
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## 🔐 Perfect for Authentication
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
// Auth service - update from anywhere
|
|
51
|
-
class AuthService {
|
|
52
|
-
async login(email: string, password: string) {
|
|
53
|
-
const { user, token } = await fetch("/api/auth/login", {
|
|
54
|
-
method: "POST",
|
|
55
|
-
body: JSON.stringify({ email, password })
|
|
56
|
-
}).then(r => r.json());
|
|
57
|
-
|
|
58
|
-
// 🎯 Update auth state - accessible everywhere!
|
|
59
|
-
queryManager.setQueryData(["auth", "user"], user);
|
|
60
|
-
queryManager.setQueryData(["auth", "isAuthenticated"], true);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
logout() {
|
|
64
|
-
// 🎯 Clear auth state from anywhere
|
|
65
|
-
queryManager.setQueryData(["auth", "user"], null);
|
|
66
|
-
queryManager.setQueryData(["auth", "isAuthenticated"], false);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Access from any environment
|
|
71
|
-
// React
|
|
72
|
-
function useAuth() {
|
|
73
|
-
const user = queryManager.getQueryData(["auth", "user"]);
|
|
74
|
-
const isAuthenticated = queryManager.getQueryData(["auth", "isAuthenticated"]);
|
|
75
|
-
return { user, isAuthenticated };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// React with hooks (qortex-react package)
|
|
79
|
-
import { useQuery, useQueryData } from "qortex-react";
|
|
80
|
-
|
|
81
|
-
function AuthComponent() {
|
|
82
|
-
const { data: user, isLoading } = useQuery(["auth", "user"]);
|
|
83
|
-
const isAuthenticated = useQueryData(["auth", "isAuthenticated"]);
|
|
84
|
-
|
|
85
|
-
if (isLoading) return <div>Loading...</div>;
|
|
86
|
-
return <div>Welcome {user?.name}</div>;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Vue
|
|
90
|
-
function useAuth() {
|
|
91
|
-
const user = ref(queryManager.getQueryData(["auth", "user"]));
|
|
92
|
-
const isAuthenticated = ref(queryManager.getQueryData(["auth", "isAuthenticated"]));
|
|
93
|
-
|
|
94
|
-
queryManager.subscribeQuery(["auth", "user"], (state) => {
|
|
95
|
-
user.value = state.data;
|
|
96
|
-
console.log("Auth state:", state.isSuccess, state.isLoading);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
return { user, isAuthenticated };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Vanilla JS
|
|
103
|
-
function checkAuth() {
|
|
104
|
-
const user = queryManager.getQueryData(["auth", "user"]);
|
|
105
|
-
const isAuthenticated = queryManager.getQueryData(["auth", "isAuthenticated"]);
|
|
106
|
-
return { user, isAuthenticated };
|
|
107
|
-
}
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
## 🎨 API Reference
|
|
111
|
-
|
|
112
|
-
### `queryManager.registerFetcher(key, options)`
|
|
113
|
-
|
|
114
|
-
```ts
|
|
115
|
-
queryManager.registerFetcher(key, {
|
|
116
|
-
fetcher: async () => Promise<T>,
|
|
117
|
-
staleTime?: number, // Default: 0
|
|
118
|
-
placeholderData?: T,
|
|
119
|
-
enabled?: boolean // Default: true
|
|
120
|
-
});
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### `queryManager.fetchQuery(key, options?)`
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
const data = await queryManager.fetchQuery(key, {
|
|
127
|
-
fetcher?: Fetcher<T>,
|
|
128
|
-
staleTime?: number
|
|
129
|
-
});
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### `queryManager.setQueryData(key, data)`
|
|
133
|
-
|
|
134
|
-
```ts
|
|
135
|
-
// Direct update
|
|
136
|
-
queryManager.setQueryData(["todos"], newTodos);
|
|
137
|
-
|
|
138
|
-
// Functional update
|
|
139
|
-
queryManager.setQueryData(["todos"], (oldData) => [
|
|
140
|
-
...(oldData || []),
|
|
141
|
-
newTodo
|
|
142
|
-
]);
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### `queryManager.getQueryData(key)`
|
|
146
|
-
|
|
147
|
-
```ts
|
|
148
|
-
const user = queryManager.getQueryData(["auth", "user"]);
|
|
149
|
-
const isAuthenticated = queryManager.getQueryData(["auth", "isAuthenticated"]);
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
### React Hooks (qortex-react package)
|
|
153
|
-
|
|
154
|
-
```tsx
|
|
155
|
-
import { useQuery, useQueryData } from "qortex-react";
|
|
156
|
-
|
|
157
|
-
// Full query state with loading, error, refetch
|
|
158
|
-
const { data, isLoading, error, refetch } = useQuery(["todos"]);
|
|
159
|
-
|
|
160
|
-
// Just the data - simpler API
|
|
161
|
-
const todos = useQueryData(["todos"]);
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### `queryManager.subscribeQuery(key, callback, options?)`
|
|
165
|
-
|
|
166
|
-
Subscribe to query state changes with flexible callback signatures.
|
|
167
|
-
|
|
168
|
-
```ts
|
|
169
|
-
// Callback receives the current state
|
|
170
|
-
const unsubscribe = queryManager.subscribeQuery(["todos"], (state) => {
|
|
171
|
-
console.log("State changed:", state);
|
|
172
|
-
console.log("Data:", state.data);
|
|
173
|
-
console.log("Loading:", state.isLoading);
|
|
174
|
-
console.log("Success:", state.isSuccess);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
// With fetcher for automatic type inference
|
|
178
|
-
const unsubscribe = queryManager.subscribeQuery(["todos"], (state) => {
|
|
179
|
-
console.log("Todos:", state.data); // Automatically typed
|
|
180
|
-
}, { fetcher: fetchTodos });
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### `queryManager.setDefaultConfig(config)`
|
|
184
|
-
|
|
185
|
-
Set global default configuration for all queries.
|
|
186
|
-
|
|
187
|
-
```ts
|
|
188
|
-
queryManager.setDefaultConfig({
|
|
189
|
-
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
190
|
-
refetchOnSubscribe: "stale",
|
|
191
|
-
throttleTime: 100, // 100ms throttle
|
|
192
|
-
usePreviousDataOnError: true,
|
|
193
|
-
equalityFn: shallowEqual
|
|
194
|
-
});
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
**Available options:**
|
|
198
|
-
- `enabled?: boolean` - Whether queries are enabled by default
|
|
199
|
-
- `refetchOnSubscribe?: "stale" | "always" | false` - Default refetch behavior
|
|
200
|
-
- `staleTime?: number` - Default time before data is considered stale
|
|
201
|
-
- `usePreviousDataOnError?: boolean` - Keep previous data on error
|
|
202
|
-
- `usePlaceholderOnError?: boolean` - Use placeholder data on error
|
|
203
|
-
- `equalityFn?: EqualityFn<any>` - Default equality function
|
|
204
|
-
- `throttleTime?: number` - Default throttle time for duplicate request prevention
|
|
205
|
-
|
|
206
|
-
## 🎯 More Examples
|
|
207
|
-
|
|
208
|
-
### WebSocket Updates
|
|
209
|
-
|
|
210
|
-
```ts
|
|
211
|
-
// Update data from WebSocket
|
|
212
|
-
const ws = new WebSocket("ws://localhost:8080");
|
|
213
|
-
ws.onmessage = (event) => {
|
|
214
|
-
const data = JSON.parse(event.data);
|
|
215
|
-
queryManager.setQueryData(["live-stats"], data);
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
// Access from any environment
|
|
219
|
-
const stats = queryManager.getQueryData(["live-stats"]);
|
|
220
|
-
console.log("Users online:", stats?.users);
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
### Cross-Framework Data Sharing
|
|
224
|
-
|
|
225
|
-
```ts
|
|
226
|
-
// React component updates data
|
|
227
|
-
function ReactComponent() {
|
|
228
|
-
const updateTheme = () => {
|
|
229
|
-
queryManager.setQueryData(["user", "preferences"], { theme: "dark" });
|
|
230
|
-
};
|
|
231
|
-
return <button onClick={updateTheme}>Update Theme</button>;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Vue component automatically reflects changes
|
|
235
|
-
function VueComponent() {
|
|
236
|
-
const preferences = ref(queryManager.getQueryData(["user", "preferences"]));
|
|
237
|
-
|
|
238
|
-
queryManager.subscribeQuery(["user", "preferences"], (state) => {
|
|
239
|
-
preferences.value = state.data;
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
return { preferences };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Vanilla JS also gets updates
|
|
246
|
-
function vanillaJSFunction() {
|
|
247
|
-
const preferences = queryManager.getQueryData(["user", "preferences"]);
|
|
248
|
-
console.log("Current theme:", preferences?.theme);
|
|
249
|
-
}
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
## 🎭 TypeScript Support
|
|
253
|
-
|
|
254
|
-
```ts
|
|
255
|
-
interface User {
|
|
256
|
-
id: string;
|
|
257
|
-
name: string;
|
|
258
|
-
email: string;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// Type-safe usage
|
|
262
|
-
queryManager.registerFetcher<User[]>(["users"], {
|
|
263
|
-
fetcher: async (): Promise<User[]> => {
|
|
264
|
-
const response = await fetch("/api/users");
|
|
265
|
-
return response.json();
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
const users = await queryManager.fetchQuery<User[]>(["users"]);
|
|
270
|
-
// users is typed as User[] | undefined
|
|
271
|
-
```
|
|
27
|
+
### 🌐 [qortex.darshannaik.com](https://qortex.darshannaik.com)
|
|
272
28
|
|
|
273
29
|
## 📄 License
|
|
274
30
|
|
|
@@ -276,10 +32,12 @@ MIT License - feel free to use this in your projects! 🎉
|
|
|
276
32
|
|
|
277
33
|
## 🎯 Support
|
|
278
34
|
|
|
279
|
-
Need help? Have questions?
|
|
35
|
+
Need help? Have questions? Want to chat about data fetching strategies?
|
|
280
36
|
|
|
37
|
+
- 📚 **Documentation**: [qortex.darshannaik.com](https://qortex.darshannaik.com)
|
|
281
38
|
- 📧 **Email**: [darshannaik.com](https://darshannaik.com)
|
|
282
39
|
- 🐛 **Issues**: [GitHub Issues](https://github.com/Darshan-Naik/qortex/issues)
|
|
40
|
+
- 💬 **Discussions**: [GitHub Discussions](https://github.com/Darshan-Naik/qortex/discussions)
|
|
283
41
|
- 🌟 **Repository**: [https://github.com/Darshan-Naik/qortex](https://github.com/Darshan-Naik/qortex)
|
|
284
42
|
|
|
285
43
|
---
|
|
@@ -287,4 +45,4 @@ Need help? Have questions?
|
|
|
287
45
|
<div align="center">
|
|
288
46
|
<p>Made with ❤️ by <a href="https://darshannaik.com">Darshan</a></p>
|
|
289
47
|
<p>⭐ Star this repo if you found it helpful!</p>
|
|
290
|
-
</div>
|
|
48
|
+
</div>
|
package/index.d.ts
CHANGED
|
@@ -3,8 +3,6 @@
|
|
|
3
3
|
* Using readonly to prevent accidental mutations
|
|
4
4
|
*/
|
|
5
5
|
type QueryKey = string | readonly (string | number)[];
|
|
6
|
-
/** Valid query key values - only strings and numbers are allowed */
|
|
7
|
-
type QueryKeyValue = string | number;
|
|
8
6
|
/** Function that fetches data, can be async or sync */
|
|
9
7
|
type Fetcher<T = any> = () => Promise<T> | T;
|
|
10
8
|
/** Function that compares two values for equality */
|
|
@@ -82,12 +80,34 @@ type QueryState<T = any, E = Error> = {
|
|
|
82
80
|
* Core query manager that handles caching, fetching, and state management
|
|
83
81
|
* Implements robust throttling and race condition prevention
|
|
84
82
|
*/
|
|
85
|
-
declare class
|
|
83
|
+
declare class QueryManagerCore {
|
|
86
84
|
private cache;
|
|
87
85
|
private subs;
|
|
88
86
|
private lastReturnedState;
|
|
89
87
|
private defaultConfig;
|
|
90
88
|
private throttleTime;
|
|
89
|
+
/**
|
|
90
|
+
* ⚠️ DANGER: Clear all cached data and subscriptions
|
|
91
|
+
*
|
|
92
|
+
* This method completely wipes all internal state including:
|
|
93
|
+
* - All cached query data
|
|
94
|
+
* - All active subscriptions
|
|
95
|
+
* - All state references
|
|
96
|
+
*
|
|
97
|
+
* @warning This should ONLY be used in testing environments or when you need to completely reset the query manager state. Using this in production will cause all active queries to lose their data and subscriptions to break.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* // ✅ Safe usage in tests
|
|
102
|
+
* beforeEach(() => {
|
|
103
|
+
* queryManager.dangerClearCache();
|
|
104
|
+
* });
|
|
105
|
+
*
|
|
106
|
+
* // ❌ Dangerous usage in production
|
|
107
|
+
* // queryManager.dangerClearCache(); // Don't do this!
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
dangerClearCache(): void;
|
|
91
111
|
/**
|
|
92
112
|
* Set default configuration for all queries
|
|
93
113
|
*/
|
|
@@ -160,7 +180,80 @@ declare class QueryManager {
|
|
|
160
180
|
*/
|
|
161
181
|
private handleMountLogic;
|
|
162
182
|
}
|
|
163
|
-
|
|
183
|
+
|
|
184
|
+
declare const registerFetcher: {
|
|
185
|
+
<T = any>(key: QueryKey, opts: QueryOptions<T>): void;
|
|
186
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
187
|
+
fetcher: F;
|
|
188
|
+
}): void;
|
|
189
|
+
};
|
|
190
|
+
declare const fetchQuery: {
|
|
191
|
+
<T = any>(key: QueryKey, opts?: QueryOptions<T>): Promise<T>;
|
|
192
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
193
|
+
fetcher: F;
|
|
194
|
+
}): Promise<InferFetcherResult<F>>;
|
|
195
|
+
};
|
|
196
|
+
declare const setQueryData: <T = any>(key: QueryKey, data: T) => void;
|
|
197
|
+
declare const getQueryData: {
|
|
198
|
+
<T = any>(key: QueryKey, opts?: QueryOptions<T>): T | undefined;
|
|
199
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
200
|
+
fetcher: F;
|
|
201
|
+
}): InferFetcherResult<F> | undefined;
|
|
202
|
+
};
|
|
203
|
+
declare const getQueryState: {
|
|
204
|
+
<T = unknown>(key: QueryKey, opts?: QueryOptions<T>): QueryState<T>;
|
|
205
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
206
|
+
fetcher: F;
|
|
207
|
+
}): QueryState<InferFetcherResult<F>>;
|
|
208
|
+
};
|
|
209
|
+
declare const invalidateQuery: (key: QueryKey) => void;
|
|
210
|
+
declare const subscribeQuery: {
|
|
211
|
+
(key: QueryKey, cb: (state: QueryState<any>) => void): () => void;
|
|
212
|
+
<F extends Fetcher>(key: QueryKey, cb: (state: QueryState<InferFetcherResult<F>>) => void, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
213
|
+
fetcher: F;
|
|
214
|
+
}): () => void;
|
|
215
|
+
<T = any>(key: QueryKey, cb: (state: QueryState<T>) => void, opts?: QueryOptions<T>): () => void;
|
|
216
|
+
};
|
|
217
|
+
declare const setDefaultConfig: ({ throttleTime, ...config }: DefaultConfig) => void;
|
|
218
|
+
declare const dangerClearCache: () => void;
|
|
219
|
+
declare const queryManager: {
|
|
220
|
+
registerFetcher: {
|
|
221
|
+
<T = any>(key: QueryKey, opts: QueryOptions<T>): void;
|
|
222
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
223
|
+
fetcher: F;
|
|
224
|
+
}): void;
|
|
225
|
+
};
|
|
226
|
+
fetchQuery: {
|
|
227
|
+
<T = any>(key: QueryKey, opts?: QueryOptions<T>): Promise<T>;
|
|
228
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
229
|
+
fetcher: F;
|
|
230
|
+
}): Promise<InferFetcherResult<F>>;
|
|
231
|
+
};
|
|
232
|
+
setQueryData: <T = any>(key: QueryKey, data: T) => void;
|
|
233
|
+
getQueryData: {
|
|
234
|
+
<T = any>(key: QueryKey, opts?: QueryOptions<T>): T | undefined;
|
|
235
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
236
|
+
fetcher: F;
|
|
237
|
+
}): InferFetcherResult<F> | undefined;
|
|
238
|
+
};
|
|
239
|
+
getQueryState: {
|
|
240
|
+
<T = unknown>(key: QueryKey, opts?: QueryOptions<T>): QueryState<T>;
|
|
241
|
+
<F extends Fetcher>(key: QueryKey, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
242
|
+
fetcher: F;
|
|
243
|
+
}): QueryState<InferFetcherResult<F>>;
|
|
244
|
+
};
|
|
245
|
+
invalidateQuery: (key: QueryKey) => void;
|
|
246
|
+
subscribeQuery: {
|
|
247
|
+
(key: QueryKey, cb: (state: QueryState<any>) => void): () => void;
|
|
248
|
+
<F extends Fetcher>(key: QueryKey, cb: (state: QueryState<InferFetcherResult<F>>) => void, opts: QueryOptions<InferFetcherResult<F>> & {
|
|
249
|
+
fetcher: F;
|
|
250
|
+
}): () => void;
|
|
251
|
+
<T = any>(key: QueryKey, cb: (state: QueryState<T>) => void, opts?: QueryOptions<T>): () => void;
|
|
252
|
+
};
|
|
253
|
+
setDefaultConfig: ({ throttleTime, ...config }: DefaultConfig) => void;
|
|
254
|
+
dangerClearCache: () => void;
|
|
255
|
+
_queryManager: QueryManagerCore;
|
|
256
|
+
};
|
|
164
257
|
|
|
165
258
|
/**
|
|
166
259
|
* Normalizes query keys to a consistent string format for internal storage
|
|
@@ -168,4 +261,4 @@ declare const queryManager: QueryManager;
|
|
|
168
261
|
*/
|
|
169
262
|
declare function serializeKey(key: QueryKey): string;
|
|
170
263
|
|
|
171
|
-
export { DefaultConfig, EqualityFn, Fetcher, InferFetcherResult, QueryKey,
|
|
264
|
+
export { DefaultConfig, EqualityFn, Fetcher, InferFetcherResult, QueryKey, QueryManagerCore, QueryOptions, QueryState, QueryStatus, dangerClearCache, fetchQuery, getQueryData, getQueryState, invalidateQuery, queryManager, registerFetcher, serializeKey, setDefaultConfig, setQueryData, subscribeQuery };
|
package/index.js
CHANGED
|
@@ -20,9 +20,18 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
|
|
23
|
+
QueryManagerCore: () => QueryManagerCore,
|
|
24
|
+
dangerClearCache: () => dangerClearCache,
|
|
25
|
+
fetchQuery: () => fetchQuery,
|
|
26
|
+
getQueryData: () => getQueryData,
|
|
27
|
+
getQueryState: () => getQueryState,
|
|
28
|
+
invalidateQuery: () => invalidateQuery,
|
|
24
29
|
queryManager: () => queryManager,
|
|
25
|
-
|
|
30
|
+
registerFetcher: () => registerFetcher,
|
|
31
|
+
serializeKey: () => serializeKey,
|
|
32
|
+
setDefaultConfig: () => setDefaultConfig,
|
|
33
|
+
setQueryData: () => setQueryData,
|
|
34
|
+
subscribeQuery: () => subscribeQuery
|
|
26
35
|
});
|
|
27
36
|
module.exports = __toCommonJS(src_exports);
|
|
28
37
|
|
|
@@ -114,9 +123,14 @@ function createPublicState(state) {
|
|
|
114
123
|
refetch: state.refetch
|
|
115
124
|
};
|
|
116
125
|
}
|
|
126
|
+
function warnNoFetcherOrData(key) {
|
|
127
|
+
console.warn(
|
|
128
|
+
`[qortex] No fetcher or data for key "${serializeKey(key)}". Register a fetcher or set initial data.`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
117
131
|
|
|
118
|
-
// src/
|
|
119
|
-
var
|
|
132
|
+
// src/queryManagerCore.ts
|
|
133
|
+
var QueryManagerCore = class {
|
|
120
134
|
constructor() {
|
|
121
135
|
this.cache = /* @__PURE__ */ new Map();
|
|
122
136
|
this.subs = /* @__PURE__ */ new Map();
|
|
@@ -124,6 +138,32 @@ var QueryManager = class {
|
|
|
124
138
|
this.defaultConfig = {};
|
|
125
139
|
this.throttleTime = THROTTLE_TIME;
|
|
126
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* ⚠️ DANGER: Clear all cached data and subscriptions
|
|
143
|
+
*
|
|
144
|
+
* This method completely wipes all internal state including:
|
|
145
|
+
* - All cached query data
|
|
146
|
+
* - All active subscriptions
|
|
147
|
+
* - All state references
|
|
148
|
+
*
|
|
149
|
+
* @warning This should ONLY be used in testing environments or when you need to completely reset the query manager state. Using this in production will cause all active queries to lose their data and subscriptions to break.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* // ✅ Safe usage in tests
|
|
154
|
+
* beforeEach(() => {
|
|
155
|
+
* queryManager.dangerClearCache();
|
|
156
|
+
* });
|
|
157
|
+
*
|
|
158
|
+
* // ❌ Dangerous usage in production
|
|
159
|
+
* // queryManager.dangerClearCache(); // Don't do this!
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
dangerClearCache() {
|
|
163
|
+
this.cache.clear();
|
|
164
|
+
this.subs.clear();
|
|
165
|
+
this.lastReturnedState.clear();
|
|
166
|
+
}
|
|
127
167
|
/**
|
|
128
168
|
* Set default configuration for all queries
|
|
129
169
|
*/
|
|
@@ -165,13 +205,8 @@ var QueryManager = class {
|
|
|
165
205
|
cb(publicState);
|
|
166
206
|
}
|
|
167
207
|
registerFetcher(key, opts) {
|
|
168
|
-
this.ensureState(key, opts);
|
|
169
|
-
|
|
170
|
-
try {
|
|
171
|
-
void this.fetchQuery(key);
|
|
172
|
-
} catch {
|
|
173
|
-
}
|
|
174
|
-
}
|
|
208
|
+
const state = this.ensureState(key, opts);
|
|
209
|
+
this.handleMountLogic(key, state);
|
|
175
210
|
}
|
|
176
211
|
fetchQuery(key, opts) {
|
|
177
212
|
const state = this.ensureState(key, opts);
|
|
@@ -179,7 +214,9 @@ var QueryManager = class {
|
|
|
179
214
|
return state.fetchPromise;
|
|
180
215
|
const fetcher = state.fetcher;
|
|
181
216
|
if (!fetcher) {
|
|
182
|
-
|
|
217
|
+
if (state.updatedAt === void 0) {
|
|
218
|
+
warnNoFetcherOrData(key);
|
|
219
|
+
}
|
|
183
220
|
return Promise.resolve(state.data);
|
|
184
221
|
}
|
|
185
222
|
;
|
|
@@ -285,10 +322,43 @@ var QueryManager = class {
|
|
|
285
322
|
}
|
|
286
323
|
}
|
|
287
324
|
};
|
|
288
|
-
var
|
|
325
|
+
var queryManagerCore_default = QueryManagerCore;
|
|
326
|
+
|
|
327
|
+
// src/queryManager.ts
|
|
328
|
+
var _queryManager = new queryManagerCore_default();
|
|
329
|
+
var registerFetcher = _queryManager.registerFetcher.bind(_queryManager);
|
|
330
|
+
var fetchQuery = _queryManager.fetchQuery.bind(_queryManager);
|
|
331
|
+
var setQueryData = _queryManager.setQueryData.bind(_queryManager);
|
|
332
|
+
var getQueryData = _queryManager.getQueryData.bind(_queryManager);
|
|
333
|
+
var getQueryState = _queryManager.getQueryState.bind(_queryManager);
|
|
334
|
+
var invalidateQuery = _queryManager.invalidateQuery.bind(_queryManager);
|
|
335
|
+
var subscribeQuery = _queryManager.subscribeQuery.bind(_queryManager);
|
|
336
|
+
var setDefaultConfig = _queryManager.setDefaultConfig.bind(_queryManager);
|
|
337
|
+
var dangerClearCache = _queryManager.dangerClearCache.bind(_queryManager);
|
|
338
|
+
var queryManager = {
|
|
339
|
+
registerFetcher,
|
|
340
|
+
fetchQuery,
|
|
341
|
+
setQueryData,
|
|
342
|
+
getQueryData,
|
|
343
|
+
getQueryState,
|
|
344
|
+
invalidateQuery,
|
|
345
|
+
subscribeQuery,
|
|
346
|
+
setDefaultConfig,
|
|
347
|
+
dangerClearCache,
|
|
348
|
+
_queryManager
|
|
349
|
+
};
|
|
289
350
|
// Annotate the CommonJS export names for ESM import in node:
|
|
290
351
|
0 && (module.exports = {
|
|
291
|
-
|
|
352
|
+
QueryManagerCore,
|
|
353
|
+
dangerClearCache,
|
|
354
|
+
fetchQuery,
|
|
355
|
+
getQueryData,
|
|
356
|
+
getQueryState,
|
|
357
|
+
invalidateQuery,
|
|
292
358
|
queryManager,
|
|
293
|
-
|
|
359
|
+
registerFetcher,
|
|
360
|
+
serializeKey,
|
|
361
|
+
setDefaultConfig,
|
|
362
|
+
setQueryData,
|
|
363
|
+
subscribeQuery
|
|
294
364
|
});
|
package/index.mjs
CHANGED
|
@@ -86,9 +86,14 @@ function createPublicState(state) {
|
|
|
86
86
|
refetch: state.refetch
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
+
function warnNoFetcherOrData(key) {
|
|
90
|
+
console.warn(
|
|
91
|
+
`[qortex] No fetcher or data for key "${serializeKey(key)}". Register a fetcher or set initial data.`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
89
94
|
|
|
90
|
-
// src/
|
|
91
|
-
var
|
|
95
|
+
// src/queryManagerCore.ts
|
|
96
|
+
var QueryManagerCore = class {
|
|
92
97
|
constructor() {
|
|
93
98
|
this.cache = /* @__PURE__ */ new Map();
|
|
94
99
|
this.subs = /* @__PURE__ */ new Map();
|
|
@@ -96,6 +101,32 @@ var QueryManager = class {
|
|
|
96
101
|
this.defaultConfig = {};
|
|
97
102
|
this.throttleTime = THROTTLE_TIME;
|
|
98
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* ⚠️ DANGER: Clear all cached data and subscriptions
|
|
106
|
+
*
|
|
107
|
+
* This method completely wipes all internal state including:
|
|
108
|
+
* - All cached query data
|
|
109
|
+
* - All active subscriptions
|
|
110
|
+
* - All state references
|
|
111
|
+
*
|
|
112
|
+
* @warning This should ONLY be used in testing environments or when you need to completely reset the query manager state. Using this in production will cause all active queries to lose their data and subscriptions to break.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* // ✅ Safe usage in tests
|
|
117
|
+
* beforeEach(() => {
|
|
118
|
+
* queryManager.dangerClearCache();
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* // ❌ Dangerous usage in production
|
|
122
|
+
* // queryManager.dangerClearCache(); // Don't do this!
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
dangerClearCache() {
|
|
126
|
+
this.cache.clear();
|
|
127
|
+
this.subs.clear();
|
|
128
|
+
this.lastReturnedState.clear();
|
|
129
|
+
}
|
|
99
130
|
/**
|
|
100
131
|
* Set default configuration for all queries
|
|
101
132
|
*/
|
|
@@ -137,13 +168,8 @@ var QueryManager = class {
|
|
|
137
168
|
cb(publicState);
|
|
138
169
|
}
|
|
139
170
|
registerFetcher(key, opts) {
|
|
140
|
-
this.ensureState(key, opts);
|
|
141
|
-
|
|
142
|
-
try {
|
|
143
|
-
void this.fetchQuery(key);
|
|
144
|
-
} catch {
|
|
145
|
-
}
|
|
146
|
-
}
|
|
171
|
+
const state = this.ensureState(key, opts);
|
|
172
|
+
this.handleMountLogic(key, state);
|
|
147
173
|
}
|
|
148
174
|
fetchQuery(key, opts) {
|
|
149
175
|
const state = this.ensureState(key, opts);
|
|
@@ -151,7 +177,9 @@ var QueryManager = class {
|
|
|
151
177
|
return state.fetchPromise;
|
|
152
178
|
const fetcher = state.fetcher;
|
|
153
179
|
if (!fetcher) {
|
|
154
|
-
|
|
180
|
+
if (state.updatedAt === void 0) {
|
|
181
|
+
warnNoFetcherOrData(key);
|
|
182
|
+
}
|
|
155
183
|
return Promise.resolve(state.data);
|
|
156
184
|
}
|
|
157
185
|
;
|
|
@@ -257,9 +285,42 @@ var QueryManager = class {
|
|
|
257
285
|
}
|
|
258
286
|
}
|
|
259
287
|
};
|
|
260
|
-
var
|
|
288
|
+
var queryManagerCore_default = QueryManagerCore;
|
|
289
|
+
|
|
290
|
+
// src/queryManager.ts
|
|
291
|
+
var _queryManager = new queryManagerCore_default();
|
|
292
|
+
var registerFetcher = _queryManager.registerFetcher.bind(_queryManager);
|
|
293
|
+
var fetchQuery = _queryManager.fetchQuery.bind(_queryManager);
|
|
294
|
+
var setQueryData = _queryManager.setQueryData.bind(_queryManager);
|
|
295
|
+
var getQueryData = _queryManager.getQueryData.bind(_queryManager);
|
|
296
|
+
var getQueryState = _queryManager.getQueryState.bind(_queryManager);
|
|
297
|
+
var invalidateQuery = _queryManager.invalidateQuery.bind(_queryManager);
|
|
298
|
+
var subscribeQuery = _queryManager.subscribeQuery.bind(_queryManager);
|
|
299
|
+
var setDefaultConfig = _queryManager.setDefaultConfig.bind(_queryManager);
|
|
300
|
+
var dangerClearCache = _queryManager.dangerClearCache.bind(_queryManager);
|
|
301
|
+
var queryManager = {
|
|
302
|
+
registerFetcher,
|
|
303
|
+
fetchQuery,
|
|
304
|
+
setQueryData,
|
|
305
|
+
getQueryData,
|
|
306
|
+
getQueryState,
|
|
307
|
+
invalidateQuery,
|
|
308
|
+
subscribeQuery,
|
|
309
|
+
setDefaultConfig,
|
|
310
|
+
dangerClearCache,
|
|
311
|
+
_queryManager
|
|
312
|
+
};
|
|
261
313
|
export {
|
|
262
|
-
|
|
314
|
+
QueryManagerCore,
|
|
315
|
+
dangerClearCache,
|
|
316
|
+
fetchQuery,
|
|
317
|
+
getQueryData,
|
|
318
|
+
getQueryState,
|
|
319
|
+
invalidateQuery,
|
|
263
320
|
queryManager,
|
|
264
|
-
|
|
321
|
+
registerFetcher,
|
|
322
|
+
serializeKey,
|
|
323
|
+
setDefaultConfig,
|
|
324
|
+
setQueryData,
|
|
325
|
+
subscribeQuery
|
|
265
326
|
};
|