reactive-query-z 1.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/LICENSE +21 -0
- package/README.md +317 -0
- package/build/core/cache.d.ts +6 -0
- package/build/core/fetcher.d.ts +5 -0
- package/build/core/globalQuery.d.ts +7 -0
- package/build/core/reactiveStore.d.ts +9 -0
- package/build/core/subscription.d.ts +11 -0
- package/build/hooks/useGraphQLMutation.d.ts +14 -0
- package/build/hooks/useGraphQLQuery.d.ts +20 -0
- package/build/hooks/useHybridQuery.d.ts +24 -0
- package/build/hooks/useMutation.d.ts +16 -0
- package/build/hooks/useQuery.d.ts +19 -0
- package/build/index.cjs +1 -0
- package/build/index.d.ts +11 -0
- package/build/index.esm.js +1 -0
- package/build/index.umd.min.js +1 -0
- package/build/utils/cacheUtils.d.ts +1 -0
- package/build/utils/retryUtils.d.ts +1 -0
- package/build/utils/timeoutUtils.d.ts +1 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Delpi.Kye
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# 📘 reactive-query-z
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/reactive-query-z)
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
**reactive-fetch** is a lightweight, reactive, and fully-featured data-fetching and state orchestration library for React.
|
|
9
|
+
It is a **React Query alternative** with built-in support for REST, GraphQL, real-time subscriptions, caching, optimistic updates, and global query invalidation.
|
|
10
|
+
|
|
11
|
+
***Note:*** For full-featured query management, see [React Query](https://tanstack.com/query/v4)
|
|
12
|
+
|
|
13
|
+
[Live Example](https://codesandbox.io/p/sandbox/tmxkm5)
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## ✨ Features
|
|
18
|
+
|
|
19
|
+
* Lightweight and reactive, fully hook-based
|
|
20
|
+
* Supports REST + GraphQL queries
|
|
21
|
+
* Full CRUD support (POST/PUT/PATCH/DELETE)
|
|
22
|
+
* Optimistic updates for smooth UI experience
|
|
23
|
+
* Auto stale-time + background refetch
|
|
24
|
+
* Global query invalidation
|
|
25
|
+
* Real-time subscriptions via WebSocket / SSE
|
|
26
|
+
* Cache + deduplication
|
|
27
|
+
* Fully TypeScript-ready
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## 📦 Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install reactive-query-z
|
|
35
|
+
# or
|
|
36
|
+
yarn add reactive-query-z
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## 🚀 Basic Usage
|
|
42
|
+
|
|
43
|
+
### 1️⃣ Querying REST Data
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { useQuery, queryRegistry } from "reactive-query-z";
|
|
47
|
+
|
|
48
|
+
type User = { id: number; name: string };
|
|
49
|
+
|
|
50
|
+
function UserList() {
|
|
51
|
+
const { data: users, loading, refetch, error } = useQuery<User[]>("/api/users", {
|
|
52
|
+
cacheKey: "users-list",
|
|
53
|
+
staleTime: 5000,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<div>
|
|
58
|
+
{loading && <p>Loading...</p>}
|
|
59
|
+
{error && <p>Error: {error.message}</p>}
|
|
60
|
+
<ul>
|
|
61
|
+
{users?.map(u => (
|
|
62
|
+
<li key={u.id}>{u.name}</li>
|
|
63
|
+
))}
|
|
64
|
+
</ul>
|
|
65
|
+
<button onClick={refetch}>Refetch</button>
|
|
66
|
+
<button onClick={() => queryRegistry.invalidate("users-list")}>Invalidate</button>
|
|
67
|
+
</div>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### 2️⃣ GraphQL Queries with Real-time Subscription
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { useHybridQuery } from "reactive-query-z";
|
|
79
|
+
|
|
80
|
+
type Message = { id: number; text: string };
|
|
81
|
+
|
|
82
|
+
function MessageList() {
|
|
83
|
+
const { data: messages } = useHybridQuery<Message[]>("/graphql", {
|
|
84
|
+
query: `
|
|
85
|
+
query GetMessages {
|
|
86
|
+
messages { id text }
|
|
87
|
+
}
|
|
88
|
+
`,
|
|
89
|
+
options: {
|
|
90
|
+
cacheKey: "messages-list",
|
|
91
|
+
subscriptionUrl: "ws://localhost:4000",
|
|
92
|
+
optimisticUpdate: (prev, newData) => [...(prev || []), ...(newData || [])],
|
|
93
|
+
staleTime: 3000,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<ul>
|
|
99
|
+
{messages?.map(m => <li key={m.id}>{m.text}</li>)}
|
|
100
|
+
</ul>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### 3️⃣ Mutations (CRUD)
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { useMutation, queryRegistry } from "reactive-query-z";
|
|
111
|
+
|
|
112
|
+
type User = { id: number; name: string };
|
|
113
|
+
|
|
114
|
+
function AddUserButton() {
|
|
115
|
+
const { mutate: createUser } = useMutation<User>("/api/users", {
|
|
116
|
+
cacheKey: "users-list",
|
|
117
|
+
optimisticUpdate: (prev, newUser) => [...(prev || []), newUser],
|
|
118
|
+
onSuccess: () => queryRegistry.invalidate("users-list"),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return <button onClick={() => createUser({ name: "New User" })}>Add User</button>;
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
### 4️⃣ GraphQL Mutations
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import { useGraphQLMutation, queryRegistry } from "reactive-query-z";
|
|
131
|
+
|
|
132
|
+
type Message = { id: number; text: string };
|
|
133
|
+
|
|
134
|
+
function SendMessageButton() {
|
|
135
|
+
const { mutate } = useGraphQLMutation<{ createMessage: Message }>("/graphql", {
|
|
136
|
+
mutation: `
|
|
137
|
+
mutation CreateMessage($text: String!) {
|
|
138
|
+
createMessage(text: $text) { id text }
|
|
139
|
+
}
|
|
140
|
+
`,
|
|
141
|
+
variables: { text: "Hello world!" },
|
|
142
|
+
cacheKey: "messages-list",
|
|
143
|
+
optimisticUpdate: (prev, newData) => [...(prev || []), newData.createMessage],
|
|
144
|
+
onSuccess: () => queryRegistry.invalidate("messages-list"),
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return <button onClick={() => mutate()}>Send Message</button>;
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
### 5️⃣ Global Query Invalidation
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { queryRegistry } from "reactive-query-z";
|
|
157
|
+
|
|
158
|
+
// Invalidate a specific query
|
|
159
|
+
queryRegistry.invalidate("users-list");
|
|
160
|
+
|
|
161
|
+
// Invalidate all queries
|
|
162
|
+
queryRegistry.invalidate();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
### 6️⃣ Real-time Subscriptions
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { useHybridQuery } from "reactive-query-z";
|
|
171
|
+
|
|
172
|
+
const { data } = useHybridQuery<Message[]>('/graphql', {
|
|
173
|
+
subscriptionUrl: "ws://localhost:4000",
|
|
174
|
+
cacheKey: "messages-list",
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## 🔧 API Reference
|
|
181
|
+
|
|
182
|
+
### `useQuery<T>(endpoint: string, options?: QueryOptions)`
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
interface QueryOptions {
|
|
186
|
+
cacheKey?: string;
|
|
187
|
+
staleTime?: number; // polling interval in ms
|
|
188
|
+
autoRefetch?: boolean;
|
|
189
|
+
interval?: number; // auto refetch interval
|
|
190
|
+
timeout?: number; // request timeout in ms
|
|
191
|
+
headers?: Record<string, string>;
|
|
192
|
+
retry?: number; // number of retries
|
|
193
|
+
retryDelay?: number; // initial retry delay in ms
|
|
194
|
+
optimisticUpdate?: <T>(prevData: T | null, newData: T) => T;
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
Returns:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
{
|
|
201
|
+
data: T | null;
|
|
202
|
+
error: Error | null;
|
|
203
|
+
loading: boolean;
|
|
204
|
+
start: () => Promise<T | null>; // initial / manual fetch
|
|
205
|
+
refetch: () => Promise<T | null>; // with retry
|
|
206
|
+
setData: (data: T) => void; // manual update
|
|
207
|
+
cancel: () => void; // cancel in-flight request
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
### `useHybridQuery<T>(endpoint: string, options: HybridQueryParams<T>)`
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
interface HybridQueryParams<T> {
|
|
217
|
+
query?: string; // GraphQL query string
|
|
218
|
+
variables?: Record<string, any>;
|
|
219
|
+
options?: {
|
|
220
|
+
cacheKey?: string;
|
|
221
|
+
staleTime?: number; // ms
|
|
222
|
+
subscriptionUrl?: string; // WebSocket / SSE
|
|
223
|
+
optimisticUpdate?: (prev: T | null, newData: T) => T;
|
|
224
|
+
headers?: Record<string, string>;
|
|
225
|
+
retry?: number;
|
|
226
|
+
retryDelay?: number;
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Returns:
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
{
|
|
235
|
+
data: T | null;
|
|
236
|
+
error: Error | null;
|
|
237
|
+
loading: boolean;
|
|
238
|
+
refetch: () => Promise<T | null>;
|
|
239
|
+
mutate: (data: T) => void; // optimistic update
|
|
240
|
+
cancel: () => void;
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
### `useMutation<T>(endpoint: string, options?: MutationOptions<T>)`
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
interface MutationOptions<T> {
|
|
250
|
+
cacheKey?: string;
|
|
251
|
+
optimisticUpdate?: (prevData: any, newData: T) => any;
|
|
252
|
+
onSuccess?: (data: T) => void;
|
|
253
|
+
onError?: (error: any) => void;
|
|
254
|
+
retry?: number;
|
|
255
|
+
retryDelay?: number;
|
|
256
|
+
headers?: Record<string, string>;
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
{
|
|
264
|
+
mutate: (body: any, method?: "POST" | "PUT" | "PATCH" | "DELETE") => Promise<T>;
|
|
265
|
+
loading: boolean;
|
|
266
|
+
error: any;
|
|
267
|
+
cancel: () => void;
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
### `useGraphQLMutation<T>(endpoint: string, options: GraphQLMutationOptions<T>)`
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
interface GraphQLMutationOptions<T> {
|
|
277
|
+
mutation: string;
|
|
278
|
+
variables?: Record<string, any>;
|
|
279
|
+
cacheKey?: string;
|
|
280
|
+
optimisticUpdate?: (prev: T | undefined, newData: T) => T;
|
|
281
|
+
retry?: number;
|
|
282
|
+
retryDelay?: number;
|
|
283
|
+
headers?: Record<string, string>;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
```
|
|
287
|
+
Returns:
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
{
|
|
291
|
+
mutate: () => Promise<T>;
|
|
292
|
+
cancel: () => void;
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
## ⚖️ Comparison with Other Libraries
|
|
299
|
+
|
|
300
|
+
| Feature | reactive-fetch | React Query | SWR | Apollo Client |
|
|
301
|
+
| -------------------------- | ------------------ | ----------- | ---- | ------------- |
|
|
302
|
+
| REST support | ✅ | ✅ | ✅ | ⚠️ Partial |
|
|
303
|
+
| GraphQL support | ✅ | ⚠️ Plugin | ❌ | ✅ |
|
|
304
|
+
| Real-time subscription | ✅ | ⚠️ Plugin | ❌ | ✅ |
|
|
305
|
+
| Global cache invalidation | ✅ | ✅ | ⚠️ | ✅ |
|
|
306
|
+
| Optimistic updates | ✅ | ✅ | ⚠️ | ✅ |
|
|
307
|
+
| Auto stale-time + refetch | ✅ | ✅ | ✅ | ✅ |
|
|
308
|
+
| Full CRUD mutation support | ✅ | ✅ | ❌ | ✅ |
|
|
309
|
+
| TypeScript friendly | ✅ | ✅ | ✅ | ✅ |
|
|
310
|
+
| Lightweight | ✅ | ⚠️ | ✅ | ⚠️ |
|
|
311
|
+
| Subscription built-in | ✅ | ❌ | ❌ | ✅ |
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## 📜 License
|
|
316
|
+
|
|
317
|
+
MIT
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const queryRegistry: {
|
|
2
|
+
register: <T = any>(cacheKey: string, refetch: () => void, setData?: ((updater: (prev?: T | undefined) => T) => void) | undefined, cancel?: () => void) => void;
|
|
3
|
+
setData: <T_1 = any>(cacheKey: string, updater: (prev?: T_1 | undefined) => T_1) => void;
|
|
4
|
+
cancel: (cacheKey: string) => void;
|
|
5
|
+
invalidate: (cacheKey?: string) => void;
|
|
6
|
+
unregister: (cacheKey: string) => void;
|
|
7
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function useReactive<T>(initial: T): [T, (v: T) => void];
|
|
2
|
+
export declare class ReactiveValue<T> {
|
|
3
|
+
private _value;
|
|
4
|
+
private listeners;
|
|
5
|
+
constructor(value: T);
|
|
6
|
+
get(): T;
|
|
7
|
+
set(value: T): void;
|
|
8
|
+
subscribe(cb: (v: T) => void): () => boolean;
|
|
9
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type Callback<T> = (data: T) => void;
|
|
2
|
+
export declare class SubscriptionManager<T> {
|
|
3
|
+
private url;
|
|
4
|
+
private callbacks;
|
|
5
|
+
private ws;
|
|
6
|
+
constructor(url: string);
|
|
7
|
+
connect(): void;
|
|
8
|
+
subscribe(cb: Callback<T>): () => boolean;
|
|
9
|
+
disconnect(): void;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface GraphQLMutationOptions<TData, TVariables = any> {
|
|
2
|
+
mutation: string;
|
|
3
|
+
variables?: TVariables;
|
|
4
|
+
cacheKey?: string;
|
|
5
|
+
retry?: number;
|
|
6
|
+
retryDelay?: number;
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
optimisticUpdate?: (prev: TData | undefined, newData: TData) => TData;
|
|
10
|
+
}
|
|
11
|
+
export declare function useGraphQLMutation<TData, TVariables = any>(endpoint: string, options: GraphQLMutationOptions<TData, TVariables>): {
|
|
12
|
+
mutate: () => Promise<TData>;
|
|
13
|
+
cancel: () => void;
|
|
14
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface GraphQueryOptions<T> {
|
|
3
|
+
query: string;
|
|
4
|
+
variables?: Record<string, any>;
|
|
5
|
+
cacheKey?: string;
|
|
6
|
+
staleTime?: number;
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
retry?: number;
|
|
10
|
+
retryDelay?: number;
|
|
11
|
+
optimisticUpdate?: (prevData: T | null, newData: T) => T;
|
|
12
|
+
}
|
|
13
|
+
export declare function useGraphQLQuery<T>(endpoint: string, options: GraphQueryOptions<T>): {
|
|
14
|
+
data: T | null;
|
|
15
|
+
error: Error | null;
|
|
16
|
+
loading: boolean;
|
|
17
|
+
refetch: () => Promise<T | null>;
|
|
18
|
+
mutate: import("react").Dispatch<import("react").SetStateAction<T | null>>;
|
|
19
|
+
cancel: () => void | undefined;
|
|
20
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface HybridQueryOptions<T> {
|
|
3
|
+
cacheKey?: string;
|
|
4
|
+
optimisticUpdate?: (prevData: T | null, newData: T) => T;
|
|
5
|
+
staleTime?: number;
|
|
6
|
+
subscriptionUrl?: string;
|
|
7
|
+
retry?: number;
|
|
8
|
+
retryDelay?: number;
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
timeout?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface HybridQueryParams<T> {
|
|
13
|
+
query?: string;
|
|
14
|
+
variables?: Record<string, any>;
|
|
15
|
+
options?: HybridQueryOptions<T>;
|
|
16
|
+
}
|
|
17
|
+
export declare function useHybridQuery<T>(endpoint: string, { query, variables, options }: HybridQueryParams<T>): {
|
|
18
|
+
data: T | null;
|
|
19
|
+
error: Error | null;
|
|
20
|
+
loading: boolean;
|
|
21
|
+
refetch: () => Promise<T | null>;
|
|
22
|
+
mutate: import("react").Dispatch<import("react").SetStateAction<T | null>>;
|
|
23
|
+
cancel: () => void | undefined;
|
|
24
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface MutationOptions<T> {
|
|
2
|
+
cacheKey?: string;
|
|
3
|
+
optimisticUpdate?: (prevData: any, newData: T) => any;
|
|
4
|
+
onSuccess?: (data: T) => void;
|
|
5
|
+
onError?: (error: any) => void;
|
|
6
|
+
retry?: number;
|
|
7
|
+
retryDelay?: number;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function useMutation<T = any>(endpoint: string, options?: MutationOptions<T>): {
|
|
12
|
+
mutate: (body: any, method?: "POST" | "PUT" | "PATCH" | "DELETE") => Promise<T>;
|
|
13
|
+
loading: boolean;
|
|
14
|
+
error: any;
|
|
15
|
+
cancel: () => void | undefined;
|
|
16
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface QueryOptions<T> {
|
|
3
|
+
variables?: Record<string, any>;
|
|
4
|
+
cacheKey?: string;
|
|
5
|
+
staleTime?: number;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
retry?: number;
|
|
9
|
+
retryDelay?: number;
|
|
10
|
+
optimisticUpdate?: (prevData: T | null, newData: T) => T;
|
|
11
|
+
}
|
|
12
|
+
export declare function useQuery<T>(endpoint: string, options: QueryOptions<T>): {
|
|
13
|
+
data: T | null;
|
|
14
|
+
error: Error | null;
|
|
15
|
+
loading: boolean;
|
|
16
|
+
refetch: () => Promise<T | null>;
|
|
17
|
+
mutate: import("react").Dispatch<import("react").SetStateAction<T | null>>;
|
|
18
|
+
cancel: () => void | undefined;
|
|
19
|
+
};
|
package/build/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=function(){return t=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},t.apply(this,arguments)};function r(e,t,r,n){return new(r||(r=Promise))(function(i,a){function u(e){try{c(n.next(e))}catch(e){a(e)}}function o(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(u,o)}c((n=n.apply(e,t||[])).next())})}function n(e,t){var r,n,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=o(0),u.throw=o(1),u.return=o(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function o(o){return function(c){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;u&&(u=0,o[0]&&(a=0)),a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,c])}}}"function"==typeof SuppressedError&&SuppressedError;var i=new Map,a={get:function(e){var t=i.get(e);if(t)return t.data},set:function(e,t){i.set(e,{data:t,timestamp:Date.now()})},delete:function(e){i.delete(e)},clear:function(){return i.clear()}},u=new Map;function o(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var i,o,c,s=this;return n(this,function(l){return i=t.cacheKey||e,u.has(i)?[2,u.get(i)]:(o=a.get(i))?[2,o]:(c=fetch(e,t).then(function(e){return r(s,void 0,void 0,function(){var t;return n(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),a.set(i,t),[2,t]}})})}).finally(function(){return u.delete(i)}),u.set(i,c),[2,c])})})}var c=new Map,s={register:function(e,t,r,n){c.set(e,{cacheKey:e,refetch:t,setData:r,cancel:n})},setData:function(e,t){var r=c.get(e);(null==r?void 0:r.setData)&&r.setData(t)},cancel:function(e){var t,r=c.get(e);null===(t=null==r?void 0:r.cancel)||void 0===t||t.call(r)},invalidate:function(e){var t;e?null===(t=c.get(e))||void 0===t||t.refetch():c.forEach(function(e){return e.refetch()})},unregister:function(e){var t,r;null===(r=null===(t=c.get(e))||void 0===t?void 0:t.cancel)||void 0===r||r.call(t),c.delete(e)}},l=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var r=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(r)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function f(e,t,i){return r(this,void 0,void 0,function(){var r;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,4]),[4,e()];case 1:return[2,n.sent()];case 2:if(r=n.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,i)})];case 3:return n.sent(),[2,f(e,t-1,2*i)];case 4:return[2]}})})}function d(e){var t=new AbortController,r=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(r)}),t.signal}function h(e,t,r){e&&(s.setData(e,function(e){return t?t(e,r):r}),s.invalidate(e))}function v(i,a){var u=this,c=a.query,v=a.variables,p=a.options,y=void 0===p?{}:p,b=e.useState(null),m=b[0],w=b[1],g=e.useState(null),K=g[0],S=g[1],x=e.useState(!1),k=x[0],T=x[1],U=e.useRef(null),C=e.useCallback(function(){return r(u,void 0,void 0,function(){var e,a,u,s,l,p=this;return n(this,function(b){switch(b.label){case 0:T(!0),S(null),null===(s=U.current)||void 0===s||s.abort(),e=new AbortController,U.current=e,a=d(null!==(l=y.timeout)&&void 0!==l?l:1e4),u=function(e,d){return void 0===e&&(e=null!==(s=y.retry)&&void 0!==s?s:3),void 0===d&&(d=null!==(l=y.retryDelay)&&void 0!==l?l:500),r(p,void 0,void 0,function(){var r,s,l;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,5,,6]),c?[4,o(i,{method:"POST",headers:t({"Content-Type":"application/json"},y.headers),body:JSON.stringify({query:c,variables:v}),cacheKey:y.cacheKey,signal:a})]:[3,2];case 1:return s=n.sent(),r=s.data,[3,4];case 2:return[4,o(i,{cacheKey:y.cacheKey,signal:a,headers:y.headers})];case 3:r=n.sent(),n.label=4;case 4:return w(function(e){return y.optimisticUpdate?y.optimisticUpdate(e,r):r}),h(y.cacheKey,y.optimisticUpdate,r),[2,r];case 5:if("AbortError"===(l=n.sent()).name)throw l;return[2,f(function(){return u(e-1,2*d)},e-1,2*d)];case 6:return[2]}})})},b.label=1;case 1:return b.trys.push([1,,3,4]),[4,u()];case 2:return[2,b.sent()];case 3:return T(!1),[7];case 4:return[2]}})})},[i,c,v,y.cacheKey,y.optimisticUpdate,y.retry,y.retryDelay,y.headers,y.timeout]),D=e.useCallback(function(){var e;return null===(e=U.current)||void 0===e?void 0:e.abort()},[]);return e.useEffect(function(){return y.cacheKey&&s.register(y.cacheKey,C,w,D),function(){y.cacheKey&&s.unregister(y.cacheKey),D()}},[y.cacheKey,C,w,D]),e.useEffect(function(){if(y.staleTime){var e=setInterval(C,y.staleTime);return function(){return clearInterval(e)}}},[C,y.staleTime]),e.useEffect(function(){if(y.subscriptionUrl){var e=new l(y.subscriptionUrl).subscribe(function(e){w(function(t){return y.optimisticUpdate?y.optimisticUpdate(t,e):e}),h(y.cacheKey,y.optimisticUpdate,e)});return function(){return e()}}},[y.subscriptionUrl,y.optimisticUpdate,y.cacheKey]),{data:m,error:K,loading:k,refetch:C,mutate:w,cancel:D}}exports.SubscriptionManager=l,exports.cache=a,exports.createTimeoutSignal=d,exports.queryRegistry=s,exports.retryOperation=f,exports.updateCache=h,exports.useGraphQLMutation=function(i,a){var u=this,c=a.mutation,l=a.variables,h=a.cacheKey,v=a.optimisticUpdate,p=a.retry,y=void 0===p?3:p,b=a.retryDelay,m=void 0===b?500:b,w=a.headers,g=a.timeout,K=void 0===g?1e4:g,S=e.useRef(null),x=e.useCallback(function(){return r(u,void 0,void 0,function(){var e,a,u,p,b=this;return n(this,function(g){switch(g.label){case 0:null===(p=S.current)||void 0===p||p.abort(),e=new AbortController,S.current=e,a=d(K),u=function(e,d){return r(b,void 0,void 0,function(){var r,p;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,o(i,{method:"POST",headers:t({"Content-Type":"application/json"},w),body:JSON.stringify({query:c,variables:l}),signal:a,cacheKey:h})];case 1:return r=n.sent(),v&&h&&s.setData(h,function(e){return v(e,r.data)}),h&&s.invalidate(h),[2,r.data];case 2:if("AbortError"===(p=n.sent()).name)throw p;return[2,f(function(){return u(e-1,2*d)},e-1,2*d)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,u(y,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[i,c,l,h,v,y,m,w,K]),k=e.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:x,cancel:k}},exports.useGraphQLQuery=function(e,t){return v(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})},exports.useHybridQuery=v,exports.useMutation=function(i,a){var u=this,c=e.useState(!1),l=c[0],h=c[1],v=e.useState(null),p=v[0],y=v[1],b=e.useRef(null),m=e.useCallback(function(e,c){return void 0===c&&(c="POST"),r(u,void 0,void 0,function(){var u,l,v,p,m,w=this;return n(this,function(g){switch(g.label){case 0:h(!0),y(null),null===(p=b.current)||void 0===p||p.abort(),u=new AbortController,b.current=u,l=d(null!==(m=null==a?void 0:a.timeout)&&void 0!==m?m:1e4),v=function(u,d){return void 0===u&&(u=null!==(p=null==a?void 0:a.retry)&&void 0!==p?p:3),void 0===d&&(d=null!==(m=null==a?void 0:a.retryDelay)&&void 0!==m?m:500),r(w,void 0,void 0,function(){var r,h,p;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,o(i,{method:c,headers:t({"Content-Type":"application/json"},null==a?void 0:a.headers),body:JSON.stringify(e),cacheKey:null==a?void 0:a.cacheKey,signal:l})];case 1:return r=n.sent(),(null==a?void 0:a.optimisticUpdate)&&a.cacheKey&&(s.setData(a.cacheKey,function(e){return a.optimisticUpdate(e,r)}),s.invalidate(a.cacheKey)),null===(p=null==a?void 0:a.onSuccess)||void 0===p||p.call(a,r),[2,r];case 2:if("AbortError"===(h=n.sent()).name)throw h;return[2,f(function(){return v(u-1,2*d)},u-1,2*d)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,v()];case 2:return[2,g.sent()];case 3:return h(!1),[7];case 4:return[2]}})})},[i,a]),w=e.useCallback(function(){var e;return null===(e=b.current)||void 0===e?void 0:e.abort()},[]);return{mutate:m,loading:l,error:p,cancel:w}},exports.useQuery=function(e,t){return v(e,t)};
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { useHybridQuery } from "./hooks/useHybridQuery";
|
|
2
|
+
export { useQuery } from "./hooks/useQuery";
|
|
3
|
+
export { useMutation } from "./hooks/useMutation";
|
|
4
|
+
export { useGraphQLQuery } from "./hooks/useGraphQLQuery";
|
|
5
|
+
export { useGraphQLMutation } from "./hooks/useGraphQLMutation";
|
|
6
|
+
export { SubscriptionManager } from "./core/subscription";
|
|
7
|
+
export { queryRegistry } from "./core/globalQuery";
|
|
8
|
+
export { cache } from "./core/cache";
|
|
9
|
+
export { updateCache } from "./utils/cacheUtils";
|
|
10
|
+
export { retryOperation } from "./utils/retryUtils";
|
|
11
|
+
export { createTimeoutSignal } from "./utils/timeoutUtils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useState as t,useRef as e,useCallback as n,useEffect as r}from"react";var i=function(){return i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},i.apply(this,arguments)};function a(t,e,n,r){return new(n||(n=Promise))(function(i,a){function o(t){try{u(r.next(t))}catch(t){a(t)}}function c(t){try{u(r.throw(t))}catch(t){a(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(o,c)}u((r=r.apply(t,e||[])).next())})}function o(t,e){var n,r,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=c(0),o.throw=c(1),o.return=c(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(u){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return a.label++,{value:c[1],done:!1};case 5:a.label++,r=c[1],c=[0];continue;case 7:c=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){a=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){a.label=c[1];break}if(6===c[0]&&a.label<i[1]){a.label=i[1],i=c;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(c);break}i[2]&&a.ops.pop(),a.trys.pop();continue}c=e.call(t,a)}catch(t){c=[6,t],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}"function"==typeof SuppressedError&&SuppressedError;var c=new Map,u={get:function(t){var e=c.get(t);if(e)return e.data},set:function(t,e){c.set(t,{data:e,timestamp:Date.now()})},delete:function(t){c.delete(t)},clear:function(){return c.clear()}},s=new Map;function l(t,e){return void 0===e&&(e={}),a(this,void 0,void 0,function(){var n,r,i,c=this;return o(this,function(l){return n=e.cacheKey||t,s.has(n)?[2,s.get(n)]:(r=u.get(n))?[2,r]:(i=fetch(t,e).then(function(t){return a(c,void 0,void 0,function(){var e;return o(this,function(r){switch(r.label){case 0:if(!t.ok)throw new Error(t.statusText);return[4,t.json()];case 1:return e=r.sent(),u.set(n,e),[2,e]}})})}).finally(function(){return s.delete(n)}),s.set(n,i),[2,i])})})}var f=new Map,d={register:function(t,e,n,r){f.set(t,{cacheKey:t,refetch:e,setData:n,cancel:r})},setData:function(t,e){var n=f.get(t);(null==n?void 0:n.setData)&&n.setData(e)},cancel:function(t){var e,n=f.get(t);null===(e=null==n?void 0:n.cancel)||void 0===e||e.call(n)},invalidate:function(t){var e;t?null===(e=f.get(t))||void 0===e||e.refetch():f.forEach(function(t){return t.refetch()})},unregister:function(t){var e,n;null===(n=null===(e=f.get(t))||void 0===e?void 0:e.cancel)||void 0===n||n.call(e),f.delete(t)}},h=function(){function t(t){this.url=t,this.callbacks=new Set,this.ws=null}return t.prototype.connect=function(){var t=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(e){var n=JSON.parse(e.data);t.callbacks.forEach(function(t){return t(n)})})},t.prototype.subscribe=function(t){var e=this;return this.callbacks.add(t),this.connect(),function(){return e.callbacks.delete(t)}},t.prototype.disconnect=function(){var t;null===(t=this.ws)||void 0===t||t.close(),this.ws=null,this.callbacks.clear()},t}();function v(t,e,n){return a(this,void 0,void 0,function(){var r;return o(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,4]),[4,t()];case 1:return[2,i.sent()];case 2:if(r=i.sent(),e<=0)throw r;return[4,new Promise(function(t){return setTimeout(t,n)})];case 3:return i.sent(),[2,v(t,e-1,2*n)];case 4:return[2]}})})}function p(t){var e=new AbortController,n=setTimeout(function(){return e.abort()},t);return e.signal.addEventListener("abort",function(){return clearTimeout(n)}),e.signal}function y(t,e,n){t&&(d.setData(t,function(t){return e?e(t,n):n}),d.invalidate(t))}function b(c,u){var s=this,f=u.query,b=u.variables,m=u.options,w=void 0===m?{}:m,g=t(null),K=g[0],T=g[1],U=t(null),D=U[0],S=U[1],O=t(!1),k=O[0],E=O[1],j=e(null),x=n(function(){return a(s,void 0,void 0,function(){var t,e,n,r,u,s=this;return o(this,function(d){switch(d.label){case 0:E(!0),S(null),null===(r=j.current)||void 0===r||r.abort(),t=new AbortController,j.current=t,e=p(null!==(u=w.timeout)&&void 0!==u?u:1e4),n=function(t,d){return void 0===t&&(t=null!==(r=w.retry)&&void 0!==r?r:3),void 0===d&&(d=null!==(u=w.retryDelay)&&void 0!==u?u:500),a(s,void 0,void 0,function(){var r,a,u;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,5,,6]),f?[4,l(c,{method:"POST",headers:i({"Content-Type":"application/json"},w.headers),body:JSON.stringify({query:f,variables:b}),cacheKey:w.cacheKey,signal:e})]:[3,2];case 1:return a=o.sent(),r=a.data,[3,4];case 2:return[4,l(c,{cacheKey:w.cacheKey,signal:e,headers:w.headers})];case 3:r=o.sent(),o.label=4;case 4:return T(function(t){return w.optimisticUpdate?w.optimisticUpdate(t,r):r}),y(w.cacheKey,w.optimisticUpdate,r),[2,r];case 5:if("AbortError"===(u=o.sent()).name)throw u;return[2,v(function(){return n(t-1,2*d)},t-1,2*d)];case 6:return[2]}})})},d.label=1;case 1:return d.trys.push([1,,3,4]),[4,n()];case 2:return[2,d.sent()];case 3:return E(!1),[7];case 4:return[2]}})})},[c,f,b,w.cacheKey,w.optimisticUpdate,w.retry,w.retryDelay,w.headers,w.timeout]),A=n(function(){var t;return null===(t=j.current)||void 0===t?void 0:t.abort()},[]);return r(function(){return w.cacheKey&&d.register(w.cacheKey,x,T,A),function(){w.cacheKey&&d.unregister(w.cacheKey),A()}},[w.cacheKey,x,T,A]),r(function(){if(w.staleTime){var t=setInterval(x,w.staleTime);return function(){return clearInterval(t)}}},[x,w.staleTime]),r(function(){if(w.subscriptionUrl){var t=new h(w.subscriptionUrl).subscribe(function(t){T(function(e){return w.optimisticUpdate?w.optimisticUpdate(e,t):t}),y(w.cacheKey,w.optimisticUpdate,t)});return function(){return t()}}},[w.subscriptionUrl,w.optimisticUpdate,w.cacheKey]),{data:K,error:D,loading:k,refetch:x,mutate:T,cancel:A}}function m(t,e){return b(t,e)}function w(r,c){var u=this,s=t(!1),f=s[0],h=s[1],y=t(null),b=y[0],m=y[1],w=e(null),g=n(function(t,e){return void 0===e&&(e="POST"),a(u,void 0,void 0,function(){var n,u,s,f,y,b=this;return o(this,function(g){switch(g.label){case 0:h(!0),m(null),null===(f=w.current)||void 0===f||f.abort(),n=new AbortController,w.current=n,u=p(null!==(y=null==c?void 0:c.timeout)&&void 0!==y?y:1e4),s=function(n,h){return void 0===n&&(n=null!==(f=null==c?void 0:c.retry)&&void 0!==f?f:3),void 0===h&&(h=null!==(y=null==c?void 0:c.retryDelay)&&void 0!==y?y:500),a(b,void 0,void 0,function(){var a,f,p;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,l(r,{method:e,headers:i({"Content-Type":"application/json"},null==c?void 0:c.headers),body:JSON.stringify(t),cacheKey:null==c?void 0:c.cacheKey,signal:u})];case 1:return a=o.sent(),(null==c?void 0:c.optimisticUpdate)&&c.cacheKey&&(d.setData(c.cacheKey,function(t){return c.optimisticUpdate(t,a)}),d.invalidate(c.cacheKey)),null===(p=null==c?void 0:c.onSuccess)||void 0===p||p.call(c,a),[2,a];case 2:if("AbortError"===(f=o.sent()).name)throw f;return[2,v(function(){return s(n-1,2*h)},n-1,2*h)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,s()];case 2:return[2,g.sent()];case 3:return h(!1),[7];case 4:return[2]}})})},[r,c]),K=n(function(){var t;return null===(t=w.current)||void 0===t?void 0:t.abort()},[]);return{mutate:g,loading:f,error:b,cancel:K}}function g(t,e){return b(t,{query:e.query,variables:e.variables,options:{cacheKey:e.cacheKey,staleTime:e.staleTime,headers:e.headers,timeout:e.timeout,retry:e.retry,retryDelay:e.retryDelay,optimisticUpdate:e.optimisticUpdate}})}function K(t,r){var c=this,u=r.mutation,s=r.variables,f=r.cacheKey,h=r.optimisticUpdate,y=r.retry,b=void 0===y?3:y,m=r.retryDelay,w=void 0===m?500:m,g=r.headers,K=r.timeout,T=void 0===K?1e4:K,U=e(null),D=n(function(){return a(c,void 0,void 0,function(){var e,n,r,c,y=this;return o(this,function(m){switch(m.label){case 0:null===(c=U.current)||void 0===c||c.abort(),e=new AbortController,U.current=e,n=p(T),r=function(e,c){return a(y,void 0,void 0,function(){var a,p;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,l(t,{method:"POST",headers:i({"Content-Type":"application/json"},g),body:JSON.stringify({query:u,variables:s}),signal:n,cacheKey:f})];case 1:return a=o.sent(),h&&f&&d.setData(f,function(t){return h(t,a.data)}),f&&d.invalidate(f),[2,a.data];case 2:if("AbortError"===(p=o.sent()).name)throw p;return[2,v(function(){return r(e-1,2*c)},e-1,2*c)];case 3:return[2]}})})},m.label=1;case 1:return m.trys.push([1,,3,4]),[4,r(b,w)];case 2:return[2,m.sent()];case 3:return[7];case 4:return[2]}})})},[t,u,s,f,h,b,w,g,T]),S=n(function(){var t;null===(t=U.current)||void 0===t||t.abort()},[]);return{mutate:D,cancel:S}}export{h as SubscriptionManager,u as cache,p as createTimeoutSignal,d as queryRegistry,v as retryOperation,y as updateCache,K as useGraphQLMutation,g as useGraphQLQuery,b as useHybridQuery,w as useMutation,m as useQuery};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactiveQuery={},e.React)}(this,function(e,t){"use strict";var n=function(){return n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},n.apply(this,arguments)};function r(e,t,n,r){return new(n||(n=Promise))(function(i,a){function u(e){try{c(r.next(e))}catch(e){a(e)}}function o(e){try{c(r.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(u,o)}c((r=r.apply(e,t||[])).next())})}function i(e,t){var n,r,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=o(0),u.throw=o(1),u.return=o(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function o(o){return function(c){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,o[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,r=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,c])}}}"function"==typeof SuppressedError&&SuppressedError;var a=new Map,u={get:function(e){var t=a.get(e);if(t)return t.data},set:function(e,t){a.set(e,{data:t,timestamp:Date.now()})},delete:function(e){a.delete(e)},clear:function(){return a.clear()}},o=new Map;function c(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var n,a,c,s=this;return i(this,function(l){return n=t.cacheKey||e,o.has(n)?[2,o.get(n)]:(a=u.get(n))?[2,a]:(c=fetch(e,t).then(function(e){return r(s,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),u.set(n,t),[2,t]}})})}).finally(function(){return o.delete(n)}),o.set(n,c),[2,c])})})}var s=new Map,l={register:function(e,t,n,r){s.set(e,{cacheKey:e,refetch:t,setData:n,cancel:r})},setData:function(e,t){var n=s.get(e);(null==n?void 0:n.setData)&&n.setData(t)},cancel:function(e){var t,n=s.get(e);null===(t=null==n?void 0:n.cancel)||void 0===t||t.call(n)},invalidate:function(e){var t;e?null===(t=s.get(e))||void 0===t||t.refetch():s.forEach(function(e){return e.refetch()})},unregister:function(e){var t,n;null===(n=null===(t=s.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t),s.delete(e)}},f=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var n=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(n)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function d(e,t,n){return r(this,void 0,void 0,function(){var r;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,4]),[4,e()];case 1:return[2,i.sent()];case 2:if(r=i.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,n)})];case 3:return i.sent(),[2,d(e,t-1,2*n)];case 4:return[2]}})})}function h(e){var t=new AbortController,n=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(n)}),t.signal}function v(e,t,n){e&&(l.setData(e,function(e){return t?t(e,n):n}),l.invalidate(e))}function p(e,a){var u=this,o=a.query,s=a.variables,p=a.options,y=void 0===p?{}:p,b=t.useState(null),m=b[0],w=b[1],g=t.useState(null),K=g[0],S=g[1],T=t.useState(!1),k=T[0],U=T[1],C=t.useRef(null),D=t.useCallback(function(){return r(u,void 0,void 0,function(){var t,a,u,l,f,p=this;return i(this,function(b){switch(b.label){case 0:U(!0),S(null),null===(l=C.current)||void 0===l||l.abort(),t=new AbortController,C.current=t,a=h(null!==(f=y.timeout)&&void 0!==f?f:1e4),u=function(t,h){return void 0===t&&(t=null!==(l=y.retry)&&void 0!==l?l:3),void 0===h&&(h=null!==(f=y.retryDelay)&&void 0!==f?f:500),r(p,void 0,void 0,function(){var r,l,f;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,5,,6]),o?[4,c(e,{method:"POST",headers:n({"Content-Type":"application/json"},y.headers),body:JSON.stringify({query:o,variables:s}),cacheKey:y.cacheKey,signal:a})]:[3,2];case 1:return l=i.sent(),r=l.data,[3,4];case 2:return[4,c(e,{cacheKey:y.cacheKey,signal:a,headers:y.headers})];case 3:r=i.sent(),i.label=4;case 4:return w(function(e){return y.optimisticUpdate?y.optimisticUpdate(e,r):r}),v(y.cacheKey,y.optimisticUpdate,r),[2,r];case 5:if("AbortError"===(f=i.sent()).name)throw f;return[2,d(function(){return u(t-1,2*h)},t-1,2*h)];case 6:return[2]}})})},b.label=1;case 1:return b.trys.push([1,,3,4]),[4,u()];case 2:return[2,b.sent()];case 3:return U(!1),[7];case 4:return[2]}})})},[e,o,s,y.cacheKey,y.optimisticUpdate,y.retry,y.retryDelay,y.headers,y.timeout]),O=t.useCallback(function(){var e;return null===(e=C.current)||void 0===e?void 0:e.abort()},[]);return t.useEffect(function(){return y.cacheKey&&l.register(y.cacheKey,D,w,O),function(){y.cacheKey&&l.unregister(y.cacheKey),O()}},[y.cacheKey,D,w,O]),t.useEffect(function(){if(y.staleTime){var e=setInterval(D,y.staleTime);return function(){return clearInterval(e)}}},[D,y.staleTime]),t.useEffect(function(){if(y.subscriptionUrl){var e=new f(y.subscriptionUrl).subscribe(function(e){w(function(t){return y.optimisticUpdate?y.optimisticUpdate(t,e):e}),v(y.cacheKey,y.optimisticUpdate,e)});return function(){return e()}}},[y.subscriptionUrl,y.optimisticUpdate,y.cacheKey]),{data:m,error:K,loading:k,refetch:D,mutate:w,cancel:O}}e.SubscriptionManager=f,e.cache=u,e.createTimeoutSignal=h,e.queryRegistry=l,e.retryOperation=d,e.updateCache=v,e.useGraphQLMutation=function(e,a){var u=this,o=a.mutation,s=a.variables,f=a.cacheKey,v=a.optimisticUpdate,p=a.retry,y=void 0===p?3:p,b=a.retryDelay,m=void 0===b?500:b,w=a.headers,g=a.timeout,K=void 0===g?1e4:g,S=t.useRef(null),T=t.useCallback(function(){return r(u,void 0,void 0,function(){var t,a,u,p,b=this;return i(this,function(g){switch(g.label){case 0:null===(p=S.current)||void 0===p||p.abort(),t=new AbortController,S.current=t,a=h(K),u=function(t,h){return r(b,void 0,void 0,function(){var r,p;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,c(e,{method:"POST",headers:n({"Content-Type":"application/json"},w),body:JSON.stringify({query:o,variables:s}),signal:a,cacheKey:f})];case 1:return r=i.sent(),v&&f&&l.setData(f,function(e){return v(e,r.data)}),f&&l.invalidate(f),[2,r.data];case 2:if("AbortError"===(p=i.sent()).name)throw p;return[2,d(function(){return u(t-1,2*h)},t-1,2*h)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,u(y,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[e,o,s,f,v,y,m,w,K]),k=t.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:T,cancel:k}},e.useGraphQLQuery=function(e,t){return p(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})},e.useHybridQuery=p,e.useMutation=function(e,a){var u=this,o=t.useState(!1),s=o[0],f=o[1],v=t.useState(null),p=v[0],y=v[1],b=t.useRef(null),m=t.useCallback(function(t,o){return void 0===o&&(o="POST"),r(u,void 0,void 0,function(){var u,s,v,p,m,w=this;return i(this,function(g){switch(g.label){case 0:f(!0),y(null),null===(p=b.current)||void 0===p||p.abort(),u=new AbortController,b.current=u,s=h(null!==(m=null==a?void 0:a.timeout)&&void 0!==m?m:1e4),v=function(u,f){return void 0===u&&(u=null!==(p=null==a?void 0:a.retry)&&void 0!==p?p:3),void 0===f&&(f=null!==(m=null==a?void 0:a.retryDelay)&&void 0!==m?m:500),r(w,void 0,void 0,function(){var r,h,p;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,c(e,{method:o,headers:n({"Content-Type":"application/json"},null==a?void 0:a.headers),body:JSON.stringify(t),cacheKey:null==a?void 0:a.cacheKey,signal:s})];case 1:return r=i.sent(),(null==a?void 0:a.optimisticUpdate)&&a.cacheKey&&(l.setData(a.cacheKey,function(e){return a.optimisticUpdate(e,r)}),l.invalidate(a.cacheKey)),null===(p=null==a?void 0:a.onSuccess)||void 0===p||p.call(a,r),[2,r];case 2:if("AbortError"===(h=i.sent()).name)throw h;return[2,d(function(){return v(u-1,2*f)},u-1,2*f)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,v()];case 2:return[2,g.sent()];case 3:return f(!1),[7];case 4:return[2]}})})},[e,a]),w=t.useCallback(function(){var e;return null===(e=b.current)||void 0===e?void 0:e.abort()},[]);return{mutate:m,loading:s,error:p,cancel:w}},e.useQuery=function(e,t){return p(e,t)},Object.defineProperty(e,"__esModule",{value:!0})});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function updateCache<T>(cacheKey: string | undefined, optimisticUpdate: ((prevData: T | null, newData: T) => T) | undefined, newData: T): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function retryOperation<T>(operation: () => Promise<T>, retries: number, delay: number): Promise<T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createTimeoutSignal(timeout: number): AbortSignal;
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "reactive-query-z",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Intent-first orchestration engine for React applications with support for REST and GraphQL",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Delpi.Kye",
|
|
7
|
+
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
|
|
10
|
+
"main": "build/index.cjs",
|
|
11
|
+
"module": "build/index.esm.js",
|
|
12
|
+
"types": "build/index.d.ts",
|
|
13
|
+
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./build/index.d.ts",
|
|
17
|
+
"import": "./build/index.esm.js",
|
|
18
|
+
"require": "./build/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
"files": [
|
|
23
|
+
"build"
|
|
24
|
+
],
|
|
25
|
+
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean": "rimraf build",
|
|
28
|
+
"build": "rollup -c",
|
|
29
|
+
"cb": "npm run clean && npm run build",
|
|
30
|
+
"prepublishOnly": "npm run build"
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/delpikye-v/reactive-query.git"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/delpikye-v/reactive-query#readme",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/delpikye-v/reactive-query/issues"
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
"keywords": [
|
|
43
|
+
"react",
|
|
44
|
+
"intent",
|
|
45
|
+
"intent-engine",
|
|
46
|
+
"orchestration",
|
|
47
|
+
"state-management",
|
|
48
|
+
"architecture",
|
|
49
|
+
"ddd",
|
|
50
|
+
"hexagonal",
|
|
51
|
+
"side-effects",
|
|
52
|
+
"async-flow",
|
|
53
|
+
"react-hooks",
|
|
54
|
+
"react-library",
|
|
55
|
+
"graphql",
|
|
56
|
+
"rest"
|
|
57
|
+
],
|
|
58
|
+
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"react": ">=17"
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
"dependencies": {},
|
|
64
|
+
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@rollup/plugin-commonjs": "^17.1.0",
|
|
67
|
+
"@rollup/plugin-node-resolve": "^11.2.1",
|
|
68
|
+
"@types/react": "^17.0.90",
|
|
69
|
+
"rimraf": "^3.0.2",
|
|
70
|
+
"rollup": "^2.56.3",
|
|
71
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
72
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
73
|
+
"rollup-plugin-typescript2": "^0.29.0",
|
|
74
|
+
"typescript": "^4.4.2",
|
|
75
|
+
"tslib": "^2.3.1"
|
|
76
|
+
}
|
|
77
|
+
}
|