Skip to content

Commit

Permalink
fix(doc-ui): 优化用户和贡献者数据获取逻辑,增强错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Dec 17, 2024
1 parent 45f3923 commit 49f39d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-papayas-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@iringo/doc-ui": patch
---

优化用户和贡献者数据获取逻辑,增强错误处理
15 changes: 9 additions & 6 deletions packages/doc-ui/src/contributors/contributor-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ const fetchUser = async (username: string): Promise<User> => {
} 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) =>
Expand Down
15 changes: 9 additions & 6 deletions packages/doc-ui/src/contributors/contributors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ const fetchContributors = async (repo: string): Promise<Contributor[]> => {
} 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}`) =>
Expand Down

0 comments on commit 49f39d6

Please sign in to comment.