vue-api-kit 1.12.1 → 1.13.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
  /**
package/dist/index.js CHANGED
@@ -1,37 +1,39 @@
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 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) {
9
9
  return e && typeof e == "object" && e !== null && typeof e.path == "string";
10
10
  }
11
- function W(e) {
11
+ function X(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 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);
24
26
  });
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);
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);
29
31
  }
30
32
  else
31
- e.append(E, a);
33
+ e.append(h, o);
32
34
  }
33
- function K(e) {
34
- const a = U.create({
35
+ function O(e) {
36
+ const o = Q.create({
35
37
  baseURL: e.baseURL,
36
38
  headers: {
37
39
  "Content-Type": "application/json",
@@ -41,232 +43,232 @@ function K(e) {
41
43
  withCredentials: e.withCredentials ?? !1,
42
44
  withXSRFToken: e.withXSRFToken ?? !1
43
45
  });
44
- let E = !1, i = null;
45
- e.onBeforeRequest && a.interceptors.request.use(
46
+ let h = !1, g = null;
47
+ e.onBeforeRequest && o.interceptors.request.use(
46
48
  async (r) => {
47
49
  try {
48
50
  return await e.onBeforeRequest(r) || r;
49
- } catch (u) {
50
- return Promise.reject(u);
51
+ } catch (i) {
52
+ return Promise.reject(i);
51
53
  }
52
54
  },
53
55
  (r) => Promise.reject(r)
54
- ), e.onStartRequest && a.interceptors.request.use(
56
+ ), e.onStartRequest && o.interceptors.request.use(
55
57
  async (r) => {
56
58
  try {
57
59
  return await e.onStartRequest(), r;
58
- } catch (u) {
59
- return Promise.reject(u);
60
+ } catch (i) {
61
+ return Promise.reject(i);
60
62
  }
61
63
  },
62
64
  (r) => Promise.reject(r)
63
- ), e.onFinishRequest && a.interceptors.response.use(
65
+ ), e.onFinishRequest && o.interceptors.response.use(
64
66
  (r) => (e.onFinishRequest(), r),
65
67
  (r) => (e.onFinishRequest(), Promise.reject(r))
66
- ), a.interceptors.request.use((r) => {
68
+ ), o.interceptors.request.use((r) => {
67
69
  if (!r.url) return r;
68
- const u = (v) => {
70
+ const i = (v) => {
69
71
  if (v)
70
- for (const [m, t] of Object.entries(v)) {
71
- const s = `{${m}}`;
72
- r.url.includes(s) && (r.url = r.url.replace(
73
- s,
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,
74
76
  encodeURIComponent(String(t))
75
- ), delete v[m]);
77
+ ), delete v[p]);
76
78
  }
77
79
  };
78
- return r.method !== "get" && r.data?.params && u(r.data.params), u(r.params), r;
79
- }), e.csrfRefreshEndpoint && a.interceptors.response.use(
80
+ return r.method !== "get" && r.data?.params && i(r.data.params), i(r.params), r;
81
+ }), e.csrfRefreshEndpoint && o.interceptors.response.use(
80
82
  (r) => r,
81
83
  async (r) => {
82
- const u = r.config;
83
- if (u?.url === e.csrfRefreshEndpoint)
84
+ const i = r.config;
85
+ if (i?.url === e.csrfRefreshEndpoint)
84
86
  return Promise.reject(r);
85
- if (r.response && (r.response.status === 403 || r.response.status === 419) && !u?._retry) {
86
- u._retry = !0;
87
+ if (r.response && (r.response.status === 403 || r.response.status === 419) && !i?._retry) {
88
+ i._retry = !0;
87
89
  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);
90
+ return h && g ? await g : (h = !0, g = o.get(e.csrfRefreshEndpoint).then(() => {
91
+ h = !1, g = null;
92
+ }), await g), o.request(i);
91
93
  } catch (v) {
92
- return E = !1, i = null, Promise.reject(v);
94
+ return h = !1, g = null, Promise.reject(v);
93
95
  }
94
96
  }
95
97
  return Promise.reject(r);
96
98
  }
97
- ), a.interceptors.response.use(
99
+ ), o.interceptors.response.use(
98
100
  (r) => r,
99
- (r) => (_(() => {
101
+ (r) => (z(() => {
100
102
  r?.code;
101
103
  }), Promise.reject(r))
102
104
  );
103
- function A(r) {
104
- const u = {};
105
+ function c(r) {
106
+ const i = {};
105
107
  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();
108
+ const p = r[v];
109
+ if (p)
110
+ if (W(p)) {
111
+ const t = p;
112
+ i[v] = (n) => {
113
+ 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();
117
119
  }, f = async () => {
118
- w.value && c(), w.value = !0, h.value = void 0;
120
+ P.value && u(), P.value = !0, m.value = void 0;
119
121
  try {
120
- t.params && n?.params && t.params.parse(n.params);
121
- let o = n?.data;
122
- t.data && o && t.data.parse(o);
122
+ t.params && a?.params && t.params.parse(a.params);
123
+ let s = a?.data;
124
+ t.data && s && t.data.parse(s);
123
125
  const l = {
124
126
  method: t.method ?? "GET",
125
127
  url: t.path,
126
- params: n?.params,
127
- signal: B.signal,
128
+ params: a?.params,
129
+ signal: k.signal,
128
130
  headers: {}
129
131
  };
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);
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);
133
135
  }
134
- if (n?.onBeforeRequest) {
135
- const y = await n.onBeforeRequest(l);
136
- y !== void 0 && Object.assign(l, y);
136
+ if (a?.onBeforeRequest) {
137
+ const R = await a.onBeforeRequest(l);
138
+ R !== void 0 && Object.assign(l, R);
137
139
  }
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);
141
- } 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 });
140
+ 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 });
146
148
  }
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
+ } 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));
151
153
  } else {
152
- const l = o.message || "An error occurred";
153
- h.value = l, n?.onError?.(l), e.onError?.({ err: o, message: l });
154
+ const l = s.message || "An error occurred";
155
+ m.value = l, a?.onError?.(l), e.onError?.({ err: s, message: l });
154
156
  }
155
157
  } finally {
156
- w.value = !1, P.value = !0;
158
+ P.value = !1, A.value = !0;
157
159
  }
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 }),
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(
164
+ () => JSON.stringify({ params: a.params, data: a.data }),
163
165
  () => {
164
- b();
166
+ j();
165
167
  },
166
168
  { immediate: !1 }
167
169
  );
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 };
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 };
171
173
  };
172
- } else typeof m == "object" && (u[v] = A(m));
174
+ } else typeof p == "object" && (i[v] = c(p));
173
175
  }
174
- return u;
176
+ return i;
175
177
  }
176
- const q = e.queries ?? {}, x = A(q);
178
+ const M = e.queries ?? {}, q = c(M);
177
179
  function $(r) {
178
- const u = {};
180
+ const i = {};
179
181
  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;
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) => {
189
+ if (!y.value) {
190
+ y.value = !0, b.value = void 0, A.value = 0;
189
191
  try {
190
- const { data: c = {}, params: f } = B ?? {};
191
- let b = c ?? {}, j = {};
192
+ const { data: u = {}, params: f } = k ?? {};
193
+ let j = u ?? {}, C = {};
192
194
  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);
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";
199
+ } else t.data && t.data.parse(u);
198
200
  t.params && f && t.params.parse(f);
199
- const o = {
201
+ const s = {
200
202
  method: t.method,
201
203
  url: t.path,
202
- data: b,
204
+ data: j,
203
205
  params: f,
204
- headers: j,
206
+ headers: C,
205
207
  onUploadProgress: (d) => {
206
208
  if (d.total) {
207
- const y = Math.round(d.loaded * 100 / d.total);
208
- P.value = y, s?.onUploadProgress?.(y);
209
+ const R = Math.round(d.loaded * 100 / d.total);
210
+ A.value = R, n?.onUploadProgress?.(R);
209
211
  }
210
212
  }
211
213
  };
212
214
  if (t.onBeforeRequest) {
213
- const d = await t.onBeforeRequest(o);
214
- d !== void 0 && Object.assign(o, d);
215
+ const d = await t.onBeforeRequest(s);
216
+ d !== void 0 && Object.assign(s, d);
215
217
  }
216
- if (s?.onBeforeRequest) {
217
- const d = await s.onBeforeRequest(o);
218
- d !== void 0 && Object.assign(o, d);
218
+ if (n?.onBeforeRequest) {
219
+ const d = await n.onBeforeRequest(s);
220
+ d !== void 0 && Object.assign(s, d);
219
221
  }
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));
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);
225
+ } catch (u) {
226
+ if (u instanceof x) {
227
+ 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) {
230
+ 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));
231
233
  } else {
232
- const f = c.message || "An error occurred";
233
- R.value = f, s?.onError?.(c), e.onError?.({ err: c, message: f });
234
+ const f = u.message || "An error occurred";
235
+ b.value = f, n?.onError?.(u), e.onError?.({ err: u, message: f });
234
236
  }
235
237
  } finally {
236
- g.value = !1, w.value = !0;
238
+ y.value = !1, P.value = !0;
237
239
  }
238
240
  }
239
241
  } };
240
242
  };
241
- } else typeof m == "object" && (u[v] = $(m));
243
+ } else typeof p == "object" && (i[v] = $(p));
242
244
  }
243
- return u;
245
+ return i;
244
246
  }
245
247
  const T = e.mutations ?? {}, L = $(T);
246
248
  return {
247
- query: x,
249
+ query: q,
248
250
  mutation: L
249
251
  };
250
252
  }
251
- function O(e) {
253
+ function ee(e) {
252
254
  return e;
253
255
  }
254
- function ee(e) {
256
+ function re(e) {
255
257
  return e;
256
258
  }
257
- function re(...e) {
259
+ function te(...e) {
258
260
  return Object.assign({}, ...e);
259
261
  }
260
- function te(...e) {
262
+ function oe(...e) {
261
263
  return Object.assign({}, ...e);
262
264
  }
263
265
  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
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
272
274
  };
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.13.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",