Skip to content

Commit fc2292a

Browse files
committed
fix(routes): preserve routeUrl compatibility
1 parent d4d7ef8 commit fc2292a

5 files changed

Lines changed: 152 additions & 193 deletions

File tree

‎packages/void/src/codegen/route-types.ts‎

Lines changed: 24 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { relative } from 'pathslash';
1+
import { fileURLToPath } from 'node:url';
2+
import { isAbsolute, relative } from 'pathslash';
23
import type { RouteDefinition } from '../router/parse-filename.ts';
34
import type { ScanResult } from '../router/scan.ts';
45
import type { WebSocketRouteDefinition } from '../router/websocket.ts';
@@ -139,175 +140,34 @@ export function generateRouteTypes(
139140
lines.push(' export interface WebSocketRouteMap {}');
140141
lines.push(' export interface PageActionMap {}');
141142
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')),
209148
);
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}`,
251153
);
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+
}
279164
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>;`,
281166
);
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.'");
302167
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>;`,
304169
);
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;`);
311171
lines.push('');
312172
lines.push(' export interface RouteTypesConfig {');
313173
if (options.typedRoutes) {

‎packages/void/src/runtime/routing/route-url.ts‎

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { EncodedRoutePathMap, RoutePathMap, RouteTypesConfig } from 'void/routes';
2+
import type { DiagnosticKeyName, InvalidValueKeys, UrlTypeError } from './url-diagnostics.ts';
23

34
/**
45
* Encode one path segment of a route param. `encodeURIComponent` keeps dots, and
@@ -157,9 +158,8 @@ type UrlOptions = {
157158
hash?: string;
158159
};
159160

160-
// Keep route/option shapes visible to editor completion while inferring the
161-
// caller's input separately, so invalid calls can explain the correction.
162-
type RouteUrlError<Message extends string> = { readonly __routeUrlError: Message };
161+
// Diagnostics describe invalid calls; the valid-call overload below remains
162+
// independent so generic helpers and structural objects retain their contract.
163163
type RouteUrlOptionsInput<P extends RoutePath> = {
164164
params?: { [K in RoutePathMap[P] & string]?: unknown } | Record<string, unknown>;
165165
query?: Record<string, unknown>;
@@ -179,10 +179,6 @@ type MissingRouteParamKeys<P extends RoutePath, Params> = {
179179
type InvalidRouteParamKeys<P extends RoutePath, Params> = {
180180
[K in RoutePathMap[P] & string]: Params extends Record<K, string | number> ? never : K;
181181
}[RoutePathMap[P] & string];
182-
type InvalidRouteValueKeys<Values, Allowed> = {
183-
[K in keyof Values]-?: [Values[K]] extends [Allowed] ? never : K;
184-
}[keyof Values] &
185-
string;
186182
type RouteUrlParamError<P extends RoutePath, O> = [RoutePathMap[P]] extends [never]
187183
? 'params' extends keyof O
188184
? [O['params']] extends [undefined]
@@ -197,12 +193,12 @@ type RouteUrlParamError<P extends RoutePath, O> = [RoutePathMap[P]] extends [nev
197193
? never
198194
: `Param ${InvalidRouteParamKeys<P, Params>} for route ${P} must be a string or number.`
199195
: MissingRouteParam<P, MissingRouteParamKeys<P, Params>>
200-
: `Unknown param ${Exclude<keyof Params, RoutePathMap[P]> & string} for route ${P}. Use the parameter names in the route pattern.`
196+
: `Unknown param ${DiagnosticKeyName<Exclude<keyof Params, RoutePathMap[P]>>} for route ${P}. Use the parameter names in the route pattern.`
201197
: `Params for route ${P} must be an object with the route's parameter names.`
202198
: MissingRouteParam<P, RoutePathMap[P] & string>;
203199
type RouteUrlQueryError<P extends RoutePath, Query> = [Query] extends [RouteQuery | undefined]
204200
? never
205-
: `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.`;
201+
: `Invalid query param ${DiagnosticKeyName<InvalidValueKeys<Exclude<Query, undefined>, RouteQueryValue | ReadonlyArray<RouteQueryValue>>>} for route ${P}. Use a string, number, boolean, null, undefined, or an array of these.`;
206202
type RouteUrlOptionsError<P extends string, O> = [P] extends [RoutePath]
207203
? O extends undefined
208204
? MissingRouteParam<P, RoutePathMap[P] & string>
@@ -215,35 +211,34 @@ type RouteUrlOptionsError<P extends string, O> = [P] extends [RoutePath]
215211
? never
216212
: `Hash for route ${P} must be a string.`
217213
: never)
218-
| (Exclude<keyof O, 'params' | 'query' | 'hash'> extends infer K extends string
219-
? `Unknown routeUrl option: ${K}. Use params, query, or hash.`
220-
: never)
214+
| `Unknown routeUrl option: ${DiagnosticKeyName<Exclude<keyof O, 'params' | 'query' | 'hash'>>}. Use params, query, or hash.`
221215
: `Options for route ${P} must be an object with params, query, or hash.`
222216
: never;
223-
type ValidateRouteUrlArgs<P extends string, A extends [options?: unknown]> = [
224-
RouteUrlOptionsError<P, A[0]>,
225-
] extends [never]
226-
? unknown
227-
: RouteUrlOptionsError<P, A[0]>;
228-
229217
/** Build a relative URL from a known route pattern. */
218+
export function routeUrl<P extends RoutePath>(path: P, ...args: RouteUrlArgs<NoInfer<P>>): RouteUrl;
219+
// The default preserves the native tuple for explicit type arguments and
220+
// Parameters<typeof routeUrl<P>>. Inferred diagnostic tuples always require
221+
// an unsatisfied marker, even when an error calculation resolves to never.
230222
export function routeUrl<
231223
P extends RoutePath | (string & {}),
232-
A extends [options?: unknown] = RouteUrlArgs<Extract<P, RoutePath>>,
224+
A extends [options?: unknown] = never,
233225
>(
234226
path: P &
235227
(
236228
| RoutePath
237-
| RouteUrlError<
229+
| UrlTypeError<
238230
string extends NoInfer<P>
239231
? 'Pass a route pattern literal to routeUrl(); string is too broad.'
240232
: `Unknown route: ${NoInfer<P>}. Pass a registered route pattern as the first argument.`
241233
>
242234
),
243-
...args: A &
244-
[options?: RouteUrlOptionsInput<Extract<NoInfer<P>, RoutePath>>] &
245-
ValidateRouteUrlArgs<NoInfer<P>, NoInfer<A>>
246-
): RouteUrl {
235+
...args: [A] extends [never]
236+
? RouteUrlArgs<Extract<RoutePath, NoInfer<P>>>
237+
: A &
238+
[options?: RouteUrlOptionsInput<Extract<NoInfer<P>, RoutePath>>] &
239+
UrlTypeError<RouteUrlOptionsError<NoInfer<P>, NoInfer<A>[0]>>
240+
): RouteUrl;
241+
export function routeUrl(path: string, ...args: [options?: unknown]): RouteUrl {
247242
return buildRouteUrl(path, args[0] as UrlOptions | undefined) as RouteUrl;
248243
}
249244

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** An unsatisfied marker for diagnostic fallbacks, never an accepted input. */
2+
export type UrlTypeError<Message extends string> = { readonly __voidUrlError: Message };
3+
4+
/** Inspect every union member, including members with disjoint property names. */
5+
export type InvalidValueKeys<Values, Allowed> = Values extends unknown
6+
? { [K in keyof Values]-?: [Values[K]] extends [Allowed] ? never : K }[keyof Values]
7+
: never;
8+
9+
/** Preserve numeric and symbol keys when explaining an invalid property. */
10+
export type DiagnosticKeyName<K> = K extends string | number
11+
? `${K}`
12+
: K extends symbol
13+
? 'symbol'
14+
: never;

0 commit comments

Comments
 (0)