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