This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client-side fetch logic for repository
- Loading branch information
1 parent
80702c2
commit 72dda38
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// GitHub repository owner and name | ||
const owner = '<owner>'; | ||
const repoName = '<repo>'; | ||
|
||
// GitHub REST API URL to get the repository information | ||
const apiUrl = `https://api.github.com/repos/${owner}/${repoName}`; | ||
|
||
// Function to format a number in shorthand like 'k' | ||
function formatNumberShorthand(number) { | ||
if (number >= 1000) { | ||
return (number / 1000).toFixed(1) + 'k'; | ||
} | ||
return number.toString(); | ||
} | ||
|
||
|
||
// Function to fetch and display repository information, including the latest release | ||
async function getRepoInfo() { | ||
try { | ||
|
||
// Dynamically import the 'node-fetch' module as an ES module | ||
const fetch = (await import('node-fetch')).default; | ||
|
||
// Send an HTTP GET request to the GitHub API | ||
const response = await fetch(apiUrl); | ||
const repoInfo = await response.json(); | ||
|
||
// Check if the response contains the desired information | ||
if (response.status === 200) { | ||
const starsCount = repoInfo.stargazers_count; | ||
const forksCount = repoInfo.forks_count; | ||
|
||
// Fetch the latest release information | ||
const releaseUrl = `${apiUrl}/releases/latest`; | ||
const releaseResponse = await fetch(releaseUrl); | ||
const releaseInfo = await releaseResponse.json(); | ||
const latestRelease = releaseInfo.tag_name; | ||
|
||
console.log(`Stars: ${formatNumberShorthand(starsCount)}`); | ||
console.log(`Forks: ${formatNumberShorthand(forksCount)}`); | ||
console.log(`Latest Release: ${latestRelease}`); | ||
} else { | ||
console.log('Error fetching repository information.'); | ||
} | ||
} catch (error) { | ||
console.error("Error fetching data:", error); | ||
} | ||
} | ||
|