@dudousxd/nestjs-inertia-codegen-extension
The Inertia extension for @dudousxd/nestjs-codegen — adds a typed navigate() helper to the generated api.ts.
Codegen now lives in @dudousxd/nestjs-codegen
The typed-client codegen was extracted into the standalone @dudousxd/nestjs-codegen package. This repo ships only the Inertia extension for it. Install both: the core codegen does discovery → IR → emit, and nestjsInertiaCodegen() adds the Inertia surface.
pnpm add -D @dudousxd/nestjs-codegen @dudousxd/nestjs-inertia-codegen-extensionnpm install --save-dev @dudousxd/nestjs-codegen @dudousxd/nestjs-inertia-codegen-extensionyarn add -D @dudousxd/nestjs-codegen @dudousxd/nestjs-inertia-codegen-extension@dudousxd/nestjs-codegen is a static-analysis tool that reads your NestJS controllers and emits a fully typed TypeScript client. @dudousxd/nestjs-inertia-codegen-extension is the extension you register so the generated api.ts gains an Inertia router import and a type-safe navigate() helper. Inertia page discovery (pages.d.ts / components.json) and shared props are driven by the core pages / app config.
Exports
| Export | From | Description |
|---|---|---|
nestjsInertiaCodegen() | @dudousxd/nestjs-inertia-codegen-extension | The extension factory. Register it in extensions: [...]. |
NestjsCodegenModule.forRoot(...) | @dudousxd/nestjs-codegen/nest | Registers the codegen (and its extensions) on the Nest module. |
defineConfig(config) | @dudousxd/nestjs-codegen | Type-safe config helper for nestjs-codegen.config.ts (CLI/CI). |
The codegen CLI (nestjs-codegen), watch mode, and doctor are all provided by @dudousxd/nestjs-codegen — see its docs for the full command and config reference.
Setup
Register the extension in the codegen's extensions: [...] array. Either via the Nest module:
import { NestjsCodegenModule } from '@dudousxd/nestjs-codegen/nest';
import { nestjsInertiaCodegen } from '@dudousxd/nestjs-inertia-codegen-extension';
NestjsCodegenModule.forRoot({
contracts: { glob: 'src/**/*.controller.ts' },
pages: { glob: 'inertia/pages/**/*.tsx' },
app: { moduleEntry: 'src/app.module.ts' },
codegen: { outDir: '.nestjs-inertia' },
extensions: [nestjsInertiaCodegen()],
});…or in nestjs-codegen.config.ts for CLI/CI runs — the same array works in both:
import { defineConfig } from '@dudousxd/nestjs-codegen';
import { nestjsInertiaCodegen } from '@dudousxd/nestjs-inertia-codegen-extension';
export default defineConfig({
contracts: { glob: 'src/**/*.controller.ts' },
pages: { glob: 'inertia/pages/**/*.tsx' },
app: { moduleEntry: 'src/app.module.ts' },
codegen: { outDir: '.nestjs-inertia' },
extensions: [nestjsInertiaCodegen()],
});What the extension adds
nestjsInertiaCodegen() is a CodegenExtension (name: 'nestjs-inertia') whose apiHeader hook contributes to the generated api.ts:
- an import of Inertia's
router—import { router } from '@inertiajs/react'; - a
NavigateOptionstype (method,data,preserveState,preserveScroll,replace) - a type-safe
navigate()helper that resolves a named route to its URL and callsrouter.visit()
import { navigate } from '~codegen/api';
navigate('users.show', { params: { id: '42' } }); // typed route + params
navigate('users.list', { preserveState: true }); // with Inertia visit optionsWithout the extension, api.ts is a plain typed-fetch client and @inertiajs/react's router is never imported.
Generated files
The core codegen emits into codegen.outDir:
| File | Contains |
|---|---|
pages.d.ts | Module augmentation for typed @Inertia() and page props (from pages.glob) |
routes.ts | Typed route map + route() helper for URL resolution |
api.ts | The typed client; with this extension, also the Inertia router import + navigate() |
Response types are inferred from Awaited<ReturnType<import(...)>> and wrapped in Jsonify<...> by default so they match the JSON wire shape (Date → string); set serialization: 'superjson' to emit the raw types instead. Page props use Parameters<typeof import(...).default>[0]. See Serialized response types.
See the Codegen guide for full configuration and the Typed Client guide for the generated client API.