@citruslime/vue-utils 1.4.5 → 2.0.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.
- package/README.md +3 -3
- package/dist/citrus-lime-vue-utils.es.js +162 -160
- package/dist/citrus-lime-vue-utils.umd.js +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Citrus-Lime Vue Utils
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A package containing various utilities for Vue-based projects. Used by [Citrus-Lime UI](https://www.npmjs.com/package/@citruslime/ui).
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|

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