Skip to content

Commit

Permalink
Merge pull request #626 from JayChae/Next.js-채종민-sprint10
Browse files Browse the repository at this point in the history
[채종민] sprint10
  • Loading branch information
kiJu2 authored Jun 10, 2024
2 parents 8b2c21f + 4d324e6 commit 9f4c90d
Show file tree
Hide file tree
Showing 57 changed files with 3,180 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
REACT_APP_API_KEY = https://panda-market-api.vercel.app/
REACT_APP_API_URL = https://panda-market-api.vercel.app/

NEXT_PUBLIC_BASE_URL=https://panda-market-api.vercel.app/
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts


.env
2 changes: 1 addition & 1 deletion apis/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
const panda_market_backend_api = process.env.NEXT_PUBLIC_BASE_URL;

export const baseAxios = axios.create({
export const axiosInstance = axios.create({
baseURL: panda_market_backend_api,
timeout: 20000,
});
40 changes: 40 additions & 0 deletions apis/getArticle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { axiosInstance } from "./api";

interface GetArticleParams {
articleId: number;
}

interface ArticleType {
updatedAt: string;
createdAt: string;
likeCount: number;
writer: {
id: number;
nickname: string;
};
image: string | null;
content: string;
title: string;
id: number;
}

export type GetArticleType = (prop: GetArticleParams) => Promise<ArticleType>;

const getArticle: GetArticleType = async ({ articleId }) => {
try {
const { data } = await axiosInstance.get<ArticleType>(
`articles/${articleId}`
);
return data;
} catch (error) {
console.error("APP ERROR: ", error);
if (error instanceof Error) {
throw new Error(`게시글을 가져오는 데 실패했습니다: ${error.message}`);
}
throw new Error(
"알 수 없는 오류로 인해 게시글을 가져오는 데 실패했습니다."
);
}
};

export default getArticle;
51 changes: 51 additions & 0 deletions apis/getArticleComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { axiosInstance } from "./api";

interface GetArticleCommentsParams {
articleId: number;
limit: number;
cursor?: number;
}

interface GetArticleCommentsReturn {
list: {
id: number;
content: string;
createdAt: string;
updatedAt: string;
writer: {
id: number;
nickname: string;
image: string | null;
};
}[];
nextCursor: number;
}


export type getArticleCommentsType = (
prop: GetArticleCommentsParams
) => Promise<GetArticleCommentsReturn>;

const getArticleComments: getArticleCommentsType = async ({
articleId,
limit,
cursor,
}) => {
try {
const { data } = await axiosInstance.get<GetArticleCommentsReturn>(
`articles/${articleId}/comments`,
{
params: { limit, cursor },
}
);
return data;
} catch (error) {
console.error("APP ERROR: ", error);
if (error instanceof Error) {
throw new Error(`댓글을 가져오는 데 실패했습니다: ${error.message}`);
}
throw new Error("알 수 없는 오류가 발생했어요 ㅠ.");
}
};

export default getArticleComments;
17 changes: 10 additions & 7 deletions apis/getArticles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { baseAxios } from "./api";
import { axiosInstance } from "./api";

interface propTypes {
interface GetArticlesParams {
page: number;
pageSize: number;
orderBy: string;
Expand All @@ -26,20 +26,23 @@ interface Articles {
totalCount: number;
}

export type GetArticleType = (prop: propTypes) => Promise<Articles | undefined>;
export type GetArticlesType = (
prop: GetArticlesParams
) => Promise<Articles | undefined>;

const getArticle: GetArticleType = async ({
const getArticles: GetArticlesType = async ({
page,
pageSize,
orderBy,
keyword,
}) => {
try {
const response = await baseAxios.get<Articles>(`articles`, {
const { data } = await axiosInstance.get<Articles>(`articles`, {
params: { page, pageSize, orderBy, keyword },
});
return response.data;
return data;
} catch (error) {
console.error("APP ERROR: ", error);
if (error instanceof Error) {
throw new Error(`게시글을 가져오는 데 실패했습니다: ${error.message}`);
}
Expand All @@ -49,4 +52,4 @@ const getArticle: GetArticleType = async ({
}
};

export { getArticle };
export { getArticles };
70 changes: 70 additions & 0 deletions apis/postArticle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { axiosInstance } from "./api";
import postImage from "./postImage";

interface PostArticleParams {
image?: File | null;
content: string;
title: string;
}

interface Response {
createdAt: string;
updatedAt: string;
likeCount: number;
writer: {
id: number;
nickname: string;
};
image: string | null;
content: string;
title: string;
id: number;
}

export type PostArticle = (prop: PostArticleParams) => Promise<Response>;

const postArticle: PostArticle = async ({ image, content, title }) => {
try {
const accessToken = localStorage.getItem("accessToken");
if (image) {
const imageUrl = await postImage({ image });
console.log(imageUrl);
const { data } = await axiosInstance.post<Response>(
`articles`,
{
image: imageUrl,
content,
title,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return data;
} else {
const { data } = await axiosInstance.post<Response>(
`articles`,
{
content,
title,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return data;
}
} catch (error) {
console.error("APP ERROR: ", error);
if (error instanceof Error) {
throw new Error(`게시글을 등록할 수 없습니다`, error);
}
throw new Error("알 수 없는 오류로 게시글을 등록할 수 없습니다.");
}
};

export default postArticle;
50 changes: 50 additions & 0 deletions apis/postArticleComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { axiosInstance } from "./api";

interface PostArticleCommentParams {
articleId: number;
content: string;
}

interface Response {
writer: {
image: string;
nickname: string;
id: number;
};
updatedAt: string;
createdAt: string;
content: string;
id: number;
}

export type PostArticleComment = (
prop: PostArticleCommentParams
) => Promise<Response>;
//Post 요청일 경우 보통 리스폰스를 받아서 확인하나요?

const postArticleComment: PostArticleComment = async ({
articleId,
content,
}) => {
try {
const accessToken = localStorage.getItem("accessToken");
const { data } = await axiosInstance.post<Response>(
`articles/${articleId}/comments`,
{ content },
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return data;
} catch (error) {
console.error("APP ERROR: ", error);
if (error instanceof Error) {
throw new Error(`댓글을 등록할 수 없습니다`);
}
throw new Error("알 수 없는 오류로 댓글을 등록할 수 없습니다.");
}
};

export default postArticleComment;
48 changes: 48 additions & 0 deletions apis/postArticleLike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { axiosInstance } from "./api";

interface PostArticleLikeParams {
articleId: number;
}

interface Response {
updatedAt: string;
createdAt: string;
likeCount: number;
writer: {
nickname: string;
id: number;
};
image: string | null;
content: string;
title: string;
id: number;
isLiked: boolean;
}

export type PostArticleLike = (
prop: PostArticleLikeParams
) => Promise<Response>;

const postArticleLike: PostArticleLike = async ({ articleId }) => {
try {
const accessToken = localStorage.getItem("accessToken");
const { data } = await axiosInstance.post<Response>(
`articles/${articleId}/like`,
{},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return data;
} catch (error) {
console.error("APP ERROR: ", error);
if (error instanceof Error) {
throw new Error(`좋아요를 할 수 없습니다`);
}
throw new Error("알 수 없는 오류로 인해 좋아요를 할 수 없습니다.");
}
};

export default postArticleLike;
33 changes: 33 additions & 0 deletions apis/postImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { axiosInstance } from "./api";

interface PostImageParams {
image: File;
}
export type PostImage = (prop: PostImageParams) => Promise<string>;

const postImage: PostImage = async ({ image }) => {
const formData = new FormData();
formData.append("image", image);
try {
const accessToken = localStorage.getItem("accessToken");
const { data } = await axiosInstance.post<Response>(
`images/upload`,
formData,
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "multipart/form-data",
},
}
);
return data.url;
} catch (error) {
console.error("APP ERROR: ", error);
if (error instanceof Error) {
throw new Error(`이미지 업로드 실패`);
}
throw new Error("알 수 없는 오류로 이미지 업로드 실패");
}
};

export default postImage;
Loading

0 comments on commit 9f4c90d

Please sign in to comment.