Skip to content

Commit

Permalink
게스트 모집글 상세보기 mock api 구현 (#80)
Browse files Browse the repository at this point in the history
* feat: CommonErrorResponse 타입 작성

* feat: 경기 상세정보 가져오는 query 훅 작성
- useGameDetailQuery 훅 작성

* feat: 경기 상세정보 가져오는 mock api handler 작성
- mockGetGameDetail 작성

* fix: mockGetGameDetail handler 에러코드 수정
dlwl98 authored Nov 3, 2023
1 parent 836aa33 commit 36da334
Showing 3 changed files with 37 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/hooks/queries/useGameDetailQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useSuspenseQuery } from '@tanstack/react-query';

import { getGameDetail } from '@api/games/getGameDetail';

export const useGameDetailQuery = (id: number) => {
return useSuspenseQuery({
queryKey: ['game-detail', id],
queryFn: () => getGameDetail({ gameId: id }),
});
};
28 changes: 24 additions & 4 deletions src/mocks/handlers/game.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { HttpResponse, http } from 'msw';
import { DefaultBodyType, HttpResponse, PathParams, http } from 'msw';

import { PostGameRequest, PostGameResponse } from '@type/api/games';
import { CommonErrorResponse } from '@type/api/error';
import {
GetGameDetailResponse,
PostGameRequest,
PostGameResponse,
} from '@type/api/games';
import { Game, Member } from '@type/models';

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

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

export const gameHandlers = [mockPostGame, mockGetGames];
const mockGetGameDetail = http.get<
{ gameId: string },
DefaultBodyType,
GetGameDetailResponse | CommonErrorResponse
>('/api/games/:gameId', ({ params }) => {
const gameId = Number(params.gameId);
const game = games.find((game) => game.id === gameId);

if (!game) {
return HttpResponse.json({ code: 'COM-002' }, { status: 400 });
}

return HttpResponse.json(game);
});

export const gameHandlers = [mockPostGame, mockGetGames, mockGetGameDetail];
3 changes: 3 additions & 0 deletions src/type/api/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type CommonErrorResponse = {
code: string;
};

0 comments on commit 36da334

Please sign in to comment.