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