vue-api-kit 1.11.0 → 1.12.0

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