@citruslime/vue-utils 1.2.0 → 1.2.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,46 +1,46 @@
1
- import { replacePlaceholders as ne } from "@citruslime/utils";
2
- import { computed as fe, ref as B, readonly as ye, unref as he, getCurrentScope as me, onScopeDispose as Oe, watch as G, isRef as X, shallowRef as M, reactive as ve, toRefs as ge } from "vue";
3
- const Ke = [
1
+ import { replacePlaceholders as te } from "@citruslime/utils";
2
+ import { computed as ce, ref as D, readonly as ye, unref as he, getCurrentScope as me, onScopeDispose as Oe, watch as G, isRef as fe, shallowRef as M, reactive as ve, toRefs as we } from "vue";
3
+ const Je = [
4
4
  "GET",
5
5
  "POST",
6
6
  "PUT",
7
7
  "DELETE"
8
8
  ];
9
- function Le(e, t, n = null, s = null) {
9
+ function Ke(e, t, n = null, s = null) {
10
10
  return {
11
11
  url: e,
12
12
  type: t,
13
- headers: n ?? we(),
14
- hasBody: be(t, s)
13
+ headers: n ?? de,
14
+ hasBody: ge(t, s)
15
15
  };
16
16
  }
17
- function we() {
17
+ function de() {
18
18
  return {
19
- Accept: "application/json",
20
- "Content-Type": "application/json"
19
+ accept: "application/json",
20
+ "content-type": "application/json"
21
21
  };
22
22
  }
23
- function be(e, t) {
23
+ function ge(e, t) {
24
24
  return !!(t === null && (e === "PUT" || e === "POST") || t);
25
25
  }
26
- var re;
27
- const Y = typeof window < "u";
28
- Y && ((re = window == null ? void 0 : window.navigator) != null && re.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
26
+ var ne;
27
+ const X = typeof window < "u";
28
+ X && ((ne = window == null ? void 0 : window.navigator) != null && ne.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
29
29
  function E(e) {
30
30
  return typeof e == "function" ? e() : he(e);
31
31
  }
32
- function oe(e, t = !1, n = "Timeout") {
32
+ function re(e, t = !1, n = "Timeout") {
33
33
  return new Promise((s, a) => {
34
34
  setTimeout(t ? () => a(n) : s, e);
35
35
  });
36
36
  }
37
- function Te(e) {
37
+ function be(e) {
38
38
  return e;
39
39
  }
40
40
  function Pe(e, ...t) {
41
41
  return t.some((n) => n in e);
42
42
  }
43
- function de(e) {
43
+ function pe(e) {
44
44
  return me() ? (Oe(e), !0) : !1;
45
45
  }
46
46
  function W() {
@@ -52,7 +52,7 @@ function W() {
52
52
  on: (a) => {
53
53
  e.push(a);
54
54
  const r = () => t(a);
55
- return de(r), {
55
+ return pe(r), {
56
56
  off: r
57
57
  };
58
58
  },
@@ -63,37 +63,37 @@ function W() {
63
63
  };
64
64
  }
65
65
  function V(e) {
66
- return typeof e == "function" ? fe(e) : B(e);
66
+ return typeof e == "function" ? ce(e) : D(e);
67
67
  }
68
68
  function q(e, t = !1) {
69
69
  function n(i, { flush: u = "sync", deep: h = !1, timeout: d, throwOnTimeout: F } = {}) {
70
70
  let p = null;
71
- const C = [new Promise((j) => {
72
- p = G(e, (I) => {
73
- i(I) !== t && (p == null || p(), j(I));
71
+ const N = [new Promise((I) => {
72
+ p = G(e, (j) => {
73
+ i(j) !== t && (p == null || p(), I(j));
74
74
  }, {
75
75
  flush: u,
76
76
  deep: h,
77
77
  immediate: !0
78
78
  });
79
79
  })];
80
- return d != null && C.push(oe(d, F).then(() => E(e)).finally(() => p == null ? void 0 : p())), Promise.race(C);
80
+ return d != null && N.push(re(d, F).then(() => E(e)).finally(() => p == null ? void 0 : p())), Promise.race(N);
81
81
  }
82
82
  function s(i, u) {
83
- if (!X(i))
84
- return n((I) => I === i, u);
83
+ if (!fe(i))
84
+ return n((j) => j === i, u);
85
85
  const { flush: h = "sync", deep: d = !1, timeout: F, throwOnTimeout: p } = u ?? {};
86
86
  let v = null;
87
- const j = [new Promise((I) => {
88
- v = G([e, i], ([T, N]) => {
89
- t !== (T === N) && (v == null || v(), I(T));
87
+ const I = [new Promise((j) => {
88
+ v = G([e, i], ([P, S]) => {
89
+ t !== (P === S) && (v == null || v(), j(P));
90
90
  }, {
91
91
  flush: h,
92
92
  deep: d,
93
93
  immediate: !0
94
94
  });
95
95
  })];
96
- return F != null && j.push(oe(F, p).then(() => E(e)).finally(() => (v == null || v(), E(e)))), Promise.race(j);
96
+ return F != null && I.push(re(F, p).then(() => E(e)).finally(() => (v == null || v(), E(e)))), Promise.race(I);
97
97
  }
98
98
  function a(i) {
99
99
  return n((u) => Boolean(u), i);
@@ -104,7 +104,7 @@ function q(e, t = !1) {
104
104
  function o(i) {
105
105
  return s(void 0, i);
106
106
  }
107
- function w(i) {
107
+ function g(i) {
108
108
  return n(Number.isNaN, i);
109
109
  }
110
110
  function y(i, u) {
@@ -133,7 +133,7 @@ function q(e, t = !1) {
133
133
  toBe: s,
134
134
  toBeTruthy: a,
135
135
  toBeNull: r,
136
- toBeNaN: w,
136
+ toBeNaN: g,
137
137
  toBeUndefined: o,
138
138
  changed: O,
139
139
  changedTimes: b,
@@ -142,18 +142,18 @@ function q(e, t = !1) {
142
142
  }
143
143
  };
144
144
  }
145
- function _e(e) {
145
+ function Te(e) {
146
146
  return q(e);
147
147
  }
148
- function Ee(e, t, n = {}) {
148
+ function _e(e, t, n = {}) {
149
149
  const {
150
150
  immediate: s = !0
151
- } = n, a = B(!1);
151
+ } = n, a = D(!1);
152
152
  let r = null;
153
153
  function o() {
154
154
  r && (clearTimeout(r), r = null);
155
155
  }
156
- function w() {
156
+ function g() {
157
157
  a.value = !1, o();
158
158
  }
159
159
  function y(...O) {
@@ -161,34 +161,34 @@ function Ee(e, t, n = {}) {
161
161
  a.value = !1, r = null, e(...O);
162
162
  }, E(t));
163
163
  }
164
- return s && (a.value = !0, Y && y()), de(w), {
164
+ return s && (a.value = !0, X && y()), pe(g), {
165
165
  isPending: ye(a),
166
166
  start: y,
167
- stop: w
167
+ stop: g
168
168
  };
169
169
  }
170
- const Fe = Y ? window : void 0, R = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, z = "__vueuse_ssr_handlers__";
171
- R[z] = R[z] || {};
172
- R[z];
173
- var je = Object.defineProperty, Ie = Object.defineProperties, Ce = Object.getOwnPropertyDescriptors, ae = Object.getOwnPropertySymbols, Ne = Object.prototype.hasOwnProperty, Se = Object.prototype.propertyIsEnumerable, ie = (e, t, n) => t in e ? je(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, f = (e, t) => {
170
+ const Ee = X ? window : void 0, z = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, R = "__vueuse_ssr_handlers__";
171
+ z[R] = z[R] || {};
172
+ z[R];
173
+ var Fe = Object.defineProperty, Ie = Object.defineProperties, je = Object.getOwnPropertyDescriptors, oe = Object.getOwnPropertySymbols, Ne = Object.prototype.hasOwnProperty, Se = Object.prototype.propertyIsEnumerable, ae = (e, t, n) => t in e ? Fe(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, f = (e, t) => {
174
174
  for (var n in t || (t = {}))
175
- Ne.call(t, n) && ie(e, n, t[n]);
176
- if (ae)
177
- for (var n of ae(t))
178
- Se.call(t, n) && ie(e, n, t[n]);
175
+ Ne.call(t, n) && ae(e, n, t[n]);
176
+ if (oe)
177
+ for (var n of oe(t))
178
+ Se.call(t, n) && ae(e, n, t[n]);
179
179
  return e;
180
- }, H = (e, t) => Ie(e, Ce(t));
181
- const Ae = {
180
+ }, H = (e, t) => Ie(e, je(t));
181
+ const Be = {
182
182
  json: "application/json",
183
183
  text: "text/plain"
184
184
  };
185
- function ue(e) {
185
+ function ie(e) {
186
186
  return e && Pe(e, "immediate", "refetch", "initialData", "timeout", "beforeFetch", "afterFetch", "onFetchError", "fetch");
187
187
  }
188
188
  function k(e) {
189
189
  return typeof Headers < "u" && e instanceof Headers ? Object.fromEntries([...e.entries()]) : e;
190
190
  }
191
- function Be(e, ...t) {
191
+ function Ce(e, ...t) {
192
192
  var n;
193
193
  const s = typeof AbortController == "function";
194
194
  let a = {}, r = { immediate: !0, refetch: !1, timeout: 0 };
@@ -197,23 +197,23 @@ function Be(e, ...t) {
197
197
  type: "text",
198
198
  payload: void 0
199
199
  };
200
- t.length > 0 && (ue(t[0]) ? r = f(f({}, r), t[0]) : a = t[0]), t.length > 1 && ue(t[1]) && (r = f(f({}, r), t[1]));
200
+ t.length > 0 && (ie(t[0]) ? r = f(f({}, r), t[0]) : a = t[0]), t.length > 1 && ie(t[1]) && (r = f(f({}, r), t[1]));
201
201
  const {
202
- fetch: w = (n = Fe) == null ? void 0 : n.fetch,
202
+ fetch: g = (n = Ee) == null ? void 0 : n.fetch,
203
203
  initialData: y,
204
204
  timeout: O
205
- } = r, b = W(), i = W(), u = W(), h = B(!1), d = B(!1), F = B(!1), p = B(null), v = M(null), C = M(null), j = M(y), I = fe(() => s && d.value);
206
- let T, N;
205
+ } = r, b = W(), i = W(), u = W(), h = D(!1), d = D(!1), F = D(!1), p = D(null), v = M(null), N = M(null), I = M(y), j = ce(() => s && d.value);
206
+ let P, S;
207
207
  const J = () => {
208
- s && T && (T.abort(), T = void 0);
208
+ s && P && (P.abort(), P = void 0);
209
209
  }, K = (l) => {
210
210
  d.value = l, h.value = !l;
211
211
  };
212
- O && (N = Ee(J, O, { immediate: !1 }));
212
+ O && (S = _e(J, O, { immediate: !1 }));
213
213
  const $ = async (l = !1) => {
214
- var g;
215
- K(!0), C.value = null, p.value = null, F.value = !1, s && (J(), T = new AbortController(), T.signal.onabort = () => F.value = !0, a = H(f({}, a), {
216
- signal: T.signal
214
+ var w;
215
+ K(!0), N.value = null, p.value = null, F.value = !1, s && (J(), P = new AbortController(), P.signal.onabort = () => F.value = !0, a = H(f({}, a), {
216
+ signal: P.signal
217
217
  }));
218
218
  const m = {
219
219
  method: o.method,
@@ -221,118 +221,118 @@ function Be(e, ...t) {
221
221
  };
222
222
  if (o.payload) {
223
223
  const Q = k(m.headers);
224
- o.payloadType && (Q["Content-Type"] = (g = Ae[o.payloadType]) != null ? g : o.payloadType);
224
+ o.payloadType && (Q["Content-Type"] = (w = Be[o.payloadType]) != null ? w : o.payloadType);
225
225
  const U = E(o.payload);
226
226
  m.body = o.payloadType === "json" ? JSON.stringify(U) : U;
227
227
  }
228
- let A = !1;
229
- const P = {
228
+ let C = !1;
229
+ const T = {
230
230
  url: E(e),
231
231
  options: f(f({}, m), a),
232
232
  cancel: () => {
233
- A = !0;
233
+ C = !0;
234
234
  }
235
235
  };
236
- if (r.beforeFetch && Object.assign(P, await r.beforeFetch(P)), A || !w)
236
+ if (r.beforeFetch && Object.assign(T, await r.beforeFetch(T)), C || !g)
237
237
  return K(!1), Promise.resolve(null);
238
238
  let _ = null;
239
- return N && N.start(), new Promise((Q, U) => {
240
- var ee;
241
- w(P.url, H(f(f({}, m), P.options), {
242
- headers: f(f({}, k(m.headers)), k((ee = P.options) == null ? void 0 : ee.headers))
239
+ return S && S.start(), new Promise((Q, U) => {
240
+ var Z;
241
+ g(T.url, H(f(f({}, m), T.options), {
242
+ headers: f(f({}, k(m.headers)), k((Z = T.options) == null ? void 0 : Z.headers))
243
243
  })).then(async (c) => {
244
- if (v.value = c, p.value = c.status, _ = await c[o.type](), r.afterFetch && p.value >= 200 && p.value < 300 && ({ data: _ } = await r.afterFetch({ data: _, response: c })), j.value = _, !c.ok)
244
+ if (v.value = c, p.value = c.status, _ = await c[o.type](), r.afterFetch && p.value >= 200 && p.value < 300 && ({ data: _ } = await r.afterFetch({ data: _, response: c })), I.value = _, !c.ok)
245
245
  throw new Error(c.statusText);
246
246
  return b.trigger(c), Q(c);
247
247
  }).catch(async (c) => {
248
- let te = c.message || c.name;
249
- return r.onFetchError && ({ data: _, error: te } = await r.onFetchError({ data: _, error: c, response: v.value })), j.value = _, C.value = te, i.trigger(c), l ? U(c) : Q(null);
248
+ let ee = c.message || c.name;
249
+ return r.onFetchError && ({ data: _, error: ee } = await r.onFetchError({ data: _, error: c, response: v.value })), I.value = _, N.value = ee, i.trigger(c), l ? U(c) : Q(null);
250
250
  }).finally(() => {
251
- K(!1), N && N.stop(), u.trigger(null);
251
+ K(!1), S && S.stop(), u.trigger(null);
252
252
  });
253
253
  });
254
- }, Z = V(r.refetch);
254
+ }, Y = V(r.refetch);
255
255
  G([
256
- Z,
256
+ Y,
257
257
  V(e)
258
258
  ], ([l]) => l && $(), { deep: !0 });
259
259
  const x = {
260
260
  isFinished: h,
261
261
  statusCode: p,
262
262
  response: v,
263
- error: C,
264
- data: j,
263
+ error: N,
264
+ data: I,
265
265
  isFetching: d,
266
- canAbort: I,
266
+ canAbort: j,
267
267
  aborted: F,
268
268
  abort: J,
269
269
  execute: $,
270
270
  onFetchResponse: b.on,
271
271
  onFetchError: i.on,
272
272
  onFetchFinally: u.on,
273
- get: S("GET"),
274
- put: S("PUT"),
275
- post: S("POST"),
276
- delete: S("DELETE"),
277
- patch: S("PATCH"),
278
- head: S("HEAD"),
279
- options: S("OPTIONS"),
280
- json: D("json"),
281
- text: D("text"),
282
- blob: D("blob"),
283
- arrayBuffer: D("arrayBuffer"),
284
- formData: D("formData")
273
+ get: B("GET"),
274
+ put: B("PUT"),
275
+ post: B("POST"),
276
+ delete: B("DELETE"),
277
+ patch: B("PATCH"),
278
+ head: B("HEAD"),
279
+ options: B("OPTIONS"),
280
+ json: A("json"),
281
+ text: A("text"),
282
+ blob: A("blob"),
283
+ arrayBuffer: A("arrayBuffer"),
284
+ formData: A("formData")
285
285
  };
286
- function S(l) {
287
- return (g, m) => {
286
+ function B(l) {
287
+ return (w, m) => {
288
288
  if (!d.value) {
289
- o.method = l, o.payload = g, o.payloadType = m, X(o.payload) && G([
290
- Z,
289
+ o.method = l, o.payload = w, o.payloadType = m, fe(o.payload) && G([
290
+ Y,
291
291
  V(o.payload)
292
- ], ([P]) => P && $(), { deep: !0 });
293
- const A = E(o.payload);
294
- return !m && A && Object.getPrototypeOf(A) === Object.prototype && !(A instanceof FormData) && (o.payloadType = "json"), H(f({}, x), {
295
- then(P, _) {
296
- return L().then(P, _);
292
+ ], ([T]) => T && $(), { deep: !0 });
293
+ const C = E(o.payload);
294
+ return !m && C && Object.getPrototypeOf(C) === Object.prototype && !(C instanceof FormData) && (o.payloadType = "json"), H(f({}, x), {
295
+ then(T, _) {
296
+ return L().then(T, _);
297
297
  }
298
298
  });
299
299
  }
300
300
  };
301
301
  }
302
302
  function L() {
303
- return new Promise((l, g) => {
304
- _e(h).toBe(!0).then(() => l(x)).catch((m) => g(m));
303
+ return new Promise((l, w) => {
304
+ Te(h).toBe(!0).then(() => l(x)).catch((m) => w(m));
305
305
  });
306
306
  }
307
- function D(l) {
307
+ function A(l) {
308
308
  return () => {
309
309
  if (!d.value)
310
310
  return o.type = l, H(f({}, x), {
311
- then(g, m) {
312
- return L().then(g, m);
311
+ then(w, m) {
312
+ return L().then(w, m);
313
313
  }
314
314
  });
315
315
  };
316
316
  }
317
317
  return r.immediate && setTimeout($, 0), H(f({}, x), {
318
- then(l, g) {
319
- return L().then(l, g);
318
+ then(l, w) {
319
+ return L().then(l, w);
320
320
  }
321
321
  });
322
322
  }
323
- var se;
323
+ var ue;
324
324
  (function(e) {
325
325
  e.UP = "UP", e.RIGHT = "RIGHT", e.DOWN = "DOWN", e.LEFT = "LEFT", e.NONE = "NONE";
326
- })(se || (se = {}));
327
- var De = Object.defineProperty, le = Object.getOwnPropertySymbols, He = Object.prototype.hasOwnProperty, $e = Object.prototype.propertyIsEnumerable, ce = (e, t, n) => t in e ? De(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, xe = (e, t) => {
326
+ })(ue || (ue = {}));
327
+ var De = Object.defineProperty, se = Object.getOwnPropertySymbols, Ae = Object.prototype.hasOwnProperty, He = Object.prototype.propertyIsEnumerable, le = (e, t, n) => t in e ? De(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, $e = (e, t) => {
328
328
  for (var n in t || (t = {}))
329
- He.call(t, n) && ce(e, n, t[n]);
330
- if (le)
331
- for (var n of le(t))
332
- $e.call(t, n) && ce(e, n, t[n]);
329
+ Ae.call(t, n) && le(e, n, t[n]);
330
+ if (se)
331
+ for (var n of se(t))
332
+ He.call(t, n) && le(e, n, t[n]);
333
333
  return e;
334
334
  };
335
- const Qe = {
335
+ const xe = {
336
336
  easeInSine: [0.12, 0, 0.39, 0],
337
337
  easeOutSine: [0.61, 1, 0.88, 1],
338
338
  easeInOutSine: [0.37, 0, 0.63, 1],
@@ -358,24 +358,23 @@ const Qe = {
358
358
  easeOutBack: [0.34, 1.56, 0.64, 1],
359
359
  easeInOutBack: [0.68, -0.6, 0.32, 1.6]
360
360
  };
361
- xe({
362
- linear: Te
363
- }, Qe);
364
- function Me(e, t) {
361
+ $e({
362
+ linear: be
363
+ }, xe);
364
+ function Le(e, t) {
365
365
  const n = {};
366
366
  return Object.keys(e).forEach((a) => {
367
367
  n[a] = (...r) => {
368
- const o = e[a], w = o.type;
368
+ const o = e[a], g = o.type;
369
369
  let y = `/api/${o.url}`;
370
- o.hasBody ? y = ne(y, ...r.splice(1)) : y = ne(y, ...r);
370
+ o.hasBody ? y = te(y, ...r.splice(1)) : y = te(y, ...r);
371
371
  const O = {
372
- Accept: "application/json",
373
- "Content-Type": "application/json",
374
- ...pe(o.headers)
375
- }, b = Be(
372
+ ...de(),
373
+ ...o.headers()
374
+ }, b = Ce(
376
375
  y,
377
376
  {
378
- method: w
377
+ method: g
379
378
  },
380
379
  {
381
380
  immediate: !1,
@@ -393,23 +392,18 @@ function Me(e, t) {
393
392
  }
394
393
  );
395
394
  return {
396
- ...Ue(O) === "application/json" ? b.json() : b.text()
395
+ ...Qe(O) === "application/json" ? b.json() : b.text()
397
396
  };
398
397
  };
399
398
  }), n;
400
399
  }
401
- function Ue(e) {
402
- const t = pe(e);
403
- return (t == null ? void 0 : t.accept) ?? "";
404
- }
405
- function pe(e) {
406
- let t = {};
407
- return X(e) ? t = e.value : t = e, t;
400
+ function Qe(e) {
401
+ return e.accept ?? "*/*";
408
402
  }
409
- function We(e) {
403
+ function Me(e) {
410
404
  return JSON.parse(JSON.stringify(e));
411
405
  }
412
- function Ve(e = 500) {
406
+ function We(e = 500) {
413
407
  const t = ve({
414
408
  debouncer: null,
415
409
  timeout: e
@@ -418,14 +412,15 @@ function Ve(e = 500) {
418
412
  t.debouncer !== null && window.clearTimeout(t.debouncer), t.debouncer = window.setTimeout(() => s(...a), t.timeout);
419
413
  }
420
414
  return {
421
- ...ge(t),
415
+ ...we(t),
422
416
  debounce: n
423
417
  };
424
418
  }
425
419
  export {
426
- We as copy,
427
- Le as createEndpoint,
428
- Ke as requestTypes,
429
- Me as useApi,
430
- Ve as useDebouncer
420
+ Me as copy,
421
+ Ke as createEndpoint,
422
+ de as getDefaultHeaders,
423
+ Je as requestTypes,
424
+ Le as useApi,
425
+ We as useDebouncer
431
426
  };
@@ -1 +1 @@
1
- (function(h,B){typeof exports=="object"&&typeof module<"u"?B(exports,require("@citruslime/utils"),require("vue")):typeof define=="function"&&define.amd?define(["exports","@citruslime/utils","vue"],B):(h=typeof globalThis<"u"?globalThis:h||self,B(h.CitrusLimeVueUtils={},h.CLUtils,h.Vue))})(this,function(h,B,u){"use strict";const fe=["GET","POST","PUT","DELETE"];function de(e,t,n=null,l=null){return{url:e,type:t,headers:n??pe(),hasBody:ye(t,l)}}function pe(){return{Accept:"application/json","Content-Type":"application/json"}}function ye(e,t){return!!(t===null&&(e==="PUT"||e==="POST")||t)}var Y;const R=typeof window<"u";R&&((Y=window==null?void 0:window.navigator)!=null&&Y.userAgent)&&/iP(ad|hone|od)/.test(window.navigator.userAgent);function P(e){return typeof e=="function"?e():u.unref(e)}function Z(e,t=!1,n="Timeout"){return new Promise((l,a)=>{setTimeout(t?()=>a(n):l,e)})}function he(e){return e}function me(e,...t){return t.some(n=>n in e)}function ee(e){return u.getCurrentScope()?(u.onScopeDispose(e),!0):!1}function q(){const e=[],t=a=>{const r=e.indexOf(a);r!==-1&&e.splice(r,1)};return{on:a=>{e.push(a);const r=()=>t(a);return ee(r),{off:r}},off:t,trigger:a=>{e.forEach(r=>r(a))}}}function J(e){return typeof e=="function"?u.computed(e):u.ref(e)}function M(e,t=!1){function n(i,{flush:s="sync",deep:O=!1,timeout:p,throwOnTimeout:I}={}){let y=null;const A=[new Promise(C=>{y=u.watch(e,S=>{i(S)!==t&&(y==null||y(),C(S))},{flush:s,deep:O,immediate:!0})})];return p!=null&&A.push(Z(p,I).then(()=>P(e)).finally(()=>y==null?void 0:y())),Promise.race(A)}function l(i,s){if(!u.isRef(i))return n(S=>S===i,s);const{flush:O="sync",deep:p=!1,timeout:I,throwOnTimeout:y}=s??{};let g=null;const C=[new Promise(S=>{g=u.watch([e,i],([E,D])=>{t!==(E===D)&&(g==null||g(),S(E))},{flush:O,deep:p,immediate:!0})})];return I!=null&&C.push(Z(I,y).then(()=>P(e)).finally(()=>(g==null||g(),P(e)))),Promise.race(C)}function a(i){return n(s=>Boolean(s),i)}function r(i){return l(null,i)}function o(i){return l(void 0,i)}function _(i){return n(Number.isNaN,i)}function m(i,s){return n(O=>{const p=Array.from(O);return p.includes(i)||p.includes(P(i))},s)}function T(i){return v(1,i)}function v(i=1,s){let O=-1;return n(()=>(O+=1,O>=i),s)}return Array.isArray(P(e))?{toMatch:n,toContains:m,changed:T,changedTimes:v,get not(){return M(e,!t)}}:{toMatch:n,toBe:l,toBeTruthy:a,toBeNull:r,toBeNaN:_,toBeUndefined:o,changed:T,changedTimes:v,get not(){return M(e,!t)}}}function Oe(e){return M(e)}function we(e,t,n={}){const{immediate:l=!0}=n,a=u.ref(!1);let r=null;function o(){r&&(clearTimeout(r),r=null)}function _(){a.value=!1,o()}function m(...T){o(),a.value=!0,r=setTimeout(()=>{a.value=!1,r=null,e(...T)},P(t))}return l&&(a.value=!0,R&&m()),ee(_),{isPending:u.readonly(a),start:m,stop:_}}const Te=R?window:void 0,V=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},K="__vueuse_ssr_handlers__";V[K]=V[K]||{},V[K];var ge=Object.defineProperty,be=Object.defineProperties,Pe=Object.getOwnPropertyDescriptors,te=Object.getOwnPropertySymbols,_e=Object.prototype.hasOwnProperty,ve=Object.prototype.propertyIsEnumerable,ne=(e,t,n)=>t in e?ge(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,c=(e,t)=>{for(var n in t||(t={}))_e.call(t,n)&&ne(e,n,t[n]);if(te)for(var n of te(t))ve.call(t,n)&&ne(e,n,t[n]);return e},U=(e,t)=>be(e,Pe(t));const Ee={json:"application/json",text:"text/plain"};function re(e){return e&&me(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 Fe(e,...t){var n;const l=typeof AbortController=="function";let a={},r={immediate:!0,refetch:!1,timeout:0};const o={method:"GET",type:"text",payload:void 0};t.length>0&&(re(t[0])?r=c(c({},r),t[0]):a=t[0]),t.length>1&&re(t[1])&&(r=c(c({},r),t[1]));const{fetch:_=(n=Te)==null?void 0:n.fetch,initialData:m,timeout:T}=r,v=q(),i=q(),s=q(),O=u.ref(!1),p=u.ref(!1),I=u.ref(!1),y=u.ref(null),g=u.shallowRef(null),A=u.shallowRef(null),C=u.shallowRef(m),S=u.computed(()=>l&&p.value);let E,D;const k=()=>{l&&E&&(E.abort(),E=void 0)},z=f=>{p.value=f,O.value=!f};T&&(D=we(k,T,{immediate:!1}));const Q=async(f=!1)=>{var b;z(!0),A.value=null,y.value=null,I.value=!1,l&&(k(),E=new AbortController,E.signal.onabort=()=>I.value=!0,a=U(c({},a),{signal:E.signal}));const w={method:o.method,headers:{}};if(o.payload){const G=W(w.headers);o.payloadType&&(G["Content-Type"]=(b=Ee[o.payloadType])!=null?b:o.payloadType);const L=P(o.payload);w.body=o.payloadType==="json"?JSON.stringify(L):L}let H=!1;const F={url:P(e),options:c(c({},w),a),cancel:()=>{H=!0}};if(r.beforeFetch&&Object.assign(F,await r.beforeFetch(F)),H||!_)return z(!1),Promise.resolve(null);let j=null;return D&&D.start(),new Promise((G,L)=>{var le;_(F.url,U(c(c({},w),F.options),{headers:c(c({},W(w.headers)),W((le=F.options)==null?void 0:le.headers))})).then(async d=>{if(g.value=d,y.value=d.status,j=await d[o.type](),r.afterFetch&&y.value>=200&&y.value<300&&({data:j}=await r.afterFetch({data:j,response:d})),C.value=j,!d.ok)throw new Error(d.statusText);return v.trigger(d),G(d)}).catch(async d=>{let ce=d.message||d.name;return r.onFetchError&&({data:j,error:ce}=await r.onFetchError({data:j,error:d,response:g.value})),C.value=j,A.value=ce,i.trigger(d),f?L(d):G(null)}).finally(()=>{z(!1),D&&D.stop(),s.trigger(null)})})},ue=J(r.refetch);u.watch([ue,J(e)],([f])=>f&&Q(),{deep:!0});const x={isFinished:O,statusCode:y,response:g,error:A,data:C,isFetching:p,canAbort:S,aborted:I,abort:k,execute:Q,onFetchResponse:v.on,onFetchError:i.on,onFetchFinally:s.on,get:N("GET"),put:N("PUT"),post:N("POST"),delete:N("DELETE"),patch:N("PATCH"),head:N("HEAD"),options:N("OPTIONS"),json:$("json"),text:$("text"),blob:$("blob"),arrayBuffer:$("arrayBuffer"),formData:$("formData")};function N(f){return(b,w)=>{if(!p.value){o.method=f,o.payload=b,o.payloadType=w,u.isRef(o.payload)&&u.watch([ue,J(o.payload)],([F])=>F&&Q(),{deep:!0});const H=P(o.payload);return!w&&H&&Object.getPrototypeOf(H)===Object.prototype&&!(H instanceof FormData)&&(o.payloadType="json"),U(c({},x),{then(F,j){return X().then(F,j)}})}}}function X(){return new Promise((f,b)=>{Oe(O).toBe(!0).then(()=>f(x)).catch(w=>b(w))})}function $(f){return()=>{if(!p.value)return o.type=f,U(c({},x),{then(b,w){return X().then(b,w)}})}}return r.immediate&&setTimeout(Q,0),U(c({},x),{then(f,b){return X().then(f,b)}})}var oe;(function(e){e.UP="UP",e.RIGHT="RIGHT",e.DOWN="DOWN",e.LEFT="LEFT",e.NONE="NONE"})(oe||(oe={}));var je=Object.defineProperty,ae=Object.getOwnPropertySymbols,Ie=Object.prototype.hasOwnProperty,Ce=Object.prototype.propertyIsEnumerable,ie=(e,t,n)=>t in e?je(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Se=(e,t)=>{for(var n in t||(t={}))Ie.call(t,n)&&ie(e,n,t[n]);if(ae)for(var n of ae(t))Ce.call(t,n)&&ie(e,n,t[n]);return e};Se({linear:he},{easeInSine:[.12,0,.39,0],easeOutSine:[.61,1,.88,1],easeInOutSine:[.37,0,.63,1],easeInQuad:[.11,0,.5,0],easeOutQuad:[.5,1,.89,1],easeInOutQuad:[.45,0,.55,1],easeInCubic:[.32,0,.67,0],easeOutCubic:[.33,1,.68,1],easeInOutCubic:[.65,0,.35,1],easeInQuart:[.5,0,.75,0],easeOutQuart:[.25,1,.5,1],easeInOutQuart:[.76,0,.24,1],easeInQuint:[.64,0,.78,0],easeOutQuint:[.22,1,.36,1],easeInOutQuint:[.83,0,.17,1],easeInExpo:[.7,0,.84,0],easeOutExpo:[.16,1,.3,1],easeInOutExpo:[.87,0,.13,1],easeInCirc:[.55,0,1,.45],easeOutCirc:[0,.55,.45,1],easeInOutCirc:[.85,0,.15,1],easeInBack:[.36,0,.66,-.56],easeOutBack:[.34,1.56,.64,1],easeInOutBack:[.68,-.6,.32,1.6]});function Ae(e,t){const n={};return Object.keys(e).forEach(a=>{n[a]=(...r)=>{const o=e[a],_=o.type;let m=`/api/${o.url}`;o.hasBody?m=B.replacePlaceholders(m,...r.splice(1)):m=B.replacePlaceholders(m,...r);const T={Accept:"application/json","Content-Type":"application/json",...se(o.headers)},v=Fe(m,{method:_},{immediate:!1,beforeFetch({options:s}){return s.headers={...s.headers,...T},o.hasBody&&r.length&&(s.body=JSON.stringify(r[0])),{options:s}},onFetchError(s){return s!=null&&s.response&&t(s),s}});return{...De(T)==="application/json"?v.json():v.text()}}}),n}function De(e){const t=se(e);return(t==null?void 0:t.accept)??""}function se(e){let t={};return u.isRef(e)?t=e.value:t=e,t}function Ne(e){return JSON.parse(JSON.stringify(e))}function Be(e=500){const t=u.reactive({debouncer:null,timeout:e});function n(l,...a){t.debouncer!==null&&window.clearTimeout(t.debouncer),t.debouncer=window.setTimeout(()=>l(...a),t.timeout)}return{...u.toRefs(t),debounce:n}}h.copy=Ne,h.createEndpoint=de,h.requestTypes=fe,h.useApi=Ae,h.useDebouncer=Be,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
1
+ (function(p,A){typeof exports=="object"&&typeof module<"u"?A(exports,require("@citruslime/utils"),require("vue")):typeof define=="function"&&define.amd?define(["exports","@citruslime/utils","vue"],A):(p=typeof globalThis<"u"?globalThis:p||self,A(p.CitrusLimeVueUtils={},p.CLUtils,p.Vue))})(this,function(p,A,u){"use strict";const fe=["GET","POST","PUT","DELETE"];function de(e,t,n=null,l=null){return{url:e,type:t,headers:n??q,hasBody:pe(t,l)}}function q(){return{accept:"application/json","content-type":"application/json"}}function pe(e,t){return!!(t===null&&(e==="PUT"||e==="POST")||t)}var Z;const J=typeof window<"u";J&&((Z=window==null?void 0:window.navigator)!=null&&Z.userAgent)&&/iP(ad|hone|od)/.test(window.navigator.userAgent);function T(e){return typeof e=="function"?e():u.unref(e)}function ee(e,t=!1,n="Timeout"){return new Promise((l,a)=>{setTimeout(t?()=>a(n):l,e)})}function he(e){return e}function ye(e,...t){return t.some(n=>n in e)}function te(e){return u.getCurrentScope()?(u.onScopeDispose(e),!0):!1}function M(){const e=[],t=a=>{const r=e.indexOf(a);r!==-1&&e.splice(r,1)};return{on:a=>{e.push(a);const r=()=>t(a);return te(r),{off:r}},off:t,trigger:a=>{e.forEach(r=>r(a))}}}function V(e){return typeof e=="function"?u.computed(e):u.ref(e)}function K(e,t=!1){function n(i,{flush:s="sync",deep:O=!1,timeout:h,throwOnTimeout:I}={}){let y=null;const D=[new Promise(S=>{y=u.watch(e,C=>{i(C)!==t&&(y==null||y(),S(C))},{flush:s,deep:O,immediate:!0})})];return h!=null&&D.push(ee(h,I).then(()=>T(e)).finally(()=>y==null?void 0:y())),Promise.race(D)}function l(i,s){if(!u.isRef(i))return n(C=>C===i,s);const{flush:O="sync",deep:h=!1,timeout:I,throwOnTimeout:y}=s??{};let b=null;const S=[new Promise(C=>{b=u.watch([e,i],([E,N])=>{t!==(E===N)&&(b==null||b(),C(E))},{flush:O,deep:h,immediate:!0})})];return I!=null&&S.push(ee(I,y).then(()=>T(e)).finally(()=>(b==null||b(),T(e)))),Promise.race(S)}function a(i){return n(s=>Boolean(s),i)}function r(i){return l(null,i)}function o(i){return l(void 0,i)}function _(i){return n(Number.isNaN,i)}function m(i,s){return n(O=>{const h=Array.from(O);return h.includes(i)||h.includes(T(i))},s)}function g(i){return v(1,i)}function v(i=1,s){let O=-1;return n(()=>(O+=1,O>=i),s)}return Array.isArray(T(e))?{toMatch:n,toContains:m,changed:g,changedTimes:v,get not(){return K(e,!t)}}:{toMatch:n,toBe:l,toBeTruthy:a,toBeNull:r,toBeNaN:_,toBeUndefined:o,changed:g,changedTimes:v,get not(){return K(e,!t)}}}function me(e){return K(e)}function Oe(e,t,n={}){const{immediate:l=!0}=n,a=u.ref(!1);let r=null;function o(){r&&(clearTimeout(r),r=null)}function _(){a.value=!1,o()}function m(...g){o(),a.value=!0,r=setTimeout(()=>{a.value=!1,r=null,e(...g)},T(t))}return l&&(a.value=!0,J&&m()),te(_),{isPending:u.readonly(a),start:m,stop:_}}const we=J?window:void 0,R=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},W="__vueuse_ssr_handlers__";R[W]=R[W]||{},R[W];var ge=Object.defineProperty,be=Object.defineProperties,Pe=Object.getOwnPropertyDescriptors,ne=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,_e=Object.prototype.propertyIsEnumerable,re=(e,t,n)=>t in e?ge(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,c=(e,t)=>{for(var n in t||(t={}))Te.call(t,n)&&re(e,n,t[n]);if(ne)for(var n of ne(t))_e.call(t,n)&&re(e,n,t[n]);return e},U=(e,t)=>be(e,Pe(t));const ve={json:"application/json",text:"text/plain"};function oe(e){return e&&ye(e,"immediate","refetch","initialData","timeout","beforeFetch","afterFetch","onFetchError","fetch")}function k(e){return typeof Headers<"u"&&e instanceof Headers?Object.fromEntries([...e.entries()]):e}function Ee(e,...t){var n;const l=typeof AbortController=="function";let a={},r={immediate:!0,refetch:!1,timeout:0};const o={method:"GET",type:"text",payload:void 0};t.length>0&&(oe(t[0])?r=c(c({},r),t[0]):a=t[0]),t.length>1&&oe(t[1])&&(r=c(c({},r),t[1]));const{fetch:_=(n=we)==null?void 0:n.fetch,initialData:m,timeout:g}=r,v=M(),i=M(),s=M(),O=u.ref(!1),h=u.ref(!1),I=u.ref(!1),y=u.ref(null),b=u.shallowRef(null),D=u.shallowRef(null),S=u.shallowRef(m),C=u.computed(()=>l&&h.value);let E,N;const z=()=>{l&&E&&(E.abort(),E=void 0)},X=f=>{h.value=f,O.value=!f};g&&(N=Oe(z,g,{immediate:!1}));const Q=async(f=!1)=>{var P;X(!0),D.value=null,y.value=null,I.value=!1,l&&(z(),E=new AbortController,E.signal.onabort=()=>I.value=!0,a=U(c({},a),{signal:E.signal}));const w={method:o.method,headers:{}};if(o.payload){const G=k(w.headers);o.payloadType&&(G["Content-Type"]=(P=ve[o.payloadType])!=null?P:o.payloadType);const L=T(o.payload);w.body=o.payloadType==="json"?JSON.stringify(L):L}let H=!1;const F={url:T(e),options:c(c({},w),a),cancel:()=>{H=!0}};if(r.beforeFetch&&Object.assign(F,await r.beforeFetch(F)),H||!_)return X(!1),Promise.resolve(null);let j=null;return N&&N.start(),new Promise((G,L)=>{var le;_(F.url,U(c(c({},w),F.options),{headers:c(c({},k(w.headers)),k((le=F.options)==null?void 0:le.headers))})).then(async d=>{if(b.value=d,y.value=d.status,j=await d[o.type](),r.afterFetch&&y.value>=200&&y.value<300&&({data:j}=await r.afterFetch({data:j,response:d})),S.value=j,!d.ok)throw new Error(d.statusText);return v.trigger(d),G(d)}).catch(async d=>{let ce=d.message||d.name;return r.onFetchError&&({data:j,error:ce}=await r.onFetchError({data:j,error:d,response:b.value})),S.value=j,D.value=ce,i.trigger(d),f?L(d):G(null)}).finally(()=>{X(!1),N&&N.stop(),s.trigger(null)})})},ue=V(r.refetch);u.watch([ue,V(e)],([f])=>f&&Q(),{deep:!0});const x={isFinished:O,statusCode:y,response:b,error:D,data:S,isFetching:h,canAbort:C,aborted:I,abort:z,execute:Q,onFetchResponse:v.on,onFetchError:i.on,onFetchFinally:s.on,get:B("GET"),put:B("PUT"),post:B("POST"),delete:B("DELETE"),patch:B("PATCH"),head:B("HEAD"),options:B("OPTIONS"),json:$("json"),text:$("text"),blob:$("blob"),arrayBuffer:$("arrayBuffer"),formData:$("formData")};function B(f){return(P,w)=>{if(!h.value){o.method=f,o.payload=P,o.payloadType=w,u.isRef(o.payload)&&u.watch([ue,V(o.payload)],([F])=>F&&Q(),{deep:!0});const H=T(o.payload);return!w&&H&&Object.getPrototypeOf(H)===Object.prototype&&!(H instanceof FormData)&&(o.payloadType="json"),U(c({},x),{then(F,j){return Y().then(F,j)}})}}}function Y(){return new Promise((f,P)=>{me(O).toBe(!0).then(()=>f(x)).catch(w=>P(w))})}function $(f){return()=>{if(!h.value)return o.type=f,U(c({},x),{then(P,w){return Y().then(P,w)}})}}return r.immediate&&setTimeout(Q,0),U(c({},x),{then(f,P){return Y().then(f,P)}})}var ae;(function(e){e.UP="UP",e.RIGHT="RIGHT",e.DOWN="DOWN",e.LEFT="LEFT",e.NONE="NONE"})(ae||(ae={}));var Fe=Object.defineProperty,ie=Object.getOwnPropertySymbols,je=Object.prototype.hasOwnProperty,Ie=Object.prototype.propertyIsEnumerable,se=(e,t,n)=>t in e?Fe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Se=(e,t)=>{for(var n in t||(t={}))je.call(t,n)&&se(e,n,t[n]);if(ie)for(var n of ie(t))Ie.call(t,n)&&se(e,n,t[n]);return e};Se({linear:he},{easeInSine:[.12,0,.39,0],easeOutSine:[.61,1,.88,1],easeInOutSine:[.37,0,.63,1],easeInQuad:[.11,0,.5,0],easeOutQuad:[.5,1,.89,1],easeInOutQuad:[.45,0,.55,1],easeInCubic:[.32,0,.67,0],easeOutCubic:[.33,1,.68,1],easeInOutCubic:[.65,0,.35,1],easeInQuart:[.5,0,.75,0],easeOutQuart:[.25,1,.5,1],easeInOutQuart:[.76,0,.24,1],easeInQuint:[.64,0,.78,0],easeOutQuint:[.22,1,.36,1],easeInOutQuint:[.83,0,.17,1],easeInExpo:[.7,0,.84,0],easeOutExpo:[.16,1,.3,1],easeInOutExpo:[.87,0,.13,1],easeInCirc:[.55,0,1,.45],easeOutCirc:[0,.55,.45,1],easeInOutCirc:[.85,0,.15,1],easeInBack:[.36,0,.66,-.56],easeOutBack:[.34,1.56,.64,1],easeInOutBack:[.68,-.6,.32,1.6]});function Ce(e,t){const n={};return Object.keys(e).forEach(a=>{n[a]=(...r)=>{const o=e[a],_=o.type;let m=`/api/${o.url}`;o.hasBody?m=A.replacePlaceholders(m,...r.splice(1)):m=A.replacePlaceholders(m,...r);const g={...q(),...o.headers()},v=Ee(m,{method:_},{immediate:!1,beforeFetch({options:s}){return s.headers={...s.headers,...g},o.hasBody&&r.length&&(s.body=JSON.stringify(r[0])),{options:s}},onFetchError(s){return s!=null&&s.response&&t(s),s}});return{...De(g)==="application/json"?v.json():v.text()}}}),n}function De(e){return e.accept??"*/*"}function Ne(e){return JSON.parse(JSON.stringify(e))}function Be(e=500){const t=u.reactive({debouncer:null,timeout:e});function n(l,...a){t.debouncer!==null&&window.clearTimeout(t.debouncer),t.debouncer=window.setTimeout(()=>l(...a),t.timeout)}return{...u.toRefs(t),debounce:n}}p.copy=Ne,p.createEndpoint=de,p.getDefaultHeaders=q,p.requestTypes=fe,p.useApi=Ce,p.useDebouncer=Be,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
@@ -1,9 +1,8 @@
1
- import type { Ref } from 'vue';
2
1
  export declare const requestTypes: readonly ["GET", "POST", "PUT", "DELETE"];
3
2
  export interface Endpoint {
4
3
  url: string;
5
4
  type: typeof requestTypes[number];
6
- headers: HeadersInit | Ref<HeadersInit>;
5
+ headers: () => HeadersInit;
7
6
  hasBody: boolean;
8
7
  }
9
8
  /**
@@ -15,4 +14,10 @@ export interface Endpoint {
15
14
  * @param hasBody Determines whether or not an endpoint should have a body.
16
15
  * @returns The created Endpoint object.
17
16
  */
18
- export declare function createEndpoint(url: string, type: typeof requestTypes[number], headers?: HeadersInit | Ref<HeadersInit> | null, hasBody?: boolean | null): Endpoint;
17
+ export declare function createEndpoint(url: string, type: typeof requestTypes[number], headers?: (() => HeadersInit) | null, hasBody?: boolean | null): Endpoint;
18
+ /**
19
+ * Gets a set of default headers.
20
+ *
21
+ * @returns A set of default headers.
22
+ */
23
+ export declare function getDefaultHeaders(): HeadersInit;
@@ -1,5 +1,5 @@
1
1
  import { useFetch } from '@vueuse/core';
2
- import type { Endpoint } from './endpoints';
2
+ import { type Endpoint } from './endpoints';
3
3
  export type ApiRequest<TResponseModel> = ReturnType<typeof useFetch<TResponseModel>>;
4
4
  /**
5
5
  * Composable that constructs an API object containing requests defined by the endpoints object.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@citruslime/vue-utils",
3
- "version": "1.2.0",
3
+ "version": "1.2.1-beta.0",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Citrus-Lime Ltd",
@@ -30,7 +30,7 @@
30
30
  "build": "vite build && copyfiles -u 2 \"dist/src/**/*\" dist && del-cli dist/src"
31
31
  },
32
32
  "dependencies": {
33
- "@citruslime/utils": "^1.1.1"
33
+ "@citruslime/utils": "^1.1.1-beta.0"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@vueuse/core": "^9.13.0",