vue-api-kit 1.12.0 → 1.12.1

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.
Files changed (2) hide show
  1. package/dist/index.js +102 -119
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import k, { ZodError as F } from "zod";
2
2
  import * as G from "zod";
3
3
  import { ZodError as ne } from "zod";
4
- import _, { AxiosError as Z } from "axios";
4
+ import U, { AxiosError as Z } from "axios";
5
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";
6
+ import { nextTick as _, ref as p, onMounted as Q, watch as z, onBeforeUnmount as I } from "vue";
7
7
  import { debounce as N } from "lodash-es";
8
8
  function H(e) {
9
9
  return e && typeof e == "object" && e !== null && typeof e.path == "string";
@@ -11,44 +11,27 @@ function H(e) {
11
11
  function W(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, 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);
36
24
  });
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;
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);
46
29
  }
47
- e.append(h, String(s));
48
- }
30
+ else
31
+ e.append(E, a);
49
32
  }
50
33
  function K(e) {
51
- const s = _.create({
34
+ const a = U.create({
52
35
  baseURL: e.baseURL,
53
36
  headers: {
54
37
  "Content-Type": "application/json",
@@ -58,8 +41,8 @@ function K(e) {
58
41
  withCredentials: e.withCredentials ?? !1,
59
42
  withXSRFToken: e.withXSRFToken ?? !1
60
43
  });
61
- let h = !1, i = null;
62
- e.onBeforeRequest && s.interceptors.request.use(
44
+ let E = !1, i = null;
45
+ e.onBeforeRequest && a.interceptors.request.use(
63
46
  async (r) => {
64
47
  try {
65
48
  return await e.onBeforeRequest(r) || r;
@@ -68,7 +51,7 @@ function K(e) {
68
51
  }
69
52
  },
70
53
  (r) => Promise.reject(r)
71
- ), e.onStartRequest && s.interceptors.request.use(
54
+ ), e.onStartRequest && a.interceptors.request.use(
72
55
  async (r) => {
73
56
  try {
74
57
  return await e.onStartRequest(), r;
@@ -77,23 +60,23 @@ function K(e) {
77
60
  }
78
61
  },
79
62
  (r) => Promise.reject(r)
80
- ), e.onFinishRequest && s.interceptors.response.use(
63
+ ), e.onFinishRequest && a.interceptors.response.use(
81
64
  (r) => (e.onFinishRequest(), r),
82
65
  (r) => (e.onFinishRequest(), Promise.reject(r))
83
- ), s.interceptors.request.use((r) => {
66
+ ), a.interceptors.request.use((r) => {
84
67
  if (!r.url) return r;
85
68
  const u = (v) => {
86
69
  if (v)
87
- for (const [p, t] of Object.entries(v)) {
88
- const n = `{${p}}`;
89
- r.url.includes(n) && (r.url = r.url.replace(
90
- n,
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,
91
74
  encodeURIComponent(String(t))
92
- ), delete v[p]);
75
+ ), delete v[m]);
93
76
  }
94
77
  };
95
78
  return r.method !== "get" && r.data?.params && u(r.data.params), u(r.params), r;
96
- }), e.csrfRefreshEndpoint && s.interceptors.response.use(
79
+ }), e.csrfRefreshEndpoint && a.interceptors.response.use(
97
80
  (r) => r,
98
81
  async (r) => {
99
82
  const u = r.config;
@@ -102,81 +85,81 @@ function K(e) {
102
85
  if (r.response && (r.response.status === 403 || r.response.status === 419) && !u?._retry) {
103
86
  u._retry = !0;
104
87
  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);
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);
108
91
  } catch (v) {
109
- return h = !1, i = null, Promise.reject(v);
92
+ return E = !1, i = null, Promise.reject(v);
110
93
  }
111
94
  }
112
95
  return Promise.reject(r);
113
96
  }
114
- ), s.interceptors.response.use(
97
+ ), a.interceptors.response.use(
115
98
  (r) => r,
116
- (r) => (Q(() => {
99
+ (r) => (_(() => {
117
100
  r?.code;
118
101
  }), Promise.reject(r))
119
102
  );
120
103
  function A(r) {
121
104
  const u = {};
122
105
  for (const v in r) {
123
- const p = r[v];
124
- if (p)
125
- if (H(p)) {
126
- const t = p;
127
- u[v] = (n) => {
128
- let a;
129
- 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);
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);
131
114
  let B = new AbortController();
132
- const l = () => {
115
+ const c = () => {
133
116
  B?.abort(), B = new AbortController();
134
117
  }, f = async () => {
135
- w.value && l(), w.value = !0, m.value = void 0;
118
+ w.value && c(), w.value = !0, h.value = void 0;
136
119
  try {
137
- t.params && a?.params && t.params.parse(a.params);
138
- let o = a?.data;
120
+ t.params && n?.params && t.params.parse(n.params);
121
+ let o = n?.data;
139
122
  t.data && o && t.data.parse(o);
140
- const c = {
123
+ const l = {
141
124
  method: t.method ?? "GET",
142
125
  url: t.path,
143
- params: a?.params,
126
+ params: n?.params,
144
127
  signal: B.signal,
145
128
  headers: {}
146
129
  };
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);
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);
150
133
  }
151
- if (a?.onBeforeRequest) {
152
- const y = await a.onBeforeRequest(c);
153
- y !== void 0 && Object.assign(c, y);
134
+ if (n?.onBeforeRequest) {
135
+ const y = await n.onBeforeRequest(l);
136
+ y !== void 0 && Object.assign(l, y);
154
137
  }
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);
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);
158
141
  } catch (o) {
159
142
  if (o instanceof Z) {
160
143
  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 });
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 });
163
146
  }
164
147
  } else if (o instanceof F) {
165
148
  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
+ 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));
168
151
  } else {
169
- const c = o.message || "An error occurred";
170
- m.value = c, a?.onError?.(c), e.onError?.({ err: o, message: c });
152
+ const l = o.message || "An error occurred";
153
+ h.value = l, n?.onError?.(l), e.onError?.({ err: o, message: l });
171
154
  }
172
155
  } finally {
173
156
  w.value = !1, P.value = !0;
174
157
  }
175
- }, b = a?.debounce ? N(f, a.debounce) : f;
158
+ }, b = n?.debounce ? N(f, n.debounce) : f;
176
159
  let j = null;
177
- return (a?.params || a?.data) && (z(() => {
178
- j && j(), j = D(
179
- () => JSON.stringify({ params: a.params, data: a.data }),
160
+ return (n?.params || n?.data) && (Q(() => {
161
+ j && j(), j = z(
162
+ () => JSON.stringify({ params: n.params, data: n.data }),
180
163
  () => {
181
164
  b();
182
165
  },
@@ -184,34 +167,34 @@ function K(e) {
184
167
  );
185
168
  }), I(() => {
186
169
  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?.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 };
188
171
  };
189
- } else typeof p == "object" && (u[v] = A(p));
172
+ } else typeof m == "object" && (u[v] = A(m));
190
173
  }
191
174
  return u;
192
175
  }
193
- const R = e.queries ?? {}, x = A(R);
176
+ const q = e.queries ?? {}, x = A(q);
194
177
  function $(r) {
195
178
  const u = {};
196
179
  for (const v in r) {
197
- const p = r[v];
198
- if (p)
199
- if (W(p)) {
200
- 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) => {
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) => {
204
187
  if (!g.value) {
205
- g.value = !0, q.value = void 0, P.value = 0;
188
+ g.value = !0, R.value = void 0, P.value = 0;
206
189
  try {
207
- const { data: l = {}, params: f } = B ?? {};
208
- let b = l ?? {}, j = {};
190
+ const { data: c = {}, params: f } = B ?? {};
191
+ let b = c ?? {}, j = {};
209
192
  if (t.isMultipart) {
210
193
  const d = new FormData();
211
- for (const [y, U] of Object.entries(l))
212
- S(d, U, y);
194
+ for (const [y, D] of Object.entries(c))
195
+ S(d, D, y);
213
196
  b = d, j["Content-Type"] = "multipart/form-data";
214
- } else t.data && t.data.parse(l);
197
+ } else t.data && t.data.parse(c);
215
198
  t.params && f && t.params.parse(f);
216
199
  const o = {
217
200
  method: t.method,
@@ -222,7 +205,7 @@ function K(e) {
222
205
  onUploadProgress: (d) => {
223
206
  if (d.total) {
224
207
  const y = Math.round(d.loaded * 100 / d.total);
225
- P.value = y, n?.onUploadProgress?.(y);
208
+ P.value = y, s?.onUploadProgress?.(y);
226
209
  }
227
210
  }
228
211
  };
@@ -230,24 +213,24 @@ function K(e) {
230
213
  const d = await t.onBeforeRequest(o);
231
214
  d !== void 0 && Object.assign(o, d);
232
215
  }
233
- if (n?.onBeforeRequest) {
234
- const d = await n.onBeforeRequest(o);
216
+ if (s?.onBeforeRequest) {
217
+ const d = await s.onBeforeRequest(o);
235
218
  d !== void 0 && Object.assign(o, d);
236
219
  }
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));
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));
248
231
  } else {
249
- const f = l.message || "An error occurred";
250
- q.value = f, n?.onError?.(l), e.onError?.({ err: l, message: f });
232
+ const f = c.message || "An error occurred";
233
+ R.value = f, s?.onError?.(c), e.onError?.({ err: c, message: f });
251
234
  }
252
235
  } finally {
253
236
  g.value = !1, w.value = !0;
@@ -255,7 +238,7 @@ function K(e) {
255
238
  }
256
239
  } };
257
240
  };
258
- } else typeof p == "object" && (u[v] = $(p));
241
+ } else typeof m == "object" && (u[v] = $(m));
259
242
  }
260
243
  return u;
261
244
  }
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.12.1",
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",