@citruslime/vue-utils 2.0.4-beta.4 → 3.0.0-beta.0

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