1.1.3 • Published 2 months ago

swagger-generator-json v1.1.3

Weekly downloads
1
License
ISC
Repository
github
Last release
2 months ago

SWAGGER GENERATOR JSON

Library generate json use for swagger-ui-express

Quick start

Example for 1 method in document:

// example.js file

const { DType, Response, Operation, API } = require('swagger-generator-json')
// By default will be get method
const documentsSearchUser = new API({
    path: '/public/users/search',
    operation: [
        new Operation({
            method: DType.get,
            parameters: [
                {
                    name: 'username',
                    type: DType.string,
                    place: DType.query,
                    description: "Username must be required"
                }
            ],
            responses: [
                new Response({
                    schema: [{
                        id: DType.number,
                        name: DType.string,
                        isAdmin: DType.boolean
                    }]
                }),
                new Response({
                    code: 302,
                    schema: [{
                        id: DType.number,
                        name: DType.string,
                        isAdmin: DType.boolean
                    }]
                })
            ],
            tags: 'public/user'
        })
    ]
})

alt text

Example for multi method in document:

// example.js file

const { DType, Response, Operation, API } = require('swagger-generator-json')
// GET method
const operationGet = new Operation({
    method: DType.get,
    parameters: [
        {
            name: 'token',
            type: DType.string,
            place: DType.body,
            description: "Token need to authenticate"
        },
        {
            name: 'userid',
            type: DType.string,
            place: DType.path,
            description: "Only number"
        }
    ],
    responses: [
        new Response({
            schema: [{
                id: DType.number,
                name: DType.string,
                isAdmin: DType.boolean
            }]
        })
    ],
    summary: "API GET USER",
    tags: 'public/user'
})

// POST method
const operationPost = new Operation({
    method: DType.post,
    parameters: [
        {
            name: 'token',
            type: DType.string,
            place: DType.header,
            description: "Token need to authenticate"
        }
    ],
    responses: [
        new Response({
            schema: {
                id: DType.number,
                name: DType.string,
                isAdmin: DType.boolean
            },
        })
    ],
    tags: 'public/user',
    summary: "API CREATE USER"
})

const documentsPublicUser = new API({
    path: '/public/users',
    operation: [operationGet, operationPost]
})

alt text

alt text

Export to use

module.exports = [documentsPublicUser, documentsSearchUser]

Create instance Document

// document.js file
const paths = require('./example')
const { Document } = require('swagger-generator-json')

module.exports = new Document({
    description: `This is the Devteam's documents of project`,
    version: "1.0.0",
    title: "App Name",
    paths
})

Use with swagger-ui-express

const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./document');
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

Some example for request (schema), params (type):

// Data response
const data = {
    status: true,
    message: "Success",
    list: [
        {
            username: "devteam",
            isAdmin: "true",
            age: 25
        }
    ]
}
// Convert to schema
new Response({
    schema: {
        status: DType.boolean,
        message: DType.string,
        list: [
            {
                username: DType.string,
                isAdmin: DType.boolean,
                age: DType.number
            }
        ]
    }
})

alt text

const param = [
        {
            name: 'list',
            type: {
                userid: DType.number,
                list: [{
                    productId: DType.number,
                    price: DType.number
                }]
            },
            place: DType.body,
            description: "User's orders"
        }
    ]

alt text

1.1.3

2 months ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago