UNPKG

@westacks/vortex

Version:

Server-based routing for SPAs

2 lines (1 loc) 5.89 kB
"use strict";const c=require("./page-CvrPUcPo.cjs"),x=require("axios"),j=({request:r})=>{const e=new Map,t=r.use(async o=>{const a=await O(o);e.forEach((i,l)=>{q(i)&&e.delete(l)});const u=x.getAdapter(o.adapter);return o.adapter=i=>i.prefetch?s(i,a,u):n(i,a,u),o});async function s(o,a,u){const i=e.get(a),l=async h=>{const d=await h;return d.config.prefetch=!0,d};if(i)return l(i.response);const f=l(u(o));return e.set(a,{...v(o.prefetch),response:f}),f}async function n(o,a,u){const i=e.get(a),l=Date.now(),f=async d=>{const g=await d;return g.config.prefetch=!1,g};if(!i)return f(u(o));const h=l-i.timestamp;return i.ttl&&h<i.ttl?f(i.response):(e.delete(a),i.stale&&h<i.stale?(exports.axios.request(o),f(i.response)):f(u(o)))}return()=>{r.eject(t),e.clear()}};async function O(r){const e=new URL(r.url,r.baseURL||window.location.origin),t=JSON.stringify({url:e.origin+e.pathname+e.search,method:(r.method||"get").toLowerCase()}),s=await crypto.subtle.digest("sha-1",new TextEncoder().encode(t));return Array.from(new Uint8Array(s)).map(n=>n.toString(16).padStart(2,"0")).join("")}function q(r){const e=Date.now()-r.timestamp,t=!!r.stale&&e<r.stale;return e>r.ttl&&!t}function v(r){let e,t;Array.isArray(r)?(e=p(r[0]),t=p(r[1])):r===!0?e=p("30s"):typeof r=="string"?e=p(r):e=0;const s=Date.now();return{ttl:e,stale:t,timestamp:s}}function p(r){if(typeof r!="string")return r;const e=r.match(/^(\d+)([a-zA-Z]+)$/);if(!e)throw new Error(`Invalid duration: ${r}`);const[,t,s]=e,n={ms:1,s:1e3,m:1e3*60,h:1e3*60*60,d:1e3*60*60*24,w:1e3*60*60*24*7}[s]||new Error(`Invalid duration unit: ${s}`);if(n instanceof Error)throw n;return Number(t)*n}exports.axios=void 0;function _(...r){if(!exports.axios)throw new Error("Router not initialized!");const e=r.reduce((t,s)=>{const n=s(exports.axios.interceptors);return typeof n=="function"&&t.push(n),t},[]);return()=>{e.forEach(t=>t())}}function S(){exports.axios=x.create(),_(j),exports.axios.reload=r=>exports.axios.request({url:c.page.get()?.url,...r}),exports.axios.defaults.withCredentials=!0,exports.axios.defaults.vortex=!0}function R(){exports.axios=void 0}const y=new Map;function b(r,e="default"){r=e&&y.get(e)||r;const t=c.signal(void 0,c.isEqual);return t.subscribe(s=>y.set(e,c.proxyUnwrap(s))),t.set(P(r,t.set)),t}function P(r,e){const t={set(n,o,a,u){const i=n[o]!==a,l=Reflect.set(n,o,a,u);return l&&e(s,i),l}},s=new Proxy(c.proxyWrap(r,t),t);return s}class T{_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!c.isEqual(this.data(),this._defaults)}constructor(e,t){const s=Object.keys(e).filter(n=>n in this);if(s.length>0)throw new Error(`Form data intersects with reserved properties: ${s.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,s)=>(t[s]=c.proxyUnwrap(this[s]),t),{})}transform(e){return this._transform=e(this.data()),this}reset(...e){const t=e.length>0?Object.keys(this._defaults).filter(s=>e.includes(s)).reduce((s,n)=>(s[n]=this._defaults[n],s),{}):this._defaults;return Object.assign(this,c.proxyWrap(t,this._handler)),this}fill(e){return Object.assign(this,c.proxyWrap(e,this._handler)),this}defaults(e,t){return typeof e=="string"&&(e={[e]:t}),e||(e=c.proxyUnwrap(Object.keys(this._defaults).reduce((s,n)=>(s[n]=this[n],s),{}))),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 E(t)&&(t=w(t)),exports.axios.request({...e,data:t}).catch(s=>Promise.reject(s)).then(s=>(this.errors=m.resolveErrors(s),Object.keys(this.errors).length===0&&(this.wasSuccessful=!0,this.recentlySuccessful=!0,this._recentlySuccessfulTimeout=setTimeout(()=>{this.recentlySuccessful=!1},2e3)),s)).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 m=function(r,e){r=typeof r=="function"?r():r;const{get:t,set:s,subscribe:n}=c.signal(void 0,c.isEqual),o=e?b({data:r,errors:{}},e).get():void 0;return s(A(r,s,o)),o&&n(a=>Object.assign(o,{data:a.data(),errors:a.errors})),{get:t,set:s,subscribe:n}};m.resolveErrors=r=>({});function A(r,e,t){const s={set(a,u,i,l){const f=a[u]!==i,h=Reflect.set(a,u,i,l);return h&&!u.startsWith("_")&&(e(o,f),t&&(t.data=o.data(),t.errors=o.errors)),h}},n=new T(r,s),o=new Proxy(n,s);if(t){const{data:a,errors:u}=c.proxyUnwrap(t);o.fill(a),o.errors=u}return o}function E(r){return typeof r!="object"||r===null?!1:Object.values(r).some(e=>e instanceof File||e instanceof Blob?!0:E(e))}function w(r,e=new FormData,t=""){for(const s in r){if(!Object.hasOwn(r,s))continue;const n=r[s],o=t?`${t}[${s}]`:s;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}exports.cache=y;exports.createRouter=S;exports.destroyRouter=R;exports.install=_;exports.useForm=m;exports.useRemember=b;