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