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