vue-api-kit 1.10.4 → 1.10.6
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/dist/core/types.d.ts +8 -11
- package/dist/index.js +157 -159
- package/package.json +1 -1
package/dist/core/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosError, InternalAxiosRequestConfig } from 'axios';
|
|
2
2
|
import { Ref } from 'vue';
|
|
3
3
|
import { ZodError, ZodType } from 'zod';
|
|
4
|
-
import { $
|
|
4
|
+
import { $ZodFlattenedError } from 'zod/v4/core';
|
|
5
5
|
/**
|
|
6
6
|
* HTTP methods supported by the API client
|
|
7
7
|
* @example
|
|
@@ -129,14 +129,11 @@ export interface ApiClientOptions<Q extends Record<string, NestedStructure<ApiQu
|
|
|
129
129
|
onBeforeRequest?: (config: InternalAxiosRequestConfig<any>) => Promise<any> | void | any;
|
|
130
130
|
onStartRequest?: () => Promise<void> | void | any;
|
|
131
131
|
onFinishRequest?: () => Promise<void> | void | any;
|
|
132
|
-
|
|
132
|
+
onError?: (error: {
|
|
133
|
+
err: AxiosError | ZodError | Error;
|
|
133
134
|
message: string;
|
|
134
|
-
status?: number;
|
|
135
|
-
code?: string;
|
|
136
|
-
data?: any;
|
|
137
|
-
url?: string;
|
|
138
135
|
}) => void;
|
|
139
|
-
onZodError?: (
|
|
136
|
+
onZodError?: (zodError: $ZodFlattenedError<any, any>) => void;
|
|
140
137
|
}
|
|
141
138
|
/**
|
|
142
139
|
* Options for configuring a query hook
|
|
@@ -165,7 +162,7 @@ export interface UseQueryOptions<TParams = any, TData = any, TResult = any> {
|
|
|
165
162
|
debounce?: number;
|
|
166
163
|
onResult?: (result: TResult) => void;
|
|
167
164
|
onError?: (error: AxiosError | ZodError | Error) => void;
|
|
168
|
-
onZodError?: (
|
|
165
|
+
onZodError?: (zodError: $ZodFlattenedError<any, any>) => void;
|
|
169
166
|
onBeforeRequest?: (config: InternalAxiosRequestConfig<any>) => Promise<any> | void | any;
|
|
170
167
|
}
|
|
171
168
|
/**
|
|
@@ -181,7 +178,7 @@ export interface UseQueryOptions<TParams = any, TData = any, TResult = any> {
|
|
|
181
178
|
export interface UseMutationOptions<TResult = any> {
|
|
182
179
|
onResult?: (result: TResult) => void;
|
|
183
180
|
onError?: (error: AxiosError | ZodError | Error) => void;
|
|
184
|
-
onZodError?: (
|
|
181
|
+
onZodError?: (zodError: $ZodFlattenedError<any, any>) => void;
|
|
185
182
|
onUploadProgress?: (progress: number) => void;
|
|
186
183
|
onBeforeRequest?: (config: InternalAxiosRequestConfig<any>) => Promise<any> | void | any;
|
|
187
184
|
}
|
|
@@ -199,7 +196,7 @@ export interface UseMutationOptions<TResult = any> {
|
|
|
199
196
|
export interface QueryResult<TResult> {
|
|
200
197
|
result: Ref<TResult | undefined>;
|
|
201
198
|
errorMessage: Ref<string | undefined>;
|
|
202
|
-
|
|
199
|
+
zodError: Ref<$ZodFlattenedError<any, any> | undefined>;
|
|
203
200
|
isLoading: Ref<boolean>;
|
|
204
201
|
isDone: Ref<boolean>;
|
|
205
202
|
refetch: () => Promise<void>;
|
|
@@ -219,7 +216,7 @@ export interface QueryResult<TResult> {
|
|
|
219
216
|
export interface MutationResult<TResult, TData = any, TParams = any> {
|
|
220
217
|
result: Ref<TResult | undefined>;
|
|
221
218
|
errorMessage: Ref<string | undefined>;
|
|
222
|
-
|
|
219
|
+
zodError: Ref<$ZodFlattenedError<any, any> | undefined>;
|
|
223
220
|
isLoading: Ref<boolean>;
|
|
224
221
|
isDone: Ref<boolean>;
|
|
225
222
|
uploadProgress: Ref<number>;
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { ZodError as
|
|
2
|
-
import * as
|
|
3
|
-
import
|
|
1
|
+
import A, { ZodError as F } from "zod";
|
|
2
|
+
import * as G from "zod";
|
|
3
|
+
import U, { AxiosError as Z } from "axios";
|
|
4
4
|
import { AxiosError as ae } from "axios";
|
|
5
|
-
import { nextTick as
|
|
6
|
-
import { debounce as
|
|
7
|
-
function
|
|
5
|
+
import { nextTick as _, ref as h, onMounted as N, watch as Q, onBeforeUnmount as z } from "vue";
|
|
6
|
+
import { debounce as J } from "lodash-es";
|
|
7
|
+
function H(e) {
|
|
8
8
|
return e && typeof e == "object" && e !== null && typeof e.path == "string";
|
|
9
9
|
}
|
|
10
|
-
function
|
|
10
|
+
function W(e) {
|
|
11
11
|
return e && typeof e == "object" && e !== null && typeof e.path == "string" && typeof e.method == "string";
|
|
12
12
|
}
|
|
13
|
-
function
|
|
14
|
-
const
|
|
13
|
+
function Y(e) {
|
|
14
|
+
const p = U.create({
|
|
15
15
|
baseURL: e.baseURL,
|
|
16
16
|
headers: {
|
|
17
17
|
"Content-Type": "application/json",
|
|
@@ -21,234 +21,232 @@ function K(e) {
|
|
|
21
21
|
withCredentials: e.withCredentials ?? !1,
|
|
22
22
|
withXSRFToken: e.withXSRFToken ?? !1
|
|
23
23
|
});
|
|
24
|
-
let
|
|
25
|
-
e.onBeforeRequest &&
|
|
24
|
+
let B = !1, w = null;
|
|
25
|
+
e.onBeforeRequest && p.interceptors.request.use(
|
|
26
26
|
async (r) => {
|
|
27
27
|
try {
|
|
28
28
|
return await e.onBeforeRequest(r) || r;
|
|
29
|
-
} catch (
|
|
30
|
-
return Promise.reject(
|
|
29
|
+
} catch (n) {
|
|
30
|
+
return Promise.reject(n);
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
(r) => Promise.reject(r)
|
|
34
|
-
), e.onStartRequest &&
|
|
34
|
+
), e.onStartRequest && p.interceptors.request.use(
|
|
35
35
|
async (r) => {
|
|
36
36
|
try {
|
|
37
37
|
return await e.onStartRequest(), r;
|
|
38
|
-
} catch (
|
|
39
|
-
return Promise.reject(
|
|
38
|
+
} catch (n) {
|
|
39
|
+
return Promise.reject(n);
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
42
|
(r) => Promise.reject(r)
|
|
43
|
-
), e.onFinishRequest &&
|
|
43
|
+
), e.onFinishRequest && p.interceptors.response.use(
|
|
44
44
|
(r) => (e.onFinishRequest(), r),
|
|
45
45
|
(r) => (e.onFinishRequest(), Promise.reject(r))
|
|
46
|
-
),
|
|
46
|
+
), p.interceptors.request.use((r) => {
|
|
47
47
|
if (!r.url) return r;
|
|
48
|
-
const
|
|
48
|
+
const n = (m) => {
|
|
49
49
|
if (m)
|
|
50
|
-
for (const [
|
|
51
|
-
const
|
|
52
|
-
r.url.includes(
|
|
53
|
-
|
|
54
|
-
encodeURIComponent(String(
|
|
55
|
-
), delete m[
|
|
50
|
+
for (const [c, s] of Object.entries(m)) {
|
|
51
|
+
const o = `{${c}}`;
|
|
52
|
+
r.url.includes(o) && (r.url = r.url.replace(
|
|
53
|
+
o,
|
|
54
|
+
encodeURIComponent(String(s))
|
|
55
|
+
), delete m[c]);
|
|
56
56
|
}
|
|
57
57
|
};
|
|
58
|
-
return r.method !== "get" && r.data?.params &&
|
|
59
|
-
}), e.csrfRefreshEndpoint &&
|
|
58
|
+
return r.method !== "get" && r.data?.params && n(r.data.params), n(r.params), r;
|
|
59
|
+
}), e.csrfRefreshEndpoint && p.interceptors.response.use(
|
|
60
60
|
(r) => r,
|
|
61
61
|
async (r) => {
|
|
62
|
-
const
|
|
63
|
-
if (
|
|
62
|
+
const n = r.config;
|
|
63
|
+
if (n?.url === e.csrfRefreshEndpoint)
|
|
64
64
|
return Promise.reject(r);
|
|
65
|
-
if (r.response && (r.response.status === 403 || r.response.status === 419) && !
|
|
66
|
-
|
|
65
|
+
if (r.response && (r.response.status === 403 || r.response.status === 419) && !n?._retry) {
|
|
66
|
+
n._retry = !0;
|
|
67
67
|
try {
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
}), await
|
|
68
|
+
return B && w ? await w : (B = !0, w = p.get(e.csrfRefreshEndpoint).then(() => {
|
|
69
|
+
B = !1, w = null;
|
|
70
|
+
}), await w), p.request(n);
|
|
71
71
|
} catch (m) {
|
|
72
|
-
return
|
|
72
|
+
return B = !1, w = null, Promise.reject(m);
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
return Promise.reject(r);
|
|
76
76
|
}
|
|
77
|
-
),
|
|
77
|
+
), p.interceptors.response.use(
|
|
78
78
|
(r) => r,
|
|
79
|
-
(r) => (
|
|
79
|
+
(r) => (_(() => {
|
|
80
80
|
r.code;
|
|
81
81
|
}), Promise.reject(r))
|
|
82
82
|
);
|
|
83
83
|
function D(r) {
|
|
84
|
-
const
|
|
84
|
+
const n = {};
|
|
85
85
|
for (const m in r) {
|
|
86
|
-
const
|
|
87
|
-
if (
|
|
88
|
-
if (
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
let
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
let
|
|
95
|
-
const
|
|
96
|
-
|
|
86
|
+
const c = r[m];
|
|
87
|
+
if (c)
|
|
88
|
+
if (H(c)) {
|
|
89
|
+
const s = c;
|
|
90
|
+
n[m] = (o) => {
|
|
91
|
+
let a;
|
|
92
|
+
o && typeof o == "object" && ("loadOnMount" in o || "debounce" in o || "onResult" in o || "onError" in o || "onZodError" in o || "onBeforeRequest" in o || "params" in o || "data" in o ? a = o : a = { params: o });
|
|
93
|
+
const g = h(), v = h(), E = h(), C = h(!1), j = h(!1), k = h(!0);
|
|
94
|
+
let P = new AbortController();
|
|
95
|
+
const i = () => {
|
|
96
|
+
P?.abort(), P = new AbortController();
|
|
97
97
|
}, l = async () => {
|
|
98
|
-
|
|
98
|
+
C.value && i(), C.value = !0, v.value = void 0;
|
|
99
99
|
try {
|
|
100
|
-
|
|
101
|
-
let
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
method:
|
|
105
|
-
url:
|
|
106
|
-
params:
|
|
107
|
-
signal:
|
|
100
|
+
s.params && a?.params && s.params.parse(a.params);
|
|
101
|
+
let t = a?.data;
|
|
102
|
+
s.data && t && s.data.parse(t);
|
|
103
|
+
const u = {
|
|
104
|
+
method: s.method ?? "GET",
|
|
105
|
+
url: s.path,
|
|
106
|
+
params: a?.params,
|
|
107
|
+
signal: P.signal
|
|
108
108
|
};
|
|
109
|
-
if (
|
|
110
|
-
const
|
|
111
|
-
|
|
109
|
+
if (s.method === "POST" && t && (u.data = t), s.onBeforeRequest) {
|
|
110
|
+
const d = await s.onBeforeRequest(u);
|
|
111
|
+
d !== void 0 && Object.assign(u, d);
|
|
112
112
|
}
|
|
113
|
-
if (
|
|
114
|
-
const
|
|
115
|
-
|
|
113
|
+
if (a?.onBeforeRequest) {
|
|
114
|
+
const d = await a.onBeforeRequest(u);
|
|
115
|
+
d !== void 0 && Object.assign(u, d);
|
|
116
116
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
v.value = void 0, E.value = void 0;
|
|
118
|
+
const b = await p.request(u), f = s.response ? s.response.parse(b.data) : b.data;
|
|
119
|
+
g.value = f, a?.onResult?.(f);
|
|
120
|
+
} catch (t) {
|
|
121
|
+
if (t instanceof Z) {
|
|
122
|
+
if (t.code !== "ERR_CANCELED") {
|
|
123
|
+
const u = t.response?.data?.message || t.message || "An error occurred";
|
|
124
|
+
v.value = u, a?.onError?.(t), e.onError?.({ err: t, message: u });
|
|
124
125
|
}
|
|
125
|
-
} else if (
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
).join(", ")}`;
|
|
130
|
-
g.value = h, o?.onError?.(s), o?.onZodError?.(q.value), e.onErrorRequest?.({ message: h, code: "VALIDATION_ERROR" }), e.onZodError && e.onZodError(q.value);
|
|
126
|
+
} else if (t instanceof F) {
|
|
127
|
+
E.value = A.flattenError(t);
|
|
128
|
+
const u = Object.keys(E.value.fieldErrors).length, b = `${Object.values(E.value.fieldErrors).at(0)}.${u > 1 ? ` (and ${u - 1} more errors)` : ""}`;
|
|
129
|
+
v.value = b, a?.onError?.(t), a?.onZodError?.(A.flattenError(t)), e.onError?.({ err: t, message: b }), e.onZodError && e.onZodError(A.flattenError(t));
|
|
131
130
|
} else {
|
|
132
|
-
const
|
|
133
|
-
|
|
131
|
+
const u = t.message || "An error occurred";
|
|
132
|
+
v.value = u, a?.onError?.(u), e.onError?.({ err: t, message: u });
|
|
134
133
|
}
|
|
135
134
|
} finally {
|
|
136
|
-
|
|
135
|
+
C.value = !1, j.value = !0;
|
|
137
136
|
}
|
|
138
|
-
},
|
|
139
|
-
let
|
|
140
|
-
return (
|
|
141
|
-
|
|
142
|
-
() => JSON.stringify({ params:
|
|
137
|
+
}, R = a?.debounce ? J(l, a.debounce) : l;
|
|
138
|
+
let q = null;
|
|
139
|
+
return (a?.params || a?.data) && (N(() => {
|
|
140
|
+
q && q(), q = Q(
|
|
141
|
+
() => JSON.stringify({ params: a.params, data: a.data }),
|
|
143
142
|
() => {
|
|
144
|
-
|
|
143
|
+
R();
|
|
145
144
|
},
|
|
146
145
|
{ immediate: !1 }
|
|
147
146
|
);
|
|
148
|
-
}),
|
|
149
|
-
|
|
150
|
-
})), (
|
|
147
|
+
}), z(() => {
|
|
148
|
+
q && q(), P?.abort();
|
|
149
|
+
})), (a?.loadOnMount === void 0 || a.loadOnMount) && !j.value && (k.value ? (k.value = !1, l()) : R()), { result: g, errorMessage: v, zodError: E, isLoading: C, isDone: j, refetch: l };
|
|
151
150
|
};
|
|
152
|
-
} else typeof
|
|
151
|
+
} else typeof c == "object" && (n[m] = D(c));
|
|
153
152
|
}
|
|
154
|
-
return
|
|
153
|
+
return n;
|
|
155
154
|
}
|
|
156
|
-
const
|
|
157
|
-
function
|
|
158
|
-
const
|
|
155
|
+
const x = e.queries ?? {}, L = D(x);
|
|
156
|
+
function S(r) {
|
|
157
|
+
const n = {};
|
|
159
158
|
for (const m in r) {
|
|
160
|
-
const
|
|
161
|
-
if (
|
|
162
|
-
if (
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
return { result:
|
|
167
|
-
if (!
|
|
168
|
-
|
|
159
|
+
const c = r[m];
|
|
160
|
+
if (c)
|
|
161
|
+
if (W(c)) {
|
|
162
|
+
const s = c;
|
|
163
|
+
n[m] = (o) => {
|
|
164
|
+
const a = h(), g = h(), v = h(), E = h(!1), C = h(!1), j = h(0);
|
|
165
|
+
return { result: a, errorMessage: g, zodError: v, isLoading: E, isDone: C, uploadProgress: j, mutate: async (P) => {
|
|
166
|
+
if (!E.value) {
|
|
167
|
+
E.value = !0, g.value = void 0, j.value = 0;
|
|
169
168
|
try {
|
|
170
|
-
const { data:
|
|
171
|
-
let
|
|
172
|
-
if (
|
|
173
|
-
const
|
|
174
|
-
for (const [
|
|
175
|
-
|
|
176
|
-
M instanceof File || M instanceof Blob ?
|
|
177
|
-
}) : typeof
|
|
178
|
-
|
|
179
|
-
} else
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
method:
|
|
183
|
-
url:
|
|
184
|
-
data:
|
|
169
|
+
const { data: i = {}, params: l } = P ?? {};
|
|
170
|
+
let R = i ?? {}, q = {};
|
|
171
|
+
if (s.isMultipart) {
|
|
172
|
+
const f = new FormData();
|
|
173
|
+
for (const [d, y] of Object.entries(i))
|
|
174
|
+
y instanceof File || y instanceof Blob ? f.append(d, y) : Array.isArray(y) ? y.forEach((M) => {
|
|
175
|
+
M instanceof File || M instanceof Blob ? f.append(d, M) : f.append(d, JSON.stringify(M));
|
|
176
|
+
}) : typeof y == "object" && y !== null ? f.append(d, JSON.stringify(y)) : f.append(d, String(y));
|
|
177
|
+
R = f, q["Content-Type"] = "multipart/form-data";
|
|
178
|
+
} else s.data && s.data.parse(i);
|
|
179
|
+
s.params && l && s.params.parse(l);
|
|
180
|
+
const t = {
|
|
181
|
+
method: s.method,
|
|
182
|
+
url: s.path,
|
|
183
|
+
data: R,
|
|
185
184
|
params: l,
|
|
186
|
-
headers:
|
|
187
|
-
onUploadProgress: (
|
|
188
|
-
if (
|
|
189
|
-
const
|
|
190
|
-
|
|
185
|
+
headers: q,
|
|
186
|
+
onUploadProgress: (f) => {
|
|
187
|
+
if (f.total) {
|
|
188
|
+
const d = Math.round(f.loaded * 100 / f.total);
|
|
189
|
+
j.value = d, o?.onUploadProgress?.(d);
|
|
191
190
|
}
|
|
192
191
|
}
|
|
193
192
|
};
|
|
194
|
-
if (
|
|
195
|
-
const
|
|
196
|
-
|
|
193
|
+
if (s.onBeforeRequest) {
|
|
194
|
+
const f = await s.onBeforeRequest(t);
|
|
195
|
+
f !== void 0 && Object.assign(t, f);
|
|
197
196
|
}
|
|
198
|
-
if (
|
|
199
|
-
const
|
|
200
|
-
|
|
197
|
+
if (o?.onBeforeRequest) {
|
|
198
|
+
const f = await o.onBeforeRequest(t);
|
|
199
|
+
f !== void 0 && Object.assign(t, f);
|
|
201
200
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
).
|
|
213
|
-
C.value = E, a?.onError?.(u), a?.onZodError?.(g.value), e.onErrorRequest?.({ message: E, code: "VALIDATION_ERROR" }), e.onZodError && e.onZodError(g.value);
|
|
201
|
+
g.value = void 0, v.value = void 0;
|
|
202
|
+
const u = await p.request(t), b = s.response ? s.response.parse(u.data) : u.data;
|
|
203
|
+
a.value = b, o?.onResult?.(b);
|
|
204
|
+
} catch (i) {
|
|
205
|
+
if (i instanceof Z) {
|
|
206
|
+
const l = i.response?.data?.message || i.message || "An error occurred";
|
|
207
|
+
g.value = l, o?.onError?.(i), e.onError?.({ err: i, message: l });
|
|
208
|
+
} else if (i instanceof F) {
|
|
209
|
+
v.value = A.flattenError(i);
|
|
210
|
+
const l = Object.keys(v.value.fieldErrors).length, R = `${Object.values(v.value.fieldErrors).at(0)}.${l > 1 ? ` (and ${l - 1} more errors)` : ""}`;
|
|
211
|
+
g.value = R, o?.onError?.(i), o?.onZodError?.(A.flattenError(i)), e.onError?.({ err: i, message: R }), e.onZodError && e.onZodError(A.flattenError(i));
|
|
214
212
|
} else {
|
|
215
|
-
const l =
|
|
216
|
-
|
|
213
|
+
const l = i.message || "An error occurred";
|
|
214
|
+
g.value = l, o?.onError?.(i), e.onError?.({ err: i, message: l });
|
|
217
215
|
}
|
|
218
216
|
} finally {
|
|
219
|
-
|
|
217
|
+
E.value = !1, C.value = !0;
|
|
220
218
|
}
|
|
221
219
|
}
|
|
222
220
|
} };
|
|
223
221
|
};
|
|
224
|
-
} else typeof
|
|
222
|
+
} else typeof c == "object" && (n[m] = S(c));
|
|
225
223
|
}
|
|
226
|
-
return
|
|
224
|
+
return n;
|
|
227
225
|
}
|
|
228
|
-
const
|
|
226
|
+
const T = e.mutations ?? {}, $ = S(T);
|
|
229
227
|
return {
|
|
230
|
-
query:
|
|
231
|
-
mutation:
|
|
228
|
+
query: L,
|
|
229
|
+
mutation: $
|
|
232
230
|
};
|
|
233
231
|
}
|
|
234
|
-
function
|
|
232
|
+
function O(e) {
|
|
235
233
|
return e;
|
|
236
234
|
}
|
|
237
|
-
function
|
|
235
|
+
function ee(e) {
|
|
238
236
|
return e;
|
|
239
237
|
}
|
|
240
|
-
function
|
|
238
|
+
function re(...e) {
|
|
241
239
|
return Object.assign({}, ...e);
|
|
242
240
|
}
|
|
243
|
-
function
|
|
241
|
+
function te(...e) {
|
|
244
242
|
return Object.assign({}, ...e);
|
|
245
243
|
}
|
|
246
244
|
export {
|
|
247
245
|
ae as AxiosError,
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
246
|
+
Y as createApiClient,
|
|
247
|
+
ee as defineMutation,
|
|
248
|
+
O as defineQuery,
|
|
249
|
+
te as mergeMutations,
|
|
250
|
+
re as mergeQueries,
|
|
251
|
+
G as z
|
|
254
252
|
};
|
package/package.json
CHANGED