2.1.0 • Published 9 months ago

@decorators/express-openapi v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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

NameTypeAttributeDescription
appexpress.ApplicationThe application object
optionsobjectoptionalDefault: {}Options to configure swagger ui and the openapi document itself
options.serveInPathstringoptionalDefault: /api-docsThe url where the swagger-ui will be served
options.infoobjectoptionalAn object that represents the info on the openapi document. For more info see https://swagger.io/docs/specification/basic-structure/
options.info.titlestringoptionalDefault: package name taken from your package.json
options.info.descriptionstringoptionalDefault: package description taken from your package.json
options.info.versionstringoptionalDefault: package version taken from your package.json
options.tagsobject[]optionalList of tags following the openapi specifications
options.tags.*.namestringThe tag name.
options.tags.*.descriptionstringoptionalThe tag description
options.serversobject[]optionalList of servers following the openapi specifications. See https://swagger.io/docs/specification/api-host-and-base-path/
options.servers*.urlstring
options.servers*.descriptionstringoptional
options.externalDocsobjectoptionalExternal documents definition following the openapi specifications.
options.externalDocs.urlstring
options.externalDocs.descriptionstringoptional
options.securityobject[]optionalSecurity schemes to be applied to the api
options.components.securitySchemesobjectoptionalAn 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:

NameTypeAttributeDescription
namestringThe name of the schema in the openapi document
schemaobjectA schema object following openapi specifications. See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schemaObject

Decorators

Class Decorators

@WithDefinitions(options: WithDefinitionsOpts)

Enables openapi definitions for a controller class (using @Controller() from @decorators/express)

Params:

NameTypeAttributeDescription
optionsobject
options.tagsstring[]optionalTags to be applied to all routes on the controller
options.securityobject[]optionalSecurity schemes to be applied to all routes on the controller
options.responsesobjectoptionalTags to be applied to all routes on the controller
options.basePathstringThe base path for all routes on the controller
@Schema(name?: string)

Defines a new schema on the openapi document. Internally uses registerSchema.

Params:

NameTypeAttributeDescription
namestringoptionalDefault: The class nameThe name of the schema

Method Decorators - Route documentation

@Summary(summary: string)

Defines the summary of the operation

Params:

NameTypeAttributeDescription
summarystringThe operation's summary
@Description(description: string)

Defines the description of the operation

Params:

NameTypeAttributeDescription
descriptionstringThe operation's description
@Param(name: string, location: ParamLocation, schema: SchemaDef, options: ParamOptions)

Adds a param definition to the operation

Params:

NameTypeAttributeDescription
namestringThe parameter name
locationstringoneOf: query, header, path or cookieWhere the parameter is located
schemaobjectA schema definition following openapi specifications
optionsobjectoptionalOptions for the parameter following openapi specifications
options.descriptionstringoptional
options.requiredbooleanoptional
options.deprecatedbooleanoptional
options.allowEmptyValuebooleanoptional
options.contentMediaTypestringoptionalMedia 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:

NameTypeAttributeDescription
tagstring
tagsstring[]optional
@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:

NameTypeAttributeDescription
mediaTyestringMedia type definition complying with RFC 6838
schemaobjectA schema definition following openapi specifications
@Responses(def: { [key: string]: ResponseDescriptor })

Defines one or more responses for the operation

Params:

NameTypeAttributeDescription
defobjectA map of responses following openapi specifications. See https://swagger.io/docs/specification/describing-responses/
def[*]object
def[*].descriptionstringThe description for the response
def[*].contentobject
@OpenApiResponse(status: string | number, description: string)

Defines the description for a response

Params:

NameTypeAttributeDescription
statusstring \| numberThe response status
descriptionstringThe description
@OpenApiResponse(status: string | number, produces: string, schema: SchemaDef)

Defines one response schema for the operation

Params:

NameTypeAttributeDescription
statusstring \| numberThe response status
producesstringThe media type described
schemaobjectA schema definition following the openapi specifications
@Security(schemeName: string, scopes?: string[])

Applies a security scheme to the operation

Params:

NameTypeAttributeDescription
schemeNamestringThe scheme name
scopesstring[]optionallist of required scopes

Property Decorators - Schema property

@Property(opts: SchemaDef & { required?: boolean })

Declares a property on a class using @Schema() decorator

Params:

NameTypeAttributeDescription
optsobjectA property definition following the openapi specifications
2.1.0

9 months ago

2.0.0

10 months ago

1.1.2

12 months ago

1.1.1

1 year ago

1.0.1

1 year ago

1.0.0-beta

1 year ago