@royaltics/ui 1.8.9 → 1.9.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.http_query.md +87 -332
- package/README.socket.md +126 -0
- package/dist/core/http/http-actions.d.ts +9 -0
- package/dist/core/http/http-actions.d.ts.map +1 -0
- package/dist/core/http/http-actions.js +207 -0
- package/dist/core/http/http-actions.js.map +1 -0
- package/dist/core/http/http-store.d.ts +0 -1
- package/dist/core/http/http-store.d.ts.map +1 -1
- package/dist/core/http/http-store.js +0 -2
- package/dist/core/http/http-store.js.map +1 -1
- package/dist/core/http/http.refresh.js +8 -8
- package/dist/core/http/http.refresh.js.map +1 -1
- package/dist/core/http/http.types.d.ts +47 -2
- package/dist/core/http/http.types.d.ts.map +1 -1
- package/dist/core/http/useHttpsubscriptions.d.ts +16 -0
- package/dist/core/http/useHttpsubscriptions.d.ts.map +1 -0
- package/dist/core/http/useHttpsubscriptions.js +110 -0
- package/dist/core/http/useHttpsubscriptions.js.map +1 -0
- package/dist/core/socket/index.d.ts +1 -0
- package/dist/core/socket/index.d.ts.map +1 -1
- package/dist/core/socket/index.js +1 -0
- package/dist/core/socket/index.js.map +1 -1
- package/dist/core/socket/socket-store.d.ts +9 -5
- package/dist/core/socket/socket-store.d.ts.map +1 -1
- package/dist/core/socket/socket-store.js +26 -36
- package/dist/core/socket/socket-store.js.map +1 -1
- package/dist/core/socket/useSocketState.d.ts +13 -0
- package/dist/core/socket/useSocketState.d.ts.map +1 -0
- package/dist/core/socket/useSocketState.js +42 -0
- package/dist/core/socket/useSocketState.js.map +1 -0
- package/dist/forms/Autocomplete.stories.d.ts.map +1 -1
- package/dist/forms/Autocomplete.stories.js.map +1 -1
- package/dist/forms/SelectSearch.stories.d.ts.map +1 -1
- package/dist/forms/SelectSearch.stories.js +5 -8
- package/dist/forms/SelectSearch.stories.js.map +1 -1
- package/dist/hooks/index.d.ts +1 -2
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -2
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/useHttpState.d.ts +2 -38
- package/dist/hooks/useHttpState.d.ts.map +1 -1
- package/dist/hooks/useHttpState.js +27 -315
- package/dist/hooks/useHttpState.js.map +1 -1
- package/dist/hooks/useHttpStore.d.ts +19 -0
- package/dist/hooks/useHttpStore.d.ts.map +1 -0
- package/dist/hooks/useHttpStore.js +29 -0
- package/dist/hooks/useHttpStore.js.map +1 -0
- package/package.json +1 -1
- package/dist/providers/http-provider.d.ts +0 -22
- package/dist/providers/http-provider.d.ts.map +0 -1
- package/dist/providers/http-provider.js +0 -31
- package/dist/providers/http-provider.js.map +0 -1
- package/dist/providers/index.d.ts +0 -3
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js +0 -3
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/websocket-provider.d.ts +0 -11
- package/dist/providers/websocket-provider.d.ts.map +0 -1
- package/dist/providers/websocket-provider.js +0 -18
- package/dist/providers/websocket-provider.js.map +0 -1
package/README.http_query.md
CHANGED
|
@@ -1,332 +1,87 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
const handleSubmit = (e: FormEvent) => {
|
|
92
|
-
e.preventDefault();
|
|
93
|
-
mutate({
|
|
94
|
-
title: 'Mi nuevo post',
|
|
95
|
-
content: 'Contenido del post'
|
|
96
|
-
});
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
return (
|
|
100
|
-
<form onSubmit={handleSubmit}>
|
|
101
|
-
<button type="submit" disabled={isLoading}>
|
|
102
|
-
{isLoading ? 'Creando...' : 'Crear Post'}
|
|
103
|
-
</button>
|
|
104
|
-
</form>
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### useInfiniteQuery - Paginación infinita
|
|
110
|
-
|
|
111
|
-
```tsx
|
|
112
|
-
function PostList() {
|
|
113
|
-
const http = useHttpState();
|
|
114
|
-
|
|
115
|
-
const { pages, isLoading, hasNextPage, fetchNextPage, isFetchingNextPage } =
|
|
116
|
-
http.useInfiniteQuery<Post[]>(
|
|
117
|
-
['posts', 'infinite'],
|
|
118
|
-
{
|
|
119
|
-
infiniteOptions: {
|
|
120
|
-
cursorKey: 'cursor' // Nombre del parámetro de paginación
|
|
121
|
-
},
|
|
122
|
-
cacheOptions: {
|
|
123
|
-
staleTime: 2 * 60 * 1000
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
return (
|
|
129
|
-
<div>
|
|
130
|
-
{pages.map((page, i) => (
|
|
131
|
-
<div key={i}>
|
|
132
|
-
{page.map(post => (
|
|
133
|
-
<PostCard key={post.id} post={post} />
|
|
134
|
-
))}
|
|
135
|
-
</div>
|
|
136
|
-
))}
|
|
137
|
-
|
|
138
|
-
{hasNextPage && (
|
|
139
|
-
<button onClick={fetchNextPage} disabled={isFetchingNextPage}>
|
|
140
|
-
{isFetchingNextPage ? 'Cargando...' : 'Cargar más'}
|
|
141
|
-
</button>
|
|
142
|
-
)}
|
|
143
|
-
</div>
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
## Métodos Directos (sin caché)
|
|
149
|
-
|
|
150
|
-
### GET Request
|
|
151
|
-
|
|
152
|
-
```tsx
|
|
153
|
-
const http = useHttpState();
|
|
154
|
-
|
|
155
|
-
http.get(['users', userId], {
|
|
156
|
-
data: { include: 'posts' },
|
|
157
|
-
onSuccess: (user) => console.log(user),
|
|
158
|
-
onError: (message) => console.error(message)
|
|
159
|
-
});
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
### POST Request
|
|
163
|
-
|
|
164
|
-
```tsx
|
|
165
|
-
http.post(['posts'], {
|
|
166
|
-
data: { title: 'Nuevo post', content: 'Contenido' },
|
|
167
|
-
onSuccess: (post) => console.log('Creado:', post)
|
|
168
|
-
});
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### Upload de Archivos
|
|
172
|
-
|
|
173
|
-
```tsx
|
|
174
|
-
// FormData simple
|
|
175
|
-
http.file(['upload'], {
|
|
176
|
-
method: 'POST',
|
|
177
|
-
content_type: 'file',
|
|
178
|
-
data: {
|
|
179
|
-
title: 'Mi imagen',
|
|
180
|
-
description: 'Descripción',
|
|
181
|
-
photo: [fileObject], // Los archivos siempre van al final
|
|
182
|
-
tags: ['tag1', 'tag2']
|
|
183
|
-
},
|
|
184
|
-
onSuccess: (response) => console.log('Subido:', response)
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
// Upload por chunks (archivos grandes)
|
|
188
|
-
http.fileChunks(['upload', 'large'], {
|
|
189
|
-
data: { file: largeFile, metadata: { type: 'video' } },
|
|
190
|
-
chunkSize: 2 * 1024 * 1024, // 2MB por chunk
|
|
191
|
-
onProgress: (percent, display) => {
|
|
192
|
-
console.log(`Progreso: ${percent}% - ${display}`);
|
|
193
|
-
},
|
|
194
|
-
onSuccess: (response) => console.log('Completado:', response)
|
|
195
|
-
});
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
## QueryManager API
|
|
199
|
-
|
|
200
|
-
Acceso directo al query manager:
|
|
201
|
-
|
|
202
|
-
```tsx
|
|
203
|
-
const http = useHttpState();
|
|
204
|
-
const { queryManager } = http;
|
|
205
|
-
|
|
206
|
-
// Obtener datos de caché
|
|
207
|
-
const userData = queryManager.getQueryData<User>(['users', userId]);
|
|
208
|
-
|
|
209
|
-
// Actualizar caché manualmente
|
|
210
|
-
queryManager.setQueryData(['users', userId], updatedUser);
|
|
211
|
-
|
|
212
|
-
// Invalidar queries (fuerza refetch)
|
|
213
|
-
queryManager.invalidateQueries(['users']);
|
|
214
|
-
|
|
215
|
-
// Prefetch (cargar en background)
|
|
216
|
-
queryManager.prefetchQuery(
|
|
217
|
-
['posts', 'featured'],
|
|
218
|
-
() => fetch('/api/posts/featured').then(r => r.json())
|
|
219
|
-
);
|
|
220
|
-
|
|
221
|
-
// Reset queries (limpiar caché)
|
|
222
|
-
queryManager.resetQueries(['users']); // Específica
|
|
223
|
-
queryManager.resetQueries(); // Todas
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
## Configuración de Headers
|
|
227
|
-
|
|
228
|
-
```tsx
|
|
229
|
-
const http = useHttpState();
|
|
230
|
-
const { setHeaders } = useHttpContext();
|
|
231
|
-
|
|
232
|
-
// Actualizar headers globalmente
|
|
233
|
-
setHeaders({ authorization: `Bearer ${token}` });
|
|
234
|
-
|
|
235
|
-
// O con función
|
|
236
|
-
setHeaders(prev => ({
|
|
237
|
-
...prev,
|
|
238
|
-
'X-Custom-Header': 'value'
|
|
239
|
-
}));
|
|
240
|
-
|
|
241
|
-
// Headers por request
|
|
242
|
-
http.get(['users'], {
|
|
243
|
-
headers: { 'X-Request-ID': uuid() }
|
|
244
|
-
});
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
## buildBody Mejorado
|
|
248
|
-
|
|
249
|
-
La función `buildBody` ahora garantiza que los archivos siempre vayan al final del FormData:
|
|
250
|
-
|
|
251
|
-
```tsx
|
|
252
|
-
const data = {
|
|
253
|
-
title: 'Post con imagen',
|
|
254
|
-
tags: ['tag1', 'tag2'],
|
|
255
|
-
metadata: { author: 'John' },
|
|
256
|
-
photo: [imageFile], // Archivo - irá al final
|
|
257
|
-
z_attached: [pdfFile] // Archivo - irá al final
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
// FormData resultante:
|
|
261
|
-
// 1. title: "Post con imagen"
|
|
262
|
-
// 2. tags: ["tag1","tag2"]
|
|
263
|
-
// 3. metadata: {"author":"John"}
|
|
264
|
-
// 4. photo[]: File (imagen)
|
|
265
|
-
// 5. z_attached[]: File (pdf)
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
Esto soluciona el problema donde algunos backends no recibían los datos regulares si los archivos iban primero.
|
|
269
|
-
|
|
270
|
-
## Mejoras vs React Query
|
|
271
|
-
|
|
272
|
-
1. **Menor tamaño**: Sin dependencias innecesarias
|
|
273
|
-
2. **Deduplicación mejorada**: Requests idénticas comparten la misma Promise
|
|
274
|
-
3. **Tipos más estrictos**: Sin `any`, todo tipado con `unknown` o genéricos
|
|
275
|
-
4. **FormData optimizado**: Archivos siempre al final
|
|
276
|
-
5. **API más simple**: Menos configuración, más convenciones
|
|
277
|
-
6. **Retry con backoff**: Espera exponencial entre reintentos (1s, 2s, 3s...)
|
|
278
|
-
7. **Refetch real**: `invalidateQueries` ejecuta el fetch, no solo marca como stale
|
|
279
|
-
|
|
280
|
-
## Tipos TypeScript
|
|
281
|
-
|
|
282
|
-
```tsx
|
|
283
|
-
interface UseHttpResult<T> {
|
|
284
|
-
data: T | null;
|
|
285
|
-
isLoading: boolean;
|
|
286
|
-
error: Error | null;
|
|
287
|
-
refetch?: () => void;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
interface UseInfiniteHttpResult<T> extends UseHttpResult<T> {
|
|
291
|
-
pages: T[];
|
|
292
|
-
fetchNextPage: () => void;
|
|
293
|
-
hasNextPage: boolean;
|
|
294
|
-
isFetchingNextPage: boolean;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
interface UseMutationResult<T> {
|
|
298
|
-
mutate: (data: Record<string, unknown>) => void;
|
|
299
|
-
isLoading: boolean;
|
|
300
|
-
error: Error | null;
|
|
301
|
-
data: T | null;
|
|
302
|
-
}
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
## Notas de Rendimiento
|
|
306
|
-
|
|
307
|
-
- **Deduplicación**: Múltiples componentes solicitando la misma query comparten el request
|
|
308
|
-
- **Garbage Collection**: Queries sin suscriptores se eliminan después de `gcTime`
|
|
309
|
-
- **Stale Time**: Datos se consideran frescos durante `staleTime`, evitando refetches innecesarios
|
|
310
|
-
- **Window Focus**: Refetch automático al volver a la pestaña (configurable)
|
|
311
|
-
- **Subscripción eficiente**: Solo notifica a componentes suscritos a cada query específica
|
|
312
|
-
|
|
313
|
-
## Migración desde React Query
|
|
314
|
-
|
|
315
|
-
```tsx
|
|
316
|
-
// React Query
|
|
317
|
-
import { useQuery, useMutation } from '@tanstack/react-query';
|
|
318
|
-
|
|
319
|
-
const { data } = useQuery({
|
|
320
|
-
queryKey: ['users', id],
|
|
321
|
-
queryFn: () => fetchUser(id)
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
// Sistema Custom
|
|
325
|
-
const http = useHttpState();
|
|
326
|
-
|
|
327
|
-
const { data } = http.useQuery(['users', id], {
|
|
328
|
-
cacheOptions: { queryKey: ['users', id] }
|
|
329
|
-
});
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
La API es similar pero más simple y directa.
|
|
1
|
+
# @royaltics/ui - HTTP Store & Hooks
|
|
2
|
+
|
|
3
|
+
The new HTTP system replaces the previous `<HttpProvider>` component entirely. It leverages a clean `Zustand` vanilla store to allow initialization at the root module level, drastically increasing integration flexibility and making sure subsequent API requests made outside of React bindings (like React Router loaders) can safely reuse context.
|
|
4
|
+
|
|
5
|
+
## 1. Global Environment Initialization
|
|
6
|
+
To configure your global default `baseUrl`, `headers`, and refresh logic, simply call `initHttpOptions` once before you interact with the system or within your main application entry file (`main.tsx` or `App.tsx` outside of the component render).
|
|
7
|
+
|
|
8
|
+
```tsx
|
|
9
|
+
import { initHttpOptions } from '@royaltics/ui/hooks';
|
|
10
|
+
|
|
11
|
+
// Setup at the root of your project
|
|
12
|
+
initHttpOptions({
|
|
13
|
+
baseUrl: 'https://api.example.com',
|
|
14
|
+
debug: false,
|
|
15
|
+
headers: { 'X-App-Version': '1.0.0' },
|
|
16
|
+
onHttpError: (err) => console.error("Global Catch", err),
|
|
17
|
+
onNotAuth: (url) => window.location.href = '/login',
|
|
18
|
+
refreshConfig: {
|
|
19
|
+
url: '/auth/refresh',
|
|
20
|
+
maxAttempts: 1,
|
|
21
|
+
onSuccess: (res) => console.log('Refreshed token!')
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
*Note: Because this modifies the vanilla store, you no longer need to wrap your components in `<HttpProvider>`.*
|
|
26
|
+
|
|
27
|
+
## 2. Reading State Direct from Store
|
|
28
|
+
Should you need access to the underlying configurations anywhere in a React component, use `useHttpStore`.
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import { useHttpStore } from '@royaltics/ui/hooks';
|
|
32
|
+
|
|
33
|
+
const AppConfigViewer = () => {
|
|
34
|
+
// Read strictly the properties you need
|
|
35
|
+
const { online, baseUrl } = useHttpStore();
|
|
36
|
+
|
|
37
|
+
return <div>{online ? 'Online' : 'Offline'}</div>
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 3. Data Fetching via useHttpState
|
|
42
|
+
The highly modular `useHttpState` hook powers your data fetching (GET, POST, PATCH, DELETE, FILE) while providing granular component-scoped state properties (`isLoading`, `error`, etc) and automated subscriptions mapping.
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import { useHttpState } from '@royaltics/ui/hooks';
|
|
46
|
+
|
|
47
|
+
const UsersList = () => {
|
|
48
|
+
const { get, post, isLoading, getInfinite, fetchNextPage } = useHttpState();
|
|
49
|
+
|
|
50
|
+
const fetchUsers = () => {
|
|
51
|
+
get('/users', {
|
|
52
|
+
// Query param formatting
|
|
53
|
+
query: { active: true },
|
|
54
|
+
onSuccess: (data, paginate) => {
|
|
55
|
+
console.log(data, paginate);
|
|
56
|
+
},
|
|
57
|
+
onError: (message) => {
|
|
58
|
+
console.error(message);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const submitUser = () => {
|
|
64
|
+
post('/users', {
|
|
65
|
+
data: { name: 'John Doe' },
|
|
66
|
+
onSuccess: (data) => console.log('Created!')
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div>
|
|
72
|
+
<button onClick={fetchUsers} disabled={isLoading}>Load Users</button>
|
|
73
|
+
<button onClick={submitUser} disabled={isLoading}>Create User</button>
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Supported HTTP verbs in `useHttpState`:
|
|
80
|
+
- `get(api, opts)`
|
|
81
|
+
- `getInfinite(api, opts)`
|
|
82
|
+
- `post(api, opts)`
|
|
83
|
+
- `patch(api, opts)`
|
|
84
|
+
- `postPatch(api, id, opts)`
|
|
85
|
+
- `del(api, opts)`
|
|
86
|
+
- `file(api, opts)`
|
|
87
|
+
- `fileChunks(api, opts)`
|
package/README.socket.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @royaltics/ui - WebSocket Store & Hooks
|
|
2
|
+
|
|
3
|
+
The WebSocket system has been refactored to use a global `Zustand` store, entirely removing the need for a `<WebSocketProviderContext>` React wrapper. This allows you to initialize socket connections anywhere in your application (even outside of React components) and strictly ties connections to the `socket.io-client` lifecycle.
|
|
4
|
+
|
|
5
|
+
## 1. Global Socket Initialization
|
|
6
|
+
To initialize your socket connection globally, call `initSocketOptions`. You should only do this once, typically in your application's entry file or authentication hook.
|
|
7
|
+
|
|
8
|
+
```tsx
|
|
9
|
+
import { initSocketOptions } from '@royaltics/ui/hooks';
|
|
10
|
+
|
|
11
|
+
// Call this when the user logs in or app initializes
|
|
12
|
+
initSocketOptions({
|
|
13
|
+
socketUrl: 'https://socket.example.com',
|
|
14
|
+
token: 'YOUR_AUTH_TOKEN_HERE',
|
|
15
|
+
options: {
|
|
16
|
+
// Standard socket.io ManagerOptions & SocketOptions
|
|
17
|
+
reconnection: true,
|
|
18
|
+
reconnectionAttempts: 5,
|
|
19
|
+
},
|
|
20
|
+
onAuthError: (err) => {
|
|
21
|
+
// Triggered when socket.io 'connect_error' is fired.
|
|
22
|
+
// You can implement token refreshing mechanisms and then re-call `initSocketOptions({ ...newConfig })`
|
|
23
|
+
console.error("Socket authentication failed", err);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
*Note: If you call `initSocketOptions` with an empty `socketUrl` or `token`, it will automatically disconnect the socket.*
|
|
28
|
+
|
|
29
|
+
## 2. Reading Global Socket State
|
|
30
|
+
You can read the socket connection status directly using the `useSocketStore` hook anywhere in your component tree.
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { useSocketState } from '@royaltics/ui/socket';
|
|
34
|
+
|
|
35
|
+
export const ConnectionStatus = () => {
|
|
36
|
+
// You can read the status ('connected', 'disconnected', 'error', 'connecting')
|
|
37
|
+
const { status, isConnected } = useSocketState();
|
|
38
|
+
|
|
39
|
+
return <div>Socket is {isConnected ? 'Online' : 'Offline'}</div>
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 3. Emitting Events
|
|
44
|
+
The store gives you direct access to the `emit` function, completely sidestepping standard React context constraints.
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import { useSocketState } from '@royaltics/ui/socket';
|
|
48
|
+
|
|
49
|
+
export const ChatInput = () => {
|
|
50
|
+
// Destructure what you need!
|
|
51
|
+
const { emit, isConnected } = useSocketState();
|
|
52
|
+
|
|
53
|
+
const sendMessage = () => {
|
|
54
|
+
if (!isConnected) return;
|
|
55
|
+
emit('chat:message', { text: 'Hello, world!' });
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return <button onClick={sendMessage}>Send</button>;
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### `setToken(token)`
|
|
63
|
+
If you only need to update the authentication token without changing the `socketUrl` or other options, you can use `setToken`. This will automatically disconnect the current socket and reconnect with the new token.
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
import { useSocketState } from '@royaltics/ui/socket';
|
|
67
|
+
|
|
68
|
+
export const ProfileSettings = () => {
|
|
69
|
+
const { setToken } = useSocketState();
|
|
70
|
+
|
|
71
|
+
const handleSessionRefresh = (newToken: string) => {
|
|
72
|
+
setToken(newToken);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return <button onClick={() => handleSessionRefresh('NEW_TOKEN')}>Refresh Socket Token</button>;
|
|
76
|
+
};
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 4. Tree-Shaking & Bundle Optimization
|
|
80
|
+
To ensure that `socket.io-client` is only bundled when you actually use WebSockets, the socket logic is isolated in `@royaltics/ui/socket`.
|
|
81
|
+
|
|
82
|
+
> [!IMPORTANT]
|
|
83
|
+
> Do **NOT** import socket hooks from `@royaltics/ui/hooks`. Always use the dedicated entry point:
|
|
84
|
+
> `import { useSocketState } from '@royaltics/ui/socket';`
|
|
85
|
+
|
|
86
|
+
## 5. Listening to Events
|
|
87
|
+
We provide two tailored hooks for listening to specific Socket.io events and managing standard React lifecycles.
|
|
88
|
+
|
|
89
|
+
### `useSocketEvent`
|
|
90
|
+
Used for managing a single distinct event listener.
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
import { useSocketEvent } from '@royaltics/ui/socket';
|
|
94
|
+
|
|
95
|
+
export const NotificationToast = () => {
|
|
96
|
+
// Automatically binds/unbinds on mount/unmount
|
|
97
|
+
useSocketEvent('notification:new', (data) => {
|
|
98
|
+
console.log('Got new notification', data);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `useSocketEvents`
|
|
106
|
+
Used when a component needs to mount a dense array of closely related events. You can easily toggle individual events using the `enabled` flag.
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
import { useSocketEvents } from '@royaltics/ui/socket';
|
|
110
|
+
|
|
111
|
+
export const ChatRoom = () => {
|
|
112
|
+
useSocketEvents([
|
|
113
|
+
{
|
|
114
|
+
event: 'chat:message',
|
|
115
|
+
onData: (message) => console.log('New Message:', message)
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
event: 'chat:typing',
|
|
119
|
+
enabled: false, // You can toggle this dynamically!
|
|
120
|
+
onData: (user) => console.log(`${user} is typing...`)
|
|
121
|
+
}
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
return <div>Chat UI</div>
|
|
125
|
+
}
|
|
126
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { HttpStateOptionsProps, QueryKey } from "./http.types.js";
|
|
2
|
+
export declare const executeGet: <T = any>(core: any, api: string | string[], opts: HttpStateOptionsProps<T>) => Promise<T | undefined>;
|
|
3
|
+
export declare const executeGetInfinite: <T = any[]>(core: any, api: string | string[], opts: HttpStateOptionsProps<T>) => Promise<void>;
|
|
4
|
+
export declare const fetchNextPageAction: (core: any, queryKey: QueryKey) => Promise<void>;
|
|
5
|
+
export declare const executeMutation: <T = any>(core: any, api: string | string[], opts: HttpStateOptionsProps<T>, method: "POST" | "PATCH" | "DELETE") => Promise<T | undefined>;
|
|
6
|
+
export declare const refetchAction: (core: any, queryKey: QueryKey, api: string | string[], opts: HttpStateOptionsProps<any>) => Promise<void>;
|
|
7
|
+
export declare const fileAction: <T = any>(core: any, api: string | string[], opts: HttpStateOptionsProps<T>) => Promise<T | undefined>;
|
|
8
|
+
export declare const fileChunksAction: <T = any>(core: any, api: string | string[], opts: HttpStateOptionsProps<T>) => Promise<void>;
|
|
9
|
+
//# sourceMappingURL=http-actions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-actions.d.ts","sourceRoot":"","sources":["../../../src/core/http/http-actions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,QAAQ,EAAoB,MAAM,iBAAiB,CAAC;AAQpF,eAAO,MAAM,UAAU,GAAU,CAAC,GAAG,GAAG,EACvC,MAAM,GAAG,EACT,KAAK,MAAM,GAAG,MAAM,EAAE,EACtB,MAAM,qBAAqB,CAAC,CAAC,CAAC,2BAqD9B,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAU,CAAC,GAAG,GAAG,EAAE,EACjD,MAAM,GAAG,EACT,KAAK,MAAM,GAAG,MAAM,EAAE,EACtB,MAAM,qBAAqB,CAAC,CAAC,CAAC,kBA2C9B,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAU,MAAM,GAAG,EAAE,UAAU,QAAQ,kBA6BtE,CAAC;AAEF,eAAO,MAAM,eAAe,GAAU,CAAC,GAAG,GAAG,EAC5C,MAAM,GAAG,EACT,KAAK,MAAM,GAAG,MAAM,EAAE,EACtB,MAAM,qBAAqB,CAAC,CAAC,CAAC,EAC9B,QAAQ,MAAM,GAAG,OAAO,GAAG,QAAQ,2BA0CnC,CAAC;AAEF,eAAO,MAAM,aAAa,GAAU,MAAM,GAAG,EAAE,UAAU,QAAQ,EAAE,KAAK,MAAM,GAAG,MAAM,EAAE,EAAE,MAAM,qBAAqB,CAAC,GAAG,CAAC,kBAoB1H,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,CAAC,GAAG,GAAG,EAAE,MAAM,GAAG,EAAE,KAAK,MAAM,GAAG,MAAM,EAAE,EAAE,MAAM,qBAAqB,CAAC,CAAC,CAAC,2BAgB1G,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAU,CAAC,GAAG,GAAG,EAAE,MAAM,GAAG,EAAE,KAAK,MAAM,GAAG,MAAM,EAAE,EAAE,MAAM,qBAAqB,CAAC,CAAC,CAAC,kBAUhH,CAAC"}
|