Swagger for Typescript-rest

1.1.7 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a simple `typescript-rest` service enhanced with `typescript-rest-swagger` decorators (`@Tags`, `@Response`) and JSDoc for OpenAPI documentation. To generate the `swagger.json` file: 1. Install `typescript-rest` and `reflect-metadata` in your project (`npm i typescript-rest reflect-metadata`). 2. Globally install `typescript-rest-swagger` (`npm i -g typescript-rest-swagger`). 3. Create a `swaggerConfig.json` (e.g., `{ "swagger": { "outputDirectory": "./dist", "entryFile": "./src/services/MyService.ts" } }`). 4. Ensure `tsconfig.json` has `"experimentalDecorators": true`, `"emitDecoratorMetadata": true`, and appropriate `"lib"` settings (e.g., `"es2018", "dom"`). 5. Run `swaggerGen -c ./swaggerConfig.json -p ./tsconfig.json`.

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
    }
}

view raw JSON →