Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/fetchArticles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ArticleListResponse } from "@/types/articleTypes";

export default async function fetchArticles(
page: number = 1,
pageSize: number = 10,
orderBy: "recent" | "like" = "recent",
keyword: string = ""
): Promise<ArticleListResponse> {
const url = `https://panda-market-api.vercel.app/articles?page=${page}&pageSize=${pageSize}&orderBy=${orderBy}&keyword=${keyword}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게되면 가독성이 좋진 않은것같아요ㅠ
그리고 서버에 따라서는 keyword가 처럼 공백문자인 경우에 이상한 값을 줄수있어서 optional이라면 아에 안보내는게 조금더 좋아요ㅎㅎ;;

그래서 이런식으로 하면 어떨까요?
나중에 추가되는 값을 바로바로 볼 수 있고, 다루기도 쉬울것같아서요!

const url = new URL("https://panda-market-api.vercel.app/articles");

url.searchParams.append("page", page.toString());
url.searchParams.append("pageSize", pageSize.toString());
url.searchParams.append("orderBy", orderBy);
url.searchParams.append("keyword", keyword);


try {
const response = await fetch(url);

if (!response.ok) {
throw new Error(`Failed to fetch articles: ${response.statusText}`);
}

const data: ArticleListResponse = await response.json();
return data;
} catch (error) {
console.error("Error fetching articles:", error);
return {
totalCount: 0,
list: [],
};
}
Comment on lines +20 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

차라리 실패했을땐 error를 throw 하는게 좀더 좋을 수 있어요ㅎㅎ

throw new Error(error);

}
7 changes: 5 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}
images: {
domains: ["sprint-fe-project.s3.ap-northeast-2.amazonaws.com"], // 허용할 도메인 추가
},
Comment on lines +4 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

};

module.exports = nextConfig
module.exports = nextConfig;
Loading
Loading