Get / Post / Put / Delete
HTTP 라우트를 정의하는 데코레이터입니다. RouterController 클래스의 메서드에 적용하여 라우팅을 선언적으로 설정합니다.
Import
import { Get, Post, Put, Delete } from '@asapjs/router';
시그니처
function Get(path: string, options: IOptions): MethodDecorator
function Post(path: string, options: IOptions): MethodDecorator
function Put(path: string, options: IOptions): MethodDecorator
function Delete(path: string, options: IOptions): MethodDecorator
파라미터
| 이름 | 타입 | 설명 |
|---|---|---|
path | string | URL 경로 (예: '/', '/:id') |
options | IOptions | 라우트 옵션 (IPreference에서 path와 excute를 제외한 나머지) |
IOptions
IOptions는 IPreference 인터페이스에서 path와 excute를 제외한 타입입니다. 주로 Swagger 문서 생성과 라우트 동작 제어에 사용됩니다.
interface IOptions {
title?: string;
description?: string;
deprecated?: boolean;
summary?: string;
auth?: boolean;
body?: DtoOrTypeIs;
bodyContentType?: BodyContentType;
query?: DtoOrTypeIs;
response?: DtoOrTypeIs;
middleware?: any[];
}
| 필드 | 타입 | 기본값 | 설명 |
|---|---|---|---|
title | string | — | Swagger 문서의 operation title |
description | string | — | 라우트에 대한 상세 설명 (Swagger description) |
summary | string | — | 라우트 요약 (Swagger summary) |
deprecated | boolean | false | true로 설정 시 Swagger에서 deprecated 표시 |
auth | boolean | — | JWT 인증 필요 여부. true이면 jwtVerification 미들웨어 적용 |
body | DtoOrTypeIs | — | 요청 body의 DTO 클래스 또는 타입 정의. Swagger request body 스키마 생성에 사용 |
bodyContentType | BodyContentType | — | body의 Content-Type 지정 (예: multipart/form-data) |
query | DtoOrTypeIs | — | 쿼리 파라미터의 DTO 클래스 또는 타입 정의. Swagger query parameters 생성에 사용 |
response | DtoOrTypeIs | — | 응답 DTO 클래스 또는 타입 정의. Swagger response 스키마 생성에 사용 |
middleware | any[] | — | 이 라우트에만 적용할 추가 Express 미들웨어 배열 |
사용 예제 (상세 옵션)
import { Post } from '@asapjs/router';
import { CreateUserDto, UserResponseDto } from './dto';
@Post('/users', {
summary: '사용자 생성',
description: '새로운 사용자를 생성합니다. 이메일 중복 시 409를 반환합니다.',
auth: true,
body: CreateUserDto,
response: UserResponseDto,
middleware: [rateLimiter({ max: 10 })],
})
async createUser({ req, res }: ExecuteArgs) {
// ...
}
사용 예제
import { RouterController, Get, Post, Put, Delete } from '@asapjs/router';
import { ExecuteArgs } from '@asapjs/router';
export default class UserController extends RouterController {
@Get('/', {
summary: '사용자 목록 조회',
description: '전체 사용자 목록을 반환합니다',
})
async getUsers({ req, res }: ExecuteArgs) {
return { users: [] };
}
@Post('/', {
summary: '사용자 생성',
})
async createUser({ req, res }: ExecuteArgs) {
const { name, email } = req.body;
return { created: true };
}
@Put('/:id', {
summary: '사용자 수정',
})
async updateUser({ req, res }: ExecuteArgs) {
const { id } = req.params;
return { updated: true };
}
@Delete('/:id', {
summary: '사용자 삭제',
})
async deleteUser({ req, res }: ExecuteArgs) {
const { id } = req.params;
return { deleted: true };
}
}
동작 원리
- 데코레이터가 적용되면 해당 메서드 정보가
routes배열에 등록됩니다. RouterController가 초기화될 때 등록된 라우트를 Express Router에 바인딩합니다.- 각 라우트는
Wrapper를 통해 에러 처리가 자동 적용됩니다.
관련 API
- RouterController — 컨트롤러 베이스 클래스
- ExecuteArgs — 핸들러 함수 인자 타입
- RouterModule — 서버 모듈