Skip to content

Getting Started

Amish Faldu edited this page Oct 5, 2022 · 4 revisions

Contents

  1. Installing
  2. Typescript compiler configuration
  3. "Hello World!"
  4. What's Next?

Installing

To install this package in your project use below command

npm i @amishfaldu/swagger-docs --save-exact

The optional --save-exact will pin this dependency to a specific latest version.

Typescript compiler configuration

This library uses typescript decorator and reflect metadata feature. This is not enabled by default, to enable it add the following 2 lines to your tsconfig.json file

{
  ...,
  "compilerOptions": {
    ...
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    ...
  },
  ...
}

That's it! Your typescript configuration is completed

Hello World!

This will show simplest use of swagger-docs, for more complex examples go here

Follow the steps to write your first hello world in swagger-docs

  1. Define route that your app will be serving. To do that we will define a controller class, which basically functions as router to store all your related routes.
    @Controller({ route: "" })
    class HelloWorld {
      @Get()
      hello(): string {
        return "Hello World!"
      }
    }
    
  2. Create web app using express
    const app = express();
    
  3. Create swagger configuration
    const swaggerConfig = new SwaggerConfig()
    .setTitle("Hello world swagger")
    .setVersion("1.0.0")
    .setDescription("Hello world swagger description")
    .finalizeConfig();
    
  4. Bootstrap controllers and prepare your app to serve swagger UI
    const swaggerDocs = new SwaggerDocs(swaggerConfig);
    swaggerDocs.bootstrapControllersToApp([HelloWorld]);
    swaggerDocs.setup(app, "api-explorer");
    
  5. Start express server
    app.listen(3000, () => {
      console.log("Listening");
    });
    

Now go to http://localhost:3000/api-explorer. You can see beautiful swagger API docs for your hello world API.

Screenshot 2022-09-27 at 12 44 35 PM

What's Next?

Next you can start looking for more complex examples and start integrating library with your project.

Swagger-Docs

Help

Clone this wiki locally