Node decorators - express-openapi
openapi
decorators and swagger-ui implementation extending @decorators/express
Installation
npm install @decorators/express-openapi
API
Functions
enableOpenApi(app: express.Application, options: OpenApiOptions = {}): Promise<void>
Initiates the openapi document and attaches it to the application.
Params::
app | express.Application | | The application object |
options | object | | Options to configure swagger ui and the openapi document itself |
options.serveInPath | string | - optional
- Default:
/api-docs
| The url where the swagger-ui will be served |
options.info | object | | An object that represents the info on the openapi document. For more info see https://swagger.io/docs/specification/basic-structure/ |
options.info.title | string | - optional
- Default: package name taken from your package.json
| |
options.info.description | string | - optional
- Default: package description taken from your package.json
| |
options.info.version | string | - optional
- Default: package version taken from your package.json
| |
options.tags | object[] | | List of tags following the openapi specifications |
options.tags.[*].name | string | | The tag name. |
options.tags.[*].description | string | | The tag description |
options.servers | object[] | | List of servers following the openapi specifications. See https://swagger.io/docs/specification/api-host-and-base-path/ |
options.servers[*].url | string | | |
options.servers[*].description | string | | |
options.externalDocs | object | | External documents definition following the openapi specifications. |
options.externalDocs.url | string | | |
options.externalDocs.description | string | | |
options.security | object[] | | Security schemes to be applied to the api |
options.components.securitySchemes | object | | An object that represents the components.securitySchemes on the openapi document. For more info see https://swagger.io/docs/specification/authentication/ |
registerSchema(name: string, schema: SchemaDef): Promise<void>
Defines a schema on the openapi document
Params:
Decorators
Class Decorators
@WithDefinitions(options: WithDefinitionsOpts)
Enables openapi definitions for a controller class (using @Controller()
from @decorators/express
)
Params:
options | object | | |
options.tags | string[] | | Tags to be applied to all routes on the controller |
options.security | object[] | | Security schemes to be applied to all routes on the controller |
options.responses | object | | Tags to be applied to all routes on the controller |
options.basePath | string | | The base path for all routes on the controller |
@Schema(name?: string)
Defines a new schema on the openapi document. Internally uses registerSchema
.
Params:
name | string | - optional
- Default: The class name
| The name of the schema |
Method Decorators - Route documentation
@Summary(summary: string)
Defines the summary of the operation
Params:
summary | string | | The operation's summary |
@Description(description: string)
Defines the description of the operation
Params:
description | string | | The operation's description |
@Param(name: string, location: ParamLocation, schema: SchemaDef, options: ParamOptions)
Adds a param definition to the operation
Params:
name | string | | The parameter name |
location | string | - oneOf:
query , header , path or cookie
| Where the parameter is located |
schema | object | | A schema definition following openapi specifications |
options | object | | Options for the parameter following openapi specifications |
options.description | string | | |
options.required | boolean | | |
options.deprecated | boolean | | |
options.allowEmptyValue | boolean | | |
options.contentMediaType | string | | Media type definition complying with RFC 6838. If present, schema is rendered under content |
Note:
When using @Query()
, @Params()
, @Headers()
or @Cookies()
from @decorators/express
defining
the name attribute, a basic parameter definition is automatically added to the openapi document.
This definition is equivalent to calling @Param(name, location)
without passing options.
If you need to define extra options, a new call of @Param(name, location, options)
will override the automatic definition.
Examples:
class Constructor {
@Get('/:id')
public getById(@Param('id') id, @Response() res) {
}
}
produces
...
"parameters": [
{ "name": "id", "in": "path", "required": true }
]
...
class Constructor {
@Get('/:id')
@Param('id', 'path', { required: true })
public getById(@Request() req, @Response() res) {
const id = req.params.id;
}
}
also produces
...
"parameters": [
{ "name": "id", "in": "path", "required": true }
]
...
@Tags(tag: string, ...tags: string[])
Defines one or more tags for the operation. If no tags are defined on method nor class level,
then the class name will be used as default tag
Params:
@Deprecated()
Marks an operation as deprecated on the openapi document
@BodyContent(mediaType: string, schema: SchemaDef)
Marks an operation as deprecated on the openapi document
Params:
mediaTye | string | | Media type definition complying with RFC 6838 |
schema | object | | A schema definition following openapi specifications |
@Responses(def: { [key: string]: ResponseDescriptor })
Defines one or more responses for the operation
Params:
@OpenApiResponse(status: string | number, description: string)
Defines the description for a response
Params:
status | string | number | | The response status |
description | string | | The description |
@OpenApiResponse(status: string | number, produces: string, schema: SchemaDef)
Defines one response schema for the operation
Params:
status | string | number | | The response status |
produces | string | | The media type described |
schema | object | | A schema definition following the openapi specifications |
@Security(schemeName: string, scopes?: string[])
Applies a security scheme to the operation
Params:
schemeName | string | | The scheme name |
scopes | string[] | | list of required scopes |
Property Decorators - Schema property
@Property(opts: SchemaDef & { required?: boolean })
Declares a property on a class using @Schema()
decorator
Params:
opts | object | | A property definition following the openapi specifications |