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