-
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.
feat(home): fetch stargazers from API
- Loading branch information
Showing
5 changed files
with
84 additions
and
2 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
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
30 changes: 30 additions & 0 deletions
30
docs/.vitepress/theme/composables/api/useStargazers.spec.ts
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,30 @@ | ||
import axios from "axios"; | ||
import {describe, expect, it, vi} from "vitest"; | ||
import {useStargazers} from "./useStargazers"; | ||
|
||
vi.mock("axios"); | ||
|
||
describe("useStargazers", () => { | ||
it("should fetch the Github Stargazers successfully", async () => { | ||
const mockData = { | ||
id: "id", | ||
stargazers_count: 100 | ||
}; | ||
vi.mocked(axios.get).mockResolvedValue({data: mockData}); | ||
|
||
const {stargazers, fetchStargazers} = useStargazers("tsedio/tsed"); | ||
await fetchStargazers(); | ||
|
||
expect(stargazers.value).toEqual(100); | ||
expect(axios.get).toHaveBeenCalledWith("https://repos/api.github.com/tsedio/tsed"); | ||
}); | ||
|
||
it("should handle errors when fetching Github Stargazers", async () => { | ||
vi.mocked(axios.get).mockRejectedValue(new Error("Network Error")); | ||
|
||
const {stargazers, fetchStargazers} = useStargazers("tsedio/tsed", 200); | ||
await fetchStargazers(); | ||
|
||
expect(stargazers.value).toEqual(200); | ||
}); | ||
}); |
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,40 @@ | ||
import {ref, type Ref} from "vue"; | ||
import axios from "axios"; | ||
import {getHost} from "./useGithubContributors"; | ||
|
||
export interface GithubRepository { | ||
id: number; | ||
html_url: string; | ||
stargazers_count: 2802; | ||
watchers_count: 2802; | ||
forks_count: 284; | ||
open_issues_count: 47; | ||
} | ||
|
||
function format(stargazers: Ref<number>) { | ||
return new Intl.NumberFormat(undefined, { | ||
notation: "compact", | ||
compactDisplay: "short" | ||
}).format(stargazers.value); | ||
} | ||
|
||
export function useStargazers(docsRepo: string, defaultValue = 0) { | ||
const stargazers = ref<number>(defaultValue); | ||
|
||
const fetchStargazers = async () => { | ||
const endpoint = getHost(docsRepo); | ||
try { | ||
const { | ||
data: {stargazers_count} | ||
} = await axios.get<GithubRepository>(endpoint); | ||
|
||
stargazers.value = stargazers_count; | ||
|
||
return stargazers; | ||
} catch (error) { | ||
stargazers.value = defaultValue; | ||
} | ||
}; | ||
|
||
return {stargazers, formattedStargazers: format(stargazers), fetchStargazers}; | ||
} |
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