@citruslime/vue-utils 1.4.6-beta.0 → 1.4.6

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