Skip to content

Commit

Permalink
refactor: Handle null or non-array data in getNews function
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaJi committed Aug 7, 2024
1 parent 053efda commit b0cc3d8
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ export interface FeaturedImage {
export async function getNews(game: string) {
const baseUrl = process.env.NEWS_API_URL;

const res = await fetch(`${baseUrl}?game=${game}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.NEWS_API_KEY ?? "",
},
next: {
tags: ["news-api"],
},
});
try {
const res = await fetch(`${baseUrl}?game=${game}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.NEWS_API_KEY ?? "",
},
next: {
tags: ["news-api"],
},
});

return res.json() as Promise<News[]>;
const data = await res.json() as Promise<News[]>;

return data ?? [];
} catch (error) {
console.error("Error fetching news:", error);
return [];
}
}

0 comments on commit b0cc3d8

Please sign in to comment.