From a6a4da69ebe9fd6245323afa9c64498a1f76d39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Masum=20Go=CC=88kyu=CC=88z?= Date: Sat, 26 Oct 2024 08:59:08 +0300 Subject: [PATCH 1/2] feat: extended action type --- src/Action.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Action.ts b/src/Action.ts index 4b71cea0..fab082fd 100644 --- a/src/Action.ts +++ b/src/Action.ts @@ -1,25 +1,25 @@ /** * Controller action properties. */ -export interface Action { +export interface Action { /** * Action Request object. */ - request: any; + request: TRequest; /** * Action Response object. */ - response: any; + response: TResponse; /** * Content in which action is executed. * Koa-specific property. */ - context?: any; + context?: TContext; /** * "Next" function used to call next middleware. */ - next?: Function; + next?: TNext; } From 03c4d8f01c4e307b7c424ffd0c335f9e621e07bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Masum=20Go=CC=88kyu=CC=88z?= Date: Sat, 26 Oct 2024 09:05:51 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index bd0364de..1d5bd259 100644 --- a/README.md +++ b/README.md @@ -1154,6 +1154,39 @@ getOne(@Param("id") id: number) { You can use `@UseInterceptor` per-action, or per-controller. If its used per-controller then interceptor will apply to all controller actions. +### Generic Action Interface + +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: + +```typescript +import { Action } from 'routing-controllers'; +import { Request, Response } from 'express'; + +@JsonController() +export class UserController { + @Get('/users') + getUsers(@Req() req: Request, @Res() res: Response) { + // Your action implementation + } + + @Post('/users') + createUser(@Body() user: User, action: Action) { + // You can now use action.request and action.response with proper typing + console.log(action.request.headers); + action.response.status(201); + } +} +``` + +The `Action` interface now accepts four generic parameters: + +1. `TRequest`: The type of the request object (default: `any`) +2. `TResponse`: The type of the response object (default: `any`) +3. `TContext`: The type of the context object (for Koa-specific usage, default: `any`) +4. `TNext`: The type of the next function (default: `Function`) + +This change allows for more flexibility and type safety when working with different frameworks or custom request/response types in your routing-controllers applications. + ### Interceptor classes You can also create a class and use it with `@UseInterceptor` decorator: