Install: @travetto/openapi
npm install @travetto/openapi
# or
yarn add @travetto/openapi
In the RESTful API module, the controllers and endpoints can be described via decorators, comments, or typings. This only provides the general metadata internally. This is not sufficient to generate a usable API doc, and so this module exists to bridge that gap.
The module is provides an OpenAPI v3.x representation of the API metadata provided via the RESTful API and Schema modules.
By installing the dependency, the OpenAPI endpoint is automatically generated and exposed at the root of the application as /openapi.yml
or /openapi.json
(by default).
All of the high level configurations can be found in the following structure:
Code: Config: OpenAPI Configuration
import path from 'node:path';
import type { ServerObject, ContactObject, LicenseObject } from 'openapi3-ts/oas31';
import { Config } from '@travetto/config';
import { Runtime } from '@travetto/runtime';
import { Required } from '@travetto/schema';
/**
* API Information, infers as much as possible from the package.json
*/
@Config('api.info')
export class ApiInfoConfig {
@Required(false)
contact: ContactObject;
@Required(false)
description?: string;
@Required(false)
license: LicenseObject;
@Required(false)
termsOfService?: string;
@Required(false)
title: string;
@Required(false)
version: string;
postConstruct(): void {
this.title ??= Runtime.main.name;
this.version ??= Runtime.main.version;
this.description ??= Runtime.main.description;
}
}
/**
* The API host, infers from rest host configuration
*/
@Config('api.host')
export class ApiHostConfig {
/**
* List of servers
*/
servers?: ServerObject[];
/**
* OpenAPI Version
*/
openapi = '3.0.0';
}
/**
* The spec file configuration
*/
@Config('api.spec')
export class ApiSpecConfig {
/**
* Where to output file to
*/
output: string = 'openapi.yml';
/**
* Should file be generated at runtime
*/
persist?: boolean;
/**
* Skip emitting all routes
*/
skipRoutes: boolean = false;
/**
* Expose all schemas, even if not referenced
*/
exposeAllSchemas: boolean = false;
async postConstruct(): Promise<void> {
if (!this.output || this.output === '-') {
this.persist = false;
} else {
this.output = path.resolve(Runtime.mainSourcePath, this.output);
this.persist ??= Runtime.dynamic;
}
if (this.persist) {
if (!/[.](json|ya?ml) $/.test(this.output)) { // Assume a folder
this.output = path.resolve(this.output, 'openapi.yml');
}
}
}
}
The framework, when in watch mode, will generate the OpenAPI specification in either JSON or YAML. This module integrates with the file watching paradigm and can regenerate the openapi spec as changes to endpoints and models are made during development. The output format is defined by the suffix of the output file, .yaml
or .json
.
The module provides a command for the Command Line Interface to allow scripting file generation.
Terminal: OpenAPI usage
$ trv openapi:spec --help
Usage: openapi:spec [options]
Options:
-o, --output <string> Output files
-m, --module <module> Module to run for
-h, --help display help for command
The command will run your application, in non-server mode, to collect all the routes and model information, to produce the openapi.yml
. Once produced, the code will store the output in the specified location.
Note: The module supports generating the OpenAPI spec in real-time while listening for changes to routes and models.
The module provides a command for the Command Line Interface to allow client generation from the API structure.
Terminal: OpenAPI usage
$ trv openapi:client --help
Usage: openapi:client [options] <format:string>
Options:
-x, --extended-help Show Extended Help
-a, --additional-properties <string> Additional Properties (default: [])
-i, --input <string> Input file (default: "./openapi.yml")
-o, --output <string> Output folder (default: "./api-client")
-d, --docker-image <string> Docker Image to user (default: "openapitools/openapi-generator-cli:latest")
-h, --help display help for command
This tool relies upon a custom build of OpenAPI client generation tools, which supports watching. This allows for fast responsive client generation as the shape of the API changes.