@braine/quantum-query 1.1.1 → 1.2.1
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 +125 -41
- package/dist/index.cjs +1312 -137
- package/dist/index.d.cts +298 -9
- package/dist/index.d.ts +298 -9
- package/dist/index.js +1301 -136
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
# @braine/quantum-query
|
|
2
2
|
|
|
3
3
|
**State Management at the Speed of Light.**
|
|
4
|
-
> A unified architecture that merges Store, Actions, and API Logic into single, high-performance
|
|
4
|
+
> A unified, signal-based architecture that merges Store, Actions, and API Logic into a single, high-performance ecosystem.
|
|
5
5
|
|
|
6
6
|
[](https://www.npmjs.com/package/@braine/quantum-query)
|
|
7
7
|
[](https://opensource.org/licenses/MIT)
|
|
8
8
|
|
|
9
|
-
## Why "Quantum"?
|
|
10
|
-
Existing libraries behave like "Buses". They stop at every station (component) to check if someone needs to get off (re-render).
|
|
9
|
+
## ⚡️ Why "Quantum"?
|
|
10
|
+
Existing libraries (Redux, RTK Query) behave like "Buses". They stop at every station (component) to check if someone needs to get off (re-render via O(n) selectors).
|
|
11
|
+
|
|
11
12
|
**Quantum-Query** behaves like a teleporter. It updates *only* the specific component listening to a specific property, instantly.
|
|
12
13
|
|
|
13
|
-
* **O(1) Reactivity**:
|
|
14
|
-
* **Zero Boilerplate**: No
|
|
15
|
-
* **Enterprise
|
|
14
|
+
* **O(1) Reactivity**: Powered by Atomic Signals. Zero selectors. No "Top-Down" re-renders.
|
|
15
|
+
* **Zero Boilerplate**: No reducers, no providers, no slices, no thunks.
|
|
16
|
+
* **Enterprise Ecosystem**: Persistence, Plugins, Deduplication, and Validation included.
|
|
16
17
|
|
|
17
18
|
---
|
|
18
19
|
|
|
19
|
-
## Installation
|
|
20
|
+
## 📦 Installation
|
|
20
21
|
|
|
21
22
|
```bash
|
|
22
23
|
npm install @braine/quantum-query
|
|
@@ -24,13 +25,32 @@ npm install @braine/quantum-query
|
|
|
24
25
|
|
|
25
26
|
---
|
|
26
27
|
|
|
27
|
-
##
|
|
28
|
+
## 🚀 Quick Start (React Hooks)
|
|
29
|
+
|
|
30
|
+
If you just want to fetch data, it works exactly like you expect.
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { useQuery } from '@braine/quantum-query';
|
|
34
|
+
|
|
35
|
+
function UserProfile({ id }) {
|
|
36
|
+
const { data, isLoading } = useQuery({
|
|
37
|
+
queryKey: ['user', id],
|
|
38
|
+
queryFn: () => fetch(`/api/user/${id}`).then(r => r.json()),
|
|
39
|
+
staleTime: 5000 // Auto-cache for 5s
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (isLoading) return <div>Loading...</div>;
|
|
43
|
+
return <div>Hello, {data.name}!</div>;
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
28
48
|
|
|
29
|
-
|
|
49
|
+
## 🧠 The "Smart Model" Pattern (Advanced)
|
|
30
50
|
|
|
31
|
-
|
|
32
|
-
`defineModel` wraps your state, computed properties, and actions into one reactive entity.
|
|
51
|
+
Stop splitting your logic between Redux (Client State) and React Query (Server State). **Smart Models** combine state, computed properties, and actions into one reactive entity.
|
|
33
52
|
|
|
53
|
+
### 1. Define Model
|
|
34
54
|
```typescript
|
|
35
55
|
import { defineModel } from '@braine/quantum-query';
|
|
36
56
|
|
|
@@ -45,48 +65,47 @@ export const TodoModel = defineModel({
|
|
|
45
65
|
computed: {
|
|
46
66
|
activeCount() {
|
|
47
67
|
return this.items.length;
|
|
68
|
+
},
|
|
69
|
+
isEmpty() {
|
|
70
|
+
return this.items.length === 0;
|
|
48
71
|
}
|
|
49
72
|
},
|
|
50
73
|
|
|
51
74
|
// 3. Actions (Sync + Async + Optimistic)
|
|
52
75
|
actions: {
|
|
53
|
-
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
await api.post('/todos', { text });
|
|
59
|
-
} catch (err) {
|
|
60
|
-
this.items.pop(); // Auto-Rollback
|
|
61
|
-
}
|
|
76
|
+
add(text: string) {
|
|
77
|
+
this.items.push(text); // Direct mutation (proxied)
|
|
78
|
+
},
|
|
79
|
+
async save() {
|
|
80
|
+
await api.post('/todos', { items: this.items });
|
|
62
81
|
}
|
|
63
82
|
}
|
|
64
83
|
});
|
|
65
84
|
```
|
|
66
85
|
|
|
67
|
-
### 2. Use
|
|
68
|
-
Just use it.
|
|
69
|
-
|
|
86
|
+
### 2. Use Model
|
|
70
87
|
```tsx
|
|
71
88
|
import { useStore } from '@braine/quantum-query';
|
|
72
89
|
import { TodoModel } from './models/TodoModel';
|
|
73
90
|
|
|
74
91
|
function TodoApp() {
|
|
75
|
-
|
|
92
|
+
// auto-subscribes ONLY to properties accessed in this component
|
|
93
|
+
const model = useStore(TodoModel);
|
|
76
94
|
|
|
77
95
|
return (
|
|
78
|
-
<
|
|
79
|
-
Active: {model.activeCount}
|
|
80
|
-
|
|
96
|
+
<div>
|
|
97
|
+
<h1>Active: {model.activeCount}</h1>
|
|
98
|
+
<button onClick={() => model.add("Ship it")}>Add</button>
|
|
99
|
+
</div>
|
|
81
100
|
);
|
|
82
101
|
}
|
|
83
102
|
```
|
|
84
103
|
|
|
85
104
|
---
|
|
86
105
|
|
|
87
|
-
## Enterprise HTTP Client
|
|
106
|
+
## 🌐 Enterprise HTTP Client
|
|
88
107
|
|
|
89
|
-
We built a fetch wrapper that matches **RTK Query** in power but keeps **Axios** simplicity.
|
|
108
|
+
We built a fetch wrapper that matches **RTK Query** in power but keeps **Axios** simplicity. It includes **Automatic Deduplication** and **Retries**.
|
|
90
109
|
|
|
91
110
|
```typescript
|
|
92
111
|
import { createHttpClient } from '@braine/quantum-query';
|
|
@@ -94,9 +113,9 @@ import { createHttpClient } from '@braine/quantum-query';
|
|
|
94
113
|
export const api = createHttpClient({
|
|
95
114
|
baseURL: 'https://api.myapp.com',
|
|
96
115
|
timeout: 5000,
|
|
97
|
-
retry: { retries: 3 }, // Exponential backoff for
|
|
116
|
+
retry: { retries: 3 }, // Exponential backoff for Network errors
|
|
98
117
|
|
|
99
|
-
// Auth Handling
|
|
118
|
+
// Auth Handling (Auto-Refresh)
|
|
100
119
|
auth: {
|
|
101
120
|
getToken: () => localStorage.getItem('token'),
|
|
102
121
|
onTokenExpired: async () => {
|
|
@@ -107,22 +126,87 @@ export const api = createHttpClient({
|
|
|
107
126
|
}
|
|
108
127
|
});
|
|
109
128
|
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
|
|
129
|
+
// data is strictly typed!
|
|
130
|
+
const user = await api.get<User>('/me');
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## 🛡️ Data Integrity (Runtime Safety)
|
|
136
|
+
|
|
137
|
+
Don't trust the backend. Validate it. We support **Zod**, **Valibot**, or **Yup** schemas directly in the hook.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { z } from 'zod';
|
|
141
|
+
|
|
142
|
+
const UserSchema = z.object({
|
|
143
|
+
id: z.string(),
|
|
144
|
+
name: z.string(),
|
|
145
|
+
role: z.enum(['admin', 'user'])
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const { data } = useQuery({
|
|
149
|
+
queryKey: ['user'],
|
|
150
|
+
queryFn: fetchUser,
|
|
151
|
+
schema: UserSchema // Throws descriptive error if API returns garbage
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## 🔌 Plugin System (Middleware) 🆕
|
|
158
|
+
|
|
159
|
+
Inject logic into every request lifecycle (Logging, Analytics, Performance Monitoring).
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { queryCache } from '@braine/quantum-query';
|
|
163
|
+
|
|
164
|
+
queryCache.use({
|
|
165
|
+
name: 'logger',
|
|
166
|
+
onFetchStart: (key) => console.log('Fetching', key),
|
|
167
|
+
onFetchError: (key, error) => console.error(`Fetch failed for ${key}:`, error)
|
|
168
|
+
});
|
|
113
169
|
```
|
|
114
170
|
|
|
115
171
|
---
|
|
116
172
|
|
|
117
|
-
##
|
|
173
|
+
## 💾 Persistence Adapter 🆕
|
|
174
|
+
|
|
175
|
+
Persist your cache to `localStorage` (or IndexedDB/AsyncStorage) automatically. Works offline.
|
|
118
176
|
|
|
119
|
-
|
|
177
|
+
```typescript
|
|
178
|
+
import { persistQueryClient, createLocalStoragePersister } from '@braine/quantum-query/persist';
|
|
179
|
+
|
|
180
|
+
persistQueryClient({
|
|
181
|
+
queryClient: queryCache,
|
|
182
|
+
persister: createLocalStoragePersister(),
|
|
183
|
+
maxAge: 1000 * 60 * 60 * 24 // 24 hours
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## 📚 Documentation
|
|
190
|
+
|
|
191
|
+
* **[API Reference](docs/api.md)**: Full method signatures and options.
|
|
192
|
+
* **[Recipes](docs/recipes.md)**: Common patterns (Auth, Infinite Scroll, Optimistic UI).
|
|
193
|
+
* **[Migration Guide](docs/migration.md)**: Step-by-step guide from RTK Query / Redux.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 🆚 Comparison
|
|
198
|
+
|
|
199
|
+
| Feature | RTK Query | TanStack Query | **Quantum-Query** |
|
|
120
200
|
| :--- | :--- | :--- | :--- |
|
|
121
|
-
| **
|
|
122
|
-
| **Boilerplate** | High | Medium | **Zero** |
|
|
123
|
-
| **
|
|
124
|
-
| **
|
|
125
|
-
| **Bundle Size** |
|
|
201
|
+
| **Architecture** | Redux (Store + Slices) | Observers | **Atomic Signals** ✅ |
|
|
202
|
+
| **Boilerplate** | High (Provider + Store) | Medium | **Zero** ✅ |
|
|
203
|
+
| **Re-Renders** | Selector-based (O(n)) | Observer-based | **Signal-based (O(1))** ✅ |
|
|
204
|
+
| **Smart Models** | ❌ (Requires Redux) | ❌ | **Built-in** ✅ |
|
|
205
|
+
| **Bundle Size** | ~17kb | ~13kb | **~3kb** ✅ |
|
|
206
|
+
| **Deduplication** | Yes | Yes | **Yes** ✅ |
|
|
207
|
+
| **Persistence** | `redux-persist` | Experimental | **Built-in First Class** ✅ |
|
|
208
|
+
|
|
209
|
+
---
|
|
126
210
|
|
|
127
211
|
## License
|
|
128
212
|
MIT
|