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