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