더북(TheBook)

15.12.1 API 함수 준비하기

댓글 기능 구현에 필요한 API를 준비해줍니다. 기존에 만든 api/comments.ts 파일을 열어서 writeComment, modifyComment, deleteComment 함수를 다음과 같이 작성해보세요.

api/comments.ts

import client from './client';
import {Comment} from './types';

export async function getComments(articleId: number) {
  const response = await client.get<Comment[]>(
    `/articles/${articleId}/comments`,
  );
  return response.data;
}

export async function writeComment(params: {
  articleId: number;
  message: string;
}) {
  const {articleId, message} = params;
  const response = await client.post<Comment>(
    `/articles/${articleId}/comments`,
    {message},
  );
  return response.data;
}

export async function modifyComment(params: {
  articleId: number;
  message: string;
  id: number;
}) {
  const {articleId, message, id} = params;
  const response = await client.put<Comment>(
    `/articles/${articleId}/comments/${id}`,
    {message},
  );
  return response.data;
}

export async function deleteComment(params: {articleId: number; id: number}) {
  const {articleId, id} = params;
  await client.delete(`/articles/${articleId}/comments/${id}`);
  return null;
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.