@dudousxd/nestjs-codegen

中文文档
0.30.0发布于 2026/11/12MITTypeScriptESM + CJS构建来源已验证近 30 天下载 4339

Extensible codegen core for NestJS: class-validator/contract discovery → neutral schema IR → pluggable validation adapters.

原始使用文档
文档目录8 节
  1. Install
  2. Quick start
  3. What it generates
  4. Extensions & the ecosystem
  5. Generative-UI sandbox kit (built in)
  6. CLI
  7. Documentation
  8. License

@dudousxd/nestjs-codegen

Extensible typed-client codegen for NestJS — routes, API client, and validation schemas.

npm

nestjs-codegen reads your NestJS controllers, defineContract contracts, DTOs, and (optionally) Inertia pages via ts-morph, builds a neutral schema IR, and emits a fully-typed client: a route map, a Tuyau-style API client, and client-side validation schemas. It works with or without Inertia.js, and every moving part is pluggable — the validation library, the HTTP client, the serializer, and the query layer.

Install

pnpm add -D @dudousxd/nestjs-codegen
# a validation adapter (no adapter is bundled in core) — zod shown; or -valibot / -arktype:
pnpm add -D @dudousxd/nestjs-codegen-zod
# the runtime the generated client imports its Fetcher type from:
pnpm add @dudousxd/nestjs-client

The generated api.ts imports its Fetcher type from @dudousxd/nestjs-client, so it's a real runtime dependency. @nestjs/common, tsx, and typescript are peer dependencies (@nestjs/common and tsx are optional — your Nest app already has them).

Quick start

Import NestjsCodegenModule into your root module. The codegen starts with your dev server and regenerates the client as you edit your controllers — no config file, no extra process. The watcher is a dev/CI concern, so the module skips itself automatically when NODE_ENV === 'production'.

import { Module } from '@nestjs/common';
import { NestjsCodegenModule } from '@dudousxd/nestjs-codegen/nest';
import { zodAdapter } from '@dudousxd/nestjs-codegen-zod';

@Module({
  imports: [
    NestjsCodegenModule.forRoot({
      // controllers to scan for routes + contracts
      contracts: { glob: 'src/**/*.controller.ts' },
      // output directory for the generated files
      codegen: { outDir: 'src/generated' },

      validation: zodAdapter, // zodAdapter | valibotAdapter | arktypeAdapter
    }),
  ],
})
export class AppModule {}

The generated api.ts exports a createApi(fetcher) factory — create the client once, injecting your fetcher. Each endpoint is a unified awaitable handle: a lazy, memoized Promise — await it (or return it from a TanStack mutationFn) to run the request.

import { createApi } from '../generated/api';
import { createFetcher } from '@dudousxd/nestjs-client';

export const api = createApi(createFetcher({ baseUrl: '/api' }));

const users = await api.users.list();              // typed User[]
const created = await api.users.create({ body });  // typed body + response

What it generates

The codegen writes these files into your output directory:

  • routes.ts — a ROUTES map, a RouteName union, and a typed route() helper.
  • api.ts — a Tuyau-style createApi(fetcher) factory, nested by route name, fully typed.
  • forms.ts — a validation schema per validated endpoint, in your chosen lib.
  • pages.d.ts / components.json — when the Inertia integration is enabled.

Extensions & the ecosystem

Everything beyond the core is pluggable. Extensions are registered via extensions: [...]; validation adapters via validation: ....

import { NestjsCodegenModule } from '@dudousxd/nestjs-codegen/nest';
import { tanstackQuery } from '@dudousxd/nestjs-codegen-tanstack';
import { nestjsFilterCodegen } from '@dudousxd/nestjs-filter-codegen';
import { nestjsInertiaCodegen } from '@dudousxd/nestjs-inertia-codegen-extension';
import { arktypeAdapter } from '@dudousxd/nestjs-codegen-arktype';

NestjsCodegenModule.forRoot({
  contracts: { glob: 'src/**/*.controller.ts' },
  codegen: { outDir: 'src/generated' },
  validation: arktypeAdapter, // render the IR as arktype instead of zod
  extensions: [tanstackQuery(), nestjsFilterCodegen(), nestjsInertiaCodegen()],
});

When the TanStack extension is registered, every generated leaf becomes a unified handle: await api.users.show({ params }) performs the request, and the same handle carries .queryOptions() / .mutationOptions() / .infiniteQueryOptions() / .queryKey().

import { useQuery, useMutation } from '@tanstack/react-query';
import { api } from '../lib/api';

const users = useQuery(api.users.list().queryOptions());
const create = useMutation(api.users.create().mutationOptions());
PackageWhat it adds
@dudousxd/nestjs-clientThe runtime fetcher — createFetcher with a pluggable transport (native fetch or axiosTransport()) and a superjson hook. Pass it to createApi(fetcher).
@dudousxd/nestjs-codegen-tanstackTanStack Query helpers (queryOptions / mutationOptions / infiniteQueryOptions / queryKey) on each generated leaf. Registered as an extension.
@dudousxd/nestjs-codegen-arktypeValidation adapter — render forms.ts as arktype. Pass via validation: arktypeAdapter.
@dudousxd/nestjs-codegen-valibotValidation adapter — render forms.ts as valibot. Pass via validation: valibotAdapter.
@dudousxd/nestjs-filter-codegenExtension — typed filterQuery helpers from the nestjs-filter repo.
@dudousxd/nestjs-inertia-codegen-extensionExtension — Inertia router / navigate output from the nestjs-inertia repo.

Generative-UI sandbox kit (built in)

genuiSandboxKit() writes the sandbox-kit docs that @dudousxd/nestjs-agent uses to render generative UI in your design system. The names and props of your components (read from their TypeScript types), and what your styles offer, go to .genui/sandbox-kit.json. That happens on every generate and in watch mode. It needs the optional peer @dudousxd/nestjs-agent-core (>=0.53.0).

import { defineConfig, genuiSandboxKit } from '@dudousxd/nestjs-codegen';

export default defineConfig({
  contracts: { glob: 'src/**/*.controller.ts' },
  codegen: { outDir: 'src/generated' },
  extensions: [genuiSandboxKit({ components: 'web/components/ui', styles: 'web/styles/globals.css' })],
});

Options (the same as the agent's Vite plugin):

  • components (default: the first shadcn components/ui dir found), styles, wrapper, stylesDocs, tsconfig;
  • output (default .genui/sandbox-kit.json, relative to the app cwd);
  • generate, to inject the docs writer.

Write your own against the @dudousxd/nestjs-codegen/extension contract:

import { defineExtension, type CodegenExtension } from '@dudousxd/nestjs-codegen/extension';

CLI

For CI (before deploy) you want a one-shot, watch-free run that fails the build if the committed client has drifted. The package ships the nestjs-codegen CLI:

npx nestjs-codegen codegen   # one-shot generate (CI); pair with `git diff --exit-code`
npx nestjs-codegen init      # scaffold nestjs-codegen.config.ts
npx nestjs-codegen doctor    # diagnose your setup

The CLI reads nestjs-codegen.config.ts (the legacy nestjs-inertia.config.ts name is still accepted) from your project root. Keep a single source of truth by authoring options with defineConfig and importing them into forRoot():

import { defineConfig } from '@dudousxd/nestjs-codegen';
import { zodAdapter } from '@dudousxd/nestjs-codegen-zod';

export default defineConfig({
  contracts: { glob: 'src/**/*.controller.ts' },
  codegen: { outDir: 'src/generated' },
  validation: zodAdapter,
});
npx nestjs-codegen codegen
git diff --exit-code src/generated   # non-zero if the client is stale

Documentation

Full docs — getting started, configuration, validation adapters, the fetcher, and the extension contract — live in the repo:

License

MIT

版本与发布记录

@dudousxd/nestjs-codegen 共发布 48 个版本,首次发布于 2026/06/11;过去 12 个月发布了 48 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.30.0latest2026/11/121 天前
0.29.12026/10/2717 天前
0.29.02026/10/111 个月前
0.28.02026/10/111 个月前
0.27.22026/09/301 个月前
0.27.12026/09/301 个月前

关于 @dudousxd/nestjs-codegen 的常见问题

@dudousxd/nestjs-codegen 是什么?

Extensible codegen core for NestJS: class-validator/contract discovery → neutral schema IR → pluggable validation adapters.

如何安装 @dudousxd/nestjs-codegen?

在项目目录运行 npm install @dudousxd/nestjs-codegen,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(>=5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 nestjs-codegen,可以用 npx nestjs-codegen 直接运行。

@dudousxd/nestjs-codegen 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dudousxd/nestjs-codegen 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dudousxd/nestjs-codegen 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dudousxd/nestjs-codegen 可以商用吗?

@dudousxd/nestjs-codegen 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dudousxd/nestjs-codegen 的下载量有多少?

根据 npm 官方下载统计,@dudousxd/nestjs-codegen 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,339 次,日均约 145 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dudousxd/nestjs-codegen 还在维护吗?

最新版本 0.30.0 发布于 2026/11/12(1 天前);至今共发布 48 个版本,其中过去 12 个月发布了 48 个;首次发布于 2026/06/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。