vue-api-kit 1.10.11 → 1.11.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 +50 -50
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import k, { ZodError as
|
|
1
|
+
import k, { ZodError as Z } from "zod";
|
|
2
2
|
import * as I from "zod";
|
|
3
3
|
import { ZodError as ne } from "zod";
|
|
4
|
-
import U, { AxiosError as
|
|
4
|
+
import U, { AxiosError as x } from "axios";
|
|
5
5
|
import { AxiosError as ie } from "axios";
|
|
6
|
-
import { nextTick as _, ref as
|
|
6
|
+
import { nextTick as _, ref as p, onMounted as Q, watch as z, onBeforeUnmount as N } from "vue";
|
|
7
7
|
import { debounce as H } from "lodash-es";
|
|
8
8
|
function W(e) {
|
|
9
9
|
return e && typeof e == "object" && e !== null && typeof e.path == "string";
|
|
@@ -72,11 +72,11 @@ function K(e) {
|
|
|
72
72
|
if (!r.url) return r;
|
|
73
73
|
const i = (v) => {
|
|
74
74
|
if (v)
|
|
75
|
-
for (const [m,
|
|
75
|
+
for (const [m, t] of Object.entries(v)) {
|
|
76
76
|
const s = `{${m}}`;
|
|
77
77
|
r.url.includes(s) && (r.url = r.url.replace(
|
|
78
78
|
s,
|
|
79
|
-
encodeURIComponent(String(
|
|
79
|
+
encodeURIComponent(String(t))
|
|
80
80
|
), delete v[m]);
|
|
81
81
|
}
|
|
82
82
|
};
|
|
@@ -111,51 +111,51 @@ function K(e) {
|
|
|
111
111
|
const m = r[v];
|
|
112
112
|
if (m)
|
|
113
113
|
if (W(m)) {
|
|
114
|
-
const
|
|
114
|
+
const t = m;
|
|
115
115
|
i[v] = (s) => {
|
|
116
116
|
let n;
|
|
117
117
|
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 =
|
|
118
|
+
const R = p(), E = p(), g = p(), P = p(!1), A = p(!1), M = p(!0);
|
|
119
119
|
let B = new AbortController();
|
|
120
120
|
const u = () => {
|
|
121
121
|
B?.abort(), B = new AbortController();
|
|
122
122
|
}, f = async () => {
|
|
123
123
|
P.value && u(), P.value = !0, E.value = void 0;
|
|
124
124
|
try {
|
|
125
|
-
|
|
126
|
-
let
|
|
127
|
-
|
|
125
|
+
t.params && n?.params && t.params.parse(n.params);
|
|
126
|
+
let o = n?.data;
|
|
127
|
+
t.data && o && t.data.parse(o);
|
|
128
128
|
const c = {
|
|
129
|
-
method:
|
|
130
|
-
url:
|
|
129
|
+
method: t.method ?? "GET",
|
|
130
|
+
url: t.path,
|
|
131
131
|
params: n?.params,
|
|
132
132
|
signal: B.signal,
|
|
133
133
|
headers: {}
|
|
134
134
|
};
|
|
135
|
-
if (
|
|
136
|
-
const y = await
|
|
135
|
+
if (t.method === "POST" && o && (c.data = o), t.onBeforeRequest) {
|
|
136
|
+
const y = await t.onBeforeRequest(c);
|
|
137
137
|
y !== void 0 && Object.assign(c, y);
|
|
138
138
|
}
|
|
139
139
|
if (n?.onBeforeRequest) {
|
|
140
140
|
const y = await n.onBeforeRequest(c);
|
|
141
141
|
y !== void 0 && Object.assign(c, y);
|
|
142
142
|
}
|
|
143
|
-
E.value = void 0,
|
|
144
|
-
const j = await a.request(c), d =
|
|
143
|
+
E.value = void 0, g.value = void 0;
|
|
144
|
+
const j = await a.request(c), d = t.response ? t.response.parse(j.data) : j.data;
|
|
145
145
|
R.value = d, n?.onResult?.(d);
|
|
146
|
-
} catch (
|
|
147
|
-
if (
|
|
148
|
-
if (
|
|
149
|
-
const c =
|
|
150
|
-
E.value = c, n?.onError?.(
|
|
146
|
+
} catch (o) {
|
|
147
|
+
if (o instanceof x) {
|
|
148
|
+
if (o.code !== "ERR_CANCELED") {
|
|
149
|
+
const c = o.response?.data?.message || o.message || "An error occurred";
|
|
150
|
+
E.value = c, n?.onError?.(o), e.onError?.({ err: o, message: c });
|
|
151
151
|
}
|
|
152
|
-
} else if (
|
|
153
|
-
|
|
154
|
-
const c = Object.keys(
|
|
155
|
-
E.value = j, n?.onError?.(
|
|
152
|
+
} else if (o instanceof Z) {
|
|
153
|
+
console.warn("Zod validation error at:", t.method, t.path), g.value = k.flattenError(o);
|
|
154
|
+
const c = Object.keys(g.value.fieldErrors).length, j = `${Object.values(g.value.fieldErrors).at(0)}.${c > 1 ? ` (and ${c - 1} more errors)` : ""}`;
|
|
155
|
+
E.value = j, n?.onError?.(o), n?.onZodError?.(k.flattenError(o)), e.onError?.({ err: o, message: j }), e.onZodError && e.onZodError(k.flattenError(o));
|
|
156
156
|
} else {
|
|
157
|
-
const c =
|
|
158
|
-
E.value = c, n?.onError?.(c), e.onError?.({ err:
|
|
157
|
+
const c = o.message || "An error occurred";
|
|
158
|
+
E.value = c, n?.onError?.(c), e.onError?.({ err: o, message: c });
|
|
159
159
|
}
|
|
160
160
|
} finally {
|
|
161
161
|
P.value = !1, A.value = !0;
|
|
@@ -172,38 +172,38 @@ function K(e) {
|
|
|
172
172
|
);
|
|
173
173
|
}), N(() => {
|
|
174
174
|
b && b(), B?.abort();
|
|
175
|
-
})), (n?.loadOnMount === void 0 || n.loadOnMount) && !A.value && (M.value ? (M.value = !1, f()) : q()), { result: R, errorMessage: E, zodError:
|
|
175
|
+
})), (n?.loadOnMount === void 0 || n.loadOnMount) && !A.value && (M.value ? (M.value = !1, f()) : q()), { result: R, errorMessage: E, zodError: g, isLoading: P, isDone: A, refetch: f };
|
|
176
176
|
};
|
|
177
177
|
} else typeof m == "object" && (i[v] = C(m));
|
|
178
178
|
}
|
|
179
179
|
return i;
|
|
180
180
|
}
|
|
181
|
-
const w = e.queries ?? {},
|
|
181
|
+
const w = e.queries ?? {}, S = C(w);
|
|
182
182
|
function F(r) {
|
|
183
183
|
const i = {};
|
|
184
184
|
for (const v in r) {
|
|
185
185
|
const m = r[v];
|
|
186
186
|
if (m)
|
|
187
187
|
if (X(m)) {
|
|
188
|
-
const
|
|
188
|
+
const t = m;
|
|
189
189
|
i[v] = (s) => {
|
|
190
|
-
const n =
|
|
191
|
-
return { result: n, errorMessage: R, zodError: E, isLoading:
|
|
192
|
-
if (!
|
|
193
|
-
|
|
190
|
+
const n = p(), R = p(), E = p(), g = p(!1), P = p(!1), A = p(0);
|
|
191
|
+
return { result: n, errorMessage: R, zodError: E, isLoading: g, isDone: P, uploadProgress: A, mutate: async (B) => {
|
|
192
|
+
if (!g.value) {
|
|
193
|
+
g.value = !0, R.value = void 0, A.value = 0;
|
|
194
194
|
try {
|
|
195
195
|
const { data: u = {}, params: f } = B ?? {};
|
|
196
196
|
let q = u ?? {}, b = {};
|
|
197
|
-
if (
|
|
197
|
+
if (t.isMultipart) {
|
|
198
198
|
const d = new FormData();
|
|
199
199
|
for (const [y, D] of Object.entries(u))
|
|
200
200
|
$(d, D, y);
|
|
201
201
|
q = d, b["Content-Type"] = "multipart/form-data";
|
|
202
|
-
} else
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
method:
|
|
206
|
-
url:
|
|
202
|
+
} else t.data && t.data.parse(u);
|
|
203
|
+
t.params && f && t.params.parse(f);
|
|
204
|
+
const o = {
|
|
205
|
+
method: t.method,
|
|
206
|
+
url: t.path,
|
|
207
207
|
data: q,
|
|
208
208
|
params: f,
|
|
209
209
|
headers: b,
|
|
@@ -214,22 +214,22 @@ function K(e) {
|
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
};
|
|
217
|
-
if (
|
|
218
|
-
const d = await
|
|
219
|
-
d !== void 0 && Object.assign(
|
|
217
|
+
if (t.onBeforeRequest) {
|
|
218
|
+
const d = await t.onBeforeRequest(o);
|
|
219
|
+
d !== void 0 && Object.assign(o, d);
|
|
220
220
|
}
|
|
221
221
|
if (s?.onBeforeRequest) {
|
|
222
|
-
const d = await s.onBeforeRequest(
|
|
223
|
-
d !== void 0 && Object.assign(
|
|
222
|
+
const d = await s.onBeforeRequest(o);
|
|
223
|
+
d !== void 0 && Object.assign(o, d);
|
|
224
224
|
}
|
|
225
225
|
R.value = void 0, E.value = void 0;
|
|
226
|
-
const c = await a.request(
|
|
226
|
+
const c = await a.request(o), j = t.response ? t.response.parse(c.data) : c.data;
|
|
227
227
|
n.value = j, s?.onResult?.(j);
|
|
228
228
|
} catch (u) {
|
|
229
|
-
if (u instanceof
|
|
229
|
+
if (u instanceof x) {
|
|
230
230
|
const f = u.response?.data?.message || u.message || "An error occurred";
|
|
231
231
|
R.value = f, s?.onError?.(u), e.onError?.({ err: u, message: f });
|
|
232
|
-
} else if (u instanceof
|
|
232
|
+
} else if (u instanceof Z) {
|
|
233
233
|
E.value = k.flattenError(u);
|
|
234
234
|
const f = Object.keys(E.value.fieldErrors).length, q = `${Object.values(E.value.fieldErrors).at(0)}.${f > 1 ? ` (and ${f - 1} more errors)` : ""}`;
|
|
235
235
|
R.value = q, s?.onError?.(u), s?.onZodError?.(k.flattenError(u)), e.onError?.({ err: u, message: q }), e.onZodError && e.onZodError(k.flattenError(u));
|
|
@@ -238,7 +238,7 @@ function K(e) {
|
|
|
238
238
|
R.value = f, s?.onError?.(u), e.onError?.({ err: u, message: f });
|
|
239
239
|
}
|
|
240
240
|
} finally {
|
|
241
|
-
|
|
241
|
+
g.value = !1, P.value = !0;
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
244
|
} };
|
|
@@ -249,7 +249,7 @@ function K(e) {
|
|
|
249
249
|
}
|
|
250
250
|
const T = e.mutations ?? {}, L = F(T);
|
|
251
251
|
return {
|
|
252
|
-
query:
|
|
252
|
+
query: S,
|
|
253
253
|
mutation: L
|
|
254
254
|
};
|
|
255
255
|
}
|
package/package.json
CHANGED