From 744cd488796a4ac00bff4721a225145f5195bf9d Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Tue, 23 Jun 2026 11:29:38 +0200 Subject: [PATCH] docs(custom-decorators): specify getRequest type --- content/custom-decorators.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/content/custom-decorators.md b/content/custom-decorators.md index 31bec3fdf5..e15f1c4282 100644 --- a/content/custom-decorators.md +++ b/content/custom-decorators.md @@ -95,6 +95,21 @@ async findOne(user) { } ``` +> info **Hint** For TypeScript users, note that `ctx.switchToHttp().getRequest()` depends on the framework used, for example if you are using express you can specify the appropriate type +> +> ```typescript +> @@filename(user.decorator) +> import { createParamDecorator, ExecutionContext } from '@nestjs/common'; +> import type { Request } from 'express'; +> export const Page = createParamDecorator( +> (data: never, ctx: ExecutionContext) => { +> const request = ctx.switchToHttp().getRequest(); +> return request.query['page'] ?? 0; +> }, +> ); +> ``` + + #### Passing data When the behavior of your decorator depends on some conditions, you can use the `data` parameter to pass an argument to the decorator's factory function. One use case for this is a custom decorator that extracts properties from the request object by key. Let's assume, for example, that our authentication layer validates requests and attaches a user entity to the request object. The user entity for an authenticated request might look like: