@citruslime/vue-utils 1.1.1 → 1.2.0-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,21 +1,431 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import "@citruslime/utils";
|
|
2
|
+
import { computed as ce, ref as B, readonly as pe, unref as ye, getCurrentScope as he, onScopeDispose as me, watch as G, isRef as X, shallowRef as M, reactive as Oe, toRefs as ve } from "vue";
|
|
3
|
+
const Je = [
|
|
4
|
+
"GET",
|
|
5
|
+
"POST",
|
|
6
|
+
"PUT",
|
|
7
|
+
"DELETE"
|
|
8
|
+
];
|
|
9
|
+
function Ke(e, t, n = null, s = null) {
|
|
10
|
+
return {
|
|
11
|
+
url: e,
|
|
12
|
+
type: t,
|
|
13
|
+
headers: n ?? ge(),
|
|
14
|
+
hasBody: we(t, s)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function ge() {
|
|
18
|
+
return {
|
|
19
|
+
Accept: "application/json",
|
|
20
|
+
"Content-Type": "application/json"
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function we(e, t) {
|
|
24
|
+
return !!(t === null && (e === "PUT" || e === "POST") || t);
|
|
25
|
+
}
|
|
26
|
+
var ne;
|
|
27
|
+
const Y = typeof window < "u";
|
|
28
|
+
Y && ((ne = window == null ? void 0 : window.navigator) != null && ne.userAgent) && /iP(ad|hone|od)/.test(window.navigator.userAgent);
|
|
29
|
+
function E(e) {
|
|
30
|
+
return typeof e == "function" ? e() : ye(e);
|
|
31
|
+
}
|
|
32
|
+
function re(e, t = !1, n = "Timeout") {
|
|
33
|
+
return new Promise((s, a) => {
|
|
34
|
+
setTimeout(t ? () => a(n) : s, e);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function be(e) {
|
|
38
|
+
return e;
|
|
39
|
+
}
|
|
40
|
+
function Pe(e, ...t) {
|
|
41
|
+
return t.some((n) => n in e);
|
|
42
|
+
}
|
|
43
|
+
function fe(e) {
|
|
44
|
+
return he() ? (me(e), !0) : !1;
|
|
45
|
+
}
|
|
46
|
+
function W() {
|
|
47
|
+
const e = [], t = (a) => {
|
|
48
|
+
const r = e.indexOf(a);
|
|
49
|
+
r !== -1 && e.splice(r, 1);
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
on: (a) => {
|
|
53
|
+
e.push(a);
|
|
54
|
+
const r = () => t(a);
|
|
55
|
+
return fe(r), {
|
|
56
|
+
off: r
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
off: t,
|
|
60
|
+
trigger: (a) => {
|
|
61
|
+
e.forEach((r) => r(a));
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function V(e) {
|
|
66
|
+
return typeof e == "function" ? ce(e) : B(e);
|
|
67
|
+
}
|
|
68
|
+
function q(e, t = !1) {
|
|
69
|
+
function n(i, { flush: u = "sync", deep: h = !1, timeout: d, throwOnTimeout: F } = {}) {
|
|
70
|
+
let p = null;
|
|
71
|
+
const C = [new Promise((j) => {
|
|
72
|
+
p = G(e, (I) => {
|
|
73
|
+
i(I) !== t && (p == null || p(), j(I));
|
|
74
|
+
}, {
|
|
75
|
+
flush: u,
|
|
76
|
+
deep: h,
|
|
77
|
+
immediate: !0
|
|
78
|
+
});
|
|
79
|
+
})];
|
|
80
|
+
return d != null && C.push(re(d, F).then(() => E(e)).finally(() => p == null ? void 0 : p())), Promise.race(C);
|
|
81
|
+
}
|
|
82
|
+
function s(i, u) {
|
|
83
|
+
if (!X(i))
|
|
84
|
+
return n((I) => I === i, u);
|
|
85
|
+
const { flush: h = "sync", deep: d = !1, timeout: F, throwOnTimeout: p } = u ?? {};
|
|
86
|
+
let v = null;
|
|
87
|
+
const j = [new Promise((I) => {
|
|
88
|
+
v = G([e, i], ([P, N]) => {
|
|
89
|
+
t !== (P === N) && (v == null || v(), I(P));
|
|
90
|
+
}, {
|
|
91
|
+
flush: h,
|
|
92
|
+
deep: d,
|
|
93
|
+
immediate: !0
|
|
94
|
+
});
|
|
95
|
+
})];
|
|
96
|
+
return F != null && j.push(re(F, p).then(() => E(e)).finally(() => (v == null || v(), E(e)))), Promise.race(j);
|
|
97
|
+
}
|
|
98
|
+
function a(i) {
|
|
99
|
+
return n((u) => Boolean(u), i);
|
|
100
|
+
}
|
|
101
|
+
function r(i) {
|
|
102
|
+
return s(null, i);
|
|
103
|
+
}
|
|
104
|
+
function o(i) {
|
|
105
|
+
return s(void 0, i);
|
|
106
|
+
}
|
|
107
|
+
function w(i) {
|
|
108
|
+
return n(Number.isNaN, i);
|
|
109
|
+
}
|
|
110
|
+
function y(i, u) {
|
|
111
|
+
return n((h) => {
|
|
112
|
+
const d = Array.from(h);
|
|
113
|
+
return d.includes(i) || d.includes(E(i));
|
|
114
|
+
}, u);
|
|
115
|
+
}
|
|
116
|
+
function O(i) {
|
|
117
|
+
return b(1, i);
|
|
118
|
+
}
|
|
119
|
+
function b(i = 1, u) {
|
|
120
|
+
let h = -1;
|
|
121
|
+
return n(() => (h += 1, h >= i), u);
|
|
122
|
+
}
|
|
123
|
+
return Array.isArray(E(e)) ? {
|
|
124
|
+
toMatch: n,
|
|
125
|
+
toContains: y,
|
|
126
|
+
changed: O,
|
|
127
|
+
changedTimes: b,
|
|
128
|
+
get not() {
|
|
129
|
+
return q(e, !t);
|
|
130
|
+
}
|
|
131
|
+
} : {
|
|
132
|
+
toMatch: n,
|
|
133
|
+
toBe: s,
|
|
134
|
+
toBeTruthy: a,
|
|
135
|
+
toBeNull: r,
|
|
136
|
+
toBeNaN: w,
|
|
137
|
+
toBeUndefined: o,
|
|
138
|
+
changed: O,
|
|
139
|
+
changedTimes: b,
|
|
140
|
+
get not() {
|
|
141
|
+
return q(e, !t);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function Te(e) {
|
|
146
|
+
return q(e);
|
|
147
|
+
}
|
|
148
|
+
function _e(e, t, n = {}) {
|
|
149
|
+
const {
|
|
150
|
+
immediate: s = !0
|
|
151
|
+
} = n, a = B(!1);
|
|
152
|
+
let r = null;
|
|
153
|
+
function o() {
|
|
154
|
+
r && (clearTimeout(r), r = null);
|
|
155
|
+
}
|
|
156
|
+
function w() {
|
|
157
|
+
a.value = !1, o();
|
|
158
|
+
}
|
|
159
|
+
function y(...O) {
|
|
160
|
+
o(), a.value = !0, r = setTimeout(() => {
|
|
161
|
+
a.value = !1, r = null, e(...O);
|
|
162
|
+
}, E(t));
|
|
163
|
+
}
|
|
164
|
+
return s && (a.value = !0, Y && y()), fe(w), {
|
|
165
|
+
isPending: pe(a),
|
|
166
|
+
start: y,
|
|
167
|
+
stop: w
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const Ee = 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 Fe = Object.defineProperty, je = Object.defineProperties, Ie = Object.getOwnPropertyDescriptors, oe = Object.getOwnPropertySymbols, Ce = Object.prototype.hasOwnProperty, Ne = 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
|
+
for (var n in t || (t = {}))
|
|
175
|
+
Ce.call(t, n) && ae(e, n, t[n]);
|
|
176
|
+
if (oe)
|
|
177
|
+
for (var n of oe(t))
|
|
178
|
+
Ne.call(t, n) && ae(e, n, t[n]);
|
|
179
|
+
return e;
|
|
180
|
+
}, H = (e, t) => je(e, Ie(t));
|
|
181
|
+
const Se = {
|
|
182
|
+
json: "application/json",
|
|
183
|
+
text: "text/plain"
|
|
184
|
+
};
|
|
185
|
+
function ie(e) {
|
|
186
|
+
return e && Pe(e, "immediate", "refetch", "initialData", "timeout", "beforeFetch", "afterFetch", "onFetchError", "fetch");
|
|
187
|
+
}
|
|
188
|
+
function k(e) {
|
|
189
|
+
return typeof Headers < "u" && e instanceof Headers ? Object.fromEntries([...e.entries()]) : e;
|
|
190
|
+
}
|
|
191
|
+
function Ae(e, ...t) {
|
|
192
|
+
var n;
|
|
193
|
+
const s = typeof AbortController == "function";
|
|
194
|
+
let a = {}, r = { immediate: !0, refetch: !1, timeout: 0 };
|
|
195
|
+
const o = {
|
|
196
|
+
method: "GET",
|
|
197
|
+
type: "text",
|
|
198
|
+
payload: void 0
|
|
199
|
+
};
|
|
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
|
+
const {
|
|
202
|
+
fetch: w = (n = Ee) == null ? void 0 : n.fetch,
|
|
203
|
+
initialData: y,
|
|
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 = ce(() => s && d.value);
|
|
206
|
+
let P, N;
|
|
207
|
+
const J = () => {
|
|
208
|
+
s && P && (P.abort(), P = void 0);
|
|
209
|
+
}, K = (l) => {
|
|
210
|
+
d.value = l, h.value = !l;
|
|
211
|
+
};
|
|
212
|
+
O && (N = _e(J, O, { immediate: !1 }));
|
|
213
|
+
const $ = async (l = !1) => {
|
|
214
|
+
var g;
|
|
215
|
+
K(!0), C.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
|
+
}));
|
|
218
|
+
const m = {
|
|
219
|
+
method: o.method,
|
|
220
|
+
headers: {}
|
|
221
|
+
};
|
|
222
|
+
if (o.payload) {
|
|
223
|
+
const Q = k(m.headers);
|
|
224
|
+
o.payloadType && (Q["Content-Type"] = (g = Se[o.payloadType]) != null ? g : o.payloadType);
|
|
225
|
+
const U = E(o.payload);
|
|
226
|
+
m.body = o.payloadType === "json" ? JSON.stringify(U) : U;
|
|
227
|
+
}
|
|
228
|
+
let A = !1;
|
|
229
|
+
const T = {
|
|
230
|
+
url: E(e),
|
|
231
|
+
options: f(f({}, m), a),
|
|
232
|
+
cancel: () => {
|
|
233
|
+
A = !0;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
if (r.beforeFetch && Object.assign(T, await r.beforeFetch(T)), A || !w)
|
|
237
|
+
return K(!1), Promise.resolve(null);
|
|
238
|
+
let _ = null;
|
|
239
|
+
return N && N.start(), new Promise((Q, U) => {
|
|
240
|
+
var ee;
|
|
241
|
+
w(T.url, H(f(f({}, m), T.options), {
|
|
242
|
+
headers: f(f({}, k(m.headers)), k((ee = T.options) == null ? void 0 : ee.headers))
|
|
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)
|
|
245
|
+
throw new Error(c.statusText);
|
|
246
|
+
return b.trigger(c), Q(c);
|
|
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);
|
|
250
|
+
}).finally(() => {
|
|
251
|
+
K(!1), N && N.stop(), u.trigger(null);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
}, Z = V(r.refetch);
|
|
255
|
+
G([
|
|
256
|
+
Z,
|
|
257
|
+
V(e)
|
|
258
|
+
], ([l]) => l && $(), { deep: !0 });
|
|
259
|
+
const x = {
|
|
260
|
+
isFinished: h,
|
|
261
|
+
statusCode: p,
|
|
262
|
+
response: v,
|
|
263
|
+
error: C,
|
|
264
|
+
data: j,
|
|
265
|
+
isFetching: d,
|
|
266
|
+
canAbort: I,
|
|
267
|
+
aborted: F,
|
|
268
|
+
abort: J,
|
|
269
|
+
execute: $,
|
|
270
|
+
onFetchResponse: b.on,
|
|
271
|
+
onFetchError: i.on,
|
|
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")
|
|
285
|
+
};
|
|
286
|
+
function S(l) {
|
|
287
|
+
return (g, m) => {
|
|
288
|
+
if (!d.value) {
|
|
289
|
+
o.method = l, o.payload = g, o.payloadType = m, X(o.payload) && G([
|
|
290
|
+
Z,
|
|
291
|
+
V(o.payload)
|
|
292
|
+
], ([T]) => T && $(), { 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(T, _) {
|
|
296
|
+
return L().then(T, _);
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function L() {
|
|
303
|
+
return new Promise((l, g) => {
|
|
304
|
+
Te(h).toBe(!0).then(() => l(x)).catch((m) => g(m));
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
function D(l) {
|
|
308
|
+
return () => {
|
|
309
|
+
if (!d.value)
|
|
310
|
+
return o.type = l, H(f({}, x), {
|
|
311
|
+
then(g, m) {
|
|
312
|
+
return L().then(g, m);
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
return r.immediate && setTimeout($, 0), H(f({}, x), {
|
|
318
|
+
then(l, g) {
|
|
319
|
+
return L().then(l, g);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
var ue;
|
|
324
|
+
(function(e) {
|
|
325
|
+
e.UP = "UP", e.RIGHT = "RIGHT", e.DOWN = "DOWN", e.LEFT = "LEFT", e.NONE = "NONE";
|
|
326
|
+
})(ue || (ue = {}));
|
|
327
|
+
var Be = Object.defineProperty, se = Object.getOwnPropertySymbols, De = Object.prototype.hasOwnProperty, He = Object.prototype.propertyIsEnumerable, le = (e, t, n) => t in e ? Be(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, $e = (e, t) => {
|
|
328
|
+
for (var n in t || (t = {}))
|
|
329
|
+
De.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
|
+
return e;
|
|
334
|
+
};
|
|
335
|
+
const xe = {
|
|
336
|
+
easeInSine: [0.12, 0, 0.39, 0],
|
|
337
|
+
easeOutSine: [0.61, 1, 0.88, 1],
|
|
338
|
+
easeInOutSine: [0.37, 0, 0.63, 1],
|
|
339
|
+
easeInQuad: [0.11, 0, 0.5, 0],
|
|
340
|
+
easeOutQuad: [0.5, 1, 0.89, 1],
|
|
341
|
+
easeInOutQuad: [0.45, 0, 0.55, 1],
|
|
342
|
+
easeInCubic: [0.32, 0, 0.67, 0],
|
|
343
|
+
easeOutCubic: [0.33, 1, 0.68, 1],
|
|
344
|
+
easeInOutCubic: [0.65, 0, 0.35, 1],
|
|
345
|
+
easeInQuart: [0.5, 0, 0.75, 0],
|
|
346
|
+
easeOutQuart: [0.25, 1, 0.5, 1],
|
|
347
|
+
easeInOutQuart: [0.76, 0, 0.24, 1],
|
|
348
|
+
easeInQuint: [0.64, 0, 0.78, 0],
|
|
349
|
+
easeOutQuint: [0.22, 1, 0.36, 1],
|
|
350
|
+
easeInOutQuint: [0.83, 0, 0.17, 1],
|
|
351
|
+
easeInExpo: [0.7, 0, 0.84, 0],
|
|
352
|
+
easeOutExpo: [0.16, 1, 0.3, 1],
|
|
353
|
+
easeInOutExpo: [0.87, 0, 0.13, 1],
|
|
354
|
+
easeInCirc: [0.55, 0, 1, 0.45],
|
|
355
|
+
easeOutCirc: [0, 0.55, 0.45, 1],
|
|
356
|
+
easeInOutCirc: [0.85, 0, 0.15, 1],
|
|
357
|
+
easeInBack: [0.36, 0, 0.66, -0.56],
|
|
358
|
+
easeOutBack: [0.34, 1.56, 0.64, 1],
|
|
359
|
+
easeInOutBack: [0.68, -0.6, 0.32, 1.6]
|
|
360
|
+
};
|
|
361
|
+
$e({
|
|
362
|
+
linear: be
|
|
363
|
+
}, xe);
|
|
364
|
+
function Le(e, t) {
|
|
365
|
+
const n = {};
|
|
366
|
+
return Object.keys(e).forEach((a) => {
|
|
367
|
+
n[a] = (...r) => {
|
|
368
|
+
const o = e[a], w = o.type;
|
|
369
|
+
let y = `/api/${o.url}`;
|
|
370
|
+
o.hasBody ? y = y.replacePlaceholders(...r.splice(1)) : y = y.replacePlaceholders(...r);
|
|
371
|
+
const O = {
|
|
372
|
+
Accept: "application/json",
|
|
373
|
+
"Content-Type": "application/json",
|
|
374
|
+
...de(o.headers)
|
|
375
|
+
}, b = Ae(
|
|
376
|
+
y,
|
|
377
|
+
{
|
|
378
|
+
method: w
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
immediate: !1,
|
|
382
|
+
beforeFetch({ options: u }) {
|
|
383
|
+
return u.headers = {
|
|
384
|
+
...u.headers,
|
|
385
|
+
...O
|
|
386
|
+
}, o.hasBody && r.length && (u.body = JSON.stringify(r[0])), {
|
|
387
|
+
options: u
|
|
388
|
+
};
|
|
389
|
+
},
|
|
390
|
+
onFetchError(u) {
|
|
391
|
+
return u != null && u.response && t(u), u;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
return {
|
|
396
|
+
...Qe(O) === "application/json" ? b.json() : b.text()
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
}), n;
|
|
400
|
+
}
|
|
401
|
+
function Qe(e) {
|
|
402
|
+
const t = de(e);
|
|
403
|
+
return (t == null ? void 0 : t.accept) ?? "";
|
|
404
|
+
}
|
|
405
|
+
function de(e) {
|
|
406
|
+
let t = {};
|
|
407
|
+
return X(e) ? t = e.value : t = e, t;
|
|
408
|
+
}
|
|
409
|
+
function Me(e) {
|
|
410
|
+
return JSON.parse(JSON.stringify(e));
|
|
411
|
+
}
|
|
412
|
+
function We(e = 500) {
|
|
413
|
+
const t = Oe({
|
|
4
414
|
debouncer: null,
|
|
5
|
-
timeout:
|
|
415
|
+
timeout: e
|
|
6
416
|
});
|
|
7
|
-
function
|
|
8
|
-
|
|
417
|
+
function n(s, ...a) {
|
|
418
|
+
t.debouncer !== null && window.clearTimeout(t.debouncer), t.debouncer = window.setTimeout(() => s(...a), t.timeout);
|
|
9
419
|
}
|
|
10
420
|
return {
|
|
11
|
-
...
|
|
12
|
-
debounce:
|
|
421
|
+
...ve(t),
|
|
422
|
+
debounce: n
|
|
13
423
|
};
|
|
14
424
|
}
|
|
15
|
-
function b(n) {
|
|
16
|
-
return JSON.parse(JSON.stringify(n));
|
|
17
|
-
}
|
|
18
425
|
export {
|
|
19
|
-
|
|
20
|
-
|
|
426
|
+
Me as copy,
|
|
427
|
+
Ke as createEndpoint,
|
|
428
|
+
Je as requestTypes,
|
|
429
|
+
Le as useApi,
|
|
430
|
+
We as useDebouncer
|
|
21
431
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(h,$){typeof exports=="object"&&typeof module<"u"?$(exports,require("@citruslime/utils"),require("vue")):typeof define=="function"&&define.amd?define(["exports","@citruslime/utils","vue"],$):(h=typeof globalThis<"u"?globalThis:h||self,$(h.CitrusLimeVueUtils={},h.CLUtils,h.Vue))})(this,function(h,$,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},H=(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=H(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 B=!1;const F={url:P(e),options:c(c({},w),a),cancel:()=>{B=!0}};if(r.beforeFetch&&Object.assign(F,await r.beforeFetch(F)),B||!_)return z(!1),Promise.resolve(null);let j=null;return D&&D.start(),new Promise((G,L)=>{var le;_(F.url,H(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:U("json"),text:U("text"),blob:U("blob"),arrayBuffer:U("arrayBuffer"),formData:U("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 B=P(o.payload);return!w&&B&&Object.getPrototypeOf(B)===Object.prototype&&!(B instanceof FormData)&&(o.payloadType="json"),H(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 U(f){return()=>{if(!p.value)return o.type=f,H(c({},x),{then(b,w){return X().then(b,w)}})}}return r.immediate&&setTimeout(Q,0),H(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=m.replacePlaceholders(...r.splice(1)):m=m.replacePlaceholders(...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"})});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Ref } from 'vue';
|
|
2
|
+
export declare const requestTypes: readonly ["GET", "POST", "PUT", "DELETE"];
|
|
3
|
+
export interface Endpoint {
|
|
4
|
+
url: string;
|
|
5
|
+
type: typeof requestTypes[number];
|
|
6
|
+
headers: HeadersInit | Ref<HeadersInit>;
|
|
7
|
+
hasBody: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates an endpoint object, using the provided values.
|
|
11
|
+
*
|
|
12
|
+
* @param url The url fragment for the request.
|
|
13
|
+
* @param type The type of HTTP request to make.
|
|
14
|
+
* @param headers The headers this request will use.
|
|
15
|
+
* @param hasBody Determines whether or not an endpoint should have a body.
|
|
16
|
+
* @returns The created Endpoint object.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createEndpoint(url: string, type: typeof requestTypes[number], headers?: HeadersInit | Ref<HeadersInit> | null, hasBody?: boolean | null): Endpoint;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useFetch } from '@vueuse/core';
|
|
2
|
+
import type { Endpoint } from './endpoints';
|
|
3
|
+
export type ApiRequest<TResponseModel> = ReturnType<typeof useFetch<TResponseModel>>;
|
|
4
|
+
/**
|
|
5
|
+
* Composable that constructs an API object containing requests defined by the endpoints object.
|
|
6
|
+
*
|
|
7
|
+
* @param endpoints The endpoints provided to build with the factory.
|
|
8
|
+
* @param handleFetchErrorResponse The error handler for the api factory.
|
|
9
|
+
* @returns An API object containing request methods.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useApi<TEndpoint extends Record<keyof TEndpoint, Endpoint>>(endpoints: TEndpoint, handleFetchErrorResponse: (context: {
|
|
12
|
+
data: any;
|
|
13
|
+
response: Response | null;
|
|
14
|
+
error: any;
|
|
15
|
+
}) => void): Record<keyof TEndpoint, <TResponseModel>(...values: unknown[]) => ApiRequest<TResponseModel>>;
|
package/dist/utils/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@citruslime/vue-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-beta.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Citrus-Lime Ltd",
|
|
@@ -29,7 +29,11 @@
|
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "vite build && copyfiles -u 2 \"dist/src/**/*\" dist && del-cli dist/src"
|
|
31
31
|
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@citruslime/utils": "~0.0.0"
|
|
34
|
+
},
|
|
32
35
|
"peerDependencies": {
|
|
36
|
+
"@vueuse/core": "^9.13.0",
|
|
33
37
|
"vue": "^3.2.47"
|
|
34
38
|
}
|
|
35
39
|
}
|