Git Product home page Git Product logo

swagger-schema's Introduction

swagger-schema

swagger-ui schema auto generation and base on swagger-autogen.

NPM Version NPM Downloads

Installation

This is a Node.js module available through the npm.

$ npm install --save swagger-schemagen

It is loaded using the require() function:

const swaggerSchema = require('swagger-schemagen')

Update

If you already have the module installed and want to update to the latest version, use the command:

$ npm install --save swagger-schemagen@last

Usage

Example express api

Usage

Function signature:

const swaggerSchema: (outputFile: <string>, schemaFolder: <Array of string>) => Promise <any>

outputFile: (Required*). Output file. It will be the file generated by the module containing the documentation in the format identified by Swagger.

schemaFolder: (Required*). This Folder is saving all of schema json file, and the parameter's type for mssql will update in Swagger file.

The code below must be inserted in a separate file, for example: swagger.js. For example:

File: swagger.js

const doc = {
    info: {
        title: "Example API Document",
        description: "Description"
    },
    host: null,
    schemes: ['http'],
    tags: alltags
}

const outputFile = './src/swagger/swagger-output.json'
const endpointsFiles = ['./index.js']

const swaggerschema = require('./src/lib/swaggerschema')
const schematest = require('swagger-schemagen')

const asyncfun = async (outputFile, endpointsFiles, doc, schemaFolder) => {
    await swaggerAutogen(outputFile, endpointsFiles, doc)
    await schematest(outputFile, schemaFolder)
}

const schemaFolder = './src/schema/'
asyncfun(outputFile, endpointsFiles, doc, schemaFolder)

Example Document

Json file in schema folder

Note.json:

[
    { "attr": "vid", "type": "Int" },
    { "attr": "currenttime", "type": "Float" },
    { "attr": "nid", "type": "Int" },
    { "attr": "title", "type": "NVarChar" },
    { "attr": "content", "type": "NVarChar" }
]

Notice

Swagger schema json file and swagger tag should be give in same name!!!

Like your router's swagger tag is 'Note', and your schema name should be 'Note.json'.

Compare Use

Before Using swagger-schema

controller -> Note.js:

router.delete('/note', async (req, res, next) => {
    // #swagger.tags = ['Note']
    // #swagger.summary = '刪除單個筆記'
    const { nid } = req.body // declare parameter's in body
    sqlcode = "update Note set bDel = 1 where NID = @nid and OwnerMID = @mid";
    let response = await runSQL(sqlcode, req, schema);
    res.json(response ? { message: "success" } : { message: "failed" });
});

router.put('/note/title', async (req, res, next) => {
    // #swagger.tags = ['Note']
    // #swagger.summary = '編輯筆記標題'
    const { nid, title } = req.body // declare parameter's in body
    sqlcode = "update Note set Title = @title, LastModifiedDT = getdate() where NID = @nid and OwnerMID = @mid";
    let response = await runSQL(sqlcode, req, schema);
    res.json(response ? { message: "success" } : { message: "failed" });
});

image

After Using swagger-schema

schema file: image

image

swagger-schema's People

Contributors

whlshy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.