{"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. */\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. */\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 normalizedBasepathWithSlash = `${normalizedBasepath}/`\n  const checkBasepath = opts.caseSensitive\n    ? normalizedBasepath\n    : normalizedBasepath.toLowerCase()\n  const checkBasepathWithSlash = opts.caseSensitive\n    ? normalizedBasepathWithSlash\n    : normalizedBasepathWithSlash.toLowerCase()\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. */\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. */\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":";;;;AAKA,SAAgB,gBAAgB,UAAkC;AAChE,QAAO;EACL,QAAQ,EAAE,UAAU;AAClB,QAAK,MAAM,WAAW,SACpB,OAAM,oBAAoB,SAAS,IAAI;AAEzC,UAAO;;EAET,SAAS,EAAE,UAAU;AACnB,QAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,IACxC,OAAM,qBAAqB,SAAS,IAAI,IAAI;AAE9C,UAAO;;EAEV;;;;AAKH,SAAgB,gBAAgB,MAG7B;CACD,MAAM,kBAAkB,aAAA,SAAS,KAAK,SAAS;CAC/C,MAAM,qBAAqB,IAAI;CAC/B,MAAM,8BAA8B,GAAG,mBAAmB;CAC1D,MAAM,gBAAgB,KAAK,gBACvB,qBACA,mBAAmB,aAAa;CACpC,MAAM,yBAAyB,KAAK,gBAChC,8BACA,4BAA4B,aAAa;AAE7C,QAAO;EACL,QAAQ,EAAE,UAAU;GAClB,MAAM,WAAW,KAAK,gBAClB,IAAI,WACJ,IAAI,SAAS,aAAa;AAG9B,OAAI,aAAa,cACf,KAAI,WAAW;YACN,SAAS,WAAW,uBAAuB,CAEpD,KAAI,WAAW,IAAI,SAAS,MAAM,mBAAmB,OAAO;AAE9D,UAAO;;EAET,SAAS,EAAE,UAAU;AACnB,OAAI,WAAW,aAAA,UAAU;IAAC;IAAK;IAAiB,IAAI;IAAS,CAAC;AAC9D,UAAO;;EAEV;;;;AAKH,SAAgB,oBACd,SACA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