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