🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@decorators/express-openapi

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@decorators/express-openapi

node decorators - openapi decorators for swagger-ui-express and @decorators/express

2.1.0
latest
Version published
Weekly downloads
257
-17.1%
Maintainers
1
Weekly downloads
 
Created

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
optionsobject
  • optional
  • Default: {}
Options to configure swagger ui and the openapi document itself
options.serveInPathstring
  • optional
  • Default: /api-docs
The url where the swagger-ui will be served
options.infoobject
  • optional
An object that represents the info on the openapi document. For more info see https://swagger.io/docs/specification/basic-structure/
options.info.titlestring
  • optional
  • Default: package name taken from your package.json
options.info.descriptionstring
  • optional
  • Default: package description taken from your package.json
options.info.versionstring
  • optional
  • Default: package version taken from your package.json
options.tagsobject[]
  • optional
List of tags following the openapi specifications
options.tags.[*].namestringThe tag name.
options.tags.[*].descriptionstring
  • optional
The tag description
options.serversobject[]
  • optional
List of servers following the openapi specifications. See https://swagger.io/docs/specification/api-host-and-base-path/
options.servers[*].urlstring
options.servers[*].descriptionstring
  • optional
options.externalDocsobject
  • optional
External documents definition following the openapi specifications.
options.externalDocs.urlstring
options.externalDocs.descriptionstring
  • optional
options.securityobject[]
  • optional
Security schemes to be applied to the api
options.components.securitySchemesobject
  • optional
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:

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[]
  • optional
Tags to be applied to all routes on the controller
options.securityobject[]
  • optional
Security schemes to be applied to all routes on the controller
options.responsesobject
  • optional
Tags 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
namestring
  • optional
  • Default: The class name
The 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
locationstring
  • oneOf: query, header, path or cookie
Where the parameter is located
schemaobjectA schema definition following openapi specifications
optionsobject
  • optional
Options for the parameter following openapi specifications
options.descriptionstring
  • optional
options.requiredboolean
  • optional
options.deprecatedboolean
  • optional
options.allowEmptyValueboolean
  • optional
options.contentMediaTypestring
  • optional
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:

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[]
  • optional
list 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

FAQs

Package last updated on 06 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts