Skip to content

Commit 03c4d8f

Browse files
committed
Update README.md
1 parent a6a4da6 commit 03c4d8f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,39 @@ getOne(@Param("id") id: number) {
11541154
You can use `@UseInterceptor` per-action, or per-controller.
11551155
If its used per-controller then interceptor will apply to all controller actions.
11561156

1157+
### Generic Action Interface
1158+
1159+
You can now use generics with the `Action` interface to specify types for the request, response, context, and next function. This allows for better type safety and intellisense when working with different frameworks or custom types. Here's an example:
1160+
1161+
```typescript
1162+
import { Action } from 'routing-controllers';
1163+
import { Request, Response } from 'express';
1164+
1165+
@JsonController()
1166+
export class UserController {
1167+
@Get('/users')
1168+
getUsers(@Req() req: Request, @Res() res: Response) {
1169+
// Your action implementation
1170+
}
1171+
1172+
@Post('/users')
1173+
createUser(@Body() user: User, action: Action<Request, Response>) {
1174+
// You can now use action.request and action.response with proper typing
1175+
console.log(action.request.headers);
1176+
action.response.status(201);
1177+
}
1178+
}
1179+
```
1180+
1181+
The `Action` interface now accepts four generic parameters:
1182+
1183+
1. `TRequest`: The type of the request object (default: `any`)
1184+
2. `TResponse`: The type of the response object (default: `any`)
1185+
3. `TContext`: The type of the context object (for Koa-specific usage, default: `any`)
1186+
4. `TNext`: The type of the next function (default: `Function`)
1187+
1188+
This change allows for more flexibility and type safety when working with different frameworks or custom request/response types in your routing-controllers applications.
1189+
11571190
### Interceptor classes
11581191

11591192
You can also create a class and use it with `@UseInterceptor` decorator:

0 commit comments

Comments
 (0)