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