UNPKG

@tanstack/router-core

Version:

Modern and scalable routing for React applications

1 lines 3.62 kB
{"version":3,"file":"rewrite.cjs","names":[],"sources":["../../src/rewrite.ts"],"sourcesContent":["import { joinPaths, trimPath } from './path'\nimport type { LocationRewrite } from './router'\n\n/** Compose multiple rewrite pairs into a single in/out rewrite. */\nexport function composeRewrites(rewrites: Array<LocationRewrite>) {\n return {\n input: ({ url }) => {\n for (const rewrite of rewrites) {\n url = executeRewriteInput(rewrite, url)\n }\n return url\n },\n output: ({ url }) => {\n for (let i = rewrites.length - 1; i >= 0; i--) {\n url = executeRewriteOutput(rewrites[i], url)\n }\n return url\n },\n } satisfies LocationRewrite\n}\n\n/** Create a rewrite pair that strips/adds a basepath on input/output. */\nexport function rewriteBasepath(opts: {\n basepath: string\n caseSensitive?: boolean\n}) {\n const trimmedBasepath = trimPath(opts.basepath)\n const normalizedBasepath = `/${trimmedBasepath}`\n const checkBasepath = opts.caseSensitive\n ? normalizedBasepath\n : normalizedBasepath.toLowerCase()\n const checkBasepathWithSlash = `${checkBasepath}/`\n\n return {\n input: ({ url }) => {\n const pathname = opts.caseSensitive\n ? url.pathname\n : url.pathname.toLowerCase()\n\n // Handle exact basepath match (e.g., /my-app -> /)\n if (pathname === checkBasepath) {\n url.pathname = '/'\n } else if (pathname.startsWith(checkBasepathWithSlash)) {\n // Handle basepath with trailing content (e.g., /my-app/users -> /users)\n url.pathname = url.pathname.slice(normalizedBasepath.length)\n }\n return url\n },\n output: ({ url }) => {\n url.pathname = joinPaths(['/', trimmedBasepath, url.pathname])\n return url\n },\n } satisfies LocationRewrite\n}\n\n/** Execute a location input rewrite if provided. */\nexport function executeRewriteInput(\n rewrite: LocationRewrite | undefined,\n url: URL,\n): URL {\n const res = rewrite?.input?.({ url })\n if (res) {\n if (typeof res === 'string') {\n return new URL(res)\n } else if (res instanceof URL) {\n return res\n }\n }\n return url\n}\n\n/** Execute a location output rewrite if provided. */\nexport function executeRewriteOutput(\n rewrite: LocationRewrite | undefined,\n url: URL,\n): URL {\n const res = rewrite?.output?.({ url })\n if (res) {\n if (typeof res === 'string') {\n return new URL(res)\n } else if (res instanceof URL) {\n return res\n }\n }\n return url\n}\n"],"mappings":";;;AAIA,SAAgB,gBAAgB,UAAkC;CAChE,OAAO;EACL,QAAQ,EAAE,UAAU;GAClB,KAAK,MAAM,WAAW,UACpB,MAAM,oBAAoB,SAAS,GAAG;GAExC,OAAO;EACT;EACA,SAAS,EAAE,UAAU;GACnB,KAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,KACxC,MAAM,qBAAqB,SAAS,IAAI,GAAG;GAE7C,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,gBAAgB,MAG7B;CACD,MAAM,kBAAkB,aAAA,SAAS,KAAK,QAAQ;CAC9C,MAAM,qBAAqB,IAAI;CAC/B,MAAM,gBAAgB,KAAK,gBACvB,qBACA,mBAAmB,YAAY;CACnC,MAAM,yBAAyB,GAAG,cAAc;CAEhD,OAAO;EACL,QAAQ,EAAE,UAAU;GAClB,MAAM,WAAW,KAAK,gBAClB,IAAI,WACJ,IAAI,SAAS,YAAY;GAG7B,IAAI,aAAa,eACf,IAAI,WAAW;QACV,IAAI,SAAS,WAAW,sBAAsB,GAEnD,IAAI,WAAW,IAAI,SAAS,MAAM,mBAAmB,MAAM;GAE7D,OAAO;EACT;EACA,SAAS,EAAE,UAAU;GACnB,IAAI,WAAW,aAAA,UAAU;IAAC;IAAK;IAAiB,IAAI;GAAQ,CAAC;GAC7D,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,oBACd,SACA,KACK;CACL,MAAM,MAAM,SAAS,QAAQ,EAAE,IAAI,CAAC;CACpC,IAAI;MACE,OAAO,QAAQ,UACjB,OAAO,IAAI,IAAI,GAAG;OACb,IAAI,eAAe,KACxB,OAAO;CAAA;CAGX,OAAO;AACT;;AAGA,SAAgB,qBACd,SACA,KACK;CACL,MAAM,MAAM,SAAS,SAAS,EAAE,IAAI,CAAC;CACrC,IAAI;MACE,OAAO,QAAQ,UACjB,OAAO,IAAI,IAAI,GAAG;OACb,IAAI,eAAe,KACxB,OAAO;CAAA;CAGX,OAAO;AACT"}