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.
@@ -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 { $ZodIssue } from 'zod/v4/core';
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
- data?: any;
137
+ response?: any;
137
138
  url?: string;
138
139
  }) => void;
139
- onZodError?: (issues: Omit<$ZodIssue, "input">[]) => void;
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?: (issues: Omit<$ZodIssue, "input">[]) => void;
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?: (issues: Omit<$ZodIssue, "input">[]) => void;
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
- zodErrors: Ref<Omit<$ZodIssue, "input">[] | undefined>;
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
- zodErrors: Ref<Omit<$ZodIssue, "input">[] | undefined>;
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 S } from "zod";
2
- import * as H from "zod";
3
- import N, { AxiosError as F } from "axios";
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 R, onMounted as U, watch as _, onBeforeUnmount as I } from "vue";
6
- import { debounce as Q } from "lodash-es";
7
- function z(e) {
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 V(e) {
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 K(e) {
14
- const y = N.create({
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 P = !1, b = null;
25
- e.onBeforeRequest && y.interceptors.request.use(
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 && y.interceptors.request.use(
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 && y.interceptors.response.use(
43
+ ), e.onFinishRequest && h.interceptors.response.use(
44
44
  (r) => (e.onFinishRequest(), r),
45
45
  (r) => (e.onFinishRequest(), Promise.reject(r))
46
- ), y.interceptors.request.use((r) => {
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, t] of Object.entries(m)) {
51
- const a = `{${d}}`;
52
- r.url.includes(a) && (r.url = r.url.replace(
53
- a,
54
- encodeURIComponent(String(t))
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 && y.interceptors.response.use(
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 P && b ? await b : (P = !0, b = y.get(e.csrfRefreshEndpoint).then(() => {
69
- P = !1, b = null;
70
- }), await b), y.request(i);
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 P = !1, b = null, Promise.reject(m);
72
+ return B = !1, A = null, Promise.reject(m);
73
73
  }
74
74
  }
75
75
  return Promise.reject(r);
76
76
  }
77
- ), y.interceptors.response.use(
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 (z(d)) {
89
- const t = d;
90
- i[m] = (a) => {
91
- let o;
92
- a && typeof a == "object" && ("loadOnMount" in a || "debounce" in a || "onResult" in a || "onError" in a || "onZodError" in a || "onBeforeRequest" in a || "params" in a || "data" in a ? o = a : o = { params: a });
93
- const C = R(), g = R(), q = R(), j = R(!1), A = R(!1), B = R(!0);
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 u = () => {
95
+ const n = () => {
96
96
  w?.abort(), w = new AbortController();
97
97
  }, l = async () => {
98
- j.value && u(), j.value = !0, g.value = void 0;
98
+ C.value && n(), C.value = !0, p.value = void 0;
99
99
  try {
100
- t.params && o?.params && t.params.parse(o.params);
101
- let s = o?.data;
102
- t.data && s && t.data.parse(s);
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: t.method ?? "GET",
105
- url: t.path,
106
- params: o?.params,
104
+ method: s.method ?? "GET",
105
+ url: s.path,
106
+ params: a?.params,
107
107
  signal: w.signal
108
108
  };
109
- if (t.method === "POST" && s && (c.data = s), t.onBeforeRequest) {
110
- const f = await t.onBeforeRequest(c);
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 (o?.onBeforeRequest) {
114
- const f = await o.onBeforeRequest(c);
113
+ if (a?.onBeforeRequest) {
114
+ const f = await a.onBeforeRequest(c);
115
115
  f !== void 0 && Object.assign(c, f);
116
116
  }
117
- const h = await y.request(c), n = t.response ? t.response.parse(h.data) : h.data;
118
- C.value = n, o?.onResult?.(n);
119
- } catch (s) {
120
- if (s instanceof F) {
121
- if (s.code !== "ERR_CANCELED") {
122
- const c = s.config?.url, h = s.response?.data?.message || s.message || "An error occurred", n = s.response?.status, f = s.code, v = s.response?.data;
123
- g.value = h, o?.onError?.(s), e.onErrorRequest?.({ message: h, status: n, code: f, data: v, url: c });
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 (s instanceof S) {
126
- q.value = s.issues || [];
127
- const h = `Validation error: ${q.value.map(
128
- (n) => `${n.path.join(".")}: ${n.message}`
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 = s.message || "An error occurred";
133
- g.value = c, o?.onError?.(c), e.onErrorRequest?.({ message: c });
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
- j.value = !1, A.value = !0;
135
+ C.value = !1, j.value = !0;
137
136
  }
138
- }, E = o?.debounce ? Q(l, o.debounce) : l;
139
- let p = null;
140
- return (o?.params || o?.data) && (U(() => {
141
- p && p(), p = _(
142
- () => JSON.stringify({ params: o.params, data: o.data }),
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
- E();
143
+ y();
145
144
  },
146
145
  { immediate: !1 }
147
146
  );
148
- }), I(() => {
149
- p && p(), w?.abort();
150
- })), (o?.loadOnMount === void 0 || o.loadOnMount) && !A.value && (B.value ? (B.value = !1, l()) : E()), { result: C, errorMessage: g, zodErrors: q, isLoading: j, isDone: A, refetch: l };
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 L = e.queries ?? {}, T = D(L);
157
- function k(r) {
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 (V(d)) {
163
- const t = d;
164
- i[m] = (a) => {
165
- const o = R(), C = R(), g = R(), q = R(!1), j = R(!1), A = R(0);
166
- return { result: o, errorMessage: C, zodErrors: g, isLoading: q, isDone: j, uploadProgress: A, mutate: async (w) => {
167
- if (!q.value) {
168
- q.value = !0, C.value = void 0, A.value = 0;
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: u = {}, params: l } = w ?? {};
171
- let E = u ?? {}, p = {};
172
- if (t.isMultipart) {
173
- const n = new FormData();
174
- for (const [f, v] of Object.entries(u))
175
- v instanceof File || v instanceof Blob ? n.append(f, v) : Array.isArray(v) ? v.forEach((M) => {
176
- M instanceof File || M instanceof Blob ? n.append(f, M) : n.append(f, JSON.stringify(M));
177
- }) : typeof v == "object" && v !== null ? n.append(f, JSON.stringify(v)) : n.append(f, String(v));
178
- E = n, p["Content-Type"] = "multipart/form-data";
179
- } else t.data && t.data.parse(u);
180
- t.params && l && t.params.parse(l);
181
- const s = {
182
- method: t.method,
183
- url: t.path,
184
- data: E,
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: p,
187
- onUploadProgress: (n) => {
188
- if (n.total) {
189
- const f = Math.round(n.loaded * 100 / n.total);
190
- A.value = f, a?.onUploadProgress?.(f);
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 (t.onBeforeRequest) {
195
- const n = await t.onBeforeRequest(s);
196
- n !== void 0 && Object.assign(s, n);
193
+ if (s.onBeforeRequest) {
194
+ const u = await s.onBeforeRequest(t);
195
+ u !== void 0 && Object.assign(t, u);
197
196
  }
198
- if (a?.onBeforeRequest) {
199
- const n = await a.onBeforeRequest(s);
200
- n !== void 0 && Object.assign(s, n);
197
+ if (o?.onBeforeRequest) {
198
+ const u = await o.onBeforeRequest(t);
199
+ u !== void 0 && Object.assign(t, u);
201
200
  }
202
- const c = await y.request(s), h = t.response ? t.response.parse(c.data) : c.data;
203
- o.value = h, a?.onResult?.(h);
204
- } catch (u) {
205
- if (u instanceof F) {
206
- const l = u.response?.data?.message || u.message || "An error occurred", E = u.response?.status, p = u.code;
207
- C.value = l, a?.onError?.(u), e.onErrorRequest?.({ message: l, status: E, code: p });
208
- } else if (u instanceof S) {
209
- g.value = u.issues || [];
210
- const E = `Validation error: ${g.value.map(
211
- (p) => `${p.path.join(".")}: ${p.message}`
212
- ).join(", ")}`;
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 = u.message || "An error occurred";
216
- C.value = l, a?.onError?.(u), e.onErrorRequest?.({ message: l });
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
- q.value = !1, j.value = !0;
217
+ g.value = !1, C.value = !0;
220
218
  }
221
219
  }
222
220
  } };
223
221
  };
224
- } else typeof d == "object" && (i[m] = k(d));
222
+ } else typeof d == "object" && (i[m] = S(d));
225
223
  }
226
224
  return i;
227
225
  }
228
- const Z = e.mutations ?? {}, x = k(Z);
226
+ const _ = e.mutations ?? {}, x = S(_);
229
227
  return {
230
- query: T,
228
+ query: Z,
231
229
  mutation: x
232
230
  };
233
231
  }
234
- function Y(e) {
232
+ function O(e) {
235
233
  return e;
236
234
  }
237
- function O(e) {
235
+ function ee(e) {
238
236
  return e;
239
237
  }
240
- function ee(...e) {
238
+ function re(...e) {
241
239
  return Object.assign({}, ...e);
242
240
  }
243
- function re(...e) {
241
+ function te(...e) {
244
242
  return Object.assign({}, ...e);
245
243
  }
246
244
  export {
247
245
  ae as AxiosError,
248
- K as createApiClient,
249
- O as defineMutation,
250
- Y as defineQuery,
251
- re as mergeMutations,
252
- ee as mergeQueries,
253
- H as z
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vue-api-kit",
3
3
  "type": "module",
4
- "version": "1.10.4",
4
+ "version": "1.10.5",
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",