vue-api-kit 1.10.10 → 1.11.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 +52 -51
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import k, { ZodError as x } from "zod";
1
+ import k, { ZodError as Z } from "zod";
2
2
  import * as I from "zod";
3
3
  import { ZodError as ne } from "zod";
4
- import U, { AxiosError as S } from "axios";
4
+ import U, { AxiosError as x } from "axios";
5
5
  import { AxiosError as ie } from "axios";
6
- import { nextTick as _, ref as g, onMounted as Q, watch as z, onBeforeUnmount as N } from "vue";
6
+ import { nextTick as _, ref as p, onMounted as Q, watch as z, onBeforeUnmount as N } from "vue";
7
7
  import { debounce as H } from "lodash-es";
8
8
  function W(e) {
9
9
  return e && typeof e == "object" && e !== null && typeof e.path == "string";
@@ -72,11 +72,11 @@ function K(e) {
72
72
  if (!r.url) return r;
73
73
  const i = (v) => {
74
74
  if (v)
75
- for (const [m, o] of Object.entries(v)) {
75
+ for (const [m, t] of Object.entries(v)) {
76
76
  const s = `{${m}}`;
77
77
  r.url.includes(s) && (r.url = r.url.replace(
78
78
  s,
79
- encodeURIComponent(String(o))
79
+ encodeURIComponent(String(t))
80
80
  ), delete v[m]);
81
81
  }
82
82
  };
@@ -111,50 +111,51 @@ function K(e) {
111
111
  const m = r[v];
112
112
  if (m)
113
113
  if (W(m)) {
114
- const o = m;
114
+ const t = m;
115
115
  i[v] = (s) => {
116
116
  let n;
117
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 = g(), E = g(), p = g(), P = g(!1), A = g(!1), M = g(!0);
118
+ const R = p(), E = p(), g = p(), P = p(!1), A = p(!1), M = p(!0);
119
119
  let B = new AbortController();
120
120
  const u = () => {
121
121
  B?.abort(), B = new AbortController();
122
122
  }, f = async () => {
123
123
  P.value && u(), P.value = !0, E.value = void 0;
124
124
  try {
125
- o.params && n?.params && o.params.parse(n.params);
126
- let t = n?.data;
127
- o.data && t && o.data.parse(t);
125
+ t.params && n?.params && t.params.parse(n.params);
126
+ let o = n?.data;
127
+ t.data && o && t.data.parse(o);
128
128
  const c = {
129
- method: o.method ?? "GET",
130
- url: o.path,
129
+ method: t.method ?? "GET",
130
+ url: t.path,
131
131
  params: n?.params,
132
- signal: B.signal
132
+ signal: B.signal,
133
+ headers: {}
133
134
  };
134
- if (o.method === "POST" && t && (c.data = t), o.onBeforeRequest) {
135
- const y = await o.onBeforeRequest(c);
135
+ if (t.method === "POST" && o && (c.data = o), t.onBeforeRequest) {
136
+ const y = await t.onBeforeRequest(c);
136
137
  y !== void 0 && Object.assign(c, y);
137
138
  }
138
139
  if (n?.onBeforeRequest) {
139
140
  const y = await n.onBeforeRequest(c);
140
141
  y !== void 0 && Object.assign(c, y);
141
142
  }
142
- E.value = void 0, p.value = void 0;
143
- const j = await a.request(c), d = o.response ? o.response.parse(j.data) : j.data;
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;
144
145
  R.value = d, n?.onResult?.(d);
145
- } catch (t) {
146
- if (t instanceof S) {
147
- if (t.code !== "ERR_CANCELED") {
148
- const c = t.response?.data?.message || t.message || "An error occurred";
149
- E.value = c, n?.onError?.(t), e.onError?.({ err: t, message: c });
146
+ } catch (o) {
147
+ if (o instanceof x) {
148
+ 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 });
150
151
  }
151
- } else if (t instanceof x) {
152
- p.value = k.flattenError(t);
153
- const c = Object.keys(p.value.fieldErrors).length, j = `${Object.values(p.value.fieldErrors).at(0)}.${c > 1 ? ` (and ${c - 1} more errors)` : ""}`;
154
- E.value = j, n?.onError?.(t), n?.onZodError?.(k.flattenError(t)), e.onError?.({ err: t, message: j }), e.onZodError && e.onZodError(k.flattenError(t));
152
+ } else if (o instanceof Z) {
153
+ 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));
155
156
  } else {
156
- const c = t.message || "An error occurred";
157
- E.value = c, n?.onError?.(c), e.onError?.({ err: t, message: c });
157
+ const c = o.message || "An error occurred";
158
+ E.value = c, n?.onError?.(c), e.onError?.({ err: o, message: c });
158
159
  }
159
160
  } finally {
160
161
  P.value = !1, A.value = !0;
@@ -171,38 +172,38 @@ function K(e) {
171
172
  );
172
173
  }), N(() => {
173
174
  b && b(), B?.abort();
174
- })), (n?.loadOnMount === void 0 || n.loadOnMount) && !A.value && (M.value ? (M.value = !1, f()) : q()), { result: R, errorMessage: E, zodError: p, isLoading: P, isDone: A, refetch: f };
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 };
175
176
  };
176
177
  } else typeof m == "object" && (i[v] = C(m));
177
178
  }
178
179
  return i;
179
180
  }
180
- const w = e.queries ?? {}, Z = C(w);
181
+ const w = e.queries ?? {}, S = C(w);
181
182
  function F(r) {
182
183
  const i = {};
183
184
  for (const v in r) {
184
185
  const m = r[v];
185
186
  if (m)
186
187
  if (X(m)) {
187
- const o = m;
188
+ const t = m;
188
189
  i[v] = (s) => {
189
- const n = g(), R = g(), E = g(), p = g(!1), P = g(!1), A = g(0);
190
- return { result: n, errorMessage: R, zodError: E, isLoading: p, isDone: P, uploadProgress: A, mutate: async (B) => {
191
- if (!p.value) {
192
- p.value = !0, R.value = void 0, A.value = 0;
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) => {
192
+ if (!g.value) {
193
+ g.value = !0, R.value = void 0, A.value = 0;
193
194
  try {
194
195
  const { data: u = {}, params: f } = B ?? {};
195
196
  let q = u ?? {}, b = {};
196
- if (o.isMultipart) {
197
+ if (t.isMultipart) {
197
198
  const d = new FormData();
198
199
  for (const [y, D] of Object.entries(u))
199
200
  $(d, D, y);
200
201
  q = d, b["Content-Type"] = "multipart/form-data";
201
- } else o.data && o.data.parse(u);
202
- o.params && f && o.params.parse(f);
203
- const t = {
204
- method: o.method,
205
- url: o.path,
202
+ } else t.data && t.data.parse(u);
203
+ t.params && f && t.params.parse(f);
204
+ const o = {
205
+ method: t.method,
206
+ url: t.path,
206
207
  data: q,
207
208
  params: f,
208
209
  headers: b,
@@ -213,22 +214,22 @@ function K(e) {
213
214
  }
214
215
  }
215
216
  };
216
- if (o.onBeforeRequest) {
217
- const d = await o.onBeforeRequest(t);
218
- d !== void 0 && Object.assign(t, d);
217
+ if (t.onBeforeRequest) {
218
+ const d = await t.onBeforeRequest(o);
219
+ d !== void 0 && Object.assign(o, d);
219
220
  }
220
221
  if (s?.onBeforeRequest) {
221
- const d = await s.onBeforeRequest(t);
222
- d !== void 0 && Object.assign(t, d);
222
+ const d = await s.onBeforeRequest(o);
223
+ d !== void 0 && Object.assign(o, d);
223
224
  }
224
225
  R.value = void 0, E.value = void 0;
225
- const c = await a.request(t), j = o.response ? o.response.parse(c.data) : c.data;
226
+ const c = await a.request(o), j = t.response ? t.response.parse(c.data) : c.data;
226
227
  n.value = j, s?.onResult?.(j);
227
228
  } catch (u) {
228
- if (u instanceof S) {
229
+ if (u instanceof x) {
229
230
  const f = u.response?.data?.message || u.message || "An error occurred";
230
231
  R.value = f, s?.onError?.(u), e.onError?.({ err: u, message: f });
231
- } else if (u instanceof x) {
232
+ } else if (u instanceof Z) {
232
233
  E.value = k.flattenError(u);
233
234
  const f = Object.keys(E.value.fieldErrors).length, q = `${Object.values(E.value.fieldErrors).at(0)}.${f > 1 ? ` (and ${f - 1} more errors)` : ""}`;
234
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,7 +238,7 @@ function K(e) {
237
238
  R.value = f, s?.onError?.(u), e.onError?.({ err: u, message: f });
238
239
  }
239
240
  } finally {
240
- p.value = !1, P.value = !0;
241
+ g.value = !1, P.value = !0;
241
242
  }
242
243
  }
243
244
  } };
@@ -248,7 +249,7 @@ function K(e) {
248
249
  }
249
250
  const T = e.mutations ?? {}, L = F(T);
250
251
  return {
251
- query: Z,
252
+ query: S,
252
253
  mutation: L
253
254
  };
254
255
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vue-api-kit",
3
3
  "type": "module",
4
- "version": "1.10.10",
4
+ "version": "1.11.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",