UNPKG

next-roots

Version:

Utility to handle internationalized routing for Next.js app folder.

3 lines (2 loc) 2.76 kB
"use strict";var l=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var S=(t,e)=>{for(var r in e)l(t,r,{get:e[r],enumerable:!0})},H=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of y(e))!A.call(t,i)&&i!==r&&l(t,i,{get:()=>e[i],enumerable:!(o=P(e,i))||o.enumerable});return t};var b=t=>H(l({},"__esModule",{value:!0}),t);var v={};S(v,{Router:()=>p,compileHref:()=>c,formatHref:()=>h,schema:()=>m});module.exports=b(v);var L=require("path");var a=require("path-to-regexp");function f({defaultLocale:t,locales:e}){return r=>{let o=r.startsWith("/")?r.slice(1):r,[i]=o.split("/");return e.includes(i)?i:t}}function x(t){return!t.includes("[")}function g(t){return t.includes("...")}function E(t){return t.includes("[")&&!g(t)}function F(t){return x(t)?1:E(t)?2:g(t)?3:0}function M(t){let e=t.name.split("/").filter(o=>o.length>0),r="0.";for(let o of e){let i=F(o);r+=i}return parseFloat(r)}function R(t){let e={},r=t.routes[t.defaultLocale]||[];for(let o of r)e[o.name]=M(o);return e}function d(t){let e=R(t);return Object.keys(t.routes).forEach(r=>{t.routes[r]=t.routes[r].sort((o,i)=>{let u=e[o.name],s=e[i.name];return u-s})}),t}var n=class t{static LOCALE="";static PAGE_HREF="/";static PARAMS;static getLocale(){return t.LOCALE}static setLocale(e){t.LOCALE=e}static async getPageHref(){return t.PARAMS?c(t.PAGE_HREF,await t.PARAMS):t.PAGE_HREF}static setPageHref(e){t.PAGE_HREF=e}static setParams(e){t.PARAMS=e}};var p=class extends n{schema;constructor(e){super(),this.schema=d(e)}getHref(e,r={}){let{locale:o=n.getLocale(),...i}=r;Object.keys(i).forEach(s=>{(i[s]===""||i[s]===null)&&delete i[s]});let u=this.findRouteByLocaleAndName(Array.isArray(o)?o.join("_"):o,e);return h(c(u?.href||"",i))}getLocaleFromHref(e){return f({locales:this.schema.locales,defaultLocale:this.schema.defaultLocale})(e)}getRouteFromHref(e){let r=this.getLocaleFromHref(e);return this.findRouteByLocaleAndHref(r,e)}getLocalizedRoutes(e){return this.schema.routes[e]||[]}findRouteByLocaleAndName(e,r){return this.getLocalizedRoutes(e).find(o=>o.name===r)}findRouteByLocaleAndHref(e,r){return this.getLocalizedRoutes(e).find(o=>(0,a.match)(o.href,{decode:decodeURIComponent})(r))}};function c(t,e){let r="";try{r=(0,a.compile)(t,{encode:encodeURIComponent})(e)}catch{r=t}return r}function h(...t){let e=t.join("/").replace(/\/\/+/g,"/").replace(/\/$/,"").replaceAll("%2F","/");return e.startsWith("/")?e:`/${e}`}var m={defaultLocale:"",locales:[],routes:{}};try{let t=require((0,L.resolve)(process.cwd(),"roots.config.js"));m.defaultLocale=t.defaultLocale,m.locales=t.locales}catch{}0&&(module.exports={Router,compileHref,formatHref,schema}); //# sourceMappingURL=mock.js.map