( route: string )
| 132 | }; |
| 133 | |
| 134 | function createPathBuilder<T extends Record<string, string | string[]>>( |
| 135 | route: string |
| 136 | ): (params: T) => string { |
| 137 | const pathArr = route.split("/"); |
| 138 | |
| 139 | let catchAllSegment: ((params: T) => string) | null = null; |
| 140 | if (pathArr.at(-1)?.startsWith("[[...")) { |
| 141 | const catchKey = pathArr.pop()!.replace("[[...", "").replace("]]", ""); |
| 142 | catchAllSegment = (params: T) => { |
| 143 | const catchAll = params[catchKey] as unknown as string[]; |
| 144 | return catchAll ? `/${catchAll.join("/")}` : ""; |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | const elems: ((params: T) => string)[] = []; |
| 149 | for (const elem of pathArr) { |
| 150 | const catchAll = elem.match(/\[\.\.\.(.*)\]/); |
| 151 | const param = elem.match(/\[(.*)\]/); |
| 152 | if (catchAll?.[1]) { |
| 153 | const key = catchAll[1]; |
| 154 | elems.push((params: T) => |
| 155 | (params[key as unknown as string] as string[]).join("/") |
| 156 | ); |
| 157 | } else if (param?.[1]) { |
| 158 | const key = param[1]; |
| 159 | elems.push((params: T) => params[key as unknown as string] as string); |
| 160 | } else if (!(elem.startsWith("(") && elem.endsWith(")"))) { |
| 161 | elems.push(() => elem); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return (params: T): string => { |
| 166 | const p = elems |
| 167 | .map((e) => e(params)) |
| 168 | .filter(Boolean) |
| 169 | .join("/"); |
| 170 | if (catchAllSegment) { |
| 171 | return p + catchAllSegment(params); |
| 172 | } else { |
| 173 | return p.length ? p : "/"; |
| 174 | } |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | function createRouteBuilder< |
| 179 | Params extends z.ZodSchema, |
no outgoing calls
no test coverage detected