vue-api-kit 1.10.5 → 1.10.6

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