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