@citruslime/vue-utils 2.0.2-beta.2 → 2.0.2-beta.3

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.
@@ -1,300 +1,305 @@
1
- import { replacePlaceholders as Y } from "@citruslime/utils";
2
- import { ref as H, readonly as J, toRef as ae, customRef as ue, getCurrentScope as le, onScopeDispose as se, unref as ce, watch as W, nextTick as Z, isRef as ne, shallowRef as q, computed as fe } from "vue";
3
- const Ee = [
1
+ import { replacePlaceholders as X } from "@citruslime/utils";
2
+ import { shallowRef as E, readonly as W, toRef as ae, ref as ie, customRef as le, toValue as P, getCurrentScope as se, onScopeDispose as ue, watch as M, nextTick as Y, isRef as te, computed as ce } from "vue";
3
+ const Oe = [
4
4
  "GET",
5
5
  "POST",
6
6
  "PUT",
7
7
  "DELETE"
8
8
  ];
9
- function Pe(e, t, u = null, l = null) {
9
+ function Ee(e, t, i = null, s = null) {
10
10
  return {
11
11
  url: e,
12
12
  type: t,
13
- headers: u ?? oe,
14
- hasBody: de(t, l)
13
+ headers: i ?? ne,
14
+ hasBody: fe(t, s)
15
15
  };
16
16
  }
17
- function oe() {
17
+ function ne() {
18
18
  return {
19
19
  accept: "application/json",
20
20
  "content-type": "application/json"
21
21
  };
22
22
  }
23
- function de(e, t) {
23
+ function fe(e, t) {
24
24
  return !!(t === null && (e === "PUT" || e === "POST") || t);
25
25
  }
26
- function re(e) {
27
- return le() ? (se(e), !0) : !1;
26
+ function oe(e) {
27
+ return se() ? (ue(e), !0) : !1;
28
28
  }
29
29
  function I() {
30
- const e = /* @__PURE__ */ new Set(), t = (a) => {
31
- e.delete(a);
30
+ const e = /* @__PURE__ */ new Set(), t = (o) => {
31
+ e.delete(o);
32
32
  };
33
33
  return {
34
- on: (a) => {
35
- e.add(a);
36
- const n = () => t(a);
37
- return re(n), {
34
+ on: (o) => {
35
+ e.add(o);
36
+ const n = () => t(o);
37
+ return oe(n), {
38
38
  off: n
39
39
  };
40
40
  },
41
41
  off: t,
42
- trigger: (...a) => Promise.all(Array.from(e).map((n) => n(...a)))
42
+ trigger: (...o) => Promise.all(Array.from(e).map((n) => n(...o))),
43
+ clear: () => {
44
+ e.clear();
45
+ }
43
46
  };
44
47
  }
45
- function P(e) {
46
- return typeof e == "function" ? e() : ce(e);
47
- }
48
- const ie = typeof window < "u" && typeof document < "u";
48
+ const re = typeof window < "u" && typeof document < "u";
49
49
  typeof WorkerGlobalScope < "u" && globalThis instanceof WorkerGlobalScope;
50
- const pe = () => {
50
+ const de = () => {
51
51
  };
52
- function ee(e, t = !1, u = "Timeout") {
53
- return new Promise((l, a) => {
54
- setTimeout(t ? () => a(u) : l, e);
55
- });
56
- }
57
- function he(e, ...t) {
58
- return t.some((u) => u in e);
59
- }
60
52
  function L(...e) {
61
53
  if (e.length !== 1)
62
54
  return ae(...e);
63
55
  const t = e[0];
64
- return typeof t == "function" ? J(ue(() => ({ get: t, set: pe }))) : H(t);
56
+ return typeof t == "function" ? W(le(() => ({ get: t, set: de }))) : ie(t);
57
+ }
58
+ function Z(e, t = !1, i = "Timeout") {
59
+ return new Promise((s, y) => {
60
+ setTimeout(t ? () => y(i) : s, e);
61
+ });
62
+ }
63
+ function pe(e, ...t) {
64
+ return t.some((i) => i in e);
65
65
  }
66
66
  function z(e, t = !1) {
67
- function u(o, { flush: f = "sync", deep: i = !1, timeout: h, throwOnTimeout: b } = {}) {
68
- let y = null;
67
+ function i(r, { flush: c = "sync", deep: a = !1, timeout: p, throwOnTimeout: b } = {}) {
68
+ let h = null;
69
69
  const D = [new Promise((F) => {
70
- y = W(
70
+ h = M(
71
71
  e,
72
72
  (w) => {
73
- o(w) !== t && (y ? y() : Z(() => y == null ? void 0 : y()), F(w));
73
+ r(w) !== t && (h ? h() : Y(() => h == null ? void 0 : h()), F(w));
74
74
  },
75
75
  {
76
- flush: f,
77
- deep: i,
76
+ flush: c,
77
+ deep: a,
78
78
  immediate: !0
79
79
  }
80
80
  );
81
81
  })];
82
- return h != null && D.push(
83
- ee(h, b).then(() => P(e)).finally(() => y == null ? void 0 : y())
82
+ return p != null && D.push(
83
+ Z(p, b).then(() => P(e)).finally(() => h == null ? void 0 : h())
84
84
  ), Promise.race(D);
85
85
  }
86
- function l(o, f) {
87
- if (!ne(o))
88
- return u((w) => w === o, f);
89
- const { flush: i = "sync", deep: h = !1, timeout: b, throwOnTimeout: y } = f ?? {};
90
- let m = null;
86
+ function s(r, c) {
87
+ if (!te(r))
88
+ return i((w) => w === r, c);
89
+ const { flush: a = "sync", deep: p = !1, timeout: b, throwOnTimeout: h } = c ?? {};
90
+ let T = null;
91
91
  const F = [new Promise((w) => {
92
- m = W(
93
- [e, o],
92
+ T = M(
93
+ [e, r],
94
94
  ([O, j]) => {
95
- t !== (O === j) && (m ? m() : Z(() => m == null ? void 0 : m()), w(O));
95
+ t !== (O === j) && (T ? T() : Y(() => T == null ? void 0 : T()), w(O));
96
96
  },
97
97
  {
98
- flush: i,
99
- deep: h,
98
+ flush: a,
99
+ deep: p,
100
100
  immediate: !0
101
101
  }
102
102
  );
103
103
  })];
104
104
  return b != null && F.push(
105
- ee(b, y).then(() => P(e)).finally(() => (m == null || m(), P(e)))
105
+ Z(b, h).then(() => P(e)).finally(() => (T == null || T(), P(e)))
106
106
  ), Promise.race(F);
107
107
  }
108
- function a(o) {
109
- return u((f) => !!f, o);
108
+ function y(r) {
109
+ return i((c) => !!c, r);
110
110
  }
111
- function n(o) {
112
- return l(null, o);
111
+ function o(r) {
112
+ return s(null, r);
113
113
  }
114
- function r(o) {
115
- return l(void 0, o);
114
+ function n(r) {
115
+ return s(void 0, r);
116
116
  }
117
- function p(o) {
118
- return u(Number.isNaN, o);
117
+ function d(r) {
118
+ return i(Number.isNaN, r);
119
119
  }
120
- function g(o, f) {
121
- return u((i) => {
122
- const h = Array.from(i);
123
- return h.includes(o) || h.includes(P(o));
124
- }, f);
120
+ function g(r, c) {
121
+ return i((a) => {
122
+ const p = Array.from(a);
123
+ return p.includes(r) || p.includes(P(r));
124
+ }, c);
125
125
  }
126
- function s(o) {
127
- return T(1, o);
126
+ function l(r) {
127
+ return m(1, r);
128
128
  }
129
- function T(o = 1, f) {
130
- let i = -1;
131
- return u(() => (i += 1, i >= o), f);
129
+ function m(r = 1, c) {
130
+ let a = -1;
131
+ return i(() => (a += 1, a >= r), c);
132
132
  }
133
133
  return Array.isArray(P(e)) ? {
134
- toMatch: u,
134
+ toMatch: i,
135
135
  toContains: g,
136
- changed: s,
137
- changedTimes: T,
136
+ changed: l,
137
+ changedTimes: m,
138
138
  get not() {
139
139
  return z(e, !t);
140
140
  }
141
141
  } : {
142
- toMatch: u,
143
- toBe: l,
144
- toBeTruthy: a,
145
- toBeNull: n,
146
- toBeNaN: p,
147
- toBeUndefined: r,
148
- changed: s,
149
- changedTimes: T,
142
+ toMatch: i,
143
+ toBe: s,
144
+ toBeTruthy: y,
145
+ toBeNull: o,
146
+ toBeNaN: d,
147
+ toBeUndefined: n,
148
+ changed: l,
149
+ changedTimes: m,
150
150
  get not() {
151
151
  return z(e, !t);
152
152
  }
153
153
  };
154
154
  }
155
- function ye(e) {
155
+ function he(e) {
156
156
  return z(e);
157
157
  }
158
- function me(e, t, u = {}) {
158
+ function ye(e, t, i = {}) {
159
159
  const {
160
- immediate: l = !0
161
- } = u, a = H(!1);
160
+ immediate: s = !0,
161
+ immediateCallback: y = !1
162
+ } = i, o = E(!1);
162
163
  let n = null;
163
- function r() {
164
+ function d() {
164
165
  n && (clearTimeout(n), n = null);
165
166
  }
166
- function p() {
167
- a.value = !1, r();
167
+ function g() {
168
+ o.value = !1, d();
168
169
  }
169
- function g(...s) {
170
- r(), a.value = !0, n = setTimeout(() => {
171
- a.value = !1, n = null, e(...s);
170
+ function l(...m) {
171
+ y && e(), d(), o.value = !0, n = setTimeout(() => {
172
+ o.value = !1, n = null, e(...m);
172
173
  }, P(t));
173
174
  }
174
- return l && (a.value = !0, ie && g()), re(p), {
175
- isPending: J(a),
176
- start: g,
177
- stop: p
175
+ return s && (o.value = !0, re && l()), oe(g), {
176
+ isPending: W(o),
177
+ start: l,
178
+ stop: g
178
179
  };
179
180
  }
180
- const Te = ie ? window : void 0, ve = {
181
+ const me = re ? window : void 0, Te = {
181
182
  json: "application/json",
182
183
  text: "text/plain"
183
184
  };
184
- function te(e) {
185
- return e && he(e, "immediate", "refetch", "initialData", "timeout", "beforeFetch", "afterFetch", "onFetchError", "fetch", "updateDataOnError");
185
+ function ee(e) {
186
+ return e && pe(e, "immediate", "refetch", "initialData", "timeout", "beforeFetch", "afterFetch", "onFetchError", "fetch", "updateDataOnError");
186
187
  }
187
188
  function V(e) {
188
189
  return typeof Headers < "u" && e instanceof Headers ? Object.fromEntries(e.entries()) : e;
189
190
  }
190
- function ge(e, ...t) {
191
- var u;
192
- const l = typeof AbortController == "function";
193
- let a = {}, n = {
191
+ function ve(e, ...t) {
192
+ var i;
193
+ const s = typeof AbortController == "function";
194
+ let y = {}, o = {
194
195
  immediate: !0,
195
196
  refetch: !1,
196
197
  timeout: 0,
197
198
  updateDataOnError: !1
198
199
  };
199
- const r = {
200
+ const n = {
200
201
  method: "GET",
201
202
  type: "text",
202
203
  payload: void 0
203
204
  };
204
- t.length > 0 && (te(t[0]) ? n = { ...n, ...t[0] } : a = t[0]), t.length > 1 && te(t[1]) && (n = { ...n, ...t[1] });
205
+ t.length > 0 && (ee(t[0]) ? o = { ...o, ...t[0] } : y = t[0]), t.length > 1 && ee(t[1]) && (o = { ...o, ...t[1] });
205
206
  const {
206
- fetch: p = (u = Te) == null ? void 0 : u.fetch,
207
+ fetch: d = (i = me) == null ? void 0 : i.fetch,
207
208
  initialData: g,
208
- timeout: s
209
- } = n, T = I(), o = I(), f = I(), i = H(!1), h = H(!1), b = H(!1), y = H(null), m = q(null), D = q(null), F = q(g || null), w = fe(() => l && h.value);
209
+ timeout: l
210
+ } = o, m = I(), r = I(), c = I(), a = E(!1), p = E(!1), b = E(!1), h = E(null), T = E(null), D = E(null), F = E(g || null), w = ce(() => s && p.value);
210
211
  let O, j;
211
- const M = () => {
212
- l && (O == null || O.abort(), O = new AbortController(), O.signal.onabort = () => b.value = !0, a = {
213
- ...a,
212
+ const _ = () => {
213
+ s && (O == null || O.abort(), O = new AbortController(), O.signal.onabort = () => b.value = !0, y = {
214
+ ...y,
214
215
  signal: O.signal
215
216
  });
216
- }, _ = (d) => {
217
- h.value = d, i.value = !d;
217
+ }, $ = (f) => {
218
+ p.value = f, a.value = !f;
218
219
  };
219
- s && (j = me(M, s, { immediate: !1 }));
220
- let $ = 0;
221
- const k = async (d = !1) => {
222
- var v, C;
223
- M(), _(!0), D.value = null, y.value = null, b.value = !1, $ += 1;
224
- const x = $, S = {
225
- method: r.method,
220
+ l && (j = ye(_, l, { immediate: !1 }));
221
+ let K = 0;
222
+ const H = async (f = !1) => {
223
+ var v, N;
224
+ _(), $(!0), D.value = null, h.value = null, b.value = !1, K += 1;
225
+ const U = K, S = {
226
+ method: n.method,
226
227
  headers: {}
227
- };
228
- if (r.payload) {
229
- const c = V(S.headers), E = P(r.payload), X = Object.getPrototypeOf(E);
230
- !r.payloadType && E && (X === Object.prototype || Array.isArray(X)) && !(E instanceof FormData) && (r.payloadType = "json"), r.payloadType && (c["Content-Type"] = (v = ve[r.payloadType]) != null ? v : r.payloadType), S.body = r.payloadType === "json" ? JSON.stringify(E) : E;
228
+ }, x = P(n.payload);
229
+ if (x) {
230
+ const u = V(S.headers), G = Object.getPrototypeOf(x);
231
+ !n.payloadType && x && (G === Object.prototype || Array.isArray(G)) && !(x instanceof FormData) && (n.payloadType = "json"), n.payloadType && (u["Content-Type"] = (v = Te[n.payloadType]) != null ? v : n.payloadType), S.body = n.payloadType === "json" ? JSON.stringify(x) : x;
231
232
  }
232
233
  let R = !1;
233
- const U = {
234
+ const C = {
234
235
  url: P(e),
235
236
  options: {
236
237
  ...S,
237
- ...a
238
+ ...y
238
239
  },
239
240
  cancel: () => {
240
241
  R = !0;
241
242
  }
242
243
  };
243
- if (n.beforeFetch && Object.assign(U, await n.beforeFetch(U)), R || !p)
244
- return _(!1), Promise.resolve(null);
244
+ if (o.beforeFetch && Object.assign(C, await o.beforeFetch(C)), R || !d)
245
+ return $(!1), Promise.resolve(null);
245
246
  let B = null;
246
- return j && j.start(), p(
247
- U.url,
247
+ return j && j.start(), d(
248
+ C.url,
248
249
  {
249
250
  ...S,
250
- ...U.options,
251
+ ...C.options,
251
252
  headers: {
252
253
  ...V(S.headers),
253
- ...V((C = U.options) == null ? void 0 : C.headers)
254
+ ...V((N = C.options) == null ? void 0 : N.headers)
254
255
  }
255
256
  }
256
- ).then(async (c) => {
257
- if (m.value = c, y.value = c.status, B = await c.clone()[r.type](), !c.ok)
258
- throw F.value = g || null, new Error(c.statusText);
259
- return n.afterFetch && ({ data: B } = await n.afterFetch({
257
+ ).then(async (u) => {
258
+ if (T.value = u, h.value = u.status, B = await u.clone()[n.type](), !u.ok)
259
+ throw F.value = g || null, new Error(u.statusText);
260
+ return o.afterFetch && ({ data: B } = await o.afterFetch({
260
261
  data: B,
261
- response: c
262
- })), F.value = B, T.trigger(c), c;
263
- }).catch(async (c) => {
264
- let E = c.message || c.name;
265
- if (n.onFetchError && ({ error: E, data: B } = await n.onFetchError({
262
+ response: u,
263
+ context: C,
264
+ execute: H
265
+ })), F.value = B, m.trigger(u), u;
266
+ }).catch(async (u) => {
267
+ let G = u.message || u.name;
268
+ if (o.onFetchError && ({ error: G, data: B } = await o.onFetchError({
266
269
  data: B,
267
- error: c,
268
- response: m.value
269
- })), D.value = E, n.updateDataOnError && (F.value = B), o.trigger(c), d)
270
- throw c;
270
+ error: u,
271
+ response: T.value,
272
+ context: C,
273
+ execute: H
274
+ })), D.value = G, o.updateDataOnError && (F.value = B), r.trigger(u), f)
275
+ throw u;
271
276
  return null;
272
277
  }).finally(() => {
273
- x === $ && _(!1), j && j.stop(), f.trigger(null);
278
+ U === K && $(!1), j && j.stop(), c.trigger(null);
274
279
  });
275
- }, Q = L(n.refetch);
276
- W(
280
+ }, Q = L(o.refetch);
281
+ M(
277
282
  [
278
283
  Q,
279
284
  L(e)
280
285
  ],
281
- ([d]) => d && k(),
286
+ ([f]) => f && H(),
282
287
  { deep: !0 }
283
288
  );
284
- const G = {
285
- isFinished: J(i),
286
- isFetching: J(h),
287
- statusCode: y,
288
- response: m,
289
+ const J = {
290
+ isFinished: W(a),
291
+ isFetching: W(p),
292
+ statusCode: h,
293
+ response: T,
289
294
  error: D,
290
295
  data: F,
291
296
  canAbort: w,
292
297
  aborted: b,
293
- abort: M,
294
- execute: k,
295
- onFetchResponse: T.on,
296
- onFetchError: o.on,
297
- onFetchFinally: f.on,
298
+ abort: _,
299
+ execute: H,
300
+ onFetchResponse: m.on,
301
+ onFetchError: r.on,
302
+ onFetchFinally: c.on,
298
303
  // method
299
304
  get: A("GET"),
300
305
  put: A("PUT"),
@@ -304,104 +309,104 @@ function ge(e, ...t) {
304
309
  head: A("HEAD"),
305
310
  options: A("OPTIONS"),
306
311
  // type
307
- json: N("json"),
308
- text: N("text"),
309
- blob: N("blob"),
310
- arrayBuffer: N("arrayBuffer"),
311
- formData: N("formData")
312
+ json: k("json"),
313
+ text: k("text"),
314
+ blob: k("blob"),
315
+ arrayBuffer: k("arrayBuffer"),
316
+ formData: k("formData")
312
317
  };
313
- function A(d) {
314
- return (v, C) => {
315
- if (!h.value)
316
- return r.method = d, r.payload = v, r.payloadType = C, ne(r.payload) && W(
318
+ function A(f) {
319
+ return (v, N) => {
320
+ if (!p.value)
321
+ return n.method = f, n.payload = v, n.payloadType = N, te(n.payload) && M(
317
322
  [
318
323
  Q,
319
- L(r.payload)
324
+ L(n.payload)
320
325
  ],
321
- ([x]) => x && k(),
326
+ ([U]) => U && H(),
322
327
  { deep: !0 }
323
328
  ), {
324
- ...G,
325
- then(x, S) {
326
- return K().then(x, S);
329
+ ...J,
330
+ then(U, S) {
331
+ return q().then(U, S);
327
332
  }
328
333
  };
329
334
  };
330
335
  }
331
- function K() {
332
- return new Promise((d, v) => {
333
- ye(i).toBe(!0).then(() => d(G)).catch(v);
336
+ function q() {
337
+ return new Promise((f, v) => {
338
+ he(a).toBe(!0).then(() => f(J)).catch(v);
334
339
  });
335
340
  }
336
- function N(d) {
341
+ function k(f) {
337
342
  return () => {
338
- if (!h.value)
339
- return r.type = d, {
340
- ...G,
341
- then(v, C) {
342
- return K().then(v, C);
343
+ if (!p.value)
344
+ return n.type = f, {
345
+ ...J,
346
+ then(v, N) {
347
+ return q().then(v, N);
343
348
  }
344
349
  };
345
350
  };
346
351
  }
347
- return n.immediate && Promise.resolve().then(() => k()), {
348
- ...G,
349
- then(d, v) {
350
- return K().then(d, v);
352
+ return o.immediate && Promise.resolve().then(() => H()), {
353
+ ...J,
354
+ then(f, v) {
355
+ return q().then(f, v);
351
356
  }
352
357
  };
353
358
  }
354
- function De(e, t, u = "/api/") {
355
- const l = {};
356
- return Object.keys(e).forEach((n) => {
357
- l[n] = (...r) => {
358
- const p = e[n], g = p.type;
359
- let s = p.url;
360
- s.startsWith("http") || (s = `${u}${s}`), p.hasBody ? s = Y(s, ...r.splice(1)) : s = Y(s, ...r);
361
- const T = {
362
- ...oe(),
363
- ...p.headers()
359
+ function Pe(e, t, i = "/api/") {
360
+ const s = {};
361
+ return Object.keys(e).forEach((o) => {
362
+ s[o] = (...n) => {
363
+ const d = e[o], g = d.type;
364
+ let l = d.url;
365
+ l.startsWith("http") || (l = `${i}${l}`), d.hasBody ? l = X(l, ...n.splice(1)) : l = X(l, ...n);
366
+ const m = {
367
+ ...ne(),
368
+ ...d.headers()
364
369
  };
365
- Fe(T);
366
- const o = ge(
367
- s,
370
+ ge(m);
371
+ const r = ve(
372
+ l,
368
373
  {
369
374
  method: g
370
375
  },
371
376
  {
372
377
  immediate: !1,
373
- beforeFetch({ options: i }) {
374
- return i.headers = {
375
- ...i.headers,
376
- ...T
377
- }, p.hasBody && r.length && (i.body = JSON.stringify(r[0])), {
378
- options: i
378
+ beforeFetch({ options: a }) {
379
+ return a.headers = {
380
+ ...a.headers,
381
+ ...m
382
+ }, d.hasBody && n.length && (a.body = JSON.stringify(n[0])), {
383
+ options: a
379
384
  };
380
385
  },
381
- onFetchError(i) {
382
- return i != null && i.response && t(i), i;
386
+ onFetchError(a) {
387
+ return a != null && a.response && t(a), a;
383
388
  }
384
389
  }
385
390
  );
386
391
  return {
387
- ...be(T) === "application/json" ? o.json() : o.text()
392
+ ...Fe(m) === "application/json" ? r.json() : r.text()
388
393
  };
389
394
  };
390
- }), l;
395
+ }), s;
391
396
  }
392
- function Fe(e) {
397
+ function ge(e) {
393
398
  e["content-type"] === "multipart/form-data" && delete e["content-type"];
394
399
  }
395
- function be(e) {
400
+ function Fe(e) {
396
401
  return e.accept ?? "*/*";
397
402
  }
398
- function je(e) {
403
+ function De(e) {
399
404
  return JSON.parse(JSON.stringify(e));
400
405
  }
401
406
  export {
402
- je as copy,
403
- Pe as createEndpoint,
404
- oe as getDefaultHeaders,
405
- Ee as requestTypes,
406
- De as useApi
407
+ De as copy,
408
+ Ee as createEndpoint,
409
+ ne as getDefaultHeaders,
410
+ Oe as requestTypes,
411
+ Pe as useApi
407
412
  };
@@ -1 +1 @@
1
- (function(T,N){typeof exports=="object"&&typeof module<"u"?N(exports,require("@citruslime/utils"),require("vue")):typeof define=="function"&&define.amd?define(["exports","@citruslime/utils","vue"],N):(T=typeof globalThis<"u"?globalThis:T||self,N(T.CitrusLimeVueUtils={},T.CLUtils,T.Vue))})(this,function(T,N,r){"use strict";const ne=["GET","POST","PUT","DELETE"];function oe(e,t,s=null,u=null){return{url:e,type:t,headers:s??M,hasBody:re(t,u)}}function M(){return{accept:"application/json","content-type":"application/json"}}function re(e,t){return!!(t===null&&(e==="PUT"||e==="POST")||t)}function z(e){return r.getCurrentScope()?(r.onScopeDispose(e),!0):!1}function W(){const e=new Set,t=l=>{e.delete(l)};return{on:l=>{e.add(l);const n=()=>t(l);return z(n),{off:n}},off:t,trigger:(...l)=>Promise.all(Array.from(e).map(n=>n(...l)))}}function E(e){return typeof e=="function"?e():r.unref(e)}const Q=typeof window<"u"&&typeof document<"u";typeof WorkerGlobalScope<"u"&&globalThis instanceof WorkerGlobalScope;const ie=()=>{};function X(e,t=!1,s="Timeout"){return new Promise((u,l)=>{setTimeout(t?()=>l(s):u,e)})}function ae(e,...t){return t.some(s=>s in e)}function L(...e){if(e.length!==1)return r.toRef(...e);const t=e[0];return typeof t=="function"?r.readonly(r.customRef(()=>({get:t,set:ie}))):r.ref(t)}function V(e,t=!1){function s(o,{flush:d="sync",deep:a=!1,timeout:y,throwOnTimeout:P}={}){let m=null;const A=[new Promise(O=>{m=r.watch(e,D=>{o(D)!==t&&(m?m():r.nextTick(()=>m==null?void 0:m()),O(D))},{flush:d,deep:a,immediate:!0})})];return y!=null&&A.push(X(y,P).then(()=>E(e)).finally(()=>m==null?void 0:m())),Promise.race(A)}function u(o,d){if(!r.isRef(o))return s(D=>D===o,d);const{flush:a="sync",deep:y=!1,timeout:P,throwOnTimeout:m}=d??{};let g=null;const O=[new Promise(D=>{g=r.watch([e,o],([j,C])=>{t!==(j===C)&&(g?g():r.nextTick(()=>g==null?void 0:g()),D(j))},{flush:a,deep:y,immediate:!0})})];return P!=null&&O.push(X(P,m).then(()=>E(e)).finally(()=>(g==null||g(),E(e)))),Promise.race(O)}function l(o){return s(d=>!!d,o)}function n(o){return u(null,o)}function i(o){return u(void 0,o)}function h(o){return s(Number.isNaN,o)}function F(o,d){return s(a=>{const y=Array.from(a);return y.includes(o)||y.includes(E(o))},d)}function c(o){return w(1,o)}function w(o=1,d){let a=-1;return s(()=>(a+=1,a>=o),d)}return Array.isArray(E(e))?{toMatch:s,toContains:F,changed:c,changedTimes:w,get not(){return V(e,!t)}}:{toMatch:s,toBe:u,toBeTruthy:l,toBeNull:n,toBeNaN:h,toBeUndefined:i,changed:c,changedTimes:w,get not(){return V(e,!t)}}}function le(e){return V(e)}function se(e,t,s={}){const{immediate:u=!0}=s,l=r.ref(!1);let n=null;function i(){n&&(clearTimeout(n),n=null)}function h(){l.value=!1,i()}function F(...c){i(),l.value=!0,n=setTimeout(()=>{l.value=!1,n=null,e(...c)},E(t))}return u&&(l.value=!0,Q&&F()),z(h),{isPending:r.readonly(l),start:F,stop:h}}const ue=Q?window:void 0,ce={json:"application/json",text:"text/plain"};function Y(e){return e&&ae(e,"immediate","refetch","initialData","timeout","beforeFetch","afterFetch","onFetchError","fetch","updateDataOnError")}function _(e){return typeof Headers<"u"&&e instanceof Headers?Object.fromEntries(e.entries()):e}function fe(e,...t){var s;const u=typeof AbortController=="function";let l={},n={immediate:!0,refetch:!1,timeout:0,updateDataOnError:!1};const i={method:"GET",type:"text",payload:void 0};t.length>0&&(Y(t[0])?n={...n,...t[0]}:l=t[0]),t.length>1&&Y(t[1])&&(n={...n,...t[1]});const{fetch:h=(s=ue)==null?void 0:s.fetch,initialData:F,timeout:c}=n,w=W(),o=W(),d=W(),a=r.ref(!1),y=r.ref(!1),P=r.ref(!1),m=r.ref(null),g=r.shallowRef(null),A=r.shallowRef(null),O=r.shallowRef(F||null),D=r.computed(()=>u&&y.value);let j,C;const K=()=>{u&&(j==null||j.abort(),j=new AbortController,j.signal.onabort=()=>P.value=!0,l={...l,signal:j.signal})},R=p=>{y.value=p,a.value=!p};c&&(C=se(K,c,{immediate:!1}));let $=0;const q=async(p=!1)=>{var b,U;K(),R(!0),A.value=null,m.value=null,P.value=!1,$+=1;const x=$,H={method:i.method,headers:{}};if(i.payload){const f=_(H.headers),S=E(i.payload),te=Object.getPrototypeOf(S);!i.payloadType&&S&&(te===Object.prototype||Array.isArray(te))&&!(S instanceof FormData)&&(i.payloadType="json"),i.payloadType&&(f["Content-Type"]=(b=ce[i.payloadType])!=null?b:i.payloadType),H.body=i.payloadType==="json"?JSON.stringify(S):S}let ee=!1;const G={url:E(e),options:{...H,...l},cancel:()=>{ee=!0}};if(n.beforeFetch&&Object.assign(G,await n.beforeFetch(G)),ee||!h)return R(!1),Promise.resolve(null);let v=null;return C&&C.start(),h(G.url,{...H,...G.options,headers:{..._(H.headers),..._((U=G.options)==null?void 0:U.headers)}}).then(async f=>{if(g.value=f,m.value=f.status,v=await f.clone()[i.type](),!f.ok)throw O.value=F||null,new Error(f.statusText);return n.afterFetch&&({data:v}=await n.afterFetch({data:v,response:f})),O.value=v,w.trigger(f),f}).catch(async f=>{let S=f.message||f.name;if(n.onFetchError&&({error:S,data:v}=await n.onFetchError({data:v,error:f,response:g.value})),A.value=S,n.updateDataOnError&&(O.value=v),o.trigger(f),p)throw f;return null}).finally(()=>{x===$&&R(!1),C&&C.stop(),d.trigger(null)})},Z=L(n.refetch);r.watch([Z,L(e)],([p])=>p&&q(),{deep:!0});const J={isFinished:r.readonly(a),isFetching:r.readonly(y),statusCode:m,response:g,error:A,data:O,canAbort:D,aborted:P,abort:K,execute:q,onFetchResponse:w.on,onFetchError:o.on,onFetchFinally:d.on,get:B("GET"),put:B("PUT"),post:B("POST"),delete:B("DELETE"),patch:B("PATCH"),head:B("HEAD"),options:B("OPTIONS"),json:k("json"),text:k("text"),blob:k("blob"),arrayBuffer:k("arrayBuffer"),formData:k("formData")};function B(p){return(b,U)=>{if(!y.value)return i.method=p,i.payload=b,i.payloadType=U,r.isRef(i.payload)&&r.watch([Z,L(i.payload)],([x])=>x&&q(),{deep:!0}),{...J,then(x,H){return I().then(x,H)}}}}function I(){return new Promise((p,b)=>{le(a).toBe(!0).then(()=>p(J)).catch(b)})}function k(p){return()=>{if(!y.value)return i.type=p,{...J,then(b,U){return I().then(b,U)}}}}return n.immediate&&Promise.resolve().then(()=>q()),{...J,then(p,b){return I().then(p,b)}}}function de(e,t,s="/api/"){const u={};return Object.keys(e).forEach(n=>{u[n]=(...i)=>{const h=e[n],F=h.type;let c=h.url;c.startsWith("http")||(c=`${s}${c}`),h.hasBody?c=N.replacePlaceholders(c,...i.splice(1)):c=N.replacePlaceholders(c,...i);const w={...M(),...h.headers()};pe(w);const o=fe(c,{method:F},{immediate:!1,beforeFetch({options:a}){return a.headers={...a.headers,...w},h.hasBody&&i.length&&(a.body=JSON.stringify(i[0])),{options:a}},onFetchError(a){return a!=null&&a.response&&t(a),a}});return{...he(w)==="application/json"?o.json():o.text()}}}),u}function pe(e){e["content-type"]==="multipart/form-data"&&delete e["content-type"]}function he(e){return e.accept??"*/*"}function ye(e){return JSON.parse(JSON.stringify(e))}T.copy=ye,T.createEndpoint=oe,T.getDefaultHeaders=M,T.requestTypes=ne,T.useApi=de,Object.defineProperty(T,Symbol.toStringTag,{value:"Module"})});
1
+ (function(m,V){typeof exports=="object"&&typeof module<"u"?V(exports,require("@citruslime/utils"),require("vue")):typeof define=="function"&&define.amd?define(["exports","@citruslime/utils","vue"],V):(m=typeof globalThis<"u"?globalThis:m||self,V(m.CitrusLimeVueUtils={},m.CLUtils,m.Vue))})(this,function(m,V,t){"use strict";const te=["GET","POST","PUT","DELETE"];function ne(e,n,l=null,u=null){return{url:e,type:n,headers:l??J,hasBody:oe(n,u)}}function J(){return{accept:"application/json","content-type":"application/json"}}function oe(e,n){return!!(n===null&&(e==="PUT"||e==="POST")||n)}function z(e){return t.getCurrentScope()?(t.onScopeDispose(e),!0):!1}function M(){const e=new Set,n=r=>{e.delete(r)};return{on:r=>{e.add(r);const o=()=>n(r);return z(o),{off:o}},off:n,trigger:(...r)=>Promise.all(Array.from(e).map(o=>o(...r))),clear:()=>{e.clear()}}}const Q=typeof window<"u"&&typeof document<"u";typeof WorkerGlobalScope<"u"&&globalThis instanceof WorkerGlobalScope;const re=()=>{};function W(...e){if(e.length!==1)return t.toRef(...e);const n=e[0];return typeof n=="function"?t.readonly(t.customRef(()=>({get:n,set:re}))):t.ref(n)}function X(e,n=!1,l="Timeout"){return new Promise((u,T)=>{setTimeout(n?()=>T(l):u,e)})}function ie(e,...n){return n.some(l=>l in e)}function v(e,n=!1){function l(i,{flush:f="sync",deep:a=!1,timeout:h,throwOnTimeout:E}={}){let y=null;const j=[new Promise(O=>{y=t.watch(e,P=>{i(P)!==n&&(y?y():t.nextTick(()=>y==null?void 0:y()),O(P))},{flush:f,deep:a,immediate:!0})})];return h!=null&&j.push(X(h,E).then(()=>t.toValue(e)).finally(()=>y==null?void 0:y())),Promise.race(j)}function u(i,f){if(!t.isRef(i))return l(P=>P===i,f);const{flush:a="sync",deep:h=!1,timeout:E,throwOnTimeout:y}=f??{};let g=null;const O=[new Promise(P=>{g=t.watch([e,i],([D,S])=>{n!==(D===S)&&(g?g():t.nextTick(()=>g==null?void 0:g()),P(D))},{flush:a,deep:h,immediate:!0})})];return E!=null&&O.push(X(E,y).then(()=>t.toValue(e)).finally(()=>(g==null||g(),t.toValue(e)))),Promise.race(O)}function T(i){return l(f=>!!f,i)}function r(i){return u(null,i)}function o(i){return u(void 0,i)}function p(i){return l(Number.isNaN,i)}function F(i,f){return l(a=>{const h=Array.from(a);return h.includes(i)||h.includes(t.toValue(i))},f)}function s(i){return w(1,i)}function w(i=1,f){let a=-1;return l(()=>(a+=1,a>=i),f)}return Array.isArray(t.toValue(e))?{toMatch:l,toContains:F,changed:s,changedTimes:w,get not(){return v(e,!n)}}:{toMatch:l,toBe:u,toBeTruthy:T,toBeNull:r,toBeNaN:p,toBeUndefined:o,changed:s,changedTimes:w,get not(){return v(e,!n)}}}function ae(e){return v(e)}function le(e,n,l={}){const{immediate:u=!0,immediateCallback:T=!1}=l,r=t.shallowRef(!1);let o=null;function p(){o&&(clearTimeout(o),o=null)}function F(){r.value=!1,p()}function s(...w){T&&e(),p(),r.value=!0,o=setTimeout(()=>{r.value=!1,o=null,e(...w)},t.toValue(n))}return u&&(r.value=!0,Q&&s()),z(F),{isPending:t.readonly(r),start:s,stop:F}}const se=Q?window:void 0,ue={json:"application/json",text:"text/plain"};function Y(e){return e&&ie(e,"immediate","refetch","initialData","timeout","beforeFetch","afterFetch","onFetchError","fetch","updateDataOnError")}function L(e){return typeof Headers<"u"&&e instanceof Headers?Object.fromEntries(e.entries()):e}function ce(e,...n){var l;const u=typeof AbortController=="function";let T={},r={immediate:!0,refetch:!1,timeout:0,updateDataOnError:!1};const o={method:"GET",type:"text",payload:void 0};n.length>0&&(Y(n[0])?r={...r,...n[0]}:T=n[0]),n.length>1&&Y(n[1])&&(r={...r,...n[1]});const{fetch:p=(l=se)==null?void 0:l.fetch,initialData:F,timeout:s}=r,w=M(),i=M(),f=M(),a=t.shallowRef(!1),h=t.shallowRef(!1),E=t.shallowRef(!1),y=t.shallowRef(null),g=t.shallowRef(null),j=t.shallowRef(null),O=t.shallowRef(F||null),P=t.computed(()=>u&&h.value);let D,S;const _=()=>{u&&(D==null||D.abort(),D=new AbortController,D.signal.onabort=()=>E.value=!0,T={...T,signal:D.signal})},K=d=>{h.value=d,a.value=!d};s&&(S=le(_,s,{immediate:!1}));let $=0;const N=async(d=!1)=>{var b,U;_(),K(!0),j.value=null,y.value=null,E.value=!1,$+=1;const x=$,C={method:o.method,headers:{}},k=t.toValue(o.payload);if(k){const c=L(C.headers),G=Object.getPrototypeOf(k);!o.payloadType&&k&&(G===Object.prototype||Array.isArray(G))&&!(k instanceof FormData)&&(o.payloadType="json"),o.payloadType&&(c["Content-Type"]=(b=ue[o.payloadType])!=null?b:o.payloadType),C.body=o.payloadType==="json"?JSON.stringify(k):k}let ee=!1;const B={url:t.toValue(e),options:{...C,...T},cancel:()=>{ee=!0}};if(r.beforeFetch&&Object.assign(B,await r.beforeFetch(B)),ee||!p)return K(!1),Promise.resolve(null);let H=null;return S&&S.start(),p(B.url,{...C,...B.options,headers:{...L(C.headers),...L((U=B.options)==null?void 0:U.headers)}}).then(async c=>{if(g.value=c,y.value=c.status,H=await c.clone()[o.type](),!c.ok)throw O.value=F||null,new Error(c.statusText);return r.afterFetch&&({data:H}=await r.afterFetch({data:H,response:c,context:B,execute:N})),O.value=H,w.trigger(c),c}).catch(async c=>{let G=c.message||c.name;if(r.onFetchError&&({error:G,data:H}=await r.onFetchError({data:H,error:c,response:g.value,context:B,execute:N})),j.value=G,r.updateDataOnError&&(O.value=H),i.trigger(c),d)throw c;return null}).finally(()=>{x===$&&K(!1),S&&S.stop(),f.trigger(null)})},Z=W(r.refetch);t.watch([Z,W(e)],([d])=>d&&N(),{deep:!0});const q={isFinished:t.readonly(a),isFetching:t.readonly(h),statusCode:y,response:g,error:j,data:O,canAbort:P,aborted:E,abort:_,execute:N,onFetchResponse:w.on,onFetchError:i.on,onFetchFinally:f.on,get:A("GET"),put:A("PUT"),post:A("POST"),delete:A("DELETE"),patch:A("PATCH"),head:A("HEAD"),options:A("OPTIONS"),json:R("json"),text:R("text"),blob:R("blob"),arrayBuffer:R("arrayBuffer"),formData:R("formData")};function A(d){return(b,U)=>{if(!h.value)return o.method=d,o.payload=b,o.payloadType=U,t.isRef(o.payload)&&t.watch([Z,W(o.payload)],([x])=>x&&N(),{deep:!0}),{...q,then(x,C){return I().then(x,C)}}}}function I(){return new Promise((d,b)=>{ae(a).toBe(!0).then(()=>d(q)).catch(b)})}function R(d){return()=>{if(!h.value)return o.type=d,{...q,then(b,U){return I().then(b,U)}}}}return r.immediate&&Promise.resolve().then(()=>N()),{...q,then(d,b){return I().then(d,b)}}}function fe(e,n,l="/api/"){const u={};return Object.keys(e).forEach(r=>{u[r]=(...o)=>{const p=e[r],F=p.type;let s=p.url;s.startsWith("http")||(s=`${l}${s}`),p.hasBody?s=V.replacePlaceholders(s,...o.splice(1)):s=V.replacePlaceholders(s,...o);const w={...J(),...p.headers()};de(w);const i=ce(s,{method:F},{immediate:!1,beforeFetch({options:a}){return a.headers={...a.headers,...w},p.hasBody&&o.length&&(a.body=JSON.stringify(o[0])),{options:a}},onFetchError(a){return a!=null&&a.response&&n(a),a}});return{...pe(w)==="application/json"?i.json():i.text()}}}),u}function de(e){e["content-type"]==="multipart/form-data"&&delete e["content-type"]}function pe(e){return e.accept??"*/*"}function he(e){return JSON.parse(JSON.stringify(e))}m.copy=he,m.createEndpoint=ne,m.getDefaultHeaders=J,m.requestTypes=te,m.useApi=fe,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});
@@ -10,7 +10,7 @@ export type ApiRequest<TResponseModel> = ReturnType<typeof useFetch<TResponseMod
10
10
  * @returns An API object containing request methods.
11
11
  */
12
12
  export declare function useApi<TEndpoint extends Record<keyof TEndpoint, Endpoint>>(endpoints: TEndpoint, handleFetchErrorResponse: (context: {
13
- data: any;
13
+ data: unknown;
14
14
  response: Response | null;
15
- error: any;
15
+ error: unknown;
16
16
  }) => void, prefix?: string): Record<keyof TEndpoint, <TResponseModel>(...values: unknown[]) => ApiRequest<TResponseModel>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@citruslime/vue-utils",
3
- "version": "2.0.2-beta.2",
3
+ "version": "2.0.2-beta.3",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Citrus-Lime Ltd",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@citruslime/utils": "2.0.5-beta.0",
32
- "@vueuse/core": "^12.0.0",
32
+ "@vueuse/core": "^13.0.0",
33
33
  "vue": "^3.5.13"
34
34
  },
35
35
  "scripts": {