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