add recorator

This commit is contained in:
2025-11-16 10:07:53 +03:30
parent 64ac2ead36
commit ad4b2e2c89
11 changed files with 119 additions and 47 deletions
+3
View File
@@ -0,0 +1,3 @@
export { UserId } from './user-id.decorator';
export { RestId } from './rest-id.decorator';
export { RateLimit } from './rate-limit.decorator';
@@ -0,0 +1,18 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
/**
* Decorator to extract restId from the authenticated request.
* Must be used after AdminAuthGuard or AuthGuard that sets request.restId.
*
* @example
* @Get('/restaurants')
* @UseGuards(AdminAuthGuard)
* getRestaurants(@RestId() restId: string) {
* return this.restaurantService.findById(restId);
* }
*/
export const RestId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
const request = ctx.switchToHttp().getRequest<Request & { restId?: string }>();
return request.restId || '';
});
@@ -0,0 +1,18 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
/**
* Decorator to extract userId from the authenticated request.
* Must be used after AdminAuthGuard or AuthGuard that sets request.userId.
*
* @example
* @Get('/profile')
* @UseGuards(AdminAuthGuard)
* getProfile(@UserId() userId: string) {
* return this.userService.findById(userId);
* }
*/
export const UserId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
const request = ctx.switchToHttp().getRequest<Request & { userId?: string }>();
return request.userId || '';
});