@ametie/vue-muza-use 0.1.0 → 0.2.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/README.md +49 -3
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +2 -2
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -218,6 +218,55 @@ const { abortAll } = useAbortController()
|
|
|
218
218
|
|
|
219
219
|
---
|
|
220
220
|
|
|
221
|
+
## 🚨 Advanced Error Handling
|
|
222
|
+
|
|
223
|
+
By default, the library attempts to normalize errors into a standard `ApiError` format. However, every backend is different. You can fully customize how errors are parsed globally.
|
|
224
|
+
|
|
225
|
+
### Default Behavior
|
|
226
|
+
If you don't provide a parser, we extract the message from `error.response.data.message` or `error.message`.
|
|
227
|
+
|
|
228
|
+
### Custom Error Parser
|
|
229
|
+
Inject your own logic to transform your backend's specific error format into our uniform structure.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
// main.ts
|
|
233
|
+
app.use(createApi({
|
|
234
|
+
axios: api,
|
|
235
|
+
// 👇 Define how to parse errors from your specific API
|
|
236
|
+
errorParser: (error: any) => {
|
|
237
|
+
const data = error.response?.data
|
|
238
|
+
|
|
239
|
+
// Example: Laravel/Rails style validation errors
|
|
240
|
+
if (data?.errors) {
|
|
241
|
+
return {
|
|
242
|
+
message: 'Validation Failed',
|
|
243
|
+
status: error.response.status,
|
|
244
|
+
code: 'VALIDATION_ERROR',
|
|
245
|
+
errors: data.errors // { email: ['Invalid email'] }
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Example: Custom wrap format { success: false, error: { msg: "..." } }
|
|
250
|
+
if (data?.error?.msg) {
|
|
251
|
+
return {
|
|
252
|
+
message: data.error.msg,
|
|
253
|
+
status: error.response.status,
|
|
254
|
+
code: data.error.code
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Fallback to default behavior
|
|
259
|
+
return {
|
|
260
|
+
message: error.message || 'Unknown error',
|
|
261
|
+
status: error.response?.status || 500,
|
|
262
|
+
details: error
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}))
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
221
270
|
## 📚 API Reference
|
|
222
271
|
|
|
223
272
|
### `useApi<T, D>(url, options)`
|
|
@@ -298,6 +347,3 @@ This happens transparently to your components. They just "wait" a bit longer for
|
|
|
298
347
|
|
|
299
348
|
---
|
|
300
349
|
|
|
301
|
-
## License
|
|
302
|
-
|
|
303
|
-
MIT © [Ametie](https://github.com/ametie)
|
package/dist/index.cjs
CHANGED
|
@@ -182,7 +182,7 @@ function useAbortController() {
|
|
|
182
182
|
|
|
183
183
|
// src/useApi.ts
|
|
184
184
|
function useApi(url, options = {}) {
|
|
185
|
-
const { axios: axios2, onError: globalErrorHandler, globalOptions } = useApiConfig();
|
|
185
|
+
const { axios: axios2, onError: globalErrorHandler, globalOptions, errorParser } = useApiConfig();
|
|
186
186
|
const {
|
|
187
187
|
method = "GET",
|
|
188
188
|
immediate = false,
|
|
@@ -247,7 +247,7 @@ function useApi(url, options = {}) {
|
|
|
247
247
|
wasCancelled = true;
|
|
248
248
|
return null;
|
|
249
249
|
}
|
|
250
|
-
const apiError = parseApiError(err);
|
|
250
|
+
const apiError = errorParser ? errorParser(err) : parseApiError(err);
|
|
251
251
|
if (!skipErrorNotification && globalErrorHandler) {
|
|
252
252
|
globalErrorHandler(apiError, err);
|
|
253
253
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -48,6 +48,11 @@ interface UseApiReturn<T = unknown, D = unknown> {
|
|
|
48
48
|
interface ApiPluginOptions {
|
|
49
49
|
axios: AxiosInstance;
|
|
50
50
|
onError?: (error: ApiError, originalError: any) => void;
|
|
51
|
+
/**
|
|
52
|
+
* Custom error parser to transform backend errors into ApiError format.
|
|
53
|
+
* Useful if your backend has a different error structure.
|
|
54
|
+
*/
|
|
55
|
+
errorParser?: (error: unknown) => ApiError;
|
|
51
56
|
globalOptions?: {
|
|
52
57
|
retry?: number | boolean;
|
|
53
58
|
retryDelay?: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -48,6 +48,11 @@ interface UseApiReturn<T = unknown, D = unknown> {
|
|
|
48
48
|
interface ApiPluginOptions {
|
|
49
49
|
axios: AxiosInstance;
|
|
50
50
|
onError?: (error: ApiError, originalError: any) => void;
|
|
51
|
+
/**
|
|
52
|
+
* Custom error parser to transform backend errors into ApiError format.
|
|
53
|
+
* Useful if your backend has a different error structure.
|
|
54
|
+
*/
|
|
55
|
+
errorParser?: (error: unknown) => ApiError;
|
|
51
56
|
globalOptions?: {
|
|
52
57
|
retry?: number | boolean;
|
|
53
58
|
retryDelay?: number;
|
package/dist/index.mjs
CHANGED
|
@@ -132,7 +132,7 @@ function useAbortController() {
|
|
|
132
132
|
|
|
133
133
|
// src/useApi.ts
|
|
134
134
|
function useApi(url, options = {}) {
|
|
135
|
-
const { axios: axios2, onError: globalErrorHandler, globalOptions } = useApiConfig();
|
|
135
|
+
const { axios: axios2, onError: globalErrorHandler, globalOptions, errorParser } = useApiConfig();
|
|
136
136
|
const {
|
|
137
137
|
method = "GET",
|
|
138
138
|
immediate = false,
|
|
@@ -197,7 +197,7 @@ function useApi(url, options = {}) {
|
|
|
197
197
|
wasCancelled = true;
|
|
198
198
|
return null;
|
|
199
199
|
}
|
|
200
|
-
const apiError = parseApiError(err);
|
|
200
|
+
const apiError = errorParser ? errorParser(err) : parseApiError(err);
|
|
201
201
|
if (!skipErrorNotification && globalErrorHandler) {
|
|
202
202
|
globalErrorHandler(apiError, err);
|
|
203
203
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ametie/vue-muza-use",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Powerful Vue 3 API composable (Muza Kit) with Axios, Auto-Refresh & TypeScript",
|
|
5
5
|
"author": "MortyQ",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
"request",
|
|
23
23
|
"auth",
|
|
24
24
|
"refresh",
|
|
25
|
-
"muza"
|
|
25
|
+
"muza",
|
|
26
|
+
"rest",
|
|
27
|
+
"crud"
|
|
26
28
|
],
|
|
27
29
|
"type": "module",
|
|
28
30
|
"types": "./dist/index.d.ts",
|