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

@qest/swagger-utils

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@qest/swagger-utils

Generate swagger data from splitted files, add swagger UI to our express, etc.

latest
Source
npmnpm
Version
1.0.11
Version published
Maintainers
1
Created
Source

Swagger utils

  • Do better documentation of API with with clearly arranged yaml files instead of one big file
  • Add swagger UI to your backend for simple documentation reading

Instalation

Install package to our dependencies from NPM.

yarn add @qest/swagger-utils

or

npm install @qest/swagger-utils

Swagger generator

It generates swagger yaml or json from partials yaml files which is merged together. You can separate documentation to folders to the code that implements functionality.

Yamls has parts of swagger yaml. You can separate routes, responses, object definitions and security definitions.

This is example for file with some route

paths:
  /API/v1/swagger:
    get:
      produces:
        - text/plain
      description: get swagger documentation of project in raw (string) format
      responses:
        200:
          description: documentation of project in raw (string) format

...other can be like this

paths:
  /API/v1/something/{id}
    get:
      responses:
        200:
          $ref: '#/responses/somethingResponse'
    delete:
      responses:
        200:
          description: delete something      

...and file with responses and object can be together.

responses:
  somethingResponse:
    description: this is response with array of someting
    schema:
      type: "object"
      properties:
        data:
          type: "array"
          items:
            $ref: '#/responses/TreeItem'
definitions:            
  Item:
    type: object
    properties:
      id:
        type: string                
      name:
        type: string        

How to configure and use generator

Swagger generator has configuration object with these properties:

PropertyTypeDefaultDescription
descriptorInfoObject{ title:string; version:string; description:string; }Info object whitch title, version and base description
includePathsstring[]['./src']Paths of folders that will be recursively crawled to find the yamls
excludedDirsFromPathsstring[]This paths will be excludet from crawling. but only this folders, not their subfolders
schemesstring[]Swagger definition of schemes (http, https, ws, etc..). If it's not defined, scheme of UI will be taken in swagger UI. Only for Swagger 2.0
hoststringSwagger host of API. If it's not defined, host of UI will be taken in swagger UI
basePathstringSwagger basepath of API
changeLogPathstringIf you have changelog txt file in your application, it can be included to description of API
swaggerVersion2.0 or 3.02.0Version of swagger documentation

Example of configuration and use:

const swaggerGenerator = new SwaggerGenerator({
    descriptorInfo: { 
        version: '1.0.0', 
        title: 'something app', 
        description: 'description of app', 
    },
    includePaths: ['./path-to-docs-folders'],
    swaggerVersion: '2.0',
});

const objectResult = swaggerGenerator.getAsObject();
const jsonResult = swaggerGenerator.getAsJson();

The resulting structure for getAsObject() will contain all of files from previous example in one object .

{
    "basePath":"/",
    "swagger":"2.0",
    "info":{
        "version":"1.0.0",
        "title":"something app",
        "description":"description of app"
    },
    "paths":{
        "/API/v1/swagger":{
            "get":{...},
        },    
        "/API/v1/something/{id}":{
            "get":{...},
            "delete":{...},
        },        
    }
    "definitions":{
        "Item":{...}
    },
    "securityDefinitions":{...},
    "tags":[]
}

Swagger express middlewares

In project are express middlewares for integraion of documentation to our project. First middleware are for swagger UI and the second for datasource.

Router()
    .get('/api/v1/swagger', swaggerDataMiddleware(swaggerGenerator))
    .use('/swagger', swaggerUiMiddleware({
            swaggerOptions: {
                url: '/api/v1/swagger',
            },
        })
    );

Configuration

Swagger Data middleware

You must have instance of swagger generator and the second parameter say how data you can (json or yaml) in response.

Swagger UI middleware

For configuration see configuration of swagger UI library

FAQs

Package last updated on 12 May 2022

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