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