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}`) =>