Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Request, Response>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The action parameter here is never resolved

// 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:
Expand Down
10 changes: 5 additions & 5 deletions src/Action.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/**
* Controller action properties.
*/
export interface Action {
export interface Action<TRequest = any, TResponse = any, TContext = any, TNext extends Function = Function> {
/**
* 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;
}