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