CLIKontractGenerators
← Generators

Server · OpenAPI

Bind a NestJS server to the schema.

The largest generator. Emits the application-facing boundary and, with the matching options, the validation and response foundation it depends on.

What it emits

Models and DTOsOperation descriptorsOne target-wide shared foundation and per-service Swagger facadesControllersTyped errorsMappersTestsBruno requests

Options

Options this generator adds on top of its base set. Set them under generator: in the target.

OptionDescriptionValuesDefault
dtoDTO technology emitted alongside models. Zod DTO constructor constants receive deterministic runtime class names matching their exported symbols, so distinct schemas remain distinct in NestJS and Swagger. `operationContract` needs a value other than `none`.none | zod | class-validator | bothnone
operationContractEmit one exact operation descriptor and generated `@ContractOperation(Operation)` decorator per route, plus operation-owned named and aggregate parameter metadata. For JSON operations, the decorator invokes native nestjs-zod response metadata and preserves the handler return constraint.true | falsefalse
foundationEmit exactly one target-wide `shared/http/foundation.ts` with the only `ContractModule`, canonical filter, parameter pipes, and DI-preserving global providers. Each service `http/foundation.ts` re-exports that boundary and keeps only service-specific Swagger configuration and parameter-schema cleanup.true | falsefalse
controllersThin generated transport adapters with schema-supported parameter coercion, exact named or aggregate parameter types, and an application-owned port.true | falsefalse
errorsTyped exceptions for documented non-success JSON responses.true | falsefalse
mappersExplicit one-to-one structural projections, with no inferred business transformations.true | falsefalse
testsProduction-parity test setup, operation manifests, and OpenAPI parity helpers.true | falsefalse
brunoBruno requests and a coverage manifest keyed by exact operation identity.true | falsefalse
swaggerSwagger metadata cleaned to match the generated operation surface.true | falsefalse
runtimeRuntime descriptors for the generated operations.true | falsefalse
sdkA nested SDK that reuses the server-owned models without depending on decorators.true | falsefalse
frameworkHTTP adapter the generated foundation targets. Requires the matching `@nestjs/platform-*` package.express | fastifyexpress
versioningNestJS versioning strategy applied to generated routes and operation metadata. URI route linting supports controller versions, method overrides, VERSION_NEUTRAL, and the generated default.uri | header | media-typeunset
defaultVersionVersion applied when an operation does not declare one.stringunset
envelopeWrap successful responses in a fixed shape. Set `dataKey`, and optionally `metaKey`, `staticFields`, and `contextFields` for path and timestamp.objectunset (no envelope)
errorEnvelopeThe default error policy is flat. For nested or custom documented errors, map the complete projection: `bodyKey`, required outer `staticFields`, and the core status/message/type/code/details keys. Requires `errors: true`; `detailsMode` selects structured issues or raw validation output.object (detailsMode: issues-object | raw)unset (flat policy)
envelopeDataKeyDeprecated. Use `envelope.dataKey`; the two cannot be combined.stringunset

Shared options

Every OpenAPI generator accepts these, so a schema choice made once stays consistent between a server and its clients.

OptionDescriptionValuesDefault
audiencesApply operation `x-kontract-audiences` policy. `exclude` is validated once against the complete selected OpenAPI source/target universe, not per generated service: siblings with no matching audience generate normally, while a token absent from the whole source fails validation and `generate --clean` preflight before deletion. NestJS may instead set `marker: true` to retain and annotate output.object (exclude: string[]; marker: true for typescript-nestjs)unset (operations belong to every audience)
zodEmit framework-neutral Zod schemas alongside the inferred request and response types.true | falsefalse
schemaModeHow faithfully the source schema is reproduced. `normalize` reshapes inline schemas into named components; `strict` keeps the document as authored.normalize | strictstrict for typescript-nestjs, normalize for SDK targets
useSingleRequestParameterCollapse an operation's parameters into one request object instead of a positional argument list.true | falsefalse
stringEnumsEmit enums as string union types rather than TypeScript `enum` declarations.true | falsefalse
enumUnknownDefaultCaseAdd a fallback member so an unrecognised wire value does not throw at the boundary.true | falsefalse
modelPropertyNamingCasing applied to model properties.camelCase | PascalCase | snake_case | originaloriginal
paramNamingCasing applied to operation parameters.camelCase | PascalCase | snake_case | originaloriginal
enumPropertyNamingCasing applied to enum members.PascalCase | camelCase | snake_case | UPPERCASE | originaloriginal
sortModelPropertiesByRequiredFlagOrder model properties with required ones first.true | falsefalse
sortParamsByRequiredFlagOrder operation parameters with required ones first.true | falsefalse

Example

type: typescript-nestjsenables

One generated @ContractOperation decorator binds the route, request type, response type, native JSON runtime metadata, and handler return constraint to the same schema operation. The ESLint rule rejects drift and redundant response decorators.

sumr.yamlyaml
targets:
  - name: api-nestjs
    output: ./apps/api/src/contracts
    generator:
      type: typescript-nestjs
      dto: both
      operationContract: true
      sdk: true
      zod: true
recipes.controller.tsts
@Controller('recipes')
export class RecipesController {
  @Get(':recipeId')
  @ContractOperation(GetRecipe)
  getRecipe(@Param('recipeId') recipeId: string): Promise<GetRecipePayload> {
    return this.recipes.getRecipe(recipeId);
  }
}

Prerequisites

Generation fails when one of these is unmet, so check here first when a target stops producing output.

OptionRequiresConflicts with
operationContractdto other than none
controllersfoundation: true
testscontrollers and foundation
errorEnvelopeerrors: true
envelopeDataKeyenvelope

Related

On this page