New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

swagger-generator-json

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-generator-json

Generate JSON document for swagger-ui-express

latest
Source
npmnpm
Version
1.1.3
Version published
Weekly downloads
18
5.88%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

swagger

FAQs

Package last updated on 12 Mar 2024

Did you know?

Socket

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.

Install

Related posts