Skip to content

Commit

Permalink
🐛 添加 GitHub API fetch 数据错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Skywt2003 committed Mar 29, 2024
1 parent 839a851 commit 88fe567
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/components/buttons/GithubButton.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ interface Props {
const { type, target } = Astro.props;
const url = new URL("https://api.github.com/repos/" + target);
// var headers = new Headers();
// headers.append("Accept", "application/vnd.github+json");
// headers.append("X-GitHub-Api-Version", "2022-11-28");
// const response = await fetch(url, { headers: headers });
const response = await fetch(url);
const data = await response.json();
const starsCount = data.stargazers_count ? data.stargazers_count : "?";
const forksCount = data.forks ? data.forks : "?";
let starsCount, forksCount;
try {
const response = await fetch(url);
const data = await response.json();
starsCount = data.stargazers_count ? data.stargazers_count : "?";
forksCount = data.forks ? data.forks : "?";
} catch(e) {
console.log(e);
starsCount = "?";
forksCount = "?";
}
---

{
Expand Down

0 comments on commit 88fe567

Please sign in to comment.