-
-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Amish Faldu edited this page Sep 29, 2022
·
3 revisions
This is where we explore a great variety of complex examples, which can be used to power your swagger UI
-
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!"; } }
-
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!"; } }
-
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
-
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!"; } }
-
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!"; } }
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.
-
- SwaggerConfig
- SwaggerDocs
-
- Controller
- RouteMiddleware
-
-
- PathParam
- QueryParam
- Header
- Body
- Request
- Response
- Next
- File
-
- Get
- Put
- Post
- Delete
- Options
- Head
- Patch
- Trace
-
- RouteResponseBody
- RouteRequestBody
- RouteTag
- RouteSummary
- RouteDescription
- RouteExternalDocs
- RouteDeprecated
- RouteSecurity
- RouteServers
-
-
- IsArray
- IsBoolean
- IsNumber
- IsObject
- IsString
- IsEnum