Aviary
Packages

@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-extension
npm install --save-dev @dudousxd/nestjs-codegen @dudousxd/nestjs-inertia-codegen-extension
yarn 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

ExportFromDescription
nestjsInertiaCodegen()@dudousxd/nestjs-inertia-codegen-extensionThe extension factory. Register it in extensions: [...].
NestjsCodegenModule.forRoot(...)@dudousxd/nestjs-codegen/nestRegisters the codegen (and its extensions) on the Nest module.
defineConfig(config)@dudousxd/nestjs-codegenType-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:

src/app.module.ts
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:

nestjs-codegen.config.ts
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 routerimport { router } from '@inertiajs/react';
  • a NavigateOptions type (method, data, preserveState, preserveScroll, replace)
  • a type-safe navigate() helper that resolves a named route to its URL and calls router.visit()
import { navigate } from '~codegen/api';

navigate('users.show', { params: { id: '42' } });   // typed route + params
navigate('users.list', { preserveState: true });     // with Inertia visit options

Without 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:

FileContains
pages.d.tsModule augmentation for typed @Inertia() and page props (from pages.glob)
routes.tsTyped route map + route() helper for URL resolution
api.tsThe 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 (Datestring); 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.

On this page