requestType / responseType
이 페이지에서 찾을 수 있는 것
| 심볼 | 분류 | 설명 |
|---|---|---|
requestType | Function | 배열/중첩 DTO를 Swagger body 스키마로 변환 |
responseType | Function | 배열/중첩 DTO를 Swagger response 스키마로 변환 |
개요
requestType과 responseType은 배열이나 중첩 DTO 구조를 나타내고자 할 때 데코레이터의 body 및 response 옵션에 사용하는 헬퍼 팩토리입니다. Swagger 스키마 생성을 위한 타입 힌트를 제공합니다.
import { requestType, responseType } from '@asapjs/router';시그니처
const requestType: (
Dto: Constructor | true,
innerType?: 'body' | 'query',
isArray?: boolean
) => () => { type: 'body' | 'query'; data: any } | undefined | true
const responseType: (
Dto: Constructor | true,
innerType?: 'body' | 'query',
isArray?: boolean
) => () => { type: 'body' | 'query'; data: any } | undefined | true| 파라미터 | 타입 | 기본값 | 설명 |
|---|---|---|---|
Dto | Constructor | true | — | 래핑할 DTO 클래스. 타입은 존재하지만 스키마가 없음을 나타내려면 true를 전달합니다. |
innerType | 'body' | 'query' | 'body' | DTO가 body 컨텍스트를 설명하는지 query 컨텍스트를 설명하는지 지정합니다. |
isArray | boolean | false | true이면 Swagger용 { type: 'array', items: ... } 구조로 DTO 스키마를 감쌉니다. |
사용 예제
배열 요청/응답
import { Post, requestType, responseType, ExecuteArgs } from '@asapjs/router';
import CreateUserDto from '../dto/CreateUserDto';
import UserInfoDto from '../dto/UserInfoDto';
@Post('/batch', {
title: 'Batch create users',
body: requestType(CreateUserDto, 'body', true), // body에 CreateUserDto 배열
response: responseType(UserInfoDto, 'body', true), // 응답에 UserInfoDto 배열
})
async batchCreate({ body }: ExecuteArgs) {
return await this.userService.batchCreate(body as CreateUserDto[]);
}단일 DTO (배열 아닌 경우)
단일 DTO는 requestType/responseType 없이 데코레이터에 직접 전달하면 됩니다:
@Post('/', {
title: 'Create user',
body: CreateUserDto, // 직접 전달 — requestType 불필요
response: UserInfoDto, // 직접 전달 — responseType 불필요
})페이지네이션 응답
페이지네이션 응답에는 responseType 대신 @asapjs/sequelize의 TypeIs.PAGING(Dto)를 사용하세요. 이 메서드가 Swagger UI에 맞는 올바른 엔벨로프 스키마를 생성합니다:
import { TypeIs } from '@asapjs/schema';
@Get('/', {
title: 'List posts',
response: TypeIs.PAGING(PostInfoDto), // 페이지네이션 래핑 스키마
})내부 동작
두 함수는 내부적으로 동일한 returnType 헬퍼를 공유합니다. requestType은 returnType('request'), responseType은 returnType('response')로 생성됩니다. 반환된 팩토리 함수는 DTO 인스턴스의 swagger() 메서드를 호출하여 Swagger 스키마를 구성하고, isArray가 true이면 { type: 'array', items: ... } 구조로 감쌉니다.
관련 항목
- HTTP Method Decorators —
body와response옵션 - Swagger — 스키마 자동 생성 흐름
- DTO — DTO 클래스와
swagger()메서드
Last updated on