@ametie/vue-muza-use 0.6.0 → 0.6.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 +20 -20
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +11 -11
- package/dist/index.d.ts +11 -11
- package/dist/index.mjs +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -378,9 +378,9 @@ const { execute } = useApi('/analytics', {
|
|
|
378
378
|
|
|
379
379
|
### Manual Data Updates
|
|
380
380
|
|
|
381
|
-
Use `
|
|
381
|
+
Use `mutate` to manually update the data ref. Supports direct values or updater functions (like React's `setState`).
|
|
382
382
|
|
|
383
|
-
> 🎓 **When to use `
|
|
383
|
+
> 🎓 **When to use `mutate`:**
|
|
384
384
|
> ✅ Adding/removing/updating items in arrays
|
|
385
385
|
> ✅ Local sorting/filtering (without refetching)
|
|
386
386
|
> ✅ Transform data in `onSuccess` (adding computed fields)
|
|
@@ -392,21 +392,21 @@ Use `setData` to manually update the data ref. Supports direct values or updater
|
|
|
392
392
|
|
|
393
393
|
#### Add/Remove/Update Items
|
|
394
394
|
```typescript
|
|
395
|
-
const { data,
|
|
395
|
+
const { data, mutate } = useApi<Todo[]>('/todos', { immediate: true })
|
|
396
396
|
|
|
397
397
|
// Add item
|
|
398
398
|
const addTodo = (newTodo: Todo) => {
|
|
399
|
-
|
|
399
|
+
mutate(prev => prev ? [...prev, newTodo] : [newTodo])
|
|
400
400
|
}
|
|
401
401
|
|
|
402
402
|
// Remove item
|
|
403
403
|
const removeTodo = (id: number) => {
|
|
404
|
-
|
|
404
|
+
mutate(prev => prev?.filter(t => t.id !== id) ?? null)
|
|
405
405
|
}
|
|
406
406
|
|
|
407
407
|
// Update item
|
|
408
408
|
const updateTodo = (id: number, updates: Partial<Todo>) => {
|
|
409
|
-
|
|
409
|
+
mutate(prev =>
|
|
410
410
|
prev?.map(t => t.id === id ? { ...t, ...updates } : t) ?? null
|
|
411
411
|
)
|
|
412
412
|
}
|
|
@@ -414,14 +414,14 @@ const updateTodo = (id: number, updates: Partial<Todo>) => {
|
|
|
414
414
|
|
|
415
415
|
#### Sort/Filter Locally
|
|
416
416
|
```typescript
|
|
417
|
-
const { data,
|
|
417
|
+
const { data, mutate } = useApi<Product[]>('/products', { immediate: true })
|
|
418
418
|
|
|
419
419
|
const sortByPrice = () => {
|
|
420
|
-
|
|
420
|
+
mutate(prev => prev ? [...prev].sort((a, b) => a.price - b.price) : null)
|
|
421
421
|
}
|
|
422
422
|
|
|
423
423
|
const filterActive = () => {
|
|
424
|
-
|
|
424
|
+
mutate(prev => prev?.filter(p => p.active) ?? null)
|
|
425
425
|
}
|
|
426
426
|
|
|
427
427
|
// Reset to original
|
|
@@ -430,7 +430,7 @@ const resetFilters = () => execute()
|
|
|
430
430
|
|
|
431
431
|
#### Transform in `onSuccess`
|
|
432
432
|
|
|
433
|
-
Use `
|
|
433
|
+
Use `mutate` in `onSuccess` to transform data right after fetching. Two approaches:
|
|
434
434
|
|
|
435
435
|
**Approach 1: Same type (recommended)**
|
|
436
436
|
```typescript
|
|
@@ -441,11 +441,11 @@ interface User {
|
|
|
441
441
|
fullName?: string // Optional field
|
|
442
442
|
}
|
|
443
443
|
|
|
444
|
-
const { data,
|
|
444
|
+
const { data, mutate } = useApi<User[]>('/users', {
|
|
445
445
|
immediate: true,
|
|
446
446
|
onSuccess: ({ data: users }) => {
|
|
447
447
|
// Add computed field - still User[] type
|
|
448
|
-
|
|
448
|
+
mutate(users.map(u => ({
|
|
449
449
|
...u,
|
|
450
450
|
fullName: `${u.firstName} ${u.lastName}`
|
|
451
451
|
})))
|
|
@@ -473,7 +473,7 @@ const users = computed(() =>
|
|
|
473
473
|
```
|
|
474
474
|
|
|
475
475
|
> 💡 **Rule of thumb:**
|
|
476
|
-
> - ✅ **Use `
|
|
476
|
+
> - ✅ **Use `mutate` in `onSuccess`** if you're adding/modifying fields but keeping the same base type
|
|
477
477
|
> - ✅ **Use `computed`** if you're completely changing the data structure (e.g., snake_case → camelCase)
|
|
478
478
|
|
|
479
479
|
---
|
|
@@ -1089,7 +1089,7 @@ The main composable for making HTTP requests.
|
|
|
1089
1089
|
|
|
1090
1090
|
// Methods
|
|
1091
1091
|
execute: (config?: AxiosRequestConfig) => Promise<T | null>
|
|
1092
|
-
|
|
1092
|
+
mutate: (data: T | null | ((prev: T | null) => T | null)) => void
|
|
1093
1093
|
abort: (reason?: string) => void
|
|
1094
1094
|
reset: () => void
|
|
1095
1095
|
}
|
|
@@ -1108,23 +1108,23 @@ await execute()
|
|
|
1108
1108
|
await execute({ params: { page: 2 } })
|
|
1109
1109
|
```
|
|
1110
1110
|
|
|
1111
|
-
#### `
|
|
1111
|
+
#### `mutate(newData)`
|
|
1112
1112
|
Manually update the `data` ref. Supports direct values or updater functions:
|
|
1113
1113
|
|
|
1114
1114
|
```typescript
|
|
1115
|
-
const { data,
|
|
1115
|
+
const { data, mutate } = useApi<User[]>('/users')
|
|
1116
1116
|
|
|
1117
1117
|
// Direct value
|
|
1118
|
-
|
|
1118
|
+
mutate([{ id: 1, name: 'John' }])
|
|
1119
1119
|
|
|
1120
1120
|
// Updater function (like React's setState)
|
|
1121
|
-
|
|
1121
|
+
mutate(prev => prev ? [...prev, newUser] : [newUser])
|
|
1122
1122
|
|
|
1123
1123
|
// Remove item
|
|
1124
|
-
|
|
1124
|
+
mutate(prev => prev?.filter(u => u.id !== userId) ?? null)
|
|
1125
1125
|
```
|
|
1126
1126
|
|
|
1127
|
-
> **Note:** `
|
|
1127
|
+
> **Note:** `mutate` automatically clears any existing error.
|
|
1128
1128
|
|
|
1129
1129
|
#### `abort(reason?)`
|
|
1130
1130
|
Cancel the current request:
|
package/dist/index.cjs
CHANGED
|
@@ -127,7 +127,7 @@ function useApiState(initialData = null, options = {}) {
|
|
|
127
127
|
const error = (0, import_vue2.ref)(null);
|
|
128
128
|
const statusCode = (0, import_vue2.ref)(null);
|
|
129
129
|
const response = (0, import_vue2.ref)(null);
|
|
130
|
-
const
|
|
130
|
+
const mutate = (newDataOrUpdater, fullResponse) => {
|
|
131
131
|
if (typeof newDataOrUpdater === "function") {
|
|
132
132
|
const updater = newDataOrUpdater;
|
|
133
133
|
data.value = updater(data.value);
|
|
@@ -161,7 +161,7 @@ function useApiState(initialData = null, options = {}) {
|
|
|
161
161
|
error,
|
|
162
162
|
statusCode,
|
|
163
163
|
response,
|
|
164
|
-
|
|
164
|
+
mutate,
|
|
165
165
|
setError,
|
|
166
166
|
setLoading,
|
|
167
167
|
setStatusCode,
|
|
@@ -273,7 +273,7 @@ function useApi(url, options = {}) {
|
|
|
273
273
|
signal: controller.signal,
|
|
274
274
|
...{ authMode: config?.authMode || authMode }
|
|
275
275
|
});
|
|
276
|
-
state.
|
|
276
|
+
state.mutate(response.data, response);
|
|
277
277
|
state.setStatusCode(response.status);
|
|
278
278
|
onSuccess?.(response);
|
|
279
279
|
return response.data;
|
package/dist/index.d.cts
CHANGED
|
@@ -56,24 +56,24 @@ interface UseApiReturn<T = unknown, D = unknown> {
|
|
|
56
56
|
abort: (message?: string) => void;
|
|
57
57
|
reset: () => void;
|
|
58
58
|
/**
|
|
59
|
-
* Manually
|
|
59
|
+
* Manually mutate data. Supports direct value or updater function.
|
|
60
60
|
* Clears any existing error when called.
|
|
61
61
|
*
|
|
62
62
|
* @example
|
|
63
63
|
* // Direct value
|
|
64
|
-
*
|
|
64
|
+
* mutate(newUsers)
|
|
65
65
|
*
|
|
66
66
|
* // Updater function (like React's setState)
|
|
67
|
-
*
|
|
67
|
+
* mutate(prev => prev?.filter(u => u.active) ?? null)
|
|
68
68
|
*
|
|
69
69
|
* // Transform data after fetch
|
|
70
|
-
* const { data,
|
|
70
|
+
* const { data, mutate } = useApi('/users', {
|
|
71
71
|
* onSuccess: ({ data }) => {
|
|
72
|
-
*
|
|
72
|
+
* mutate(data.map(user => ({ ...user, fullName: `${user.first} ${user.last}` })))
|
|
73
73
|
* }
|
|
74
74
|
* })
|
|
75
75
|
*/
|
|
76
|
-
|
|
76
|
+
mutate: (newData: T | null | ((prev: T | null) => T | null)) => void;
|
|
77
77
|
}
|
|
78
78
|
interface ApiPluginOptions {
|
|
79
79
|
axios: AxiosInstance;
|
|
@@ -304,7 +304,7 @@ declare function useApiBatch<T = unknown>(urls: MaybeRefOrGetter<string[]>, opti
|
|
|
304
304
|
* - !data && !error && !loading → idle
|
|
305
305
|
*/
|
|
306
306
|
|
|
307
|
-
/** Input type for
|
|
307
|
+
/** Input type for mutate - supports direct value or updater function */
|
|
308
308
|
type SetDataInput<T> = T | null | ((prev: T | null) => T | null);
|
|
309
309
|
interface UseApiStateReturn<T = unknown> {
|
|
310
310
|
/** Response data */
|
|
@@ -318,15 +318,15 @@ interface UseApiStateReturn<T = unknown> {
|
|
|
318
318
|
/** Full Axios response - includes headers, status, config, etc (optional, for advanced use cases) */
|
|
319
319
|
response: Ref<AxiosResponse<T> | null>;
|
|
320
320
|
/**
|
|
321
|
-
*
|
|
321
|
+
* Mutate data and clear error. Supports direct value or updater function.
|
|
322
322
|
* @example
|
|
323
323
|
* // Direct value
|
|
324
|
-
*
|
|
324
|
+
* mutate(newUsers)
|
|
325
325
|
*
|
|
326
326
|
* // Updater function (like React's setState)
|
|
327
|
-
*
|
|
327
|
+
* mutate(prev => prev?.filter(u => u.active) ?? null)
|
|
328
328
|
*/
|
|
329
|
-
|
|
329
|
+
mutate: (newData: SetDataInput<T>, fullResponse?: AxiosResponse<T> | null) => void;
|
|
330
330
|
/** Set error */
|
|
331
331
|
setError: (newError: ApiError | null) => void;
|
|
332
332
|
/** Set loading state */
|
package/dist/index.d.ts
CHANGED
|
@@ -56,24 +56,24 @@ interface UseApiReturn<T = unknown, D = unknown> {
|
|
|
56
56
|
abort: (message?: string) => void;
|
|
57
57
|
reset: () => void;
|
|
58
58
|
/**
|
|
59
|
-
* Manually
|
|
59
|
+
* Manually mutate data. Supports direct value or updater function.
|
|
60
60
|
* Clears any existing error when called.
|
|
61
61
|
*
|
|
62
62
|
* @example
|
|
63
63
|
* // Direct value
|
|
64
|
-
*
|
|
64
|
+
* mutate(newUsers)
|
|
65
65
|
*
|
|
66
66
|
* // Updater function (like React's setState)
|
|
67
|
-
*
|
|
67
|
+
* mutate(prev => prev?.filter(u => u.active) ?? null)
|
|
68
68
|
*
|
|
69
69
|
* // Transform data after fetch
|
|
70
|
-
* const { data,
|
|
70
|
+
* const { data, mutate } = useApi('/users', {
|
|
71
71
|
* onSuccess: ({ data }) => {
|
|
72
|
-
*
|
|
72
|
+
* mutate(data.map(user => ({ ...user, fullName: `${user.first} ${user.last}` })))
|
|
73
73
|
* }
|
|
74
74
|
* })
|
|
75
75
|
*/
|
|
76
|
-
|
|
76
|
+
mutate: (newData: T | null | ((prev: T | null) => T | null)) => void;
|
|
77
77
|
}
|
|
78
78
|
interface ApiPluginOptions {
|
|
79
79
|
axios: AxiosInstance;
|
|
@@ -304,7 +304,7 @@ declare function useApiBatch<T = unknown>(urls: MaybeRefOrGetter<string[]>, opti
|
|
|
304
304
|
* - !data && !error && !loading → idle
|
|
305
305
|
*/
|
|
306
306
|
|
|
307
|
-
/** Input type for
|
|
307
|
+
/** Input type for mutate - supports direct value or updater function */
|
|
308
308
|
type SetDataInput<T> = T | null | ((prev: T | null) => T | null);
|
|
309
309
|
interface UseApiStateReturn<T = unknown> {
|
|
310
310
|
/** Response data */
|
|
@@ -318,15 +318,15 @@ interface UseApiStateReturn<T = unknown> {
|
|
|
318
318
|
/** Full Axios response - includes headers, status, config, etc (optional, for advanced use cases) */
|
|
319
319
|
response: Ref<AxiosResponse<T> | null>;
|
|
320
320
|
/**
|
|
321
|
-
*
|
|
321
|
+
* Mutate data and clear error. Supports direct value or updater function.
|
|
322
322
|
* @example
|
|
323
323
|
* // Direct value
|
|
324
|
-
*
|
|
324
|
+
* mutate(newUsers)
|
|
325
325
|
*
|
|
326
326
|
* // Updater function (like React's setState)
|
|
327
|
-
*
|
|
327
|
+
* mutate(prev => prev?.filter(u => u.active) ?? null)
|
|
328
328
|
*/
|
|
329
|
-
|
|
329
|
+
mutate: (newData: SetDataInput<T>, fullResponse?: AxiosResponse<T> | null) => void;
|
|
330
330
|
/** Set error */
|
|
331
331
|
setError: (newError: ApiError | null) => void;
|
|
332
332
|
/** Set loading state */
|
package/dist/index.mjs
CHANGED
|
@@ -76,7 +76,7 @@ function useApiState(initialData = null, options = {}) {
|
|
|
76
76
|
const error = ref(null);
|
|
77
77
|
const statusCode = ref(null);
|
|
78
78
|
const response = ref(null);
|
|
79
|
-
const
|
|
79
|
+
const mutate = (newDataOrUpdater, fullResponse) => {
|
|
80
80
|
if (typeof newDataOrUpdater === "function") {
|
|
81
81
|
const updater = newDataOrUpdater;
|
|
82
82
|
data.value = updater(data.value);
|
|
@@ -110,7 +110,7 @@ function useApiState(initialData = null, options = {}) {
|
|
|
110
110
|
error,
|
|
111
111
|
statusCode,
|
|
112
112
|
response,
|
|
113
|
-
|
|
113
|
+
mutate,
|
|
114
114
|
setError,
|
|
115
115
|
setLoading,
|
|
116
116
|
setStatusCode,
|
|
@@ -222,7 +222,7 @@ function useApi(url, options = {}) {
|
|
|
222
222
|
signal: controller.signal,
|
|
223
223
|
...{ authMode: config?.authMode || authMode }
|
|
224
224
|
});
|
|
225
|
-
state.
|
|
225
|
+
state.mutate(response.data, response);
|
|
226
226
|
state.setStatusCode(response.status);
|
|
227
227
|
onSuccess?.(response);
|
|
228
228
|
return response.data;
|