New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

adonis5-swagger

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adonis5-swagger

Swagger provider for AdonisJS 5

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
decreased by-25.89%
Maintainers
1
Weekly downloads
 
Created
Source

adonis5-swagger

Swagger, AdonisJS, SwaggerUI

typescript-image npm-image license-image

Create API documentation easily in Adonis 5 using Swagger

Table of contents

Installation

npm i --save adonis5-swagger

Compile your code:

node ace serve --watch

Connect all dependences:

node ace invoke adonis5-swagger
  • For other configuration, please update the config/swagger.ts.

Sample Usage

  • Add new route:

    Route.get('/api/hello', 'TestController.hello')
    
  • Create TestController using node ace make:controller Test command:

    import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
    
    export default class TestController {
      /**
      * @swagger
      * /api/hello:
      *   get:
      *     tags:
      *       - Test
      *     summary: Sample API
      *     parameters:
      *       - name: name
      *         description: Name of the user
      *         in: query
      *         required: false
      *         type: string
      *     responses:
      *       200:
      *         description: Send hello message
      *         example:
      *           message: Hello Guess
      */
      public async hello({ request, response }: HttpContextContract) {
        const name = request.input('name', 'Guess')
        return response.send({ message: 'Hello ' + name })
      }
    }
    
  • You can also define the schema in the Models:

    import { BaseModel } from '@ioc:Adonis/Lucid/Orm'
    
    /** 
    *  @swagger
    *  definitions:
    *    User:
    *      type: object
    *      properties:
    *        id:
    *          type: uint
    *        username:
    *          type: string
    *        email:
    *          type: string
    *        password:
    *          type: string
    *      required:
    *        - username
    *        - email
    *        - password
    */
    export default class User extends BaseModel {
    }
    
  • Or create a separate file containing documentation from the APIs in either TS or YAML formats, sample structure:

    project
    ├── app
    ├── config 
    ├── docs
    │   ├── controllers
    │   │   ├── **/*.ts
    │   │   ├── **/*.yml
    │   └── models
    │       ├── **/*.ts
    │       ├── **/*.yml
    

Best usage

  • Create files into docs/swagger, for example docs/swagger/auth.yml may contains:
/api/auth/login:
  post:
    tags:
      - Auth
    security: []
    description: Login
    parameters:
      - name: credentials
        in:  body
        required: true
        schema:
          properties:
            phone:
              type: string
              example: '1234567890'
              required: true
    produces:
      - application/json
    responses:
      200:
        description: Success
  • You can change default settings in config/swagger.ts
  • For other sample in YAML and JS format, please refer to this link.

Open http://localhost:3333/docs in your browser For detail usage, please check the Swagger specification in this SwaggerSpec

Keywords

FAQs

Package last updated on 11 Aug 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc