vue-bag-admin
Version:
Unified entry package for the Vue Bag Admin runtime ecosystem.
902 lines (901 loc) • 25.4 kB
JavaScript
import V from "axios";
import { useWebSocket as me } from "@vueuse/core";
import ve from "lodash-es/debounce";
import be from "lodash-es/throttle";
import { shallowRef as U, computed as Se } from "vue";
const Y = 300 * 1e3, X = "bag-request", Ae = "bag-request:pwa-cache", O = V.create({
timeout: 1e4
});
let Z = () => null, ee = () => {
}, te = () => {
}, re = () => {
}, ae = () => {
}, ne = (e) => e.data, se = () => null;
class v extends Error {
constructor(t, a) {
super(t), this.name = "HttpError", Object.assign(this, a);
}
}
let q = {
enabled: !1,
cache: {
enabled: !1,
namespace: X,
strategy: "cache-first",
ttl: Y
}
};
const W = () => typeof window < "u", Pe = (e) => ["GET", "HEAD"].includes(e.toUpperCase()), Re = (e) => e.startsWith("/") ? e : `/${e}`, qe = (e, t) => !e || /^https?:\/\//i.test(t) ? t : `${e.replace(/\/+$/, "")}${Re(t)}`, Ce = (e, t) => {
if (!t)
return e;
const a = new URLSearchParams();
Object.entries(t).forEach(([n, s]) => {
if (s != null) {
if (Array.isArray(s)) {
s.forEach((c) => {
a.append(n, String(c));
});
return;
}
a.append(n, String(s));
}
});
const r = a.toString();
return r ? `${e}${e.includes("?") ? "&" : "?"}${r}` : e;
}, ge = (e) => {
if (!W())
return null;
try {
return window.localStorage.getItem(e);
} catch {
return null;
}
}, Te = (e, t) => {
if (!W())
return !1;
try {
return window.localStorage.setItem(e, t), !0;
} catch {
return !1;
}
}, M = (e) => {
if (W())
try {
window.localStorage.removeItem(e);
} catch {
}
}, k = (e, t) => `${Ae}:${e}:${encodeURIComponent(t)}`, ce = (e, t, a = {}) => {
const r = ge(k(e, t));
if (!r)
return null;
try {
const n = JSON.parse(r);
return !a.allowStale && n.expiresAt < Date.now() ? (M(k(e, t)), null) : n;
} catch {
return M(k(e, t)), null;
}
}, _e = (e, t, a, r) => {
try {
const n = JSON.stringify(a), s = {
payload: JSON.parse(n),
createdAt: Date.now(),
expiresAt: Date.now() + r
};
Te(k(e, t), JSON.stringify(s));
} catch {
}
}, xe = (e) => {
var a, r, n, s;
const t = ((a = e == null ? void 0 : e.cache) == null ? void 0 : a.enabled) ?? !!(e != null && e.enabled);
return {
enabled: (e == null ? void 0 : e.enabled) ?? t,
cache: {
enabled: t,
namespace: ((r = e == null ? void 0 : e.cache) == null ? void 0 : r.namespace) ?? X,
strategy: ((n = e == null ? void 0 : e.cache) == null ? void 0 : n.strategy) ?? "cache-first",
ttl: ((s = e == null ? void 0 : e.cache) == null ? void 0 : s.ttl) ?? Y
}
};
}, j = (e) => {
const t = e.baseURL ?? O.defaults.baseURL ?? "", a = e.url ?? "", r = qe(t, a), n = e.params;
return Ce(r, n);
}, D = (e) => {
const t = (e.method ?? "GET").toUpperCase(), a = e.pwa;
if (!q.enabled || a === !1)
return {
enabled: !1,
strategy: "network-only",
ttl: q.cache.ttl,
cacheEnabled: !1,
cacheNamespace: q.cache.namespace,
cacheKey: `${t}:${j(e)}`
};
const r = a ?? {}, n = r.enabled ?? !0, s = j(e);
return {
enabled: n,
strategy: r.strategy ?? q.cache.strategy,
ttl: r.ttl ?? q.cache.ttl,
cacheEnabled: n && q.cache.enabled && Pe(t),
cacheNamespace: q.cache.namespace,
cacheKey: r.cacheKey ?? `${t}:${s}`
};
}, Oe = (e) => new v(`No cached response available for ${e}.`, {
code: "PWA_CACHE_MISS",
status: 0
}), Ue = (e) => async (t) => ({
data: e,
status: 200,
statusText: "OK",
headers: {
"x-bag-cache": "HIT"
},
config: t,
request: void 0
}), $e = (e) => {
const t = {
...e,
adapter: void 0,
__bagBypassPwaCache: !0,
pwa: e.pwa ? {
...e.pwa,
strategy: "network-first"
} : {
strategy: "network-first"
}
};
O.request(t).catch(() => {
});
}, mt = (e) => {
const {
getToken: t,
pwa: a,
onUnauthorized: r,
onForbidden: n,
onServerError: s,
onBusinessError: c,
extractData: w,
resolveError: o,
...l
} = e;
Z = t, ee = r, te = n ?? (() => {
}), re = s ?? (() => {
}), ae = c ?? (() => {
}), ne = w ?? ((i) => i.data), se = o ?? (() => null), q = xe(a), Object.assign(O.defaults, l);
}, vt = (e) => {
const {
getToken: t,
onUnauthorized: a,
onForbidden: r = () => {
},
onServerError: n = () => {
},
onBusinessError: s = () => {
},
extractData: c,
resolveError: w,
...o
} = e, l = V.create({
timeout: 1e4,
...o
}), i = c ?? ((h) => h.data), f = w ?? (() => null);
return l.interceptors.request.use((h) => {
const S = t();
return S && (h.headers = h.headers ?? {}, h.headers.Authorization = `Bearer ${S}`), h;
}), l.interceptors.response.use(
((h) => {
const S = i(h), d = f(S, h);
if (d) {
const P = new v(d.message, {
code: d.code,
status: h.status,
payload: S,
response: h
});
return s(P), Promise.reject(P);
}
return S;
}),
(h) => {
if (h instanceof v)
return Promise.reject(h);
const S = h, d = S.response, P = new v((d == null ? void 0 : d.statusText) || S.message || "Request failed", {
status: d == null ? void 0 : d.status,
payload: d == null ? void 0 : d.data,
response: d
});
return (d == null ? void 0 : d.status) === 401 ? a() : (d == null ? void 0 : d.status) === 403 ? r(P) : ((d == null ? void 0 : d.status) ?? 0) >= 500 && n(P), Promise.reject(P);
}
), l;
};
O.interceptors.request.use((e) => {
const t = e, a = Z();
if (a && (t.headers = t.headers ?? {}, t.headers.Authorization = `Bearer ${a}`), t.__bagBypassPwaCache)
return t;
const r = D(t);
if (!r.enabled || !r.cacheEnabled || r.strategy === "network-only")
return t;
const n = r.strategy === "stale-while-revalidate" || r.strategy === "cache-only", s = ce(r.cacheNamespace, r.cacheKey, {
allowStale: n
});
return s ? (r.strategy === "stale-while-revalidate" && $e(t), t.adapter = Ue(s.payload), t) : r.strategy === "cache-only" ? Promise.reject(Oe(j(t))) : t;
});
O.interceptors.response.use(
((e) => {
const t = ne(e), a = se(t, e);
if (a) {
const n = new v(a.message, {
code: a.code,
status: e.status,
payload: t,
response: e
});
return ae(n), Promise.reject(n);
}
const r = D(e.config);
return r.enabled && r.cacheEnabled && _e(r.cacheNamespace, r.cacheKey, t, r.ttl), t;
}),
(e) => {
if (e instanceof v)
return Promise.reject(e);
const t = e, a = t.config;
if (!t.response && a && !a.__bagBypassPwaCache) {
const s = D(a);
if (s.enabled && s.cacheEnabled) {
const c = ce(s.cacheNamespace, s.cacheKey, {
allowStale: !0
});
if (c)
return Promise.resolve(c.payload);
}
}
const r = t.response, n = new v((r == null ? void 0 : r.statusText) || t.message || "Request failed", {
status: r == null ? void 0 : r.status,
payload: r == null ? void 0 : r.data,
response: r
});
return (r == null ? void 0 : r.status) === 401 ? ee() : (r == null ? void 0 : r.status) === 403 ? te(n) : ((r == null ? void 0 : r.status) ?? 0) >= 500 && re(n), Promise.reject(n);
}
);
const Ne = 300 * 1e3, ke = "bag-request", Be = "bag-request:pwa-queue", Le = "bag-request:pwa-cache";
let $ = null;
const He = async (e, t = "auto") => {
var n;
if (t === "raw")
return e;
if (e.status === 204 || e.status === 205)
return null;
if (t === "text")
return e.text();
if (t === "blob")
return e.blob();
if (t === "arrayBuffer")
return e.arrayBuffer();
if (t === "formData")
return e.formData();
const a = await e.text();
if (!a)
return null;
if (t === "json")
return JSON.parse(a);
const r = ((n = e.headers.get("content-type")) == null ? void 0 : n.toLowerCase()) ?? "";
return r.includes("application/json") ? JSON.parse(a) : (r.startsWith("text/") || r.includes("xml") || r.includes("html") || r.includes("x-www-form-urlencoded"), a);
}, F = () => typeof window < "u", oe = (e) => Object.prototype.toString.call(e) === "[object Object]", Fe = (e) => e.startsWith("/") ? e : `/${e}`, je = (e, t) => !e || /^https?:\/\//i.test(t) ? t : `${e.replace(/\/+$/, "")}${Fe(t)}`, De = (e, t) => {
if (!t)
return e;
const a = new URLSearchParams();
Object.entries(t).forEach(([n, s]) => {
if (s != null) {
if (Array.isArray(s)) {
s.forEach((c) => {
a.append(n, String(c));
});
return;
}
a.append(n, String(s));
}
});
const r = a.toString();
return r ? `${e}${e.includes("?") ? "&" : "?"}${r}` : e;
}, We = (e) => {
const t = e ?? globalThis.fetch;
if (!t)
throw new Error(
"Fetch API is not available in the current runtime. Please provide a custom fetch implementation."
);
return t;
}, ze = (e, t) => {
if (e != null)
return typeof e == "string" || e instanceof FormData || e instanceof URLSearchParams || e instanceof Blob || e instanceof ArrayBuffer || ArrayBuffer.isView(e) ? e : oe(e) || Array.isArray(e) ? (t.has("Content-Type") || t.set("Content-Type", "application/json"), JSON.stringify(e)) : e;
}, Ie = (e, t) => {
const a = typeof t == "object" && t && "message" in t && typeof t.message == "string" ? t.message : null;
return new v(a ?? e.statusText ?? "Request failed", {
status: e.status,
payload: t,
response: e
});
}, Ke = (e, t, a) => {
var r, n;
e.status === 401 ? a.onUnauthorized() : e.status === 403 ? (r = a.onForbidden) == null || r.call(a, t) : e.status >= 500 && ((n = a.onServerError) == null || n.call(a, t));
}, Je = (e) => {
var r, n, s, c, w, o, l;
const t = ((r = e == null ? void 0 : e.cache) == null ? void 0 : r.enabled) ?? !!(e != null && e.enabled), a = ((n = e == null ? void 0 : e.queue) == null ? void 0 : n.enabled) ?? !!(e != null && e.enabled);
return {
enabled: (e == null ? void 0 : e.enabled) ?? (t || a),
cache: {
enabled: t,
namespace: ((s = e == null ? void 0 : e.cache) == null ? void 0 : s.namespace) ?? ke,
strategy: ((c = e == null ? void 0 : e.cache) == null ? void 0 : c.strategy) ?? "cache-first",
ttl: ((w = e == null ? void 0 : e.cache) == null ? void 0 : w.ttl) ?? Ne
},
queue: {
enabled: a,
storageKey: ((o = e == null ? void 0 : e.queue) == null ? void 0 : o.storageKey) ?? Be,
autoReplay: ((l = e == null ? void 0 : e.queue) == null ? void 0 : l.autoReplay) ?? !0
}
};
}, z = (e) => ({
...e,
baseURL: e.baseURL ?? "",
fetch: We(e.fetch),
pwa: Je(e.pwa)
}), Me = (e) => ({
cache: e.cache,
credentials: e.credentials,
integrity: e.integrity,
keepalive: e.keepalive,
mode: e.mode,
redirect: e.redirect,
referrer: e.referrer,
referrerPolicy: e.referrerPolicy,
window: e.window
}), le = (e) => {
if (!F())
return null;
try {
return window.localStorage.getItem(e);
} catch {
return null;
}
}, ue = (e, t) => {
if (!F())
return !1;
try {
return window.localStorage.setItem(e, t), !0;
} catch {
return !1;
}
}, L = (e) => {
if (F())
try {
window.localStorage.removeItem(e);
} catch {
}
}, B = (e, t) => `${Le}:${e}:${encodeURIComponent(t)}`, Qe = (e, t, a = {}) => {
const r = le(B(e, t));
if (!r)
return null;
try {
const n = JSON.parse(r);
return !a.allowStale && n.expiresAt < Date.now() ? (L(B(e, t)), null) : n;
} catch {
return L(B(e, t)), null;
}
}, Ge = (e, t, a, r) => {
if (!(a instanceof Response || a instanceof Blob || a instanceof FormData || a instanceof ArrayBuffer))
try {
const n = JSON.stringify(a), s = {
payload: JSON.parse(n),
createdAt: Date.now(),
expiresAt: Date.now() + r
};
ue(B(e, t), JSON.stringify(s));
} catch {
}
}, I = (e) => {
const t = le(e);
if (!t)
return [];
try {
const a = JSON.parse(t);
return Array.isArray(a) ? a : [];
} catch {
return L(e), [];
}
}, K = (e, t) => {
if (!t.length) {
L(e);
return;
}
ue(e, JSON.stringify(t));
}, Ve = (e) => {
const t = {};
return e.forEach((a, r) => {
r.toLowerCase() !== "authorization" && (t[r] = a);
}), t;
}, Ye = (e) => {
if (e == null)
return {
type: "null",
value: null
};
if (typeof e == "string")
return {
type: "text",
value: e
};
if (e instanceof URLSearchParams)
return {
type: "searchParams",
value: e.toString()
};
if (oe(e) || Array.isArray(e))
try {
return {
type: "json",
value: JSON.stringify(e)
};
} catch {
return null;
}
return null;
}, Xe = (e) => {
if (e) {
if (e.type === "null")
return null;
if (e.type === "text")
return e.value ?? "";
if (e.type === "searchParams")
return new URLSearchParams(e.value ?? "");
if (e.type === "json")
return e.value ? JSON.parse(e.value) : null;
}
}, Q = (e) => ["GET", "HEAD"].includes(e.toUpperCase()), x = () => typeof navigator > "u" ? "online" : navigator.onLine === !1 ? "offline" : "online", Ze = (e) => {
if (!F())
return () => {
};
const t = () => {
e(x());
};
return window.addEventListener("online", t), window.addEventListener("offline", t), () => {
window.removeEventListener("online", t), window.removeEventListener("offline", t);
};
}, et = (e, t) => new v("Request queued for replay when the network recovers.", {
code: "PWA_OFFLINE_QUEUED",
status: 0,
payload: {
queued: !0,
path: e,
method: t
}
}), tt = (e) => new v(`No cached response available for ${e}.`, {
code: "PWA_CACHE_MISS",
status: 0
}), rt = (e, t) => {
const a = Ye(t.originalBody);
if (t.originalBody != null && !a)
throw new v("Current request body cannot be persisted for offline replay.", {
code: "PWA_QUEUE_UNSUPPORTED",
status: 0
});
const r = I(e.pwa.queue.storageKey);
r.push({
path: t.path,
method: t.method,
headers: Ve(t.headers),
params: t.params,
body: a ?? void 0,
skipAuth: t.skipAuth,
responseType: t.responseType,
queuedAt: Date.now()
}), K(e.pwa.queue.storageKey, r);
}, N = (e) => e instanceof v ? e : e instanceof Error ? new v(e.message || "Network request failed", {
code: x() === "offline" ? "PWA_OFFLINE" : "FETCH_ERROR",
status: 0,
payload: e
}) : new v("Network request failed", {
code: x() === "offline" ? "PWA_OFFLINE" : "FETCH_ERROR",
status: 0,
payload: e
}), at = (e, t, a, r) => {
if (!e.pwa.enabled || r === !1)
return {
enabled: !1,
strategy: "network-only",
ttl: e.pwa.cache.ttl,
cacheEnabled: !1,
cacheNamespace: e.pwa.cache.namespace,
cacheKey: `${t}:${a}`,
queueWhenOffline: !1
};
const n = r ?? {}, s = n.enabled ?? !0, c = n.strategy ?? e.pwa.cache.strategy, w = s && e.pwa.cache.enabled && Q(t);
return {
enabled: s,
strategy: c,
ttl: n.ttl ?? e.pwa.cache.ttl,
cacheEnabled: w,
cacheNamespace: e.pwa.cache.namespace,
cacheKey: n.cacheKey ?? `${t}:${a}`,
queueWhenOffline: s && e.pwa.queue.enabled && !Q(t) ? n.queueWhenOffline ?? !0 : !1
};
}, T = async (e, t, a) => {
var w, o;
const r = await e.fetch(t.url, {
...Me(e),
...t.requestInit,
method: t.method,
headers: t.headers
}), n = await (t.parseBody ?? e.parseBody ?? ((l) => He(l, t.responseType)))(r), s = (t.extractData ?? e.extractData ?? ((l) => l))(
n,
r
);
if (!r.ok) {
const l = Ie(r, s);
throw Ke(r, l, e), l;
}
const c = (w = t.resolveError ?? e.resolveError) == null ? void 0 : w(s, r);
if (c) {
const l = new v(c.message, {
code: c.code,
status: r.status,
payload: s,
response: r
});
throw (o = e.onBusinessError) == null || o.call(e, l), l;
}
return a.enabled && a.cacheEnabled && Ge(a.cacheNamespace, a.cacheKey, s, a.ttl), s;
}, _ = (e, t = {}) => {
if (!e.enabled || !e.cacheEnabled)
return null;
const a = Qe(e.cacheNamespace, e.cacheKey, t);
return a ? a.payload : null;
}, nt = () => {
$ && ($(), $ = null), !(!b.pwa.enabled || !b.pwa.queue.enabled || !b.pwa.queue.autoReplay) && ($ = Ze((e) => {
e === "online" && ct();
}));
}, ie = (e) => {
const t = async (a, r = {}) => {
const n = e(), {
body: s,
headers: c,
method: w = "GET",
params: o,
responseType: l = "auto",
skipAuth: i = !1,
pwa: f,
parseBody: h,
extractData: S,
resolveError: d,
...P
} = r, m = w.toUpperCase(), y = new Headers(n.headers);
new Headers(c).forEach((E, R) => {
y.set(R, E);
});
const C = n.getToken();
C && !i && !y.has("Authorization") && y.set("Authorization", `Bearer ${C}`);
const A = ze(s, y), g = De(je(n.baseURL, a), o), p = {
headers: y,
method: m,
path: a,
url: g,
requestInit: {
...P,
body: A
},
responseType: l,
parseBody: h,
extractData: S,
resolveError: d,
params: o,
skipAuth: i,
originalBody: s
}, u = at(n, m, g, f), J = x() === "offline";
if (u.enabled && u.queueWhenOffline && J)
throw rt(n, p), et(a, m);
if (!u.enabled || u.strategy === "network-only")
try {
return await T(n, p, u);
} catch (E) {
throw N(E);
}
if (u.strategy === "cache-only") {
const E = _(u, {
allowStale: !0
});
if (E !== null)
return E;
throw tt(a);
}
if (u.strategy === "cache-first") {
const E = _(u);
if (E !== null)
return E;
try {
return await T(n, p, u);
} catch (R) {
throw N(R);
}
}
if (u.strategy === "stale-while-revalidate") {
const E = _(u, {
allowStale: !0
});
if (E !== null)
return T(n, p, u).catch(() => {
}), E;
try {
return await T(n, p, u);
} catch (R) {
throw N(R);
}
}
if (J) {
const E = _(u, {
allowStale: !0
});
if (E !== null)
return E;
}
try {
return await T(n, p, u);
} catch (E) {
const R = _(u, {
allowStale: !0
});
if (R !== null)
return R;
throw N(E);
}
};
return {
request: t,
get: (a, r) => t(a, { ...r, method: "GET" }),
delete: (a, r) => t(a, { ...r, method: "DELETE" }),
post: (a, r, n) => t(a, { ...n, method: "POST", body: r }),
put: (a, r, n) => t(a, { ...n, method: "PUT", body: r }),
patch: (a, r, n) => t(a, { ...n, method: "PATCH", body: r })
};
};
let b = z({
getToken: () => null,
onUnauthorized: () => {
},
onForbidden: () => {
},
onServerError: () => {
},
onBusinessError: () => {
}
});
const bt = (e) => {
var t, a;
b = z({
...b,
...e,
headers: e.headers ?? b.headers,
pwa: {
...b.pwa,
...e.pwa,
cache: {
...b.pwa.cache,
...(t = e.pwa) == null ? void 0 : t.cache
},
queue: {
...b.pwa.queue,
...(a = e.pwa) == null ? void 0 : a.queue
}
}
}), nt();
}, St = (e) => {
const t = z(e);
return ie(() => t);
}, st = () => I(b.pwa.queue.storageKey).length, At = () => {
K(b.pwa.queue.storageKey, []);
}, ct = async (e = ot) => {
if (x() === "offline")
return {
replayed: 0,
failed: 0,
remaining: st()
};
const t = I(b.pwa.queue.storageKey);
if (!t.length)
return {
replayed: 0,
failed: 0,
remaining: 0
};
const a = [];
let r = 0, n = 0;
for (const s of t)
try {
await e.request(s.path, {
method: s.method,
headers: s.headers,
params: s.params,
body: Xe(s.body),
responseType: s.responseType,
skipAuth: s.skipAuth,
pwa: {
queueWhenOffline: !1
}
}), r += 1;
} catch {
n += 1, a.push(s);
}
return K(b.pwa.queue.storageKey, a), {
replayed: r,
failed: n,
remaining: a.length
};
}, ot = ie(() => b);
let de = () => null, he = "ws://localhost:8080";
const Pt = (e) => {
de = e.getToken, e.baseURL && (he = e.baseURL);
}, Rt = (e) => {
const t = de(), a = `${he}${e}?token=${t || ""}`;
return me(a, {
autoReconnect: {
retries: 3,
delay: 1e3
},
heartbeat: {
message: "ping",
interval: 5e3,
pongTimeout: 2e3
}
});
};
class lt extends Error {
constructor(t = "Request execution canceled.") {
super(t), this.name = "RequestCanceledError";
}
}
const fe = "Request execution canceled.", ut = "Request is already running.", it = "Cannot refresh before the first run.", dt = "Request state reset.", ht = "Request superseded by a newer run.", G = (e) => new lt(e ?? fe), qt = (e, t = {}) => {
const a = t.strategy ?? "latest", r = U(
t.defaultData
), n = U(), s = U(), c = U(0), w = Se(() => c.value > 0), o = /* @__PURE__ */ new Map();
let l = 0;
const i = (m) => {
const y = o.get(m);
if (y)
return y.counted && (c.value = Math.max(0, c.value - 1), y.counted = !1), o.delete(m), y;
}, f = (m, y = fe) => {
m.forEach((C) => {
const A = o.get(C);
A && (A.canceled = !0, A.reason = y, A.counted && (c.value = Math.max(0, c.value - 1), A.counted = !1));
});
}, h = async (...m) => {
var C, A, g;
if (a === "block" && c.value > 0)
throw new Error(ut);
a === "latest" && o.size > 0 && f(Array.from(o.keys()), ht), n.value = void 0, s.value = m;
const y = ++l;
o.set(y, {
canceled: !1,
counted: !0,
params: m
}), c.value += 1;
try {
const p = await Promise.resolve(e(...m)), u = o.get(y);
if (!u || u.canceled)
throw G(u == null ? void 0 : u.reason);
return r.value = p, (C = t.onSuccess) == null || C.call(t, p, m), p;
} catch (p) {
const u = o.get(y);
throw u != null && u.canceled ? G(u.reason) : (n.value = p, (A = t.onError) == null || A.call(t, p, m), p);
} finally {
const p = i(y);
p && ((g = t.onFinally) == null || g.call(t, p.params));
}
};
return {
loading: w,
data: r,
error: n,
run: h,
refresh: () => {
if (!s.value)
throw new Error(it);
return h(...s.value);
},
cancel: (m) => {
f(Array.from(o.keys()), m);
},
reset: () => {
f(Array.from(o.keys()), dt), r.value = t.defaultData, n.value = void 0, s.value = void 0;
}
};
}, we = () => {
let e, t;
return {
promise: new Promise((r, n) => {
e = r, t = n;
}),
resolve: e,
reject: t
};
}, H = (e) => new Error(e ?? "Request execution canceled."), ye = (e, t) => {
const a = {
leading: (e == null ? void 0 : e.leading) ?? t.leading,
trailing: (e == null ? void 0 : e.trailing) ?? t.trailing,
maxWait: e == null ? void 0 : e.maxWait
};
if (!a.leading && !a.trailing)
throw new Error("At least one of leading or trailing must be enabled.");
return a;
}, pe = (e, t, a, r) => {
const n = Promise.resolve(e.apply(t, a));
return r && n.then(r.resolve, r.reject), n;
}, Ee = (e) => ({
leading: e.leading,
trailing: e.trailing,
maxWait: e.maxWait
}), Ct = (e, t, a) => {
const r = ye(a, {
leading: !1,
trailing: !0
});
let n = null, s = null, c = !1;
const w = (i, f) => {
const h = n;
return n = null, c = !1, s = pe(e, i, f, h ?? void 0), s;
}, o = ve(
(i, f) => {
w(i, f);
},
t,
Ee(r)
), l = function(...i) {
if (!r.trailing)
return c = !0, o(this, i), s ?? Promise.reject(H("Request skipped by debounce control."));
const f = n ?? we();
return n = f, c = !0, o(this, i), f.promise;
};
return l.cancel = (i) => {
o.cancel(), c = !1, n && (n.reject(H(i)), n = null);
}, l.flush = () => c ? (o.flush(), (n == null ? void 0 : n.promise) ?? s ?? Promise.resolve(void 0)) : Promise.resolve(void 0), l.pending = () => c, l;
}, gt = (e, t, a) => {
const r = ye(a, {
leading: !0,
trailing: !0
});
let n = null, s = null, c = !1;
const w = (i, f) => {
const h = n;
return n = null, c = !1, s = pe(e, i, f, h ?? void 0), s;
}, o = be(
(i, f) => {
w(i, f);
},
t,
Ee(r)
), l = function(...i) {
if (!r.trailing)
return c = !0, o(this, i), s ?? Promise.reject(H("Request skipped by throttle control."));
const f = n ?? we();
return n = f, c = !0, o(this, i), f.promise;
};
return l.cancel = (i) => {
o.cancel(), c = !1, n && (n.reject(H(i)), n = null);
}, l.flush = () => c ? (o.flush(), (n == null ? void 0 : n.promise) ?? s ?? Promise.resolve(void 0)) : Promise.resolve(void 0), l.pending = () => c, l;
};
export {
v as HttpError,
lt as RequestCanceledError,
At as clearOfflineQueue,
St as createFetchClient,
vt as createHttpClient,
Rt as createWsClient,
Ct as debounceRequest,
ot as fetchHttp,
x as getNetworkStatus,
st as getOfflineQueueSize,
O as http,
ct as replayOfflineQueue,
bt as setupFetch,
mt as setupHttp,
Pt as setupWs,
Ze as subscribeNetworkStatus,
gt as throttleRequest,
qt as useRequest
};