RouterController
RouterController는 라우터를 구성할 때 사용합니다.
상위 라우터 구성
기본적인 사용은 라우터 class에 직접적으로 extends 해서 사용하는 것을 원칙으로 합니다.
import { RouterController } from "@asapjs/router";
class PostController extends RouterController {
public tag = "게시글 관련";
public basePath = "/posts";
}
asap.js는 Swagger 기반의 API 문서 자동 생성 기능을 제공하고 있어 상위 컨트롤러에 아래와 같은 정보를 추가적으로 기재해야 합니다.
상위 라우터 설정 파라미터 설명
| 이름 | 필수 여부 | 타입 | 비고 |
|---|---|---|---|
tag | O | string | 상위 라우터 카테고리 이름 |
basePath | O | string | 상위 라우터 기본 경로 |
하위 라우터 구성
해당 class는 Express.js 라이브러리를 기반으로 구성되어 있습니다.
this.app.get({
// 라우터 이름
title: "게시글 리스트 조회",
// 하위 라우터
path: "/",
// 인증 필수 여부
auth: true,
// 쿼리 DTO
query: GetPostDto,
// 응답 DTO
response: TypeIs.ARRAY(PostDTO),
// 실행 Function
excute: this.GetPostListController,
});
하위 라우터 생성 파라미터 설명
| 이름 | 필수 여부 | 타입 | 비고 |
|---|---|---|---|
title | O | string | Swagger에 노출되는 라우터 이름 |
path | O | string | 하위 라우터 경로 |
auth | X | boolean | 인증 필수 여부 |
query | X | Dto | 쿼리 파라미터 DTO |
body | X | TypeIs | Dto | JSON 요청 BODY DTO |
response | X | TypeIs | Dto | JSON 응답 BODY DTO |
excute | X | function | 라우터 실행 함수 |
다른 HTTP method에 대한 Example은 다음과 같습니다.
this.app.get(...props);
this.app.post(...props);
this.app.put(...props);
this.app.delete(...props);
Full Example
import { ExecuteArgs, RouterController } from "@asapjs/router";
import { PostApplication } from "@post/application/PostApplication";
import { TypeIs } from "@asapjs/sequelize";
import PostDto from "@post/dto/PostDto";
import GetPostDto from "@post/dto/GetPostDto";
import CreatePostDto from "@post/dto/CreatePostDto";
import UpdatePostDto from "@post/dto/UpdatePostDto";
export default class PostController extends RouterController {
public tag = "게시글 관련";
public basePath = "/posts";
private postService: PostApplication;
constructor() {
super();
this.initializeRoutes();
this.postService = new PostApplication();
}
private initializeRoutes() {
this.router.get({
title: "게시글 리스트 조회",
path: "/",
auth: true,
query: GetPostDto,
response: TypeIs.ARRAY(PostDto),
excute: this.PostListController,
});
this.router.get({
title: "게시글 상세 조회",
path: "/:postId",
auth: true,
response: PostDto,
excute: this.PostInfoController,
});
this.router.post({
title: "게시글 등록",
path: "/",
auth: true,
body: CreatePostDto,
response: TypeIs.BOOLEAN(),
excute: this.CreatePostController,
});
this.router.put({
title: "게시글 수정",
path: "/:postId",
auth: true,
body: UpdatePostDto,
response: TypeIs.BOOLEAN(),
excute: this.UpdatePostController,
});
this.router.delete({
title: "게시글 삭제",
path: "/:postId",
auth: true,
response: TypeIs.BOOLEAN(),
excute: this.DeletePostController,
});
}
private PostListController = async ({
query,
}: ExecuteArgs<undefined, GetPostDto>) => {
const result = await this.postService.getPostList(query?.type);
return { result };
};
private PostInfoController = async ({
path,
}: ExecuteArgs<undefined, GetPostDto>) => {
const result = await this.postService.getPostInfo(path?.postId);
return { result };
};
private CreatePostController = async ({
body,
}: ExecuteArgs<undefined, undefined, CreatePostDto>) => {
const result = await this.postService.createPost(body);
return { result };
};
private UpdatePostController = async ({
path,
body,
}: ExecuteArgs<undefined, undefined, CreatePostDto>) => {
const result = await this.postService.updatePost(path?.postId, body);
return { result };
};
private DeletePostController = async ({
path,
body,
}: ExecuteArgs<undefined, undefined, CreatePostDto>) => {
const result = await this.postService.removePost(path?.postId);
return { result };
};
}