@westacks/vortex
Version:
Server-based routing for SPAs
219 lines (218 loc) • 7.28 kB
JavaScript
import { p as x, s as E, a as y, i as g, b as m } from "./page-DkBioJVC.js";
import S, { getAdapter as v } from "axios";
const q = ({ request: s }) => {
const e = /* @__PURE__ */ new Map(), t = s.use(async (o) => {
const a = await P(o);
e.forEach((i, c) => {
R(i) && e.delete(c);
});
const u = v(o.adapter);
return o.adapter = (i) => i.prefetch ? r(i, a, u) : n(i, a, u), o;
});
async function r(o, a, u) {
const i = e.get(a), c = async (h) => {
const d = await h;
return d.config.prefetch = !0, d;
};
if (i)
return c(i.response);
const f = c(u(o));
return e.set(a, { ...T(o.prefetch), response: f }), f;
}
async function n(o, a, u) {
const i = e.get(a), c = Date.now(), f = async (d) => {
const b = await d;
return b.config.prefetch = !1, b;
};
if (!i)
return f(u(o));
const h = c - i.timestamp;
return i.ttl && h < i.ttl ? f(i.response) : (e.delete(a), i.stale && h < i.stale ? (l.request(o), f(i.response)) : f(u(o)));
}
return () => {
s.eject(t), e.clear();
};
};
async function P(s) {
const e = new URL(s.url, s.baseURL || window.location.origin), t = JSON.stringify({
url: e.origin + e.pathname + e.search,
method: (s.method || "get").toLowerCase()
}), r = await crypto.subtle.digest("sha-1", new TextEncoder().encode(t));
return Array.from(new Uint8Array(r)).map((n) => n.toString(16).padStart(2, "0")).join("");
}
function R(s) {
const e = Date.now() - s.timestamp, t = !!s.stale && e < s.stale;
return e > s.ttl && !t;
}
function T(s) {
let e, t;
Array.isArray(s) ? (e = p(s[0]), t = p(s[1])) : s === !0 ? e = p("30s") : typeof s == "string" ? e = p(s) : e = 0;
const r = Date.now();
return { ttl: e, stale: t, timestamp: r };
}
function p(s) {
if (typeof s != "string")
return s;
const e = s.match(/^(\d+)([a-zA-Z]+)$/);
if (!e)
throw new Error(`Invalid duration: ${s}`);
const [, t, r] = e, n = {
ms: 1,
s: 1e3,
m: 1e3 * 60,
h: 1e3 * 60 * 60,
d: 1e3 * 60 * 60 * 24,
w: 1e3 * 60 * 60 * 24 * 7
}[r] || new Error(`Invalid duration unit: ${r}`);
if (n instanceof Error)
throw n;
return Number(t) * n;
}
let l;
function A(...s) {
if (!l) throw new Error("Router not initialized!");
const e = s.reduce((t, r) => {
const n = r(l.interceptors);
return typeof n == "function" && t.push(n), t;
}, []);
return () => {
e.forEach((t) => t());
};
}
function z() {
l = S.create(), A(q), l.reload = (s) => l.request({ url: x.get()?.url, ...s }), l.defaults.withCredentials = !0, l.defaults.vortex = !0;
}
function B() {
l = void 0;
}
const _ = /* @__PURE__ */ new Map();
function $(s, e = "default") {
s = e && _.get(e) || s;
const t = E(void 0, g);
return t.subscribe((r) => _.set(e, y(r))), t.set(F(s, t.set)), t;
}
function F(s, e) {
const t = {
set(n, o, a, u) {
const i = n[o] !== a, c = Reflect.set(n, o, a, u);
return c && e(r, i), c;
}
}, r = new Proxy(m(s, t), t);
return r;
}
class D {
_defaults;
_transform = void 0;
_recentlySuccessfulTimeout = null;
_handler;
wasSuccessful = !1;
recentlySuccessful = !1;
processing = !1;
errors = {};
get hasErrors() {
return Object.keys(this.errors).length > 0;
}
get isDirty() {
return !g(this.data(), this._defaults);
}
constructor(e, t) {
const r = Object.keys(e).filter((n) => n in this);
if (r.length > 0)
throw new Error(`Form data intersects with reserved properties: ${r.join(", ")}`);
this._defaults = e, this._handler = t, Object.defineProperty(this, "_defaults", { enumerable: !1 }), Object.defineProperty(this, "_transform", { enumerable: !1 }), Object.defineProperty(this, "_recentlySuccessfulTimeout", { enumerable: !1 }), Object.defineProperty(this, "_handler", { enumerable: !1, writable: !1, configurable: !1 }), this.reset();
}
data() {
return Object.keys(this._defaults).reduce((t, r) => (t[r] = y(this[r]), t), {});
}
transform(e) {
return this._transform = e(this.data()), this;
}
reset(...e) {
const t = e.length > 0 ? Object.keys(this._defaults).filter((r) => e.includes(r)).reduce((r, n) => (r[n] = this._defaults[n], r), {}) : this._defaults;
return Object.assign(this, m(t, this._handler)), this;
}
fill(e) {
return Object.assign(this, m(e, this._handler)), this;
}
defaults(e, t) {
return typeof e == "string" && (e = { [e]: t }), e || (e = y(Object.keys(this._defaults).reduce((r, n) => (r[n] = this[n], r), {}))), this._defaults = { ...this._defaults, ...e }, this;
}
clearErrors(...e) {
if (e.length === 0)
this.errors = {};
else for (const t of e)
delete this.errors[t];
return this;
}
setError(e, t) {
return typeof e == "string" && (e = { [e]: t }), this.errors = { ...this.errors, ...e }, this;
}
request(e) {
this._recentlySuccessfulTimeout && clearTimeout(this._recentlySuccessfulTimeout), this.wasSuccessful = !1, this.processing = !0;
let t = this._transform || this.data();
return O(t) && (t = w(t)), l.request({ ...e, data: t }).catch((r) => Promise.reject(r)).then((r) => (this.errors = j.resolveErrors(r), Object.keys(this.errors).length === 0 && (this.wasSuccessful = !0, this.recentlySuccessful = !0, this._recentlySuccessfulTimeout = setTimeout(() => {
this.recentlySuccessful = !1;
}, 2e3)), r)).finally(() => {
this._transform = void 0, this.processing = !1;
});
}
get(e, t) {
return this.request({ ...t, url: e, method: "get" });
}
post(e, t) {
return this.request({ ...t, url: e, method: "post" });
}
put(e, t) {
return this.request({ ...t, url: e, method: "put" });
}
patch(e, t) {
return this.request({ ...t, url: e, method: "patch" });
}
delete(e, t) {
return this.request({ ...t, url: e, method: "delete" });
}
}
const j = function(s, e) {
s = typeof s == "function" ? s() : s;
const { get: t, set: r, subscribe: n } = E(void 0, g), o = e ? $({ data: s, errors: {} }, e).get() : void 0;
return r(U(s, r, o)), o && n((a) => Object.assign(o, { data: a.data(), errors: a.errors })), { get: t, set: r, subscribe: n };
};
j.resolveErrors = (s) => ({});
function U(s, e, t) {
const r = {
set(a, u, i, c) {
const f = a[u] !== i, h = Reflect.set(a, u, i, c);
return h && !u.startsWith("_") && (e(o, f), t && (t.data = o.data(), t.errors = o.errors)), h;
}
}, n = new D(s, r), o = new Proxy(n, r);
if (t) {
const { data: a, errors: u } = y(t);
o.fill(a), o.errors = u;
}
return o;
}
function O(s) {
return typeof s != "object" || s === null ? !1 : Object.values(s).some(
(e) => e instanceof File || e instanceof Blob ? !0 : O(e)
);
}
function w(s, e = new FormData(), t = "") {
for (const r in s) {
if (!Object.hasOwn(s, r)) continue;
const n = s[r], o = t ? `${t}[${r}]` : r;
n instanceof File || n instanceof Blob ? e.append(o, n) : Array.isArray(n) ? n.forEach((a, u) => {
const i = `${o}[${u}]`;
typeof a == "object" && a !== null ? w(a, e, i) : e.append(i, a);
}) : typeof n == "object" && n !== null ? w(n, e, o) : n != null && e.append(o, n);
}
return e;
}
export {
l as a,
$ as b,
z as c,
B as d,
_ as e,
A as i,
j as u
};