Skip to content

Commit

Permalink
chore(web):
Browse files Browse the repository at this point in the history
- reuse `fetchSafe`
- omit `github-actions[bot]` from contributors list
  • Loading branch information
SutuSebastian committed Aug 1, 2024
1 parent dc805db commit 0b0b95a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
8 changes: 5 additions & 3 deletions apps/web/components/homepage/contributors-section.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Tooltip } from "flowbite-react";
import Image from "next/image";
import Link from "next/link";
import { safeResJson } from "~/helpers/http";
import { fetchSafe } from "~/helpers/http";

interface Contributor {
id: number;
Expand All @@ -12,9 +12,11 @@ interface Contributor {

async function fetchContributors(): Promise<Contributor[]> {
try {
const result = await fetch("https://api.github.com/repos/themesberg/flowbite-react/contributors?per_page=21");
const result = await fetchSafe<Contributor[]>(
"https://api.github.com/repos/themesberg/flowbite-react/contributors?per_page=21",
);

return safeResJson(result);
return result.filter((contributor) => contributor.login !== "github-actions[bot]");
} catch (error) {
return [];
}
Expand Down
6 changes: 1 addition & 5 deletions apps/web/components/homepage/social-proof-section.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import Image from "next/image";
import { safeResJson } from "~/helpers/http";

async function fetchSafe<T>(endpoint: string): Promise<T> {
return safeResJson(await fetch(endpoint));
}
import { fetchSafe } from "~/helpers/http";

async function fetchStargazers(): Promise<string> {
try {
Expand Down
10 changes: 7 additions & 3 deletions apps/web/helpers/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export function safeResJson<T>(res: Response) {
if (res.ok) return res.json() as Promise<T>;
export async function fetchSafe<T>(endpoint: string): Promise<T> {
const response = await fetch(endpoint);

throw new Error("Internal server error!");
if (!response.ok) {
throw new Error("Internal server error!");
}

return response.json();
}

0 comments on commit 0b0b95a

Please sign in to comment.