UNPKG

@eyedea/next-roots

Version:

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

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