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