Skip to content

Examples

Amish Faldu edited this page Sep 29, 2022 · 3 revisions

Content

  1. More examples
  2. What's Next?

More examples

This is where we explore a great variety of complex examples, which can be used to power your swagger UI

  1. Add Path and Query parameters to your API

    @Controller({ route: "" })
    class HelloWorld {
      @Get(":path")
      hello(
        @PathParam("path") path: string,
        @QueryParam("query") query: string
      ): string {
        return "Hello World!";
      }
    }
    

    Screenshot 2022-09-29 at 12 47 13 PM

  2. Add request body

    enum TestEnum {
      ONE = "One",
      TWO = "Two",
    }
    
    class ExampleDTO {
      @IsString()
      example: string;
    
      @IsEnum(TestEnum)
      exampleEnum: TestEnum
    
      @IsArray("Boolean")
      exampleBoolean: boolean[]
    }
    
    class RequestBodyDTO {
      @IsString()
      string: string;
    
      @IsBoolean()
      boolean: boolean;
    
      @IsNumber()
      number: number;
    
      @IsEnum(TestEnum)
      enum: TestEnum;
    
      @IsObject()
      object: ExampleDTO;
    
      @IsArray("String")
      array: string[]
    }
    
    @Controller({ route: "" })
    class HelloWorld {
      @Post(":path")
      hello(
        @PathParam("path") path: string,
        @QueryParam("query") query: string,
        @Body() body: RequestBodyDTO,
      ): string {
        return "Hello World!";
      }
    }
    

    Screenshot 2022-09-29 at 1 08 19 PM

  3. Use middleware for api route

    const middleware = (
      req: express.Request,
      res: express.Response,
      next: express.NextFunction,
    ) => {
      res.sendStatus(200);
      next();
    };
    
    @Controller({ route: "" })
    class HelloWorld {
      @RouteMiddleware([middleware])
      @Get(":path")
      hello(
        @PathParam("path") path: string,
        @QueryParam("query") query: string
      ): string {
        return "Hello World!";
      }
    }
    

    This will execute middleware and send 200 status code with no data

  4. File upload

    const upload = multer();
    
    @Controller({ route: "" })
    class HelloWorld {
      @RouteMiddleware([
        upload.fields([
          { name: "file", maxCount: 2 },
        ])
      ])
      @Post()
      hello(
        @File("file", { maxFiles: 2 }) file: any[]
      ): string {
        return "Hello World!";
      }
    }
    

    Screenshot 2022-09-29 at 1 15 58 PM

  5. Add metadata to route handlers

    const upload = multer();
    
    @RouteTag("Hello world!")
    @Controller({ route: "" })
    class HelloWorld {
      @RouteSummary("This is hello world route")
      @RouteDescription("This is hello world route")
      @RouteDeprecated()
      @RouteMiddleware([
        upload.fields([
          { name: "file", maxCount: 2 },
        ])
      ])
      @Post()
      hello(
        @File("file", { maxFiles: 2 }) file: any[]
      ): string {
        return "Hello World!";
      }
    }
    

    Screenshot 2022-09-29 at 1 23 37 PM

What's Next?

These complex examples are just to show how to create great API docs, but sky is the limit here. You can now start with API Reference and build powerful swagger UI.

Swagger-Docs

Help

Clone this wiki locally