From 49f39d60c806c96f6242c01d404f044924591ff0 Mon Sep 17 00:00:00 2001 From: baranwang Date: Tue, 17 Dec 2024 16:11:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(doc-ui):=20=E4=BC=98=E5=8C=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=92=8C=E8=B4=A1=E7=8C=AE=E8=80=85=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/hot-papayas-unite.md | 5 +++++ .../doc-ui/src/contributors/contributor-item.tsx | 15 +++++++++------ packages/doc-ui/src/contributors/contributors.tsx | 15 +++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 .changeset/hot-papayas-unite.md diff --git a/.changeset/hot-papayas-unite.md b/.changeset/hot-papayas-unite.md new file mode 100644 index 0000000..57df97f --- /dev/null +++ b/.changeset/hot-papayas-unite.md @@ -0,0 +1,5 @@ +--- +"@iringo/doc-ui": patch +--- + +优化用户和贡献者数据获取逻辑,增强错误处理 diff --git a/packages/doc-ui/src/contributors/contributor-item.tsx b/packages/doc-ui/src/contributors/contributor-item.tsx index 54fb49b..a9590bd 100644 --- a/packages/doc-ui/src/contributors/contributor-item.tsx +++ b/packages/doc-ui/src/contributors/contributor-item.tsx @@ -27,14 +27,17 @@ const fetchUser = async (username: string): Promise => { } catch (error) {} } - const response = await fetch(`https://api.github.com/users/${username}`); - if (!response.ok) { + try { + const response = await fetch(`https://api.github.com/users/${username}`); + if (!response.ok) { + return { login: username } as User; + } + const user = await response.json(); + sessionStorage.setItem(`user_${username}`, JSON.stringify(user)); + return user; + } catch (error) { return { login: username } as User; } - - const user = await response.json(); - sessionStorage.setItem(`user_${username}`, JSON.stringify(user)); - return user; }; const createUserCard = (username: string) => diff --git a/packages/doc-ui/src/contributors/contributors.tsx b/packages/doc-ui/src/contributors/contributors.tsx index a67a6d6..ad8cec6 100644 --- a/packages/doc-ui/src/contributors/contributors.tsx +++ b/packages/doc-ui/src/contributors/contributors.tsx @@ -17,14 +17,17 @@ const fetchContributors = async (repo: string): Promise => { } catch (error) {} } - const response = await fetch(`https://api.github.com/repos/${repo}/contributors`).catch(() => undefined); - if (!response?.ok) { + try { + const response = await fetch(`https://api.github.com/repos/${repo}/contributors`); + if (!response.ok) { + return []; + } + const contributors = await response.json(); + sessionStorage.setItem(`contributors_${repo}`, JSON.stringify(contributors)); + return contributors; + } catch (error) { return []; } - - const contributors = await response.json(); - sessionStorage.setItem(`contributors_${repo}`, JSON.stringify(contributors)); - return contributors; }; const createContributorsComponent = (repo: `${string}/${string}`) =>