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