Type-safe API and message generator for TypeScript projects (NestJS-tested).
npm install @gentleduck/genDuck Gen scans your TypeScript server code and generates type-safe route maps and
message registries. It is currently tested with NestJS.
``bash`
pnpm add -D @gentleduck/gen
Create a duck-gen.json at your project root:
`json`
{
"$schema": "node_modules/@gentleduck/gen/duck-gen.schema.json",
"framework": "nestjs",
"extensions": {
"shared": {
"includeNodeModules": false,
"outputSource": "./generated",
"sourceGlobs": ["src//.ts", "src//.tsx"],
"tsconfigPath": "./tsconfig.json"
},
"apiRoutes": {
"enabled": true,
"globalPrefix": "/api",
"normalizeAnyToUnknown": true,
"outputSource": ["./generated", "./src/generated"]
},
"messages": {
"enabled": true,
"outputSource": "./generated"
}
}
}
Add a message group tagged for Duck Gen:
`ts`
/**
* @duckgen messages
*/
export const AuthMessages = [
'AUTH_SIGNIN_SUCCESS',
'AUTH_SIGNIN_FAILED',
] as const
Run the generator:
`bash`
pnpm exec duck-gen
Import generated types:
`ts`
import type {
ApiRoutes,
DuckGenI18nMessages,
DuckgenScopedI18nByGroup,
} from '@gentleduck/gen/nestjs'
Duck Gen writes type definitions to @gentleduck/gen/generated/ and@gentleduck/gen/nestjs
exposes them via framework entrypoints like .extensions.apiRoutes.outputSource
You can override the output file per feature by setting or extensions.messages.outputSource induck-gen.json (paths resolve relative to the config file). Outputs are alwaysgenerated
written to the package folder; outputSource adds extra outputextensions.shared.outputSource
files or directories. Use to add shared output
directories. When you customize outputs, import types from those files directly
instead of the package entrypoints.
Generated files include:
- duck-gen-api-routes.d.tsduck-gen-messages.d.ts
- index.d.ts
-
- If duck-gen.json is missing, defaults are used.as const` so keys are literal types.
- Run the CLI from the project root so paths resolve correctly.
- Message arrays should be