Friends Of AdonisFriends Of Adonis

Getting Started

Installation

Install and configure the package using the following command :

node ace add @foadonis/openapi

Getting started

Decorate your controller

The OpenAPI schema is automatically generated using Typescript decorators. The routes of your application are automatically loaded but you might want to add some information like the description, response type, etc.

app/controllers/posts_controller.ts
import  from '#models/post'
import {  } from '#validators/post'
import { , ,  } from '@foadonis/openapi/decorators'
 
export default class  {
  @({ : 'List all Posts' }) 
  @({ : [] }) 
  () {
    // ...your logic
  }
 
  @({ : 'Create a new Post' }) 
  @({ : () =>  }) 
  @({ :  }) 
  () {}
}

The above decorators act as the following:

@ApiOperation():
Configures the operation. This is not required as the operations are automatically loaded from the Adonis Router.

@ApiResponse({ type: [Post] }):
Defines the response return type to be a list of Posts.

@ApiBody({ type: () => createPostValidator }):
Defines the request body schema to match the Vine compiled schema createPostValidator.

Decorate your Model

It is now time to decorate your model to configure which properties are available.

app/models/post.ts
import {  } from '@foadonis/openapi/decorators'
 
export default class  {
  @() 
  declare : number
 
  @() 
  declare : string
 
  @() 
  declare : string
}

Lucid decorators have been removed from the example for readability reasons but you can generate your OpenAPI schemas directly from your models.

Register documentation routes

Let's register the routes to access our OpenAPI documentation in start/routes.ts:

start/routes.ts
import  from '@foadonis/openapi/services/main'
 
.()

Access the documentation

You can now head over your OpenAPI documentation at http://localhost:3000/api. It will welcome you with a beautiful documentation page of your api thanks to Scalar.

OpenAPI documentation with Scalar

On this page