vue-api-kit 1.5.1 → 1.5.2
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/index.js +95 -94
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,40 +1,44 @@
|
|
|
1
1
|
import { ZodError as L } from "zod";
|
|
2
|
-
import * as
|
|
3
|
-
import
|
|
2
|
+
import * as U from "zod";
|
|
3
|
+
import x, { AxiosError as S } from "axios";
|
|
4
4
|
import { AxiosError as K } from "axios";
|
|
5
|
-
import { nextTick as
|
|
6
|
-
import { debounce as
|
|
7
|
-
function z(
|
|
8
|
-
const h =
|
|
9
|
-
baseURL:
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
import { nextTick as F, ref as m, onMounted as N, watch as T, onBeforeUnmount as Z } from "vue";
|
|
6
|
+
import { debounce as $ } from "lodash-es";
|
|
7
|
+
function z(s) {
|
|
8
|
+
const h = x.create({
|
|
9
|
+
baseURL: s.baseURL,
|
|
10
|
+
headers: {
|
|
11
|
+
"Content-Type": "application/json",
|
|
12
|
+
Accept: "application/json",
|
|
13
|
+
...s.headers
|
|
14
|
+
},
|
|
15
|
+
withCredentials: s.withCredentials ?? !1
|
|
12
16
|
});
|
|
13
|
-
let
|
|
14
|
-
|
|
17
|
+
let b = !1, q = null;
|
|
18
|
+
s.onBeforeRequest && h.interceptors.request.use(
|
|
15
19
|
async (e) => {
|
|
16
20
|
try {
|
|
17
|
-
return await
|
|
18
|
-
} catch (
|
|
19
|
-
return Promise.reject(
|
|
21
|
+
return await s.onBeforeRequest(e) || e;
|
|
22
|
+
} catch (r) {
|
|
23
|
+
return Promise.reject(r);
|
|
20
24
|
}
|
|
21
25
|
},
|
|
22
26
|
(e) => Promise.reject(e)
|
|
23
|
-
),
|
|
27
|
+
), s.onStartRequest && h.interceptors.request.use(
|
|
24
28
|
async (e) => {
|
|
25
29
|
try {
|
|
26
|
-
return await
|
|
27
|
-
} catch (
|
|
28
|
-
return Promise.reject(
|
|
30
|
+
return await s.onStartRequest(), e;
|
|
31
|
+
} catch (r) {
|
|
32
|
+
return Promise.reject(r);
|
|
29
33
|
}
|
|
30
34
|
},
|
|
31
35
|
(e) => Promise.reject(e)
|
|
32
|
-
),
|
|
33
|
-
(e) => (
|
|
34
|
-
(e) => (
|
|
36
|
+
), s.onFinishRequest && h.interceptors.response.use(
|
|
37
|
+
(e) => (s.onFinishRequest(), e),
|
|
38
|
+
(e) => (s.onFinishRequest(), Promise.reject(e))
|
|
35
39
|
), h.interceptors.request.use((e) => {
|
|
36
40
|
if (!e.url) return e;
|
|
37
|
-
const
|
|
41
|
+
const r = (a) => {
|
|
38
42
|
if (a)
|
|
39
43
|
for (const [t, v] of Object.entries(a)) {
|
|
40
44
|
const l = `{${t}}`;
|
|
@@ -44,117 +48,114 @@ function z(r) {
|
|
|
44
48
|
), delete a[t]);
|
|
45
49
|
}
|
|
46
50
|
};
|
|
47
|
-
return e.method !== "get" && e.data?.params &&
|
|
48
|
-
}),
|
|
51
|
+
return e.method !== "get" && e.data?.params && r(e.data.params), r(e.params), e;
|
|
52
|
+
}), s.csrfRefreshEndpoint && h.interceptors.response.use(
|
|
49
53
|
(e) => e,
|
|
50
54
|
async (e) => {
|
|
51
|
-
const
|
|
52
|
-
if (
|
|
55
|
+
const r = e.config;
|
|
56
|
+
if (r?.url === s.csrfRefreshEndpoint)
|
|
53
57
|
return Promise.reject(e);
|
|
54
|
-
if (e.response && (e.response.status === 403 || e.response.status === 419) && !
|
|
55
|
-
|
|
58
|
+
if (e.response && (e.response.status === 403 || e.response.status === 419) && !r?._retry) {
|
|
59
|
+
r._retry = !0;
|
|
56
60
|
try {
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}).then(() => {
|
|
61
|
-
P = !1, q = null;
|
|
62
|
-
}), await q), h(s);
|
|
61
|
+
return b && q ? await q : (b = !0, q = h.get(s.csrfRefreshEndpoint).then(() => {
|
|
62
|
+
b = !1, q = null;
|
|
63
|
+
}), await q), h.request(r);
|
|
63
64
|
} catch (a) {
|
|
64
|
-
return
|
|
65
|
+
return b = !1, q = null, Promise.reject(a);
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
return Promise.reject(e);
|
|
68
69
|
}
|
|
69
70
|
), h.interceptors.response.use(
|
|
70
71
|
(e) => e,
|
|
71
|
-
(e) => (
|
|
72
|
+
(e) => (F(() => {
|
|
72
73
|
e.code;
|
|
73
74
|
}), Promise.reject(e))
|
|
74
75
|
);
|
|
75
|
-
const C =
|
|
76
|
+
const C = s.queries ?? {}, M = {};
|
|
76
77
|
for (const e in C) {
|
|
77
|
-
const
|
|
78
|
-
|
|
78
|
+
const r = C[e];
|
|
79
|
+
r && (M[e] = (a) => {
|
|
79
80
|
let t;
|
|
80
81
|
a && typeof a == "object" && ("loadOnMount" in a || "debounce" in a || "onResult" in a || "onError" in a || "data" in a ? t = a : t = { params: a });
|
|
81
|
-
const v = m(), l = m(),
|
|
82
|
-
let
|
|
82
|
+
const v = m(), l = m(), E = m(), g = m(!1), y = m(!1), P = m(!0);
|
|
83
|
+
let j = new AbortController();
|
|
83
84
|
const u = () => {
|
|
84
|
-
|
|
85
|
+
j?.abort(), j = new AbortController();
|
|
85
86
|
}, c = async () => {
|
|
86
87
|
g.value && u(), g.value = !0, l.value = void 0;
|
|
87
88
|
try {
|
|
88
|
-
|
|
89
|
+
r.params && t?.params && r.params.parse(t.params);
|
|
89
90
|
let o = t?.data;
|
|
90
|
-
|
|
91
|
+
r.data && o && r.data.parse(o);
|
|
91
92
|
const f = {
|
|
92
|
-
method:
|
|
93
|
-
url:
|
|
93
|
+
method: r.method ?? "GET",
|
|
94
|
+
url: r.path,
|
|
94
95
|
params: t?.params,
|
|
95
|
-
signal:
|
|
96
|
+
signal: j.signal
|
|
96
97
|
};
|
|
97
|
-
|
|
98
|
-
const n = await h.request(f), i =
|
|
98
|
+
r.method === "POST" && o && (f.data = o);
|
|
99
|
+
const n = await h.request(f), i = r.response ? r.response.parse(n.data) : n.data;
|
|
99
100
|
v.value = i, t?.onResult?.(i);
|
|
100
101
|
} catch (o) {
|
|
101
102
|
if (o instanceof S) {
|
|
102
103
|
if (o.code !== "ERR_CANCELED") {
|
|
103
104
|
const f = o.response?.data?.message || o.message || "An error occurred", n = o.response?.status, i = o.code, p = o.response?.data;
|
|
104
|
-
l.value = f, t?.onError?.(o),
|
|
105
|
+
l.value = f, t?.onError?.(o), s.onErrorRequest?.({ message: f, status: n, code: i, data: p });
|
|
105
106
|
}
|
|
106
107
|
} else if (o instanceof L) {
|
|
107
|
-
|
|
108
|
-
const n = `Validation error: ${
|
|
108
|
+
E.value = o.issues || [];
|
|
109
|
+
const n = `Validation error: ${E.value.map(
|
|
109
110
|
(i) => `${i.path.join(".")}: ${i.message}`
|
|
110
111
|
).join(", ")}`;
|
|
111
|
-
l.value = n, t?.onError?.(o), t?.onZodError?.(
|
|
112
|
+
l.value = n, t?.onError?.(o), t?.onZodError?.(E.value), s.onErrorRequest?.({ message: n, code: "VALIDATION_ERROR" }), s.onZodError && s.onZodError(E.value);
|
|
112
113
|
} else {
|
|
113
114
|
const f = o.message || "An error occurred";
|
|
114
|
-
l.value = f, t?.onError?.(f),
|
|
115
|
+
l.value = f, t?.onError?.(f), s.onErrorRequest?.({ message: f });
|
|
115
116
|
}
|
|
116
117
|
} finally {
|
|
117
118
|
g.value = !1, y.value = !0;
|
|
118
119
|
}
|
|
119
|
-
},
|
|
120
|
+
}, R = t?.debounce ? $(c, t.debounce) : c;
|
|
120
121
|
let d = null;
|
|
121
|
-
return (t?.params || t?.data) && (
|
|
122
|
-
d && d(), d =
|
|
122
|
+
return (t?.params || t?.data) && (N(() => {
|
|
123
|
+
d && d(), d = T(
|
|
123
124
|
() => JSON.stringify({ params: t.params, data: t.data }),
|
|
124
125
|
() => {
|
|
125
|
-
|
|
126
|
+
R();
|
|
126
127
|
},
|
|
127
128
|
{ immediate: !1 }
|
|
128
129
|
);
|
|
129
130
|
}), Z(() => {
|
|
130
|
-
d && d(),
|
|
131
|
-
})), (t?.loadOnMount === void 0 || t.loadOnMount) && !y.value && (
|
|
131
|
+
d && d(), j?.abort();
|
|
132
|
+
})), (t?.loadOnMount === void 0 || t.loadOnMount) && !y.value && (P.value ? (P.value = !1, c()) : R()), { result: v, errorMessage: l, zodErrors: E, isLoading: g, isDone: y, refetch: c };
|
|
132
133
|
});
|
|
133
134
|
}
|
|
134
|
-
const D =
|
|
135
|
+
const D = s.mutations ?? {}, w = {};
|
|
135
136
|
for (const e in D) {
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
const t = m(), v = m(), l = m(),
|
|
139
|
-
return { result: t, errorMessage: v, zodErrors: l, isLoading:
|
|
140
|
-
if (!
|
|
141
|
-
|
|
137
|
+
const r = D[e];
|
|
138
|
+
r && (w[e] = (a) => {
|
|
139
|
+
const t = m(), v = m(), l = m(), E = m(!1), g = m(!1), y = m(0);
|
|
140
|
+
return { result: t, errorMessage: v, zodErrors: l, isLoading: E, isDone: g, uploadProgress: y, mutate: async (j) => {
|
|
141
|
+
if (!E.value) {
|
|
142
|
+
E.value = !0, v.value = void 0, y.value = 0;
|
|
142
143
|
try {
|
|
143
|
-
const { data: u = {}, params: c } =
|
|
144
|
-
let
|
|
145
|
-
if (
|
|
144
|
+
const { data: u = {}, params: c } = j ?? {};
|
|
145
|
+
let R = u ?? {}, d = {};
|
|
146
|
+
if (r.isMultipart) {
|
|
146
147
|
const n = new FormData();
|
|
147
148
|
for (const [i, p] of Object.entries(u))
|
|
148
|
-
p instanceof File || p instanceof Blob ? n.append(i, p) : Array.isArray(p) ? p.forEach((
|
|
149
|
-
|
|
149
|
+
p instanceof File || p instanceof Blob ? n.append(i, p) : Array.isArray(p) ? p.forEach((A) => {
|
|
150
|
+
A instanceof File || A instanceof Blob ? n.append(i, A) : n.append(i, JSON.stringify(A));
|
|
150
151
|
}) : typeof p == "object" && p !== null ? n.append(i, JSON.stringify(p)) : n.append(i, String(p));
|
|
151
|
-
|
|
152
|
-
} else
|
|
153
|
-
|
|
152
|
+
R = n, d["Content-Type"] = "multipart/form-data";
|
|
153
|
+
} else r.data && r.data.parse(u);
|
|
154
|
+
r.params && c && r.params.parse(c);
|
|
154
155
|
const o = await h.request({
|
|
155
|
-
method:
|
|
156
|
-
url:
|
|
157
|
-
data:
|
|
156
|
+
method: r.method,
|
|
157
|
+
url: r.path,
|
|
158
|
+
data: R,
|
|
158
159
|
params: c,
|
|
159
160
|
headers: d,
|
|
160
161
|
onUploadProgress: (n) => {
|
|
@@ -163,24 +164,24 @@ function z(r) {
|
|
|
163
164
|
y.value = i, a?.onUploadProgress?.(i);
|
|
164
165
|
}
|
|
165
166
|
}
|
|
166
|
-
}), f =
|
|
167
|
+
}), f = r.response ? r.response.parse(o.data) : o.data;
|
|
167
168
|
t.value = f, a?.onResult?.(f);
|
|
168
169
|
} catch (u) {
|
|
169
170
|
if (u instanceof S) {
|
|
170
|
-
const c = u.response?.data?.message || u.message || "An error occurred",
|
|
171
|
-
v.value = c, a?.onError?.(u),
|
|
171
|
+
const c = u.response?.data?.message || u.message || "An error occurred", R = u.response?.status, d = u.code;
|
|
172
|
+
v.value = c, a?.onError?.(u), s.onErrorRequest?.({ message: c, status: R, code: d });
|
|
172
173
|
} else if (u instanceof L) {
|
|
173
174
|
l.value = u.issues || [];
|
|
174
|
-
const
|
|
175
|
+
const R = `Validation error: ${l.value.map(
|
|
175
176
|
(d) => `${d.path.join(".")}: ${d.message}`
|
|
176
177
|
).join(", ")}`;
|
|
177
|
-
v.value =
|
|
178
|
+
v.value = R, a?.onError?.(u), a?.onZodError?.(l.value), s.onErrorRequest?.({ message: R, code: "VALIDATION_ERROR" }), s.onZodError && s.onZodError(l.value);
|
|
178
179
|
} else {
|
|
179
180
|
const c = u.message || "An error occurred";
|
|
180
|
-
v.value = c, a?.onError?.(u),
|
|
181
|
+
v.value = c, a?.onError?.(u), s.onErrorRequest?.({ message: c });
|
|
181
182
|
}
|
|
182
183
|
} finally {
|
|
183
|
-
|
|
184
|
+
E.value = !1, g.value = !0;
|
|
184
185
|
}
|
|
185
186
|
}
|
|
186
187
|
} };
|
|
@@ -191,17 +192,17 @@ function z(r) {
|
|
|
191
192
|
mutation: w
|
|
192
193
|
};
|
|
193
194
|
}
|
|
194
|
-
function V(
|
|
195
|
-
return
|
|
195
|
+
function V(s) {
|
|
196
|
+
return s;
|
|
196
197
|
}
|
|
197
|
-
function J(
|
|
198
|
-
return
|
|
198
|
+
function J(s) {
|
|
199
|
+
return s;
|
|
199
200
|
}
|
|
200
|
-
function Q(...
|
|
201
|
-
return Object.assign({}, ...
|
|
201
|
+
function Q(...s) {
|
|
202
|
+
return Object.assign({}, ...s);
|
|
202
203
|
}
|
|
203
|
-
function W(...
|
|
204
|
-
return Object.assign({}, ...
|
|
204
|
+
function W(...s) {
|
|
205
|
+
return Object.assign({}, ...s);
|
|
205
206
|
}
|
|
206
207
|
export {
|
|
207
208
|
K as AxiosError,
|
|
@@ -210,5 +211,5 @@ export {
|
|
|
210
211
|
V as defineQuery,
|
|
211
212
|
W as mergeMutations,
|
|
212
213
|
Q as mergeQueries,
|
|
213
|
-
|
|
214
|
+
U as z
|
|
214
215
|
};
|
package/package.json
CHANGED