npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-swagger-autoconfigure

v2.1.1

Published

This is a engine for express developers

Downloads

124

Readme

Express-swagger-autoconfigure

License npm version

Manage route configurations for your express application automatically and generate automatic documentation with swagger.

Installation and Usage

npm i express-swagger-autoconfigure

Add the property below to your file tsconfig.json.

{
    "compilerOptions": {
        //...
        "experimentalDecorators": true
        //...
    }
}

Documentation

Below is a walkthrough of all available decorators

Frist Configuration:

  • @SwaggerInitializer - Loads Swagger in your express application | String
  • @SwaggerEndpoint - Defines the path to access documentation | String
  • @ApiDefaultPath - Defines the main path of your API | String
  • @Description - Describes your application within documenta*tion | String
  • @Title - Puts a title on your documentation | String
  • @Version - Defines API version | String
  • @BearerTokenJWT - Defines if the API uses JWT Tokens as a security mechanism | Boolean
  • @ExpressInitializer - Initializes an express app and configures its routes

Second Configuration

  • @Controller - Specifies a controller within the express context | String

Third Configuration

  • @Get - Specifies GET type endpoints | String, middlewares
  • @Post - Specifies POST type endpoints | String, middlewares
  • @Delete - Specifies DELETE type endpoints | String, middlewares
  • @Patch - Specifies PATCH type endpoints | String, middlewares
  • @Put - Specifies PUT type endpoints | String, middlewares

Fourth Configuration

  • @StatusResponse - Adds HTTP response codes and description | number
  • @Body - Adds a Body as a request object | Object
  • @ParamPath - Adds a ParamPath as a request object | Object
  • @FormData - Adds a FormData as a request objet | Object. Utilize FormDataTypes for grant types
  • @Header - Adds a Header as a request objet | object
  • @Query - Adds a Query as a request objet | object

Themes

To configure the themes, use

  • @Theme - Specifies Theme type of Swagger | If not specified, use default swagger theme
    • ThemesType.FEELING_BLUE
    • ThemesType.FLATTOP
    • ThemesType.MATERIAL
    • ThemesType.MONOKAI
    • ThemesType.MUTED
    • ThemesType.NEWS_PAPER
    • ThemesType.OUTLINE

Usage

Express Configuration

import  { Express,  } from "express";
import cors from "cors";
import {BasePath,
    BearerTokenJWT,
    Description,
    SwaggerInitializer,
    Title,
    Version,
    ExpressInitializer} from "express-swagger-autoconfigure";

// Configure a class with the first configuration
@SwaggerInitializer
@SwaggerEndpoint("/documentation") // swagger documentation will be available on http://localhost:5000/documentation (Optional, default /)
@ApiDefaultPath("/api") // swagger request to http://localhost:5000/api/endpoint | (Optional, default /)
@Description("Essa api é responsável pelo exemplo de utilização do express-swagger-autoconfigure")
@Title("Example-of-express-swagger-autoconfigure")
@Version("1.0.0")
@BearerTokenJWT(true)
@Theme(ThemesType.FEELING_BLUE)
export default class App {

    @ExpressInitializer
    private app: Express;

    constructor () {
        this.configApp();
        this.initControllers();
    }

    private configApp():void {
        this.app.use( cors() );
    }

    private initControllers(){
        // It is important to instantiate all of your controllers
        // in this class so that the decorators can be applied.
        new MyController1();
        new MyController2();
    }
    public getApp(): Express {
        return this.app;
    }
}

Controller Configuration

@Controller("/controller1")
export default class MyController1 {
    
    //health-check
    @StatusResponse(200, "Check API successfully")
    @StatusResponse(400,"Check API unsuccessfully")
    @Get() // It is important to put the Http Method Decorator as the first configuration.
    public check(request: Request, response: Response): Promise<Response> {

        //... implementation

    }
}
@Controller("/controller2")
export default class MyController2 {
    
    
    @StatusResponse(202) // if you dont pass description, express-swagger-autoconfigure add for you 
    @StatusResponse(400) // if you dont pass description, express-swagger-autoconfigure add for you 
    @Body({email:"Description", password:"Description"})
    @Post("/login")// It is important to put the Http Method Decorator as the first configuration.
    public login( request: Request, response: Response): Promise<Response> {
       //... implementation
    }
    
    @StatusResponse(200) // if you dont pass description, express-swagger-autoconfigure add for you 
    @StatusResponse(400)// if you dont pass description, express-swagger-autoconfigure add for you 
    @Get("/", authorizationMiddleware)// It is important to put the Http Method Decorator as the first configuration.
    public read(request: Request, response: Response): Promise<Response> {
        //... implementation
    }

    @StatusResponse(200)// if you dont pass description, express-swagger-autoconfigure add for you 
    @StatusResponse(400)// if you dont pass description, express-swagger-autoconfigure add for you 
    @ParamPath({uuid: "Description"})
    @Get("/find-by-uuid/{uuid}", authorizationMiddleware)// It is important to put the Http Method Decorator as the first configuration.
    public findByUuid(request: Request, response: Response): Promise<Response> {
        //... implementation
    }

    @StatusResponse(200)// if you dont pass description, express-swagger-autoconfigure add for you 
    @StatusResponse(400)// if you dont pass description, express-swagger-autoconfigure add for you 
    @Body({
        name : "Description",
        email : "Description",
        password: "Description"
    })
    // Default = "/" 
    @Post() // It is important to put the Http Method Decorator as the first configuration.
    public create(request: Request, response: Response): Promise<Response> {
        //... implementation
    }
    
    @StatusResponse(200)// if you dont pass description, express-swagger-autoconfigure add for you 
    @StatusResponse(400)// if you dont pass description, express-swagger-autoconfigure add for you 
    @Query({
        uuid:"Description"
    })
    // Default = "/" 
    @Post("/query-profile") // It is important to put the Http Method Decorator as the first configuration.
    public queryProfile(request: Request, response: Response): Promise<Response> {
        //... implementation
    }

    @StatusResponse(200)// if you dont pass description, express-swagger-autoconfigure add for you 
    @StatusResponse(400)// if you dont pass description, express-swagger-autoconfigure add for you 
    @Header({
        profileType:"Description"
    })
    // Default = "/" 
    @Post("/type-profile") // It is important to put the Http Method Decorator as the first configuration.
    public headerProfile(request: Request, response: Response): Promise<Response> {
        //... implementation
    }

    @StatusResponse(200)// if you dont pass description, express-swagger-autoconfigure add for you 
    @StatusResponse(400)// if you dont pass description, express-swagger-autoconfigure add for you 
    @FormData({
        img: FormDataTypes.FILE,
        name: FormDataTypes.STRING,
        rules: FormDataTypes.ARRAY,
        age: FormDataTypes.NUMBER,
        isMarried:FormDataTypes.BOOLEAN

    })
    // Default = "/" 
    @Post("/create-profile") // It is important to put the Http Method Decorator as the first configuration.
    public createProfile(request: Request, response: Response): Promise<Response> {
        //... implementation
    }
}

Theme examples

  • FEELING_BLUE alt text
  • FLATTOP alt text
  • MATERIAL alt text
  • MONOKAI alt text
  • MUTED alt text
  • NEWS_PAPER alt text
  • OUTLINE alt text

Example

Example-of-express-swagger-autoconfigure.

Contributing

You can download the code and help develop more features.
A more robust architecture is also necessary.
To contribute, simply open a pull request with your changes; if it makes sense, it will be approved.

License

This project is licensed under the license. GNU AFFERO GENERAL.

Contact

[email protected]