npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@decorators/express-openapi

v2.1.0

Published

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

Downloads

189

Readme

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

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | app | express.Application | | The application object | | options | object | optionalDefault: {} | Options to configure swagger ui and the openapi document itself | | options.serveInPath | string | optionalDefault: /api-docs | The url where the swagger-ui will be served | | options.info | object | optional | 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 | optionalDefault: package name taken from your package.json | | | options.info.description | string | optionalDefault: package description taken from your package.json | | | options.info.version | string | optionalDefault: package version taken from your package.json | | | options.tags | object[] | optional | List of tags following the openapi specifications | | options.tags.[].name | string | | The tag name. | | options.tags.[].description | string | optional | The tag description | | options.servers | object[] | optional | 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 | optional | | | options.externalDocs | object | optional | External documents definition following the openapi specifications. | | options.externalDocs.url | string | | | | options.externalDocs.description | string | optional | | | options.security | object[] | optional | Security schemes to be applied to the api | | options.components.securitySchemes | object | 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:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | name | string | | The name of the schema in the openapi document | | schema | object | | A 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:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | options | object | | | | options.tags | string[] | optional | Tags to be applied to all routes on the controller | | options.security | object[] | optional | Security schemes to be applied to all routes on the controller | | options.responses | object | optional | 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 | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | name | string | optionalDefault: The class name | The name of the schema |

Method Decorators - Route documentation

@Summary(summary: string)

Defines the summary of the operation

Params:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | summary | string | | The operation's summary |

@Description(description: string)

Defines the description of the operation

Params:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | description | string | | The operation's description |

@Param(name: string, location: ParamLocation, schema: SchemaDef, options: ParamOptions)

Adds a param definition to the operation

Params:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | 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 | optional | Options for the parameter following openapi specifications | | options.description | string | optional | | | options.required | boolean | optional | | | options.deprecated | boolean | optional | | | options.allowEmptyValue | boolean | optional | | | options.contentMediaType | string | 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:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | tag | string | | | | tags | string[] | 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:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | 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:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | def | object | | A map of responses following openapi specifications. See https://swagger.io/docs/specification/describing-responses/ | | def[] | object | | | | def[].description | string | | The description for the response | | def[*].content | object | |

@OpenApiResponse(status: string | number, description: string)

Defines the description for a response

Params:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | 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:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | 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:

| Name | Type | Attribute | Description | | ---- | ---- | --------- | ----------- | | schemeName | string | | The scheme name | | scopes | string[] | optional | list of required scopes |

Property Decorators - Schema property

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

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

Params:

| Name | Type | Attribute | Description | | ---- |----- | --------- | ----------- | | opts | object | | A property definition following the openapi specifications |