ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฏ NestJS Custom Decorators – ๋๋ง์ ๋ฐ์ฝ๋ ์ดํฐ ๋ง๋ค๊ธฐ ์์ ๊ฐ์ด๋
octo54 2025. 4. 24. 11:35๐ฏ NestJS Custom Decorators – ๋๋ง์ ๋ฐ์ฝ๋ ์ดํฐ ๋ง๋ค๊ธฐ ์์ ๊ฐ์ด๋
NestJS๋ Express ๊ธฐ๋ฐ ํ๋ ์์ํฌ์ง๋ง, ๋ฉํํ๋ก๊ทธ๋๋ฐ์ ์ ๊ทน ํ์ฉํ์ฌ ๋ฐ์ฝ๋ ์ดํฐ ๊ธฐ๋ฐ ๊ฐ๋ฐ์ ํต์ฌ ์ํคํ
์ฒ๋ก ์ผ๊ณ ์์ต๋๋ค.
์ด๋ฒ ๊ธ์์๋ NestJS ๊ณต์ ๋ฌธ์ Custom Decorators๋ฅผ ๊ธฐ๋ฐ์ผ๋ก,
**๋๋ง์ ๋ฐ์ฝ๋ ์ดํฐ(Custom Decorator)**๋ฅผ ๋ง๋ค๊ณ ์ค๋ฌด์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ํ๊ธ๋ก ํด์คํฉ๋๋ค.
๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ ๋ค๋ฃจ๋ฉด ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ, ๊ฐ๋ ์ฑ, ๊ตฌ์กฐํ๊ฐ ๋์ ๋๊ฒ ํฅ์๋ฉ๋๋ค.
โ Custom Decorator๋?
NestJS์์ @Body(), @Query() ๊ฐ์ ๊ธฐ๋ณธ ๋ฐ์ฝ๋ ์ดํฐ ์ธ์๋,
๊ฐ๋ฐ์๊ฐ ์ง์ ๋ง๋ ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ํตํด ๋ฐ๋ณต๋๋ ์ฝ๋๋ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ถ์ํํ ์ ์์ต๋๋ค.
1๏ธโฃ ์์ 1 – ์ฌ์ฉ์ ์ ๋ณด ์ถ์ถ ๋ฐ์ฝ๋ ์ดํฐ (@User())
โ ์ฌ์ฉ์ ์ ์ ๋ฐ์ฝ๋ ์ดํฐ ๋ง๋ค๊ธฐ
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
export const User = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.user;
},
);
- createParamDecorator()๋ฅผ ํตํด ํ๋ผ๋ฏธํฐ ๋ฐ์ฝ๋ ์ดํฐ ์์ฑ
- ctx.switchToHttp().getRequest()๋ก Express ์์ฒญ ๊ฐ์ฒด ์ ๊ทผ
โ ์ฌ์ฉ ์์
@Get('profile')
getProfile(@User() user: any) {
return user; // request.user ๊ฐ
}
Passport ๋ฑ์ ํตํด ์ธ์ฆ๋ ์ฌ์ฉ์์ ์ ๋ณด๋ฅผ ์์ฒญ ํธ๋ค๋ฌ์์ ๊ฐํธํ๊ฒ ์ถ์ถ ๊ฐ๋ฅ
์ด ๊ตฌ๊ฐ์ ์ปค์คํ ๋ฐ์ฝ๋ ์ดํฐ์ ์ค์ฌ์ฉ ์์ ๊ฐ ๋ฑ์ฅํ๋ ์ค์ํ ์ง์ ์ ๋๋ค.
์ ๋์ผ์ค ๊ด๊ณ ๋ฅผ ์ฝ์ ํ๋ฉด CTR์ด ๋๊ฒ ๋์ค๋ ๊ตฌ๊ฐ์ ๋๋ค.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXX"
data-ad-slot="YYYYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
2๏ธโฃ ์์ 2 – @Roles() ๋ฐ์ฝ๋ ์ดํฐ + Guard ์ฐ๋
โ Roles ๋ฐ์ฝ๋ ์ดํฐ ์ ์
import { SetMetadata } from '@nestjs/common';
export const Roles = (...roles: string[]) => SetMetadata('roles', roles);
- NestJS์์๋ SetMetadata()๋ฅผ ์ด์ฉํด ๋ฐ์ฝ๋ ์ดํฐ๋ก ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ ์ ์์
โ Guard์์ ๋ฉํ๋ฐ์ดํฐ ์ฝ๊ธฐ
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
const roles = this.reflector.get<string[]>('roles', context.getHandler());
const request = context.switchToHttp().getRequest();
const user = request.user;
return roles?.includes(user.role);
}
}
โ ์ ์ฉ ์์
@UseGuards(RolesGuard)
@Roles('admin')
@Get('admin')
getAdminPanel() {
return '๊ด๋ฆฌ์ ์ ์ฉ ํ์ด์ง์
๋๋ค.';
}
3๏ธโฃ ์์ 3 – Header์์ ํน์ ๊ฐ ์ถ์ถํ๋ ๋ฐ์ฝ๋ ์ดํฐ
export const Device = createParamDecorator(
(data: string, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.headers[data.toLowerCase()];
},
);
// ์ฌ์ฉ ์์
@Get()
handleRequest(@Device('User-Agent') userAgent: string) {
return `๋น์ ์ ๋ธ๋ผ์ฐ์ ๋: ${userAgent}`;
}
๐ง ์ค๋ฌด ํ
์ํฉ ์ถ์ฒ ๋ฐ์ฝ๋ ์ดํฐ
์ฌ์ฉ์ ์ ๋ณด ์ถ์ถ | @User() |
์์ฒญ ํค๋ ์ ๋ณด ์ถ์ถ | @Header('x-...') |
์ญํ /๊ถํ ์ค์ | @Roles('admin') |
๋ฉํ๋ฐ์ดํฐ ๋ถ๊ธฐ ์ฒ๋ฆฌ | @SetMetadata() + Reflector |
โ ๋ง๋ฌด๋ฆฌ ์์ฝ
๊ธฐ๋ฅ ์ค๋ช
createParamDecorator() | ํ๋ผ๋ฏธํฐ์์ ๊ฐ ์ถ์ถ |
SetMetadata() | ์ปค์คํ ๋ฉํ๋ฐ์ดํฐ ์ ์ |
Reflector | Guard, Interceptor ๋ฑ์์ ๋ฉํ๋ฐ์ดํฐ ์ฝ๊ธฐ |
์ฌ์ฌ์ฉ์ฑ | ์ค๋ณต ์ฝ๋ ์ ๊ฑฐ ๋ฐ ๊ตฌ์กฐํ ๊ฐ๋ฅ |
ํ์ฅ์ฑ | Guard, Interceptor, Pipe์ ์กฐํฉ ์ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ ๊ตฌํ ๊ฐ๋ฅ |
NestJS Custom Decorator,NestJS createParamDecorator,NestJS SetMetadata,NestJS Roles Decorator,NestJS Reflector ์ฌ์ฉ๋ฒ,NestJS ๋ฐ์ฝ๋ ์ดํฐ ํ์ฅ,NestJS ์ฌ์ฉ์ ์ถ์ถ,NestJS Request ๋ฐ์ฝ๋ ์ดํฐ,NestJS ๊ถํ ์ค์ ,NestJS ์ค๋ฌด ๊ตฌ์กฐ ์ค๊ณ
'framework > NestJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- Next.js
- LangChain
- kotlin
- AI ์๋ํ
- ๋ฐฑ์๋๊ฐ๋ฐ
- App Router
- fastapi
- ์ค๋งํธ ์ปจํธ๋ํธ
- gatsbyjs
- ๋ฐฑ์๋
- CI/CD
- rag
- nodejs
- llm
- ๊ด๋ฆฌ์
- REACT
- Ktor
- AI์ฑ๋ด
- seo ์ต์ ํ 10๊ฐ
- Webpack
- github
- NestJS
- SEO์ต์ ํ
- ์น๊ฐ๋ฐ
- PostgreSQL
- nextJS
- Prisma
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
- ํ๋ก ํธ์๋
- Docker
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |