UNPKG

@eyedea/next-roots

Version:

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

3 lines (2 loc) 2.04 kB
"use strict";var a=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var L=(r,e)=>{for(var t in e)a(r,t,{get:e[t],enumerable:!0})},H=(r,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of R(e))!y.call(r,n)&&n!==t&&a(r,n,{get:()=>e[n],enumerable:!(o=h(e,n))||o.enumerable});return r};var x=r=>H(a({},"__esModule",{value:!0}),r);var F={};L(F,{Router:()=>l,compileHref:()=>f,formatHref:()=>d,schema:()=>p});module.exports=x(F);var i=require("path-to-regexp");function g({defaultLocale:r,locales:e}){return t=>{let o=t.startsWith("/")?t.slice(1):t,[n]=o.split("/");return e.includes(n)?n:r}}var s=class r{static PAGE_HREF="/";static getPageHref(){return r.PAGE_HREF}static setPageHref(e){r.PAGE_HREF=e}};var l=class extends s{schema;constructor(e){super(),this.schema=e}getHref(e,t={}){let{locale:o=this.getLocaleFromHref(s.getPageHref()),...n}=t,c=this.findRouteByLocaleAndName(o,e);return d(f((c==null?void 0:c.href)||"",n))}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,n)=>{let c=o.name.indexOf("["),u=n.name.indexOf("["),m=c!==-1;return m&&u!==-1?u-c:m?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 f(r,e){let t="";try{t=(0,i.compile)(r,{encode:encodeURIComponent})(e)}catch{t=r}return t}function d(...r){let e=r.join("/").replace(/\/\/+/g,"/").replace(/\/$/,"");return e.startsWith("/")?e:`/${e}`}var p;try{p=require("./cache/schema")}catch{throw new Error("Roots schema not found. Did you forget to run 'yarn roots'?")}0&&(module.exports={Router,compileHref,formatHref,schema}); //# sourceMappingURL=index.js.map