Skip to content

Commit 36da334

Browse files
authored
게스트 모집글 상세보기 mock api 구현 (#80)
* feat: CommonErrorResponse 타입 작성 * feat: 경기 상세정보 가져오는 query 훅 작성 - useGameDetailQuery 훅 작성 * feat: 경기 상세정보 가져오는 mock api handler 작성 - mockGetGameDetail 작성 * fix: mockGetGameDetail handler 에러코드 수정
1 parent 836aa33 commit 36da334

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useSuspenseQuery } from '@tanstack/react-query';
2+
3+
import { getGameDetail } from '@api/games/getGameDetail';
4+
5+
export const useGameDetailQuery = (id: number) => {
6+
return useSuspenseQuery({
7+
queryKey: ['game-detail', id],
8+
queryFn: () => getGameDetail({ gameId: id }),
9+
});
10+
};

src/mocks/handlers/game.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { HttpResponse, http } from 'msw';
1+
import { DefaultBodyType, HttpResponse, PathParams, http } from 'msw';
22

3-
import { PostGameRequest, PostGameResponse } from '@type/api/games';
3+
import { CommonErrorResponse } from '@type/api/error';
4+
import {
5+
GetGameDetailResponse,
6+
PostGameRequest,
7+
PostGameResponse,
8+
} from '@type/api/games';
49
import { Game, Member } from '@type/models';
510

611
import { games } from '@mocks/data/game';
712

813
const mockPostGame = http.post<
9-
{ gameId: string },
14+
PathParams,
1015
{ data: PostGameRequest },
1116
PostGameResponse
1217
>('/api/games', async ({ request }) => {
@@ -59,4 +64,19 @@ const mockGetGames = http.get('/api/games', ({ request }) => {
5964
return HttpResponse.json(games.slice(startIndex, startIndex + size));
6065
});
6166

62-
export const gameHandlers = [mockPostGame, mockGetGames];
67+
const mockGetGameDetail = http.get<
68+
{ gameId: string },
69+
DefaultBodyType,
70+
GetGameDetailResponse | CommonErrorResponse
71+
>('/api/games/:gameId', ({ params }) => {
72+
const gameId = Number(params.gameId);
73+
const game = games.find((game) => game.id === gameId);
74+
75+
if (!game) {
76+
return HttpResponse.json({ code: 'COM-002' }, { status: 400 });
77+
}
78+
79+
return HttpResponse.json(game);
80+
});
81+
82+
export const gameHandlers = [mockPostGame, mockGetGames, mockGetGameDetail];

src/type/api/error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type CommonErrorResponse = {
2+
code: string;
3+
};

0 commit comments

Comments
 (0)