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