one
Version:
One is a React framework for web, iOS, and Android apps.
1 lines • 7.74 kB
Source Map (JSON)
{"version":3,"file":"getTypedRoutesDeclarationFile.js","names":[],"sources":["esm/typed-routes/getTypedRoutesDeclarationFile.js"],"sourcesContent":["import { getRoutes } from \"../router/getRoutes\";\nimport { isTypedRoute, removeSupportedExtensions } from \"../router/matchers\";\nconst CATCH_ALL = /\\[\\.\\.\\..+?\\]/g;\nconst SLUG = /\\[.+?\\]/g;\nfunction getTypedRoutesDeclarationFile(ctx) {\n const staticRoutes = /* @__PURE__ */ new Set();\n const dynamicRoutes = /* @__PURE__ */ new Set();\n const dynamicRouteContextKeys = /* @__PURE__ */ new Set();\n walkRouteNode(\n getRoutes(ctx, {\n platformRoutes: false,\n // We don't need to generate platform specific routes\n ignoreEntryPoints: true,\n ignoreRequireErrors: true\n // importMode: 'async',\n }),\n \"\",\n staticRoutes,\n dynamicRoutes,\n dynamicRouteContextKeys\n );\n const hasRoutes = dynamicRouteContextKeys.size > 0;\n return `// deno-lint-ignore-file\n/* eslint-disable */\n// biome-ignore: needed import\nimport type { OneRouter } from 'one'\n\ndeclare module 'one' {\n export namespace OneRouter {\n export interface __routes<T extends string = string> extends Record<string, unknown> {\n StaticRoutes:${setToUnionType(staticRoutes)}\n DynamicRoutes:${setToUnionType(dynamicRoutes)}\n DynamicRouteTemplate:${setToUnionType(dynamicRouteContextKeys)}\n IsTyped: true\n ${hasRoutes ? `RouteTypes: ${generateRouteTypesMap(dynamicRouteContextKeys)}` : \"\"}\n }\n }\n}\n${hasRoutes ? `\n/**\n * Helper type for route information\n */\ntype RouteInfo<Params = Record<string, never>> = {\n Params: Params\n LoaderProps: { path: string; search?: string; subdomain?: string; params: Params; request?: Request }\n}` : \"\"}\n`.trim();\n}\nfunction generateRouteTypesMap(dynamicRouteContextKeys) {\n if (dynamicRouteContextKeys.size === 0) {\n return \"{}\";\n }\n const routes = [...dynamicRouteContextKeys].sort();\n const entries = routes.map((routePath) => {\n const params = extractParams(routePath);\n const paramsType = params.length === 0 ? \"{}\" : generateInlineParamsType(params);\n return ` '${routePath}': RouteInfo<${paramsType}>`;\n }).join(\"\\n\");\n return `{\n${entries}\n }`;\n}\nfunction extractParams(routePath) {\n const params = [];\n const paramRegex = /\\[(\\.\\.\\.)?([\\w]+)\\]/g;\n let match;\n while ((match = paramRegex.exec(routePath)) !== null) {\n params.push({\n name: match[2],\n isCatchAll: match[1] === \"...\"\n });\n }\n return params;\n}\nfunction generateInlineParamsType(params) {\n const entries = params.map((p) => {\n const type = p.isCatchAll ? \"string[]\" : \"string\";\n return `${p.name}: ${type}`;\n });\n return `{ ${entries.join(\"; \")} }`;\n}\nfunction walkRouteNode(routeNode, parentRoutePath, staticRoutes, dynamicRoutes, dynamicRouteContextKeys) {\n if (!routeNode) return;\n addRouteNode(\n routeNode,\n parentRoutePath,\n staticRoutes,\n dynamicRoutes,\n dynamicRouteContextKeys\n );\n parentRoutePath = `${removeSupportedExtensions(`${parentRoutePath}/${routeNode.route}`).replace(/\\/?index$/, \"\")}`;\n for (const child of routeNode.children) {\n walkRouteNode(\n child,\n parentRoutePath,\n staticRoutes,\n dynamicRoutes,\n dynamicRouteContextKeys\n );\n }\n}\nfunction addRouteNode(routeNode, parentRoutePath, staticRoutes, dynamicRoutes, dynamicRouteContextKeys) {\n if (!routeNode?.route) return;\n if (!isTypedRoute(routeNode.route)) return;\n let routePath = `${parentRoutePath}/${removeSupportedExtensions(routeNode.route).replace(/\\/?index$/, \"\")}`;\n if (!routePath.startsWith(\"/\")) {\n routePath = `/${routePath}`;\n }\n if (routeNode.dynamic) {\n for (const path of generateCombinations(routePath)) {\n dynamicRouteContextKeys.add(path);\n dynamicRoutes.add(\n // biome-ignore lint/suspicious/noTemplateCurlyInString: intentionally generating type string\n `${path.replaceAll(CATCH_ALL, \"${string}\").replaceAll(SLUG, \"${OneRouter.SingleRoutePart<T>}\")}`\n );\n }\n } else {\n for (const combination of generateCombinations(routePath)) {\n staticRoutes.add(combination);\n }\n }\n}\nconst setToUnionType = (set) => {\n if (set.size === 0) return \" never\";\n const sorted = [...set].sort();\n if (sorted.length === 1) return ` \\`${sorted[0]}\\``;\n return \"\\n | \" + sorted.map((s) => `\\`${s}\\``).join(\"\\n | \");\n};\nfunction generateCombinations(pathname) {\n const groups = pathname.split(\"/\").filter((part) => part.startsWith(\"(\") && part.endsWith(\")\"));\n const combinations = [];\n function generate(currentIndex, currentPath) {\n if (currentIndex === groups.length) {\n combinations.push(currentPath.replace(/\\/{2,}/g, \"/\"));\n return;\n }\n const group = groups[currentIndex];\n const withoutGroup = currentPath.replace(`/${group}`, \"\");\n generate(currentIndex + 1, withoutGroup);\n generate(currentIndex + 1, currentPath);\n }\n generate(0, pathname);\n return combinations;\n}\nexport {\n getTypedRoutesDeclarationFile\n};\n//# sourceMappingURL=getTypedRoutesDeclarationFile.js.map\n"],"mappings":";;;;AAEA,MAAM,YAAY;AAClB,MAAM,OAAO;AACb,SAAS,8BAA8B,KAAK;CAC1C,MAAM,+BAA+B,IAAI,IAAI;CAC7C,MAAM,gCAAgC,IAAI,IAAI;CAC9C,MAAM,0CAA0C,IAAI,IAAI;CACxD,cACE,UAAU,KAAK;EACb,gBAAgB;EAEhB,mBAAmB;EACnB,qBAAqB;CAEvB,CAAC,GACD,IACA,cACA,eACA,uBACF;CACA,MAAM,YAAY,wBAAwB,OAAO;CACjD,OAAO;;;;;;;;qBAQY,eAAe,YAAY,EAAE;sBAC5B,eAAe,aAAa,EAAE;6BACvB,eAAe,uBAAuB,EAAE;;QAE7D,YAAY,eAAe,sBAAsB,uBAAuB,MAAM,GAAG;;;;EAIvF,YAAY;;;;;;;KAOT,GAAG;EACN,KAAK;AACP;AACA,SAAS,sBAAsB,yBAAyB;CACtD,IAAI,wBAAwB,SAAS,GAAG;EACtC,OAAO;CACT;CACA,MAAM,SAAS,CAAC,GAAG,uBAAuB,CAAC,CAAC,KAAK;CACjD,MAAM,UAAU,OAAO,KAAK,cAAc;EACxC,MAAM,SAAS,cAAc,SAA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