Generates OpenApi 3 document with paths from koa yup router.
Install using npm
:
npm install @rudi23/koa-yup-router-docs
NodeJS >= 12.0.0.
is required.
import * as yup from 'yup';
import YupRouter from '@rudi23/koa-yup-router';
import createDocument from '@rudi23/koa-yup-router-docs';
const router = new YupRouter();
router.addRoute({
method: 'get',
path: '/user',
handler: () => {},
});
router.addRoute({
method: ['post', 'put'],
path: '/user/:id',
validate: {
type: 'json',
body: yup.object({
firstName: yup.string().required(),
lastName: yup.string().required(),
age: yup.number().required().min(0).max(100),
}),
params: yup.object({
id: yup.number().required(),
}),
headers: yup.object({
custom: yup.string(),
}),
output: {
200: {
description: 'Success OK',
body: yup.string().required(),
},
201: {
description: 'Created OK',
body: {
'application/json': yup.object({
id: yup.number().required(),
}),
},
},
400: {
description: 'Bad request',
headers: {
'X-Rate-Limit-Limit': {
schema: yup.number().required(),
description: 'The number of allowed requests in the current period',
},
},
},
},
},
handler: () => {},
});
router.addRoute({
method: 'delete',
path: '/user/:id',
validate: {
type: 'json',
params: yup.object({
id: yup.number().required(),
}),
headers: yup.object({
custom: yup.string(),
}),
},
handler: () => {},
});
const openApiDoc = createDocument(router, {
openapi: '3.0.3',
info: {
title: 'Sample API',
description: 'Sample API',
version: '1.0.0',
},
servers: [
{
url: 'https://www.example.com/api/v1',
},
],
paths: {},
components: {
securitySchemes: {
BasicAuth: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
},
},
},
});