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