@ametie/vue-muza-use 0.0.4 → 0.1.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 +40 -8
- package/dist/index.cjs +5 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +6 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -151,21 +151,52 @@ const { execute } = useApiPost('/auth/register', {
|
|
|
151
151
|
})
|
|
152
152
|
```
|
|
153
153
|
|
|
154
|
-
###
|
|
155
|
-
|
|
154
|
+
### 🔄 Auto-Refetching (Watch & Debounce)
|
|
155
|
+
You can automatically trigger the request whenever specific Refs change. This is perfect for **Live Search** or **Auto-Save** forms.
|
|
156
156
|
|
|
157
|
+
> **💡 Pro Tip:** When watching text inputs (like search), **always** use `debounce` to prevent flooding your server with requests on every keystroke.
|
|
158
|
+
|
|
159
|
+
#### Live Search
|
|
157
160
|
```typescript
|
|
158
161
|
const searchQuery = ref('')
|
|
162
|
+
// Use a computed URL so it updates when execute() is triggered
|
|
163
|
+
const url = computed(() => `/search?q=${searchQuery.value}`)
|
|
159
164
|
|
|
160
|
-
const { data,
|
|
165
|
+
const { data, loading } = useApi(url, {
|
|
161
166
|
method: 'GET',
|
|
162
|
-
|
|
163
|
-
|
|
167
|
+
watch: searchQuery, // 👀 Auto-execute when this ref changes
|
|
168
|
+
debounce: 500, // ⏳ Wait 500ms after user stops typing
|
|
169
|
+
abortPrevious: true // Kill previous request if it's still running
|
|
164
170
|
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### Filters (Immediate Refetch)
|
|
174
|
+
For inputs like Selects, Toggles, or Tabs where you want instant updates without delay. `watch` accepts an array of Refs.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
const category = ref('all')
|
|
178
|
+
const inStock = ref(true)
|
|
179
|
+
|
|
180
|
+
// URL updates reactively
|
|
181
|
+
const url = computed(() =>
|
|
182
|
+
`/products?cat=${category.value}&stock=${inStock.value}`
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
const { data } = useApi(url, {
|
|
186
|
+
watch: [category, inStock], // 👀 Re-fetch instantly when any filter changes
|
|
187
|
+
// default debounce is 0
|
|
188
|
+
})
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
#### Auto-Save Form
|
|
192
|
+
```typescript
|
|
193
|
+
const settings = ref({ theme: 'dark', notifications: true })
|
|
165
194
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
195
|
+
useApiPost('/user/settings', {
|
|
196
|
+
data: settings, // Passing ref: library automatically unwraps it
|
|
197
|
+
watch: settings, // Watch the form for changes (deep watch supported)
|
|
198
|
+
debounce: 1000, // Save after 1 second of inactivity
|
|
199
|
+
onSuccess: () => console.log('Settings saved!')
|
|
169
200
|
})
|
|
170
201
|
```
|
|
171
202
|
|
|
@@ -209,6 +240,7 @@ The main composable.
|
|
|
209
240
|
| `immediate` | `boolean` | `false` | Trigger request automatically on creation. |
|
|
210
241
|
| `retry` | `boolean \| number` | `false` | Number of retries on failure. |
|
|
211
242
|
| `debounce` | `number` | `0` | Debounce time in ms. |
|
|
243
|
+
| `watch` | `WatchSource \| WatchSource[]` | `undefined` | Ref(s) to watch for auto-execution. |
|
|
212
244
|
| `authMode` | `'default' \| 'public'` | `'default'` | `'public'` skips token injection. |
|
|
213
245
|
| `initialData` | `T` | `null` | Initial value for `data` ref. |
|
|
214
246
|
| `onSuccess` | `(res) => void` | - | Callback on 2xx response. |
|
package/dist/index.cjs
CHANGED
|
@@ -273,6 +273,11 @@ function useApi(url, options = {}) {
|
|
|
273
273
|
state.reset();
|
|
274
274
|
state.setLoading(false);
|
|
275
275
|
};
|
|
276
|
+
if (options.watch) {
|
|
277
|
+
(0, import_vue4.watch)(options.watch, () => {
|
|
278
|
+
execute();
|
|
279
|
+
}, { deep: true });
|
|
280
|
+
}
|
|
276
281
|
if ((0, import_vue4.getCurrentScope)()) {
|
|
277
282
|
(0, import_vue4.onScopeDispose)(() => abort("Scope disposed"));
|
|
278
283
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
|
-
import { MaybeRefOrGetter, Ref, App } from 'vue';
|
|
2
|
+
import { MaybeRefOrGetter, WatchSource, Ref, App } from 'vue';
|
|
3
3
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse, CreateAxiosDefaults } from 'axios';
|
|
4
4
|
|
|
5
5
|
interface ApiError {
|
|
@@ -33,6 +33,7 @@ interface UseApiOptions<T = unknown, D = unknown> extends ApiRequestConfig<D> {
|
|
|
33
33
|
debounce?: number;
|
|
34
34
|
useGlobalAbort?: boolean;
|
|
35
35
|
initialLoading?: boolean;
|
|
36
|
+
watch?: WatchSource | WatchSource[];
|
|
36
37
|
}
|
|
37
38
|
interface UseApiReturn<T = unknown, D = unknown> {
|
|
38
39
|
data: Ref<T | null>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
|
-
import { MaybeRefOrGetter, Ref, App } from 'vue';
|
|
2
|
+
import { MaybeRefOrGetter, WatchSource, Ref, App } from 'vue';
|
|
3
3
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse, CreateAxiosDefaults } from 'axios';
|
|
4
4
|
|
|
5
5
|
interface ApiError {
|
|
@@ -33,6 +33,7 @@ interface UseApiOptions<T = unknown, D = unknown> extends ApiRequestConfig<D> {
|
|
|
33
33
|
debounce?: number;
|
|
34
34
|
useGlobalAbort?: boolean;
|
|
35
35
|
initialLoading?: boolean;
|
|
36
|
+
watch?: WatchSource | WatchSource[];
|
|
36
37
|
}
|
|
37
38
|
interface UseApiReturn<T = unknown, D = unknown> {
|
|
38
39
|
data: Ref<T | null>;
|
package/dist/index.mjs
CHANGED
|
@@ -34,7 +34,7 @@ function debounceFn(fn, delay) {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
// src/useApi.ts
|
|
37
|
-
import { ref as ref3, getCurrentScope, onScopeDispose, toValue } from "vue";
|
|
37
|
+
import { ref as ref3, getCurrentScope, onScopeDispose, toValue, watch } from "vue";
|
|
38
38
|
|
|
39
39
|
// src/utils/errorParser.ts
|
|
40
40
|
function parseApiError(error) {
|
|
@@ -223,6 +223,11 @@ function useApi(url, options = {}) {
|
|
|
223
223
|
state.reset();
|
|
224
224
|
state.setLoading(false);
|
|
225
225
|
};
|
|
226
|
+
if (options.watch) {
|
|
227
|
+
watch(options.watch, () => {
|
|
228
|
+
execute();
|
|
229
|
+
}, { deep: true });
|
|
230
|
+
}
|
|
226
231
|
if (getCurrentScope()) {
|
|
227
232
|
onScopeDispose(() => abort("Scope disposed"));
|
|
228
233
|
}
|