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.
@@ -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
- * onErrorRequest: (error) => console.error(error.message)
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 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) {
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 W(e) {
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 Y(e) {
15
- const h = U.create({
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 B = !1, w = null;
26
- e.onBeforeRequest && h.interceptors.request.use(
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 (n) {
31
- return Promise.reject(n);
46
+ } catch (u) {
47
+ return Promise.reject(u);
32
48
  }
33
49
  },
34
50
  (r) => Promise.reject(r)
35
- ), e.onStartRequest && h.interceptors.request.use(
51
+ ), e.onStartRequest && a.interceptors.request.use(
36
52
  async (r) => {
37
53
  try {
38
54
  return await e.onStartRequest(), r;
39
- } catch (n) {
40
- return Promise.reject(n);
55
+ } catch (u) {
56
+ return Promise.reject(u);
41
57
  }
42
58
  },
43
59
  (r) => Promise.reject(r)
44
- ), e.onFinishRequest && h.interceptors.response.use(
60
+ ), e.onFinishRequest && a.interceptors.response.use(
45
61
  (r) => (e.onFinishRequest(), r),
46
62
  (r) => (e.onFinishRequest(), Promise.reject(r))
47
- ), h.interceptors.request.use((r) => {
63
+ ), a.interceptors.request.use((r) => {
48
64
  if (!r.url) return r;
49
- const n = (m) => {
50
- if (m)
51
- for (const [c, s] of Object.entries(m)) {
52
- const o = `{${c}}`;
53
- r.url.includes(o) && (r.url = r.url.replace(
54
- o,
55
- encodeURIComponent(String(s))
56
- ), delete m[c]);
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 && n(r.data.params), n(r.params), r;
60
- }), e.csrfRefreshEndpoint && h.interceptors.response.use(
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 n = r.config;
64
- if (n?.url === e.csrfRefreshEndpoint)
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) && !n?._retry) {
67
- n._retry = !0;
82
+ if (r.response && (r.response.status === 403 || r.response.status === 419) && !u?._retry) {
83
+ u._retry = !0;
68
84
  try {
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);
72
- } catch (m) {
73
- return B = !1, w = null, Promise.reject(m);
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
- ), h.interceptors.response.use(
94
+ ), a.interceptors.response.use(
79
95
  (r) => r,
80
96
  (r) => (_(() => {
81
- r.code;
97
+ r?.code;
82
98
  }), Promise.reject(r))
83
99
  );
84
- function D(r) {
85
- const n = {};
86
- for (const m in r) {
87
- const c = r[m];
88
- if (c)
89
- if (H(c)) {
90
- const s = c;
91
- n[m] = (o) => {
92
- let a;
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 });
94
- const g = p(), v = p(), E = p(), C = p(!1), j = p(!1), k = p(!0);
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
- }, l = async () => {
99
- C.value && i(), C.value = !0, v.value = void 0;
114
+ }, f = async () => {
115
+ C.value && i(), C.value = !0, E.value = void 0;
100
116
  try {
101
- s.params && a?.params && s.params.parse(a.params);
102
- let t = a?.data;
103
- s.data && t && s.data.parse(t);
104
- const u = {
105
- method: s.method ?? "GET",
106
- url: s.path,
107
- params: a?.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 (s.method === "POST" && t && (u.data = t), s.onBeforeRequest) {
111
- const d = await s.onBeforeRequest(u);
112
- d !== void 0 && Object.assign(u, d);
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 (a?.onBeforeRequest) {
115
- const d = await a.onBeforeRequest(u);
116
- d !== void 0 && Object.assign(u, d);
130
+ if (n?.onBeforeRequest) {
131
+ const y = await n.onBeforeRequest(c);
132
+ y !== void 0 && Object.assign(c, y);
117
133
  }
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);
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 Z) {
138
+ if (t instanceof $) {
123
139
  if (t.code !== "ERR_CANCELED") {
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 });
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 F) {
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));
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 u = t.message || "An error occurred";
133
- v.value = u, a?.onError?.(u), e.onError?.({ err: t, message: u });
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, j.value = !0;
152
+ C.value = !1, w.value = !0;
137
153
  }
138
- }, R = a?.debounce ? J(l, a.debounce) : l;
139
- let q = null;
140
- return (a?.params || a?.data) && (N(() => {
141
- q && q(), q = Q(
142
- () => JSON.stringify({ params: a.params, data: a.data }),
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
- R();
160
+ q();
145
161
  },
146
162
  { immediate: !1 }
147
163
  );
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 };
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 c == "object" && (n[m] = D(c));
168
+ } else typeof m == "object" && (u[v] = B(m));
153
169
  }
154
- return n;
170
+ return u;
155
171
  }
156
- const x = e.queries ?? {}, L = D(x);
172
+ const k = e.queries ?? {}, x = B(k);
157
173
  function S(r) {
158
- const n = {};
159
- for (const m in r) {
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;
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: l } = P ?? {};
171
- let R = i ?? {}, q = {};
172
- if (s.isMultipart) {
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);
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: s.method,
183
- url: s.path,
184
- data: R,
185
- params: l,
186
- headers: q,
187
- onUploadProgress: (f) => {
188
- if (f.total) {
189
- const d = Math.round(f.loaded * 100 / f.total);
190
- j.value = d, o?.onUploadProgress?.(d);
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 (s.onBeforeRequest) {
195
- const f = await s.onBeforeRequest(t);
196
- f !== void 0 && Object.assign(t, f);
208
+ if (o.onBeforeRequest) {
209
+ const d = await o.onBeforeRequest(t);
210
+ d !== void 0 && Object.assign(t, d);
197
211
  }
198
- if (o?.onBeforeRequest) {
199
- const f = await o.onBeforeRequest(t);
200
- f !== void 0 && Object.assign(t, f);
212
+ if (s?.onBeforeRequest) {
213
+ const d = await s.onBeforeRequest(t);
214
+ d !== void 0 && Object.assign(t, d);
201
215
  }
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);
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 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));
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 l = i.message || "An error occurred";
215
- g.value = l, o?.onError?.(i), e.onError?.({ err: i, message: l });
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
- E.value = !1, C.value = !0;
232
+ p.value = !1, C.value = !0;
219
233
  }
220
234
  }
221
235
  } };
222
236
  };
223
- } else typeof c == "object" && (n[m] = S(c));
237
+ } else typeof m == "object" && (u[v] = S(m));
224
238
  }
225
- return n;
239
+ return u;
226
240
  }
227
- const T = e.mutations ?? {}, $ = S(T);
241
+ const T = e.mutations ?? {}, L = S(T);
228
242
  return {
229
- query: L,
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
- ie as AxiosError,
247
- ae as ZodError,
248
- Y as createApiClient,
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
- G as z
267
+ I as z
254
268
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vue-api-kit",
3
3
  "type": "module",
4
- "version": "1.10.7",
4
+ "version": "1.10.9",
5
5
  "description": "A powerful and flexible API client for Vue 3 applications, built with TypeScript and Zod for type-safe API interactions.",
6
6
  "keywords": [
7
7
  "vue3",