@citruslime/vue-utils 1.4.3 → 1.4.4

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