Skip to content

Commit 0acf98f

Browse files
committed
fix(routes): preserve generic forwarding and stable type imports
1 parent fc2292a commit 0acf98f

4 files changed

Lines changed: 110 additions & 18 deletions

File tree

‎packages/void/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@
313313
"require": "./dist/runtime/env/public.mjs"
314314
}
315315
},
316+
"./_route-url-types": {
317+
"types": "./dist/runtime/routing/route-url.d.mts"
318+
},
316319
"./_env": {
317320
"types": "./dist/runtime/env/runtime.d.mts",
318321
"import": "./dist/runtime/env/runtime.mjs",

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { fileURLToPath } from 'node:url';
2-
import { isAbsolute, relative } from 'pathslash';
1+
import { relative } from 'pathslash';
32
import type { RouteDefinition } from '../router/parse-filename.ts';
43
import type { ScanResult } from '../router/scan.ts';
54
import type { WebSocketRouteDefinition } from '../router/websocket.ts';
@@ -140,17 +139,9 @@ export function generateRouteTypes(
140139
lines.push(' export interface WebSocketRouteMap {}');
141140
lines.push(' export interface PageActionMap {}');
142141
lines.push('');
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')),
148-
);
149-
const sdkRoutesModule = JSON.stringify(
150-
isAbsolute(sdkRoutesPath) || sdkRoutesPath.startsWith('.')
151-
? sdkRoutesPath
152-
: `./${sdkRoutesPath}`,
153-
);
142+
// A bare types-only entry follows the installed SDK through upgrades.
143+
// void/routes would be circular because this file augments that entry.
144+
const sdkRoutesModule = JSON.stringify('void/_route-url-types');
154145
for (const name of [
155146
'RoutePath',
156147
'RouteQueryValue',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export function routeUrl<
233233
>
234234
),
235235
...args: [A] extends [never]
236-
? RouteUrlArgs<Extract<RoutePath, NoInfer<P>>>
236+
? RouteUrlArgs<NoInfer<P> & RoutePath>
237237
: A &
238238
[options?: RouteUrlOptionsInput<Extract<NoInfer<P>, RoutePath>>] &
239239
UrlTypeError<RouteUrlOptionsError<NoInfer<P>, NoInfer<A>[0]>>

‎packages/void/test/integration/route-url-builder.test.ts‎

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { spawnSync } from 'node:child_process';
2-
import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
2+
import {
3+
cpSync,
4+
mkdirSync,
5+
mkdtempSync,
6+
readFileSync,
7+
rmSync,
8+
symlinkSync,
9+
writeFileSync,
10+
} from 'node:fs';
311
import { createRequire } from 'node:module';
412
import { tmpdir } from 'node:os';
513
import { dirname, join } from 'node:path';
14+
import { pathToFileURL } from 'node:url';
615
import { Miniflare, convertV4MiniflareOptions } from 'miniflare';
716
import { chromium } from 'playwright';
8-
import { build } from 'vite';
17+
import { build, transformWithOxc } from 'vite';
918
import { afterEach, describe, expect, it } from 'vite-plus/test';
1019
import { analyzeApplication } from '../../src/application-routing.ts';
1120
import { createProjectTsconfig } from '../../src/codegen/project-tsconfig.ts';
@@ -484,7 +493,7 @@ describe('standalone routeUrl utility', () => {
484493
async (declarations) => {
485494
const root = await createFixture({
486495
extraFiles: {
487-
'check-compatibility.ts': `import { routeUrl, type RouteDestination, type RouteOptions } from 'void/routes';
496+
'check-compatibility.ts': `import { routeUrl, type RouteDestination, type RouteOptions, type RoutePath } from 'void/routes';
488497
function userUrl<T extends string | number>(id: T) {
489498
return routeUrl('/users/[id]', { params: { id } });
490499
}
@@ -500,6 +509,9 @@ function forwardDestination<T extends RouteDestination<'/users/[id]'>>(destinati
500509
function forwardArgs(...args: Parameters<typeof routeUrl<'/users/[id]'>>) {
501510
return routeUrl(...args);
502511
}
512+
function forwardGenericArgs<P extends RoutePath>(...args: Parameters<typeof routeUrl<P>>) {
513+
return routeUrl(...args);
514+
}
503515
forwardArgs('/users/[id]', { params: { id: 42 } });
504516
const user = { id: 42, name: 'Alice' };
505517
routeUrl('/users/[id]', { params: user });
@@ -511,7 +523,7 @@ const destination: RouteDestination<'/users/[id]'> = {
511523
routeUrl('/users/[id]', destination);
512524
const staticDestination: RouteDestination<'/about'> = { pathname: '/about' };
513525
routeUrl('/about', staticDestination);
514-
void [userUrl, forwardOptions, forwardStaticOptions, forwardDestination, forwardArgs];
526+
void [userUrl, forwardOptions, forwardStaticOptions, forwardDestination, forwardArgs, forwardGenericArgs];
515527
`,
516528
},
517529
});
@@ -522,6 +534,92 @@ void [userUrl, forwardOptions, forwardStaticOptions, forwardDestination, forward
522534
},
523535
);
524536

537+
it.each(['generated', 'package'] as const)(
538+
'retains URL checks after an SDK upgrade without regenerating the %s declarations',
539+
async (declarations) => {
540+
const root = mkdtempSync(join(tmpdir(), 'void-route-upgrade-'));
541+
roots.push(root);
542+
const outDir = join(root, '.void');
543+
const packageLink = join(root, 'node_modules/void');
544+
mkdirSync(outDir, { recursive: true });
545+
const manifest = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf8'));
546+
const generatorCode = (
547+
await transformWithOxc(
548+
readFileSync(join(packageRoot, 'src/codegen/route-types.ts'), 'utf8'),
549+
'route-types.ts',
550+
)
551+
).code;
552+
function createSdkVersion(version: string): string {
553+
const directory = join(root, 'node_modules/.pnpm', `void@${version}/node_modules/void`);
554+
mkdirSync(join(directory, 'src/codegen'), { recursive: true });
555+
cpSync(join(packageRoot, 'dist'), join(directory, 'dist'), { recursive: true });
556+
symlinkSync(join(packageRoot, 'node_modules'), join(directory, 'node_modules'), 'junction');
557+
writeFileSync(join(directory, 'src/codegen/route-types.mjs'), generatorCode);
558+
writeFileSync(join(directory, 'package.json'), JSON.stringify({ ...manifest, version }));
559+
return directory;
560+
}
561+
const oldSdk = createSdkVersion('1.9.1');
562+
const newSdk = createSdkVersion('1.9.2');
563+
symlinkSync(oldSdk, packageLink, 'junction');
564+
writeFileSync(join(root, 'package.json'), '{"type":"module"}');
565+
const generator = pathToFileURL(join(oldSdk, 'src/codegen/route-types.mjs')).href;
566+
const generation = spawnSync(
567+
process.execPath,
568+
[
569+
'--input-type=module',
570+
'-e',
571+
`import { generateRouteTypes } from ${JSON.stringify(generator)};
572+
const scan = { routes: [
573+
{ pattern: '/about', params: [], methods: [] },
574+
{ pattern: '/users/:id', params: ['id'], methods: [] },
575+
], pages: { pages: [] } };
576+
process.stdout.write(generateRouteTypes(scan, ${JSON.stringify(join(root, 'routes'))}, ${JSON.stringify(outDir)}, undefined, { typedRoutes: true }).dts);`,
577+
],
578+
{ encoding: 'utf8' },
579+
);
580+
expect({ status: generation.status, errors: generation.stderr }).toEqual({
581+
status: 0,
582+
errors: '',
583+
});
584+
const routeTypesPath = join(outDir, 'routes.d.ts');
585+
writeFileSync(routeTypesPath, generation.stdout);
586+
writeFileSync(
587+
join(outDir, 'tsconfig.json'),
588+
JSON.stringify(createProjectTsconfig(root, true)),
589+
);
590+
writeFileSync(
591+
join(root, 'check-upgrade.ts'),
592+
`import { routeUrl, type Href } from 'void/routes';
593+
const href: Href = routeUrl('/users/[id]', { params: { id: 42 } });
594+
routeUrl('/about');
595+
// @ts-expect-error unknown route patterns remain checked
596+
routeUrl('/missing');
597+
// @ts-expect-error required params remain checked
598+
routeUrl('/users/[id]');
599+
// @ts-expect-error param values remain checked
600+
routeUrl('/users/[id]', { params: { id: true } });
601+
// @ts-expect-error raw URLs retain their existing route checks
602+
const unknown: Href = '/missing';
603+
void [href, unknown];
604+
`,
605+
);
606+
expect(typecheck(root, declarations, ['check-upgrade.ts'])).toEqual({
607+
output: '',
608+
status: 0,
609+
});
610+
611+
rmSync(packageLink, { force: true, recursive: true });
612+
symlinkSync(newSdk, packageLink, 'junction');
613+
rmSync(oldSdk, { force: true, recursive: true });
614+
615+
expect(readFileSync(routeTypesPath, 'utf8')).toBe(generation.stdout);
616+
expect(typecheck(root, declarations, ['check-upgrade.ts'])).toEqual({
617+
output: '',
618+
status: 0,
619+
});
620+
},
621+
);
622+
525623
it.each(['generated', 'package'] as const)(
526624
'rejects invalid unions and numeric keys through the %s declarations',
527625
async (declarations) => {

0 commit comments

Comments
 (0)