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