@citruslime/vue-utils 2.0.4-beta.1 → 2.0.4-beta.2

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