|
1 | | -import { relative } from 'pathslash'; |
| 1 | +import { fileURLToPath } from 'node:url'; |
| 2 | +import { isAbsolute, relative } from 'pathslash'; |
2 | 3 | import type { RouteDefinition } from '../router/parse-filename.ts'; |
3 | 4 | import type { ScanResult } from '../router/scan.ts'; |
4 | 5 | import type { WebSocketRouteDefinition } from '../router/websocket.ts'; |
@@ -139,175 +140,34 @@ export function generateRouteTypes( |
139 | 140 | lines.push(' export interface WebSocketRouteMap {}'); |
140 | 141 | lines.push(' export interface PageActionMap {}'); |
141 | 142 | lines.push(''); |
142 | | - // Projects resolve void/routes to this generated declaration through their |
143 | | - // tsconfig paths, so keep the standalone helper's declarations here as well. |
144 | | - lines.push(' export type RoutePath = keyof RoutePathMap & string;'); |
145 | | - lines.push(' export type RouteQueryValue = string | number | boolean | null | undefined;'); |
146 | | - lines.push( |
147 | | - ' export type RouteQuery = Record<string, RouteQueryValue | ReadonlyArray<RouteQueryValue>>;', |
148 | | - ); |
149 | | - lines.push(' export type RouteOptions<P extends RoutePath> = {'); |
150 | | - lines.push(' query?: RouteQuery;'); |
151 | | - lines.push(' hash?: string;'); |
152 | | - lines.push(' } & ([RoutePathMap[P]] extends [never]'); |
153 | | - lines.push(' ? { params?: never }'); |
154 | | - lines.push(' : { params: { [K in RoutePathMap[P] & string]: string | number } });'); |
155 | | - lines.push( |
156 | | - ' export type RouteDestination<P extends RoutePath = RoutePath> = P extends RoutePath', |
157 | | - ); |
158 | | - lines.push(' ? { pathname: P } & RouteOptions<P>'); |
159 | | - lines.push(' : never;'); |
160 | | - lines.push( |
161 | | - ' type RouteUrlSegment<S extends string> = S extends `:${string}` | `[${string}]` | "*" ? string : S;', |
162 | | - ); |
163 | | - lines.push( |
164 | | - ' type CheckedRoutePath<P extends string> = P extends `/${infer Segment}/${infer Rest}`', |
165 | | - ); |
166 | | - lines.push(' ? Rest extends ""'); |
167 | | - lines.push(' ? string extends RouteUrlSegment<Segment> ? never : P'); |
168 | | - lines.push(' : P'); |
169 | | - lines.push(' : P extends `/${infer Segment}`'); |
170 | | - lines.push(' ? string extends RouteUrlSegment<Segment> ? never : P'); |
171 | | - lines.push(' : P;'); |
172 | | - lines.push( |
173 | | - ' type RouteSegmentsUrl<P extends string> = P extends `${infer Segment}/${infer Rest}`', |
174 | | - ); |
175 | | - lines.push(' ? `${RouteUrlSegment<Segment>}/${RouteSegmentsUrl<Rest>}`'); |
176 | | - lines.push(' : RouteUrlSegment<P>;'); |
177 | | - lines.push( |
178 | | - ' type RoutePatternUrl<P extends string> = P extends `${infer Prefix}/${"*" | "[...]"}/`', |
179 | | - ); |
180 | | - lines.push(' ? `${RouteSegmentsUrl<Prefix>}/` | `${RouteSegmentsUrl<Prefix>}/${string}/`'); |
181 | | - lines.push(' : P extends `${infer Prefix}/${"*" | "[...]"}`'); |
182 | | - lines.push( |
183 | | - ' ? (Prefix extends "" ? "/" : RouteSegmentsUrl<Prefix>) | `${RouteSegmentsUrl<Prefix>}/${string}`', |
184 | | - ); |
185 | | - lines.push(' : RouteSegmentsUrl<P>;'); |
186 | | - lines.push(' type EncodedRoutePath = EncodedRoutePathMap[keyof EncodedRoutePathMap] & string;'); |
187 | | - lines.push( |
188 | | - ' type RoutePathUrl = RoutePatternUrl<CheckedRoutePath<RoutePath | EncodedRoutePath>>;', |
189 | | - ); |
190 | | - lines.push(' type BuiltRouteUrl = string & { readonly __voidRouteUrl: true };'); |
191 | | - lines.push(' type TypedRoutes = RouteTypesConfig extends { typedRoutes: true } ? true : false;'); |
192 | | - lines.push(' export type RouteUrl = TypedRoutes extends true'); |
193 | | - lines.push(' ? BuiltRouteUrl | RoutePathUrl | `${RoutePathUrl}${"?" | "#"}${string}`'); |
194 | | - lines.push(' : string;'); |
195 | | - lines.push(' export type Href = TypedRoutes extends true'); |
196 | | - lines.push( |
197 | | - ' ? RouteUrl | `${"?" | "#"}${string}` | `${"http://" | "https://" | "mailto:" | "tel:"}${string}`', |
198 | | - ); |
199 | | - lines.push(' : string;'); |
200 | | - lines.push(' export type RouteUrlInput = Href | URL | RouteDestination;'); |
201 | | - lines.push(' // Keep route/option shapes visible to editor completion while inferring the'); |
202 | | - lines.push(" // caller's input separately, so invalid calls can explain the correction."); |
203 | | - lines.push( |
204 | | - ' type RouteUrlError<Message extends string> = { readonly __routeUrlError: Message };', |
205 | | - ); |
206 | | - lines.push(' type RouteUrlOptionsInput<P extends RoutePath> = {'); |
207 | | - lines.push( |
208 | | - ' params?: { [K in RoutePathMap[P] & string]?: unknown } | Record<string, unknown>;', |
| 143 | + // Keep URL contracts and diagnostics in the runtime declarations. Referencing |
| 144 | + // void/routes here would be circular when this file augments that package entry. |
| 145 | + const sdkRoutesPath = relative( |
| 146 | + outDir, |
| 147 | + fileURLToPath(import.meta.resolve('#self/runtime/route-url')), |
209 | 148 | ); |
210 | | - lines.push(' query?: Record<string, unknown>;'); |
211 | | - lines.push(' hash?: unknown;'); |
212 | | - lines.push(' };'); |
213 | | - lines.push(' type RouteUrlArgs<P extends RoutePath> = [RoutePathMap[P]] extends [never]'); |
214 | | - lines.push(' ? [options?: RouteOptions<P>]'); |
215 | | - lines.push(' : [options: RouteOptions<P>];'); |
216 | | - lines.push( |
217 | | - ' type MissingRouteParam<P extends RoutePath, K extends string> = P extends RoutePath', |
218 | | - ); |
219 | | - lines.push(' ? K extends RoutePathMap[P]'); |
220 | | - lines.push(' ? `Missing param ${K} for route ${P}. Pass it in { params: { ... } }.`'); |
221 | | - lines.push(' : never'); |
222 | | - lines.push(' : never;'); |
223 | | - lines.push(' type MissingRouteParamKeys<P extends RoutePath, Params> = {'); |
224 | | - lines.push(' [K in RoutePathMap[P] & string]: Params extends Record<K, unknown> ? never : K;'); |
225 | | - lines.push(' }[RoutePathMap[P] & string];'); |
226 | | - lines.push(' type InvalidRouteParamKeys<P extends RoutePath, Params> = {'); |
227 | | - lines.push( |
228 | | - ' [K in RoutePathMap[P] & string]: Params extends Record<K, string | number> ? never : K;', |
229 | | - ); |
230 | | - lines.push(' }[RoutePathMap[P] & string];'); |
231 | | - lines.push(' type InvalidRouteValueKeys<Values, Allowed> = {'); |
232 | | - lines.push(' [K in keyof Values]-?: [Values[K]] extends [Allowed] ? never : K;'); |
233 | | - lines.push(' }[keyof Values] &'); |
234 | | - lines.push(' string;'); |
235 | | - lines.push( |
236 | | - ' type RouteUrlParamError<P extends RoutePath, O> = [RoutePathMap[P]] extends [never]', |
237 | | - ); |
238 | | - lines.push(" ? 'params' extends keyof O"); |
239 | | - lines.push(" ? [O['params']] extends [undefined]"); |
240 | | - lines.push(' ? never'); |
241 | | - lines.push(' : `Route ${P} has no path params. Remove params.`'); |
242 | | - lines.push(' : never'); |
243 | | - lines.push(' : O extends { params: infer Params }'); |
244 | | - lines.push(' ? Params extends object'); |
245 | | - lines.push(' ? [Exclude<keyof Params, RoutePathMap[P]>] extends [never]'); |
246 | | - lines.push(' ? [MissingRouteParamKeys<P, Params>] extends [never]'); |
247 | | - lines.push(' ? [InvalidRouteParamKeys<P, Params>] extends [never]'); |
248 | | - lines.push(' ? never'); |
249 | | - lines.push( |
250 | | - ' : `Param ${InvalidRouteParamKeys<P, Params>} for route ${P} must be a string or number.`', |
| 149 | + const sdkRoutesModule = JSON.stringify( |
| 150 | + isAbsolute(sdkRoutesPath) || sdkRoutesPath.startsWith('.') |
| 151 | + ? sdkRoutesPath |
| 152 | + : `./${sdkRoutesPath}`, |
251 | 153 | ); |
252 | | - lines.push(' : MissingRouteParam<P, MissingRouteParamKeys<P, Params>>'); |
253 | | - lines.push( |
254 | | - ' : `Unknown param ${Exclude<keyof Params, RoutePathMap[P]> & string} for route ${P}. Use the parameter names in the route pattern.`', |
255 | | - ); |
256 | | - lines.push( |
257 | | - " : `Params for route ${P} must be an object with the route's parameter names.`", |
258 | | - ); |
259 | | - lines.push(' : MissingRouteParam<P, RoutePathMap[P] & string>;'); |
260 | | - lines.push( |
261 | | - ' type RouteUrlQueryError<P extends RoutePath, Query> = [Query] extends [RouteQuery | undefined]', |
262 | | - ); |
263 | | - lines.push(' ? never'); |
264 | | - lines.push( |
265 | | - ' : `Invalid query param ${InvalidRouteValueKeys<Exclude<Query, undefined>, RouteQueryValue | ReadonlyArray<RouteQueryValue>>} for route ${P}. Use a string, number, boolean, null, undefined, or an array of these.`;', |
266 | | - ); |
267 | | - lines.push(' type RouteUrlOptionsError<P extends string, O> = [P] extends [RoutePath]'); |
268 | | - lines.push(' ? O extends undefined'); |
269 | | - lines.push(' ? MissingRouteParam<P, RoutePathMap[P] & string>'); |
270 | | - lines.push(' : O extends object'); |
271 | | - lines.push(' ?'); |
272 | | - lines.push(' | RouteUrlParamError<P, O>'); |
273 | | - lines.push(" | ('query' extends keyof O ? RouteUrlQueryError<P, O['query']> : never)"); |
274 | | - lines.push(" | ('hash' extends keyof O"); |
275 | | - lines.push(" ? [O['hash']] extends [string | undefined]"); |
276 | | - lines.push(' ? never'); |
277 | | - lines.push(' : `Hash for route ${P} must be a string.`'); |
278 | | - lines.push(' : never)'); |
| 154 | + for (const name of [ |
| 155 | + 'RoutePath', |
| 156 | + 'RouteQueryValue', |
| 157 | + 'RouteQuery', |
| 158 | + 'RouteUrl', |
| 159 | + 'Href', |
| 160 | + 'RouteUrlInput', |
| 161 | + ]) { |
| 162 | + lines.push(` export type ${name} = import(${sdkRoutesModule}).${name};`); |
| 163 | + } |
279 | 164 | lines.push( |
280 | | - " | (Exclude<keyof O, 'params' | 'query' | 'hash'> extends infer K extends string", |
| 165 | + ` export type RouteOptions<P extends RoutePath> = import(${sdkRoutesModule}).RouteOptions<P>;`, |
281 | 166 | ); |
282 | | - lines.push(' ? `Unknown routeUrl option: ${K}. Use params, query, or hash.`'); |
283 | | - lines.push(' : never)'); |
284 | | - lines.push(' : `Options for route ${P} must be an object with params, query, or hash.`'); |
285 | | - lines.push(' : never;'); |
286 | | - lines.push(' type ValidateRouteUrlArgs<P extends string, A extends [options?: unknown]> = ['); |
287 | | - lines.push(' RouteUrlOptionsError<P, A[0]>,'); |
288 | | - lines.push(' ] extends [never]'); |
289 | | - lines.push(' ? unknown'); |
290 | | - lines.push(' : RouteUrlOptionsError<P, A[0]>;'); |
291 | | - lines.push(''); |
292 | | - lines.push(' export function routeUrl<'); |
293 | | - lines.push(' P extends RoutePath | (string & {}),'); |
294 | | - lines.push(' A extends [options?: unknown] = RouteUrlArgs<Extract<P, RoutePath>>,'); |
295 | | - lines.push(' >('); |
296 | | - lines.push(' path: P &'); |
297 | | - lines.push(' ('); |
298 | | - lines.push(' | RoutePath'); |
299 | | - lines.push(' | RouteUrlError<'); |
300 | | - lines.push(' string extends NoInfer<P>'); |
301 | | - lines.push(" ? 'Pass a route pattern literal to routeUrl(); string is too broad.'"); |
302 | 167 | lines.push( |
303 | | - ' : `Unknown route: ${NoInfer<P>}. Pass a registered route pattern as the first argument.`', |
| 168 | + ` export type RouteDestination<P extends RoutePath = RoutePath> = import(${sdkRoutesModule}).RouteDestination<P>;`, |
304 | 169 | ); |
305 | | - lines.push(' >'); |
306 | | - lines.push(' ),'); |
307 | | - lines.push(' ...args: A &'); |
308 | | - lines.push(' [options?: RouteUrlOptionsInput<Extract<NoInfer<P>, RoutePath>>] &'); |
309 | | - lines.push(' ValidateRouteUrlArgs<NoInfer<P>, NoInfer<A>>'); |
310 | | - lines.push(' ): RouteUrl;'); |
| 170 | + lines.push(` export const routeUrl: typeof import(${sdkRoutesModule}).routeUrl;`); |
311 | 171 | lines.push(''); |
312 | 172 | lines.push(' export interface RouteTypesConfig {'); |
313 | 173 | if (options.typedRoutes) { |
|
0 commit comments