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