@citruslime/vue-utils 1.3.0-beta.1 → 1.4.0-beta.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.
@@ -1,5 +1,5 @@
1
1
  import { replacePlaceholders as Y } from "@citruslime/utils";
2
- import { toRef as se, readonly as re, customRef as le, ref as $, getCurrentScope as ce, onScopeDispose as fe, unref as de, watch as I, isRef as oe, shallowRef as q, computed as pe, reactive as he, toRefs as me } from "vue";
2
+ import { toRef as se, readonly as re, customRef as le, ref as $, getCurrentScope as ce, onScopeDispose as fe, unref as de, watch as I, isRef as oe, shallowRef as W, computed as pe, reactive as he, toRefs as me } from "vue";
3
3
  const He = [
4
4
  "GET",
5
5
  "POST",
@@ -10,11 +10,11 @@ function $e(e, t, n = null, s = null) {
10
10
  return {
11
11
  url: e,
12
12
  type: t,
13
- headers: n ?? ae,
13
+ headers: n ?? ie,
14
14
  hasBody: ye(t, s)
15
15
  };
16
16
  }
17
- function ae() {
17
+ function ie() {
18
18
  return {
19
19
  accept: "application/json",
20
20
  "content-type": "application/json"
@@ -23,23 +23,23 @@ function ae() {
23
23
  function ye(e, t) {
24
24
  return !!(t === null && (e === "PUT" || e === "POST") || t);
25
25
  }
26
- function ie(e) {
26
+ function ae(e) {
27
27
  return ce() ? (fe(e), !0) : !1;
28
28
  }
29
- function L() {
30
- const e = /* @__PURE__ */ new Set(), t = (a) => {
31
- e.delete(a);
29
+ function q() {
30
+ const e = /* @__PURE__ */ new Set(), t = (i) => {
31
+ e.delete(i);
32
32
  };
33
33
  return {
34
- on: (a) => {
35
- e.add(a);
36
- const r = () => t(a);
37
- return ie(r), {
34
+ on: (i) => {
35
+ e.add(i);
36
+ const r = () => t(i);
37
+ return ae(r), {
38
38
  off: r
39
39
  };
40
40
  },
41
41
  off: t,
42
- trigger: (a) => Promise.all(Array.from(e).map((r) => r(a)))
42
+ trigger: (i) => Promise.all(Array.from(e).map((r) => r(i)))
43
43
  };
44
44
  }
45
45
  function O(e) {
@@ -48,27 +48,27 @@ function O(e) {
48
48
  const ue = typeof window < "u", ve = () => {
49
49
  };
50
50
  function Z(e, t = !1, n = "Timeout") {
51
- return new Promise((s, a) => {
52
- setTimeout(t ? () => a(n) : s, e);
51
+ return new Promise((s, i) => {
52
+ setTimeout(t ? () => i(n) : s, e);
53
53
  });
54
54
  }
55
55
  function we(e, ...t) {
56
56
  return t.some((n) => n in e);
57
57
  }
58
- function R(...e) {
58
+ function L(...e) {
59
59
  if (e.length !== 1)
60
60
  return se(...e);
61
61
  const t = e[0];
62
62
  return typeof t == "function" ? re(le(() => ({ get: t, set: ve }))) : $(t);
63
63
  }
64
64
  function k(e, t = !1) {
65
- function n(i, { flush: u = "sync", deep: h = !1, timeout: p, throwOnTimeout: F } = {}) {
65
+ function n(a, { flush: u = "sync", deep: h = !1, timeout: p, throwOnTimeout: F } = {}) {
66
66
  let y = null;
67
67
  const D = [new Promise((E) => {
68
68
  y = I(
69
69
  e,
70
70
  (_) => {
71
- i(_) !== t && (y == null || y(), E(_));
71
+ a(_) !== t && (y == null || y(), E(_));
72
72
  },
73
73
  {
74
74
  flush: u,
@@ -81,14 +81,14 @@ function k(e, t = !1) {
81
81
  Z(p, F).then(() => O(e)).finally(() => y == null ? void 0 : y())
82
82
  ), Promise.race(D);
83
83
  }
84
- function s(i, u) {
85
- if (!oe(i))
86
- return n((_) => _ === i, u);
84
+ function s(a, u) {
85
+ if (!oe(a))
86
+ return n((_) => _ === a, u);
87
87
  const { flush: h = "sync", deep: p = !1, timeout: F, throwOnTimeout: y } = u ?? {};
88
88
  let w = null;
89
89
  const E = [new Promise((_) => {
90
90
  w = I(
91
- [e, i],
91
+ [e, a],
92
92
  ([j, S]) => {
93
93
  t !== (j === S) && (w == null || w(), _(j));
94
94
  },
@@ -103,34 +103,34 @@ function k(e, t = !1) {
103
103
  Z(F, y).then(() => O(e)).finally(() => (w == null || w(), O(e)))
104
104
  ), Promise.race(E);
105
105
  }
106
- function a(i) {
107
- return n((u) => !!u, i);
106
+ function i(a) {
107
+ return n((u) => !!u, a);
108
108
  }
109
- function r(i) {
110
- return s(null, i);
109
+ function r(a) {
110
+ return s(null, a);
111
111
  }
112
- function o(i) {
113
- return s(void 0, i);
112
+ function o(a) {
113
+ return s(void 0, a);
114
114
  }
115
- function b(i) {
116
- return n(Number.isNaN, i);
115
+ function b(a) {
116
+ return n(Number.isNaN, a);
117
117
  }
118
- function d(i, u) {
118
+ function l(a, u) {
119
119
  return n((h) => {
120
120
  const p = Array.from(h);
121
- return p.includes(i) || p.includes(O(i));
121
+ return p.includes(a) || p.includes(O(a));
122
122
  }, u);
123
123
  }
124
- function v(i) {
125
- return g(1, i);
124
+ function v(a) {
125
+ return g(1, a);
126
126
  }
127
- function g(i = 1, u) {
127
+ function g(a = 1, u) {
128
128
  let h = -1;
129
- return n(() => (h += 1, h >= i), u);
129
+ return n(() => (h += 1, h >= a), u);
130
130
  }
131
131
  return Array.isArray(O(e)) ? {
132
132
  toMatch: n,
133
- toContains: d,
133
+ toContains: l,
134
134
  changed: v,
135
135
  changedTimes: g,
136
136
  get not() {
@@ -139,7 +139,7 @@ function k(e, t = !1) {
139
139
  } : {
140
140
  toMatch: n,
141
141
  toBe: s,
142
- toBeTruthy: a,
142
+ toBeTruthy: i,
143
143
  toBeNull: r,
144
144
  toBeNaN: b,
145
145
  toBeUndefined: o,
@@ -156,27 +156,27 @@ function Pe(e) {
156
156
  function be(e, t, n = {}) {
157
157
  const {
158
158
  immediate: s = !0
159
- } = n, a = $(!1);
159
+ } = n, i = $(!1);
160
160
  let r = null;
161
161
  function o() {
162
162
  r && (clearTimeout(r), r = null);
163
163
  }
164
164
  function b() {
165
- a.value = !1, o();
165
+ i.value = !1, o();
166
166
  }
167
- function d(...v) {
168
- o(), a.value = !0, r = setTimeout(() => {
169
- a.value = !1, r = null, e(...v);
167
+ function l(...v) {
168
+ o(), i.value = !0, r = setTimeout(() => {
169
+ i.value = !1, r = null, e(...v);
170
170
  }, O(t));
171
171
  }
172
- return s && (a.value = !0, ue && d()), ie(b), {
173
- isPending: re(a),
174
- start: d,
172
+ return s && (i.value = !0, ue && l()), ae(b), {
173
+ isPending: re(i),
174
+ start: l,
175
175
  stop: b
176
176
  };
177
177
  }
178
178
  const ge = ue ? window : void 0;
179
- var Te = Object.defineProperty, Oe = Object.defineProperties, Fe = Object.getOwnPropertyDescriptors, ee = Object.getOwnPropertySymbols, Ee = Object.prototype.hasOwnProperty, _e = Object.prototype.propertyIsEnumerable, te = (e, t, n) => t in e ? Te(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, f = (e, t) => {
179
+ var Te = Object.defineProperty, Oe = Object.defineProperties, Fe = Object.getOwnPropertyDescriptors, ee = Object.getOwnPropertySymbols, Ee = Object.prototype.hasOwnProperty, _e = Object.prototype.propertyIsEnumerable, te = (e, t, n) => t in e ? Te(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, d = (e, t) => {
180
180
  for (var n in t || (t = {}))
181
181
  Ee.call(t, n) && te(e, n, t[n]);
182
182
  if (ee)
@@ -191,34 +191,34 @@ const je = {
191
191
  function ne(e) {
192
192
  return e && we(e, "immediate", "refetch", "initialData", "timeout", "beforeFetch", "afterFetch", "onFetchError", "fetch");
193
193
  }
194
- function W(e) {
194
+ function R(e) {
195
195
  return typeof Headers < "u" && e instanceof Headers ? Object.fromEntries([...e.entries()]) : e;
196
196
  }
197
197
  function De(e, ...t) {
198
198
  var n;
199
199
  const s = typeof AbortController == "function";
200
- let a = {}, r = { immediate: !0, refetch: !1, timeout: 0 };
200
+ let i = {}, r = { immediate: !0, refetch: !1, timeout: 0 };
201
201
  const o = {
202
202
  method: "GET",
203
203
  type: "text",
204
204
  payload: void 0
205
205
  };
206
- t.length > 0 && (ne(t[0]) ? r = f(f({}, r), t[0]) : a = t[0]), t.length > 1 && ne(t[1]) && (r = f(f({}, r), t[1]));
206
+ t.length > 0 && (ne(t[0]) ? r = d(d({}, r), t[0]) : i = t[0]), t.length > 1 && ne(t[1]) && (r = d(d({}, r), t[1]));
207
207
  const {
208
208
  fetch: b = (n = ge) == null ? void 0 : n.fetch,
209
- initialData: d,
209
+ initialData: l,
210
210
  timeout: v
211
- } = r, g = L(), i = L(), u = L(), h = $(!1), p = $(!1), F = $(!1), y = $(null), w = q(null), D = q(null), E = q(d || null), _ = pe(() => s && p.value);
211
+ } = r, g = q(), a = q(), u = q(), h = $(!1), p = $(!1), F = $(!1), y = $(null), w = W(null), D = W(null), E = W(l || null), _ = pe(() => s && p.value);
212
212
  let j, S;
213
213
  const M = () => {
214
- s && (j == null || j.abort(), j = new AbortController(), j.signal.onabort = () => F.value = !0, a = N(f({}, a), {
214
+ s && (j == null || j.abort(), j = new AbortController(), j.signal.onabort = () => F.value = !0, i = N(d({}, i), {
215
215
  signal: j.signal
216
216
  }));
217
- }, K = (l) => {
218
- p.value = l, h.value = !l;
217
+ }, K = (c) => {
218
+ p.value = c, h.value = !c;
219
219
  };
220
220
  v && (S = be(M, v, { immediate: !1 }));
221
- const U = async (l = !1) => {
221
+ const U = async (c = !1) => {
222
222
  var P;
223
223
  M(), K(!0), D.value = null, y.value = null, F.value = !1;
224
224
  const m = {
@@ -226,7 +226,7 @@ function De(e, ...t) {
226
226
  headers: {}
227
227
  };
228
228
  if (o.payload) {
229
- const J = W(m.headers);
229
+ const J = R(m.headers);
230
230
  o.payloadType && (J["Content-Type"] = (P = je[o.payloadType]) != null ? P : o.payloadType);
231
231
  const G = O(o.payload);
232
232
  m.body = o.payloadType === "json" ? JSON.stringify(G) : G;
@@ -234,7 +234,7 @@ function De(e, ...t) {
234
234
  let H = !1;
235
235
  const T = {
236
236
  url: O(e),
237
- options: f(f({}, m), a),
237
+ options: d(d({}, m), i),
238
238
  cancel: () => {
239
239
  H = !0;
240
240
  }
@@ -246,27 +246,27 @@ function De(e, ...t) {
246
246
  var Q;
247
247
  b(
248
248
  T.url,
249
- N(f(f({}, m), T.options), {
250
- headers: f(f({}, W(m.headers)), W((Q = T.options) == null ? void 0 : Q.headers))
249
+ N(d(d({}, m), T.options), {
250
+ headers: d(d({}, R(m.headers)), R((Q = T.options) == null ? void 0 : Q.headers))
251
251
  })
252
- ).then(async (c) => {
253
- if (w.value = c, y.value = c.status, B = await c[o.type](), !c.ok)
254
- throw E.value = d || null, new Error(c.statusText);
255
- return r.afterFetch && ({ data: B } = await r.afterFetch({ data: B, response: c })), E.value = B, g.trigger(c), J(c);
256
- }).catch(async (c) => {
257
- let X = c.message || c.name;
258
- return r.onFetchError && ({ error: X } = await r.onFetchError({ data: B, error: c, response: w.value })), D.value = X, i.trigger(c), l ? G(c) : J(null);
252
+ ).then(async (f) => {
253
+ if (w.value = f, y.value = f.status, B = await f[o.type](), !f.ok)
254
+ throw E.value = l || null, new Error(f.statusText);
255
+ return r.afterFetch && ({ data: B } = await r.afterFetch({ data: B, response: f })), E.value = B, g.trigger(f), J(f);
256
+ }).catch(async (f) => {
257
+ let X = f.message || f.name;
258
+ return r.onFetchError && ({ error: X } = await r.onFetchError({ data: B, error: f, response: w.value })), D.value = X, a.trigger(f), c ? G(f) : J(null);
259
259
  }).finally(() => {
260
260
  K(!1), S && S.stop(), u.trigger(null);
261
261
  });
262
262
  });
263
- }, z = R(r.refetch);
263
+ }, z = L(r.refetch);
264
264
  I(
265
265
  [
266
266
  z,
267
- R(e)
267
+ L(e)
268
268
  ],
269
- ([l]) => l && U(),
269
+ ([c]) => c && U(),
270
270
  { deep: !0 }
271
271
  );
272
272
  const x = {
@@ -281,7 +281,7 @@ function De(e, ...t) {
281
281
  abort: M,
282
282
  execute: U,
283
283
  onFetchResponse: g.on,
284
- onFetchError: i.on,
284
+ onFetchError: a.on,
285
285
  onFetchFinally: u.on,
286
286
  // method
287
287
  get: A("GET"),
@@ -298,19 +298,19 @@ function De(e, ...t) {
298
298
  arrayBuffer: C("arrayBuffer"),
299
299
  formData: C("formData")
300
300
  };
301
- function A(l) {
301
+ function A(c) {
302
302
  return (P, m) => {
303
303
  if (!p.value) {
304
- o.method = l, o.payload = P, o.payloadType = m, oe(o.payload) && I(
304
+ o.method = c, o.payload = P, o.payloadType = m, oe(o.payload) && I(
305
305
  [
306
306
  z,
307
- R(o.payload)
307
+ L(o.payload)
308
308
  ],
309
309
  ([T]) => T && U(),
310
310
  { deep: !0 }
311
311
  );
312
312
  const H = O(o.payload);
313
- return !m && H && Object.getPrototypeOf(H) === Object.prototype && !(H instanceof FormData) && (o.payloadType = "json"), N(f({}, x), {
313
+ return !m && H && Object.getPrototypeOf(H) === Object.prototype && !(H instanceof FormData) && (o.payloadType = "json"), N(d({}, x), {
314
314
  then(T, B) {
315
315
  return V().then(T, B);
316
316
  }
@@ -319,38 +319,38 @@ function De(e, ...t) {
319
319
  };
320
320
  }
321
321
  function V() {
322
- return new Promise((l, P) => {
323
- Pe(h).toBe(!0).then(() => l(x)).catch((m) => P(m));
322
+ return new Promise((c, P) => {
323
+ Pe(h).toBe(!0).then(() => c(x)).catch((m) => P(m));
324
324
  });
325
325
  }
326
- function C(l) {
326
+ function C(c) {
327
327
  return () => {
328
328
  if (!p.value)
329
- return o.type = l, N(f({}, x), {
329
+ return o.type = c, N(d({}, x), {
330
330
  then(P, m) {
331
331
  return V().then(P, m);
332
332
  }
333
333
  });
334
334
  };
335
335
  }
336
- return r.immediate && Promise.resolve().then(() => U()), N(f({}, x), {
337
- then(l, P) {
338
- return V().then(l, P);
336
+ return r.immediate && Promise.resolve().then(() => U()), N(d({}, x), {
337
+ then(c, P) {
338
+ return V().then(c, P);
339
339
  }
340
340
  });
341
341
  }
342
342
  function Ce(e, t) {
343
343
  const n = {};
344
- return Object.keys(e).forEach((a) => {
345
- n[a] = (...r) => {
346
- const o = e[a], b = o.type;
347
- let d = `/api/${o.url}`;
348
- o.hasBody ? d = Y(d, ...r.splice(1)) : d = Y(d, ...r);
344
+ return Object.keys(e).forEach((i) => {
345
+ n[i] = (...r) => {
346
+ const o = e[i], b = o.type;
347
+ let l = o.url;
348
+ l.startsWith("http") || (l = `/api/${l}`), o.hasBody ? l = Y(l, ...r.splice(1)) : l = Y(l, ...r);
349
349
  const v = {
350
- ...ae(),
350
+ ...ie(),
351
351
  ...o.headers()
352
352
  }, g = De(
353
- d,
353
+ l,
354
354
  {
355
355
  method: b
356
356
  },
@@ -386,8 +386,8 @@ function Ue(e = 500) {
386
386
  debouncer: null,
387
387
  timeout: e
388
388
  });
389
- function n(s, ...a) {
390
- t.debouncer !== null && window.clearTimeout(t.debouncer), t.debouncer = window.setTimeout(() => s(...a), t.timeout);
389
+ function n(s, ...i) {
390
+ t.debouncer !== null && window.clearTimeout(t.debouncer), t.debouncer = window.setTimeout(() => s(...i), t.timeout);
391
391
  }
392
392
  return {
393
393
  ...me(t),
@@ -397,7 +397,7 @@ function Ue(e = 500) {
397
397
  export {
398
398
  Ne as copy,
399
399
  $e as createEndpoint,
400
- ae as getDefaultHeaders,
400
+ ie as getDefaultHeaders,
401
401
  He as requestTypes,
402
402
  Ce as useApi,
403
403
  Ue as useDebouncer
@@ -1 +1 @@
1
- (function(p,$){typeof exports=="object"&&typeof module<"u"?$(exports,require("@citruslime/utils"),require("vue")):typeof define=="function"&&define.amd?define(["exports","@citruslime/utils","vue"],$):(p=typeof globalThis<"u"?globalThis:p||self,$(p.CitrusLimeVueUtils={},p.CLUtils,p.Vue))})(this,function(p,$,s){"use strict";const ie=["GET","POST","PUT","DELETE"];function ae(e,t,n=null,u=null){return{url:e,type:t,headers:n??I,hasBody:se(t,u)}}function I(){return{accept:"application/json","content-type":"application/json"}}function se(e,t){return!!(t===null&&(e==="PUT"||e==="POST")||t)}function Q(e){return s.getCurrentScope()?(s.onScopeDispose(e),!0):!1}function L(){const e=new Set,t=i=>{e.delete(i)};return{on:i=>{e.add(i);const r=()=>t(i);return Q(r),{off:r}},off:t,trigger:i=>Promise.all(Array.from(e).map(r=>r(i)))}}function O(e){return typeof e=="function"?e():s.unref(e)}const X=typeof window<"u",le=()=>{};function Y(e,t=!1,n="Timeout"){return new Promise((u,i)=>{setTimeout(t?()=>i(n):u,e)})}function ue(e,...t){return t.some(n=>n in e)}function R(...e){if(e.length!==1)return s.toRef(...e);const t=e[0];return typeof t=="function"?s.readonly(s.customRef(()=>({get:t,set:le}))):s.ref(t)}function K(e,t=!1){function n(a,{flush:l="sync",deep:m=!1,timeout:y,throwOnTimeout:v}={}){let T=null;const A=[new Promise(_=>{T=s.watch(e,D=>{a(D)!==t&&(T==null||T(),_(D))},{flush:l,deep:m,immediate:!0})})];return y!=null&&A.push(Y(y,v).then(()=>O(e)).finally(()=>T==null?void 0:T())),Promise.race(A)}function u(a,l){if(!s.isRef(a))return n(D=>D===a,l);const{flush:m="sync",deep:y=!1,timeout:v,throwOnTimeout:T}=l??{};let b=null;const _=[new Promise(D=>{b=s.watch([e,a],([S,B])=>{t!==(S===B)&&(b==null||b(),D(S))},{flush:m,deep:y,immediate:!0})})];return v!=null&&_.push(Y(v,T).then(()=>O(e)).finally(()=>(b==null||b(),O(e)))),Promise.race(_)}function i(a){return n(l=>!!l,a)}function r(a){return u(null,a)}function o(a){return u(void 0,a)}function F(a){return n(Number.isNaN,a)}function h(a,l){return n(m=>{const y=Array.from(m);return y.includes(a)||y.includes(O(a))},l)}function P(a){return E(1,a)}function E(a=1,l){let m=-1;return n(()=>(m+=1,m>=a),l)}return Array.isArray(O(e))?{toMatch:n,toContains:h,changed:P,changedTimes:E,get not(){return K(e,!t)}}:{toMatch:n,toBe:u,toBeTruthy:i,toBeNull:r,toBeNaN:F,toBeUndefined:o,changed:P,changedTimes:E,get not(){return K(e,!t)}}}function ce(e){return K(e)}function fe(e,t,n={}){const{immediate:u=!0}=n,i=s.ref(!1);let r=null;function o(){r&&(clearTimeout(r),r=null)}function F(){i.value=!1,o()}function h(...P){o(),i.value=!0,r=setTimeout(()=>{i.value=!1,r=null,e(...P)},O(t))}return u&&(i.value=!0,X&&h()),Q(F),{isPending:s.readonly(i),start:h,stop:F}}const de=X?window:void 0;var pe=Object.defineProperty,he=Object.defineProperties,ye=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,me=Object.prototype.hasOwnProperty,we=Object.prototype.propertyIsEnumerable,ee=(e,t,n)=>t in e?pe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,c=(e,t)=>{for(var n in t||(t={}))me.call(t,n)&&ee(e,n,t[n]);if(Z)for(var n of Z(t))we.call(t,n)&&ee(e,n,t[n]);return e},U=(e,t)=>he(e,ye(t));const Te={json:"application/json",text:"text/plain"};function te(e){return e&&ue(e,"immediate","refetch","initialData","timeout","beforeFetch","afterFetch","onFetchError","fetch")}function W(e){return typeof Headers<"u"&&e instanceof Headers?Object.fromEntries([...e.entries()]):e}function Pe(e,...t){var n;const u=typeof AbortController=="function";let i={},r={immediate:!0,refetch:!1,timeout:0};const o={method:"GET",type:"text",payload:void 0};t.length>0&&(te(t[0])?r=c(c({},r),t[0]):i=t[0]),t.length>1&&te(t[1])&&(r=c(c({},r),t[1]));const{fetch:F=(n=de)==null?void 0:n.fetch,initialData:h,timeout:P}=r,E=L(),a=L(),l=L(),m=s.ref(!1),y=s.ref(!1),v=s.ref(!1),T=s.ref(null),b=s.shallowRef(null),A=s.shallowRef(null),_=s.shallowRef(h||null),D=s.computed(()=>u&&y.value);let S,B;const k=()=>{u&&(S==null||S.abort(),S=new AbortController,S.signal.onabort=()=>v.value=!0,i=U(c({},i),{signal:S.signal}))},x=f=>{y.value=f,m.value=!f};P&&(B=fe(k,P,{immediate:!1}));const J=async(f=!1)=>{var g;k(),x(!0),A.value=null,T.value=null,v.value=!1;const w={method:o.method,headers:{}};if(o.payload){const V=W(w.headers);o.payloadType&&(V["Content-Type"]=(g=Te[o.payloadType])!=null?g:o.payloadType);const G=O(o.payload);w.body=o.payloadType==="json"?JSON.stringify(G):G}let N=!1;const j={url:O(e),options:c(c({},w),i),cancel:()=>{N=!0}};if(r.beforeFetch&&Object.assign(j,await r.beforeFetch(j)),N||!F)return x(!1),Promise.resolve(null);let C=null;return B&&B.start(),new Promise((V,G)=>{var re;F(j.url,U(c(c({},w),j.options),{headers:c(c({},W(w.headers)),W((re=j.options)==null?void 0:re.headers))})).then(async d=>{if(b.value=d,T.value=d.status,C=await d[o.type](),!d.ok)throw _.value=h||null,new Error(d.statusText);return r.afterFetch&&({data:C}=await r.afterFetch({data:C,response:d})),_.value=C,E.trigger(d),V(d)}).catch(async d=>{let oe=d.message||d.name;return r.onFetchError&&({error:oe}=await r.onFetchError({data:C,error:d,response:b.value})),A.value=oe,a.trigger(d),f?G(d):V(null)}).finally(()=>{x(!1),B&&B.stop(),l.trigger(null)})})},ne=R(r.refetch);s.watch([ne,R(e)],([f])=>f&&J(),{deep:!0});const M={isFinished:m,statusCode:T,response:b,error:A,data:_,isFetching:y,canAbort:D,aborted:v,abort:k,execute:J,onFetchResponse:E.on,onFetchError:a.on,onFetchFinally:l.on,get:H("GET"),put:H("PUT"),post:H("POST"),delete:H("DELETE"),patch:H("PATCH"),head:H("HEAD"),options:H("OPTIONS"),json:q("json"),text:q("text"),blob:q("blob"),arrayBuffer:q("arrayBuffer"),formData:q("formData")};function H(f){return(g,w)=>{if(!y.value){o.method=f,o.payload=g,o.payloadType=w,s.isRef(o.payload)&&s.watch([ne,R(o.payload)],([j])=>j&&J(),{deep:!0});const N=O(o.payload);return!w&&N&&Object.getPrototypeOf(N)===Object.prototype&&!(N instanceof FormData)&&(o.payloadType="json"),U(c({},M),{then(j,C){return z().then(j,C)}})}}}function z(){return new Promise((f,g)=>{ce(m).toBe(!0).then(()=>f(M)).catch(w=>g(w))})}function q(f){return()=>{if(!y.value)return o.type=f,U(c({},M),{then(g,w){return z().then(g,w)}})}}return r.immediate&&Promise.resolve().then(()=>J()),U(c({},M),{then(f,g){return z().then(f,g)}})}function be(e,t){const n={};return Object.keys(e).forEach(i=>{n[i]=(...r)=>{const o=e[i],F=o.type;let h=`/api/${o.url}`;o.hasBody?h=$.replacePlaceholders(h,...r.splice(1)):h=$.replacePlaceholders(h,...r);const P={...I(),...o.headers()},E=Pe(h,{method:F},{immediate:!1,beforeFetch({options:l}){return l.headers={...l.headers,...P},o.hasBody&&r.length&&(l.body=JSON.stringify(r[0])),{options:l}},onFetchError(l){return l!=null&&l.response&&t(l),l}});return{...ge(P)==="application/json"?E.json():E.text()}}}),n}function ge(e){return e.accept??"*/*"}function Oe(e){return JSON.parse(JSON.stringify(e))}function Fe(e=500){const t=s.reactive({debouncer:null,timeout:e});function n(u,...i){t.debouncer!==null&&window.clearTimeout(t.debouncer),t.debouncer=window.setTimeout(()=>u(...i),t.timeout)}return{...s.toRefs(t),debounce:n}}p.copy=Oe,p.createEndpoint=ae,p.getDefaultHeaders=I,p.requestTypes=ie,p.useApi=be,p.useDebouncer=Fe,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,$){typeof exports=="object"&&typeof module<"u"?$(exports,require("@citruslime/utils"),require("vue")):typeof define=="function"&&define.amd?define(["exports","@citruslime/utils","vue"],$):(h=typeof globalThis<"u"?globalThis:h||self,$(h.CitrusLimeVueUtils={},h.CLUtils,h.Vue))})(this,function(h,$,s){"use strict";const ie=["GET","POST","PUT","DELETE"];function ae(e,t,n=null,u=null){return{url:e,type:t,headers:n??I,hasBody:se(t,u)}}function I(){return{accept:"application/json","content-type":"application/json"}}function se(e,t){return!!(t===null&&(e==="PUT"||e==="POST")||t)}function Q(e){return s.getCurrentScope()?(s.onScopeDispose(e),!0):!1}function L(){const e=new Set,t=i=>{e.delete(i)};return{on:i=>{e.add(i);const r=()=>t(i);return Q(r),{off:r}},off:t,trigger:i=>Promise.all(Array.from(e).map(r=>r(i)))}}function O(e){return typeof e=="function"?e():s.unref(e)}const X=typeof window<"u",le=()=>{};function Y(e,t=!1,n="Timeout"){return new Promise((u,i)=>{setTimeout(t?()=>i(n):u,e)})}function ue(e,...t){return t.some(n=>n in e)}function R(...e){if(e.length!==1)return s.toRef(...e);const t=e[0];return typeof t=="function"?s.readonly(s.customRef(()=>({get:t,set:le}))):s.ref(t)}function K(e,t=!1){function n(a,{flush:l="sync",deep:m=!1,timeout:y,throwOnTimeout:v}={}){let T=null;const A=[new Promise(_=>{T=s.watch(e,D=>{a(D)!==t&&(T==null||T(),_(D))},{flush:l,deep:m,immediate:!0})})];return y!=null&&A.push(Y(y,v).then(()=>O(e)).finally(()=>T==null?void 0:T())),Promise.race(A)}function u(a,l){if(!s.isRef(a))return n(D=>D===a,l);const{flush:m="sync",deep:y=!1,timeout:v,throwOnTimeout:T}=l??{};let b=null;const _=[new Promise(D=>{b=s.watch([e,a],([S,B])=>{t!==(S===B)&&(b==null||b(),D(S))},{flush:m,deep:y,immediate:!0})})];return v!=null&&_.push(Y(v,T).then(()=>O(e)).finally(()=>(b==null||b(),O(e)))),Promise.race(_)}function i(a){return n(l=>!!l,a)}function r(a){return u(null,a)}function o(a){return u(void 0,a)}function F(a){return n(Number.isNaN,a)}function c(a,l){return n(m=>{const y=Array.from(m);return y.includes(a)||y.includes(O(a))},l)}function P(a){return E(1,a)}function E(a=1,l){let m=-1;return n(()=>(m+=1,m>=a),l)}return Array.isArray(O(e))?{toMatch:n,toContains:c,changed:P,changedTimes:E,get not(){return K(e,!t)}}:{toMatch:n,toBe:u,toBeTruthy:i,toBeNull:r,toBeNaN:F,toBeUndefined:o,changed:P,changedTimes:E,get not(){return K(e,!t)}}}function ce(e){return K(e)}function fe(e,t,n={}){const{immediate:u=!0}=n,i=s.ref(!1);let r=null;function o(){r&&(clearTimeout(r),r=null)}function F(){i.value=!1,o()}function c(...P){o(),i.value=!0,r=setTimeout(()=>{i.value=!1,r=null,e(...P)},O(t))}return u&&(i.value=!0,X&&c()),Q(F),{isPending:s.readonly(i),start:c,stop:F}}const de=X?window:void 0;var pe=Object.defineProperty,he=Object.defineProperties,ye=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,me=Object.prototype.hasOwnProperty,we=Object.prototype.propertyIsEnumerable,ee=(e,t,n)=>t in e?pe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,f=(e,t)=>{for(var n in t||(t={}))me.call(t,n)&&ee(e,n,t[n]);if(Z)for(var n of Z(t))we.call(t,n)&&ee(e,n,t[n]);return e},U=(e,t)=>he(e,ye(t));const Te={json:"application/json",text:"text/plain"};function te(e){return e&&ue(e,"immediate","refetch","initialData","timeout","beforeFetch","afterFetch","onFetchError","fetch")}function W(e){return typeof Headers<"u"&&e instanceof Headers?Object.fromEntries([...e.entries()]):e}function Pe(e,...t){var n;const u=typeof AbortController=="function";let i={},r={immediate:!0,refetch:!1,timeout:0};const o={method:"GET",type:"text",payload:void 0};t.length>0&&(te(t[0])?r=f(f({},r),t[0]):i=t[0]),t.length>1&&te(t[1])&&(r=f(f({},r),t[1]));const{fetch:F=(n=de)==null?void 0:n.fetch,initialData:c,timeout:P}=r,E=L(),a=L(),l=L(),m=s.ref(!1),y=s.ref(!1),v=s.ref(!1),T=s.ref(null),b=s.shallowRef(null),A=s.shallowRef(null),_=s.shallowRef(c||null),D=s.computed(()=>u&&y.value);let S,B;const k=()=>{u&&(S==null||S.abort(),S=new AbortController,S.signal.onabort=()=>v.value=!0,i=U(f({},i),{signal:S.signal}))},x=d=>{y.value=d,m.value=!d};P&&(B=fe(k,P,{immediate:!1}));const J=async(d=!1)=>{var g;k(),x(!0),A.value=null,T.value=null,v.value=!1;const w={method:o.method,headers:{}};if(o.payload){const V=W(w.headers);o.payloadType&&(V["Content-Type"]=(g=Te[o.payloadType])!=null?g:o.payloadType);const G=O(o.payload);w.body=o.payloadType==="json"?JSON.stringify(G):G}let N=!1;const j={url:O(e),options:f(f({},w),i),cancel:()=>{N=!0}};if(r.beforeFetch&&Object.assign(j,await r.beforeFetch(j)),N||!F)return x(!1),Promise.resolve(null);let C=null;return B&&B.start(),new Promise((V,G)=>{var re;F(j.url,U(f(f({},w),j.options),{headers:f(f({},W(w.headers)),W((re=j.options)==null?void 0:re.headers))})).then(async p=>{if(b.value=p,T.value=p.status,C=await p[o.type](),!p.ok)throw _.value=c||null,new Error(p.statusText);return r.afterFetch&&({data:C}=await r.afterFetch({data:C,response:p})),_.value=C,E.trigger(p),V(p)}).catch(async p=>{let oe=p.message||p.name;return r.onFetchError&&({error:oe}=await r.onFetchError({data:C,error:p,response:b.value})),A.value=oe,a.trigger(p),d?G(p):V(null)}).finally(()=>{x(!1),B&&B.stop(),l.trigger(null)})})},ne=R(r.refetch);s.watch([ne,R(e)],([d])=>d&&J(),{deep:!0});const M={isFinished:m,statusCode:T,response:b,error:A,data:_,isFetching:y,canAbort:D,aborted:v,abort:k,execute:J,onFetchResponse:E.on,onFetchError:a.on,onFetchFinally:l.on,get:H("GET"),put:H("PUT"),post:H("POST"),delete:H("DELETE"),patch:H("PATCH"),head:H("HEAD"),options:H("OPTIONS"),json:q("json"),text:q("text"),blob:q("blob"),arrayBuffer:q("arrayBuffer"),formData:q("formData")};function H(d){return(g,w)=>{if(!y.value){o.method=d,o.payload=g,o.payloadType=w,s.isRef(o.payload)&&s.watch([ne,R(o.payload)],([j])=>j&&J(),{deep:!0});const N=O(o.payload);return!w&&N&&Object.getPrototypeOf(N)===Object.prototype&&!(N instanceof FormData)&&(o.payloadType="json"),U(f({},M),{then(j,C){return z().then(j,C)}})}}}function z(){return new Promise((d,g)=>{ce(m).toBe(!0).then(()=>d(M)).catch(w=>g(w))})}function q(d){return()=>{if(!y.value)return o.type=d,U(f({},M),{then(g,w){return z().then(g,w)}})}}return r.immediate&&Promise.resolve().then(()=>J()),U(f({},M),{then(d,g){return z().then(d,g)}})}function be(e,t){const n={};return Object.keys(e).forEach(i=>{n[i]=(...r)=>{const o=e[i],F=o.type;let c=o.url;c.startsWith("http")||(c=`/api/${c}`),o.hasBody?c=$.replacePlaceholders(c,...r.splice(1)):c=$.replacePlaceholders(c,...r);const P={...I(),...o.headers()},E=Pe(c,{method:F},{immediate:!1,beforeFetch({options:l}){return l.headers={...l.headers,...P},o.hasBody&&r.length&&(l.body=JSON.stringify(r[0])),{options:l}},onFetchError(l){return l!=null&&l.response&&t(l),l}});return{...ge(P)==="application/json"?E.json():E.text()}}}),n}function ge(e){return e.accept??"*/*"}function Oe(e){return JSON.parse(JSON.stringify(e))}function Fe(e=500){const t=s.reactive({debouncer:null,timeout:e});function n(u,...i){t.debouncer!==null&&window.clearTimeout(t.debouncer),t.debouncer=window.setTimeout(()=>u(...i),t.timeout)}return{...s.toRefs(t),debounce:n}}h.copy=Oe,h.createEndpoint=ae,h.getDefaultHeaders=I,h.requestTypes=ie,h.useApi=be,h.useDebouncer=Fe,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@citruslime/vue-utils",
3
- "version": "1.3.0-beta.1",
3
+ "version": "1.4.0-beta.0",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Citrus-Lime Ltd",
@@ -33,9 +33,7 @@
33
33
  "build-types": "vue-tsc --declaration --emitDeclarationOnly --declarationDir ./dist --project ./tsconfig.app.json --composite false"
34
34
  },
35
35
  "dependencies": {
36
- "@citruslime/utils": "^1.2.0-beta.0"
37
- },
38
- "peerDependencies": {
36
+ "@citruslime/utils": "^1.2.0-beta.0",
39
37
  "@vueuse/core": "^10.1.2",
40
38
  "vue": "^3.2.47"
41
39
  }