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