API Reference
defineConfig()
Type-safe configuration helper for nestjs-openapi
A helper function that provides TypeScript type inference for configuration objects. It's a pass-through function that returns the config unchanged but enables IDE autocomplete and type checking.
Signature
function defineConfig(config: Config): Config;Parameters
| Parameter | Type | Description |
|---|---|---|
config | Config | Configuration object |
Returns
Config - The same configuration object (unchanged).
Example
Basic Usage
import { } from 'nestjs-openapi';
export default ({
: 'openapi.json',
: { : 'src/app.module.ts' },
: {
: {
: 'My API',
: '1.0.0',
},
},
});Full Configuration
import { } from 'nestjs-openapi';
export default ({
: 'src/openapi/spec.json',
: 'json',
: {
: 'src/app.module.ts',
: 'tsconfig.json',
: [
'src/**/*.dto.ts',
'src/**/*.entity.ts',
'src/**/*.model.ts',
'src/**/*.schema.ts',
],
: ['**/*.spec.ts', '**/*.test.ts'],
},
: {
: {
: 'My API',
: '1.0.0',
: 'API description',
: {
: 'Support',
: 'support@example.com',
: 'https://support.example.com',
},
: {
: 'MIT',
: 'https://opensource.org/licenses/MIT',
},
},
: [
{ : 'https://api.example.com', : 'Production' },
{ : 'http://localhost:3000', : 'Development' },
],
: [
{ : 'users', : 'User operations' },
{ : 'posts', : 'Post operations' },
],
: {
: [
{
: 'bearerAuth',
: 'http',
: 'bearer',
: 'JWT',
},
],
: [{ : [] }],
},
},
: {
: '/api/v1',
: true,
: ['ApiExcludeEndpoint', 'Internal'],
: /^(?!.*\/internal\/).*/,
},
});Sharing config across files
extends is defined in the types but not executed by the Promise-based generator. Use manual composition instead (see Config Inheritance).
Without defineConfig
You can export a plain object, but you lose type checking:
// Works but no autocomplete or type checking
export default {
output: 'openapi.json',
openapi: {
info: {
title: 'My API',
version: '1.0.0',
},
},
};
// Or use type annotation
import type { Config } from 'nestjs-openapi';
const config: Config = {
output: 'openapi.json',
openapi: {
info: {
title: 'My API',
version: '1.0.0',
},
},
};
export default config;Config Structure
Required Fields
defineConfig({
// Where to write the generated spec (required)
output: 'openapi.json',
// OpenAPI metadata (required)
openapi: {
info: {
title: 'My API', // required
version: '1.0.0', // required
},
},
});Optional Fields
defineConfig({
output: 'openapi.json',
// Output format (default: 'json')
format: 'json', // or 'yaml'
// Input files
files: {
entry: 'src/app.module.ts', // default
tsconfig: 'tsconfig.json', // generator will try to find it if omitted
dtoGlob: ['src/**/*.dto.ts', 'src/**/*.entity.ts', '...'],
include: ['src/**/*.ts'],
exclude: ['**/*.spec.ts'],
},
openapi: {
info: {
title: 'My API',
version: '1.0.0',
description: 'Optional description',
contact: {
/* optional */
},
license: {
/* optional */
},
},
servers: [
/* optional */
],
tags: [
/* optional */
],
security: {
/* optional */
},
},
// Generation options
options: {
basePath: '/api',
extractValidation: true, // default
excludeDecorators: ['ApiExcludeEndpoint'],
pathFilter: /regex/ /* or function */,
schemas: {
aliasRefs: 'collapse', // default
},
},
});If a referenced schema isn't generated from your dtoGlob patterns, the
generator will attempt to resolve and merge it automatically. The CLI warns if
any broken refs remain.
Type Inference
defineConfig enables full TypeScript type inference:
import { defineConfig } from 'nestjs-openapi';
export default defineConfig({
output: 'openapi.json',
openapi: {
security: {
schemes: [
{
name: 'auth',
type: 'http', // Autocomplete: 'http' | 'apiKey' | 'oauth2' | 'openIdConnect'
scheme: 'bearer', // Only valid when type is 'http'
// in: 'header', // TypeScript error: 'in' only valid for 'apiKey'
},
],
},
},
});See Also
- Configuration Guide - Detailed config options
- Config Types - TypeScript type definitions
- generate() - Generation function