@haxtheweb/haxcms-nodejs
Version:
HAXcms single and multisite nodejs server, api, and administration
1 lines • 2.54 kB
JavaScript
export function toArray(e){return e=e||[],Array.isArray(e)?e:[e]}export function log(e){return`[Vaadin.Router] ${e}`}export function logValue(e){if("object"!=typeof e)return String(e);const t=Object.prototype.toString.call(e).match(/ (.*)\]$/)[1];return"Object"===t||"Array"===t?`${t} ${JSON.stringify(e)}`:t}const e="module",t="nomodule",r=[e,t];function ensureBundle(e){if(!e.match(/.+\.[m]?js$/))throw new Error(log(`Unsupported type for bundle "${e}": .js or .mjs expected.`))}export function ensureRoute(o){if(!o||!isString(o.path))throw new Error(log('Expected route config to be an object with a "path" string property, or an array of such objects'));const n=o.bundle,i=["component","redirect","bundle"];if(!(isFunction(o.action)||Array.isArray(o.children)||isFunction(o.children)||isObject(n)||i.some((e=>isString(o[e])))))throw new Error(log(`Expected route config "${o.path}" to include either "${i.join('", "')}" or "action" function but none found.`));if(n)if(isString(n))ensureBundle(n);else{if(!r.some((e=>e in n)))throw new Error(log('Expected route bundle to include either "'+t+'" or "'+e+'" keys, or both'));r.forEach((e=>e in n&&ensureBundle(n[e])))}o.redirect&&["bundle","component"].forEach((e=>{e in o&&console.warn(log(`Route config "${o.path}" has both "redirect" and "${e}" properties, and "redirect" will always override the latter. Did you mean to only use "${e}"?`))}))}export function ensureRoutes(e){toArray(e).forEach((e=>ensureRoute(e)))}function loadScript(r,o){let n=document.head.querySelector('script[src="'+r+'"][async]');return n||(n=document.createElement("script"),n.setAttribute("src",r),o===e?n.setAttribute("type",e):o===t&&n.setAttribute(t,""),n.async=!0),new Promise(((e,t)=>{n.onreadystatechange=n.onload=t=>{n.__dynamicImportLoaded=!0,e(t)},n.onerror=e=>{n.parentNode&&n.parentNode.removeChild(n),t(e)},null===n.parentNode?document.head.appendChild(n):n.__dynamicImportLoaded&&e()}))}export function loadBundle(e){return isString(e)?loadScript(e):Promise.race(r.filter((t=>t in e)).map((t=>loadScript(e[t],t))))}export function fireRouterEvent(e,t){return!window.dispatchEvent(new CustomEvent(`vaadin-router-${e}`,{cancelable:"go"===e,detail:t}))}export function isObject(e){return"object"==typeof e&&!!e}export function isFunction(e){return"function"==typeof e}export function isString(e){return"string"==typeof e}export function getNotFoundError(e){const t=new Error(log(`Page not found (${e.pathname})`));return t.context=e,t.code=404,t}export const notFoundResult=new class NotFoundResult{};