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