swagger-jsdoc
Version:
Generates swagger doc based on JSDoc
74 lines (52 loc) • 1.64 kB
Markdown
This library reads your [JSDoc](https://jsdoc.app/)-annotated source code and generates an [OpenAPI (Swagger) specification](https://swagger.io/specification/).
[](https://www.npmjs.com/package/swagger-jsdoc)

Imagine having API files like these:
```javascript
/**
* @openapi
* /:
* get:
* description: Welcome to swagger-jsdoc!
* responses:
* 200:
* description: Returns a mysterious string.
*/
app.get('/', (req, res) => {
res.send('Hello World!');
});
```
The library will take the contents of `@openapi` (or `@swagger`) with the following configuration:
```javascript
import swaggerJsdoc from 'swagger-jsdoc';
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Hello World',
version: '1.0.0',
},
},
apis: ['./src/routes*.js'], // files containing annotations as above
};
const openapiSpecification = await swaggerJsdoc(options);
```
The resulting `openapiSpecification` will be a [swagger tools](https://swagger.io/tools/)-compatible (and validated) specification.

Notes on CJS and ESM.
```bash
npm install swagger-jsdoc --save
```
Or
```bash
yarn add swagger-jsdoc
```
- OpenAPI 3.x
- Swagger 2
Detailed documentation is available within [`/docs`](https://github.com/Surnet/swagger-jsdoc/tree/master/docs/README.md) folder.