2.2.6 • Published 2 years ago

@rudi23/koa-yup-router-docs v2.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

koa-yup-router-docs

Generates OpenApi 3 document with paths from koa yup router.

NPM version npm download

Installation

Install using npm:

npm install @rudi23/koa-yup-router-docs

NodeJS >= 12.0.0. is required.

Example

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',
            },
        },
    },
});

LICENSE

MIT

2.2.6

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.5

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.0

3 years ago

1.0.0

3 years ago