Swagger for Typescript-rest
typescript-rest-swagger is a utility designed to generate OpenAPI (Swagger) specification files directly from projects built with the typescript-rest framework. It analyzes typescript-rest decorators, along with its own set of typescript-rest-swagger decorators (like @Tags, @Response, @Example), and JSDoc comments to construct comprehensive API documentation. The current stable version is 1.1.7, with recent releases focusing on bug fixes and dependency updates, indicating a maintenance-oriented release cadence. Its primary differentiator is its deep integration with typescript-rest, streamlining the documentation process for services developed using that specific REST framework by leveraging existing code annotations rather than requiring a separate definition language.
Common errors
-
'swaggerGen' is not recognized as an internal or external command, operable program or batch file.
cause The `swaggerGen` CLI tool was either not installed globally, or its installation directory is not in your system's PATH.fixRun `npm install -g typescript-rest-swagger` to install it globally, or use `npx typescript-rest-swagger` to execute the local package without global installation. -
Error: Cannot find module 'typescript-rest' from '[path]'
cause The project that `swaggerGen` is trying to analyze does not have `typescript-rest` installed as a dependency.fixInstall `typescript-rest` in your project: `npm install typescript-rest` (or `yarn add typescript-rest`). Also ensure `reflect-metadata` is installed and imported once at your application's entry point. -
[TS-REST-SWAGGER] Error: Type error when generating swagger doc
cause An internal type resolution issue during the Swagger generation process. This was a common bug in earlier versions of the library.fixUpdate `typescript-rest-swagger` to version 1.1.4 or newer: `npm install typescript-rest-swagger@latest`. If the issue persists, review your service types for potential complexities or circular dependencies. -
Error: [TS-REST-SWAGGER] Could not resolve entry file: [path]
cause The `entryFile` path specified in `swaggerConfig.json` is incorrect, or the `tsconfig.json` (if used with `-p`) does not correctly resolve the path, especially with `baseUrl` or `paths` configurations.fixVerify the `entryFile` path in `swaggerConfig.json`. If using `tsconfig.json`, ensure its `compilerOptions.baseUrl` and `compilerOptions.paths` are correctly configured for your project's module resolution.
Warnings
- gotcha Installing 'typescript-rest-swagger' globally via `npm install -g` can lead to version mismatches or conflicts if your project's local dependency on `typescript-rest-swagger` or `typescript-rest` differs.
- breaking This tool relies heavily on the internal structure and decorators of 'typescript-rest'. Major version updates in 'typescript-rest' (e.g., from v2 to v3) might introduce breaking changes that prevent accurate Swagger generation until 'typescript-rest-swagger' is updated to support them.
- gotcha Correct configuration of 'tsconfig.json' is crucial, especially for `experimentalDecorators`, `emitDecoratorMetadata`, `lib`, `baseUrl`, and `paths`. Missing or incorrect settings can lead to compilation errors or an inability to resolve project imports.
- gotcha The generated OpenAPI specification heavily depends on JSDoc comments on classes, methods, and parameters. Incomplete or malformed JSDoc might result in missing or inaccurate documentation in the final Swagger file.
Install
-
npm install typescript-rest-swagger -
yarn add typescript-rest-swagger -
pnpm add typescript-rest-swagger
Imports
- swaggerGen
import { swaggerGen } from 'typescript-rest-swagger'npx swaggerGen -c ./swaggerConfig.json -p ./tsconfig.json
- Tags
import { Tags } from 'typescript-rest'import { Tags } from 'typescript-rest-swagger' - Response
import { Response } from 'typescript-rest'import { Response } from 'typescript-rest-swagger' - IsInt
import { IsInt } from 'class-validator'import { IsInt } from 'typescript-rest-swagger'
Quickstart
import { Path, GET, QueryParam } from 'typescript-rest';
import { Tags, Response } from 'typescript-rest-swagger';
interface Person { name: string; age: number; }
/**
* Service for managing people.
* @hidden
*/
@Path('people')
export class PeopleService {
/**
* Retrieves a list of people.
* @param minAge Optional: Filter by minimum age.
*/
@GET
@Tags('UserManagement', 'ReadOperations')
@Response<Person[]>(200, 'Successfully retrieved people list')
@Response<{ message: string }>(400, 'Invalid input for minAge')
async getPeople(@QueryParam('minAge') minAge?: number): Promise<Person[]> {
return [{ name: 'Alice', age: 30 }]; // Example data
}
}