• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swagger provider for AdonisJS 5

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";
	import User from "App/Models/User";

	export default class UsersController {
		/**
		* @swagger
		* /api/users:
		* post:
		*     tags:
		*       - Users
		*     requestBody:
		*       required: true
		*       content:
		*         application/json:
		*           description: User payload
		*           schema:
		*             type: object
		*             properties:
		*               phone:
		*                 type: string
		*                 example: 'James Bond'
		*                 required: true
		*               email:
		*                 type: string
		*                 example: '[email protected]'
		*                 required: true
		*     produces:
		*       - application/json
		*     responses:
		*       200:
		*         description: Success
		*         content:
		*           application/json:
		*             schema:
		*               $ref: '#/components/schemas/User'
		*/
		public async create({ request, response }: HttpContextContract): Promise<User> {
				// User saving and returns
		}
	}
  • You can also define the schema in the Models:
		import {DateTime} from 'luxon'
		import {BaseModel, column} from '@ioc:Adonis/Lucid/Orm'

		/**
		* @swagger
		* components:
			* schemas:
			*      User:
			*        type: object
			*        properties:
			*          name:
			*            type: string
			*          email:
			*            type: string
			* 
		*/
		export default class User extends BaseModel {
		@column({isPrimary: true})
		public id: number
		
		@column()
		public name: string
		
		@column()
		public email: string
		
		@column.dateTime({autoCreate: true})
		public createdAt: DateTime
		
		@column.dateTime({autoCreate: true, autoUpdate: true})
		public updatedAt: DateTime
	}
  • 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

Custom UI

For using custom ui you should use own build of swagger ui. Current package contains only preconfigured and already built ui bundle. For example, you can use Adonis Edge for rendering ui with custom params.

First, install edge:

npm i @adonisjs/view

Once installed, run the following ace command to setup the package.

node ace invoke @adonisjs/view

Generate new template file using Adonis generator:

node ace make:view swagger

And then add route for custom UI:

Route.get('/', async ({ view }) => {
	const specUrl = 'your spec url'
	return view.render('swagger', { specUrl })
})

Your template should have similar content:

<!DOCTYPE html>
<head>
	<script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js"></script>
	<script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
	<link rel="stylesheet" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css"/>
	<script>
		window.onload = () => {
			let ui = SwaggerUIBundle({
				url: "{{ specUrl }}",
				dom_id: "#swagger-ui",
				presets: [
					SwaggerUIBundle.presets.apis,
					SwaggerUIBundle.SwaggerUIStandalonePreset
				],
				plugins: [
					SwaggerUIBundle.plugins.DownloadUrl
				],
			})

			window.ui = ui
		}
	</script>
</head>
<body>
	<div id="swagger-ui"></div>
</body>
</html>

It is the simplest way for using custom swagger ui, but of course you could use Webpack or another bundler tool for bundling your pre configured Swagger ui.

Build swagger spec file

You can build swagger spec file the next way:

Set specFilePath option to your swagger config:

const swaggerConfig = {
	specFilePath: 'docs/swagger.json'
}

And then run adonis command:

node ace swagger:generate

Generated file will be written to by path configured in config.

Swagger modes

This package support two modes:

  • PRODUCTION
  • RUNTIME

By default RUNTIME mode enabled. When RUNTIME mode enabled package rebuild swagger spec file on each request. When you use PRODUCTION you should build your swagger spec once and then package will be respond this file content on each request.

Production using

For using swagger in production you should make some preparations:

  • Setup swagger config:
const swaggerConfig = { 
	mode: process.env.NODE_ENV === 'production' ? 'PRODUCTION' : 'RUNTIME',
	specFilePath: 'docs/swagger.json'
}
  • Add post hook for npm build script to your package.json:
{
	"scripts": {
		"build": "npm run compile",
		"postbuild": "node ace swagger:generate && cp -a docs/ build/docs"
	}
}
  • Deliver your source code to production server.

Swagger basic auth

Package supports auth via basic auth schema. For using auth you should add config in config/swagger.ts

import Env from '@ioc:Adonis/Core/Env'

export default {
	// ...Swagger congig
	swaggerAuth: {
		authMiddleware: 'swagger-auth',

		authCredentials: {
			login: Env.get('SWAGGER_AUTH_LOGIN'),
			password: Env.get('SWAGGER_AUTH_PASSWORD')
		}
	}
}

Register auth middleware in your start/kernel.ts

Server.middleware.registerNamed({
  'swagger-auth': 'Adonis/Addons/Swagger/AuthMiddleware',
})

That's all. Your swagger docs secured by basic auth.

Instead of using credentials, you can use function for verifying access in more complex way.

import Env from '@ioc:Adonis/Core/Env'
import { verifyDocsAccess } from 'App/Services/Auth/Docs'

export default {
	// ...Swagger congig
	swaggerAuth: {
		authMiddleware: 'swagger-auth',

		authCheck: async (login, password) => {
			return await verifyDocsAccess({ login, password })
		}
	}
}

Recipes

JWT auth for endpoints

Define JWT component inside your .yaml declaration:

components:
	securitySchemes:
		bearerAuth:            
		type: http
		scheme: bearer
		bearerFormat: JWT 

Or add to your swagger config:

export default {
	// ... config options
	options: {
		definition: {
		openapi: '3.0.0',
		info: {
			title: 'Application with swagger docs',
			version: '1.0.0',
			description: 'My application with swagger docs'
		},
		components: {
			securitySchemes: {
			bearerAuth: {
				type: "http",
				scheme: "bearer",
				bearerFormat: "JWT"
			}
			}
		}
		}
	//... config options
	}
} as SwaggerConfig

Then you can add to your controller auth security option:

@swagger
/api/users:
post:
	security:
	- bearerAuth: []

More Repositories

1

adonis5-scheduler

This library provides an easy way to schedule recurring tasks for AdonisJS 5
TypeScript
49
star
2

adonis5-cache

Simple cache provider for AdonisJS 5
TypeScript
32
star
3

adonis5-sentry

Sentry for Adonis 5
HTML
27
star
4

adonis-request-throttler

Adonis 5 request throttle based on Redis or Memory
TypeScript
20
star
5

adonis5-bullmq

Adonis 5 BullMQ provider
TypeScript
16
star
6

adonis5-queue

Queue for adonis 5
TypeScript
11
star
7

adonis-mailbuilder

AdonisJS 5 simple mailbuilder with livereload
TypeScript
11
star
8

tradingview-ta-docker

Docker image for Unofficial TradingView technical analysis API wrapper.
Python
8
star
9

adonis5-redis-rpc

Simple RPC Provider for Adonis 5, based on Redis pub/sub transport
TypeScript
6
star
10

oc-jsonable

October CMS jsonable FormWidget
PHP
6
star
11

adonis5-nats

Scale your Adonis 5 application to infinity with microservices based on classes (with code completion from client side!) and NATS transport
TypeScript
5
star
12

adonis-locales

Locales for adonis
JavaScript
4
star
13

seo_generator

PHP
3
star
14

adonis5-tsx-templates

Use JSX in your Adonis 5 app with or instead "Edge" template system
TypeScript
3
star
15

laravel-email-database-log

PHP
2
star
16

adonis-vue-websocket

This package add new layer for use original AdonisWSClient for Adonis 4.1 in your vue.js project
JavaScript
2
star
17

telegraf-router

JavaScript
2
star
18

laravel-selectel

This is package for use Selectel as CDN service
PHP
2
star
19

oc-range

Widget range for october CMS
PHP
1
star
20

adonis-selectel-drive

Selectel cloudStorage driver for Adonis drive
JavaScript
1
star
21

kp_node

JavaScript
1
star
22

vue-logux

Logux, NuxtJs, VueJS, ExpressJS
JavaScript
1
star
23

CI_HMVC_SECURITY

This is my version assembly Codeigniter and HMVC extension. My package used Composer.
JavaScript
1
star