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