
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
adonis5-swagger
Advanced tools
Swagger, AdonisJS, SwaggerUI
Create API documentation easily in Adonis 5 using Swagger
npm i --save adonis5-swagger
Compile your code:
node ace serve --watch
Connect all dependences:
node ace invoke adonis5-swagger
config/swagger.ts
.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: 'Bond007@example.com'
* 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
}
}
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
}
project
├── app
├── config
├── docs
│ ├── controllers
│ │ ├── **/*.ts
│ │ ├── **/*.yml
│ └── models
│ ├── **/*.ts
│ ├── **/*.yml
/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
Open http://localhost:3333/docs in your browser For detail usage, please check the Swagger specification in this SwaggerSpec
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.
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.
This package support two modes:
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.
For using swagger in production you should make some preparations:
const swaggerConfig = {
mode: process.env.NODE_ENV === 'production' ? 'PRODUCTION' : 'RUNTIME',
specFilePath: 'docs/swagger.json'
}
package.json
:{
"scripts": {
"build": "npm run compile",
"postbuild": "node ace swagger:generate && cp -a docs/ build/docs"
}
}
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 })
}
}
}
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: []
FAQs
Swagger provider for AdonisJS 5
The npm package adonis5-swagger receives a total of 800 weekly downloads. As such, adonis5-swagger popularity was classified as not popular.
We found that adonis5-swagger demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.