diff options
Diffstat (limited to 'src/utils/hooks/useGithubApi.tsx')
| -rw-r--r-- | src/utils/hooks/useGithubApi.tsx | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utils/hooks/useGithubApi.tsx b/src/utils/hooks/useGithubApi.tsx new file mode 100644 index 0000000..4b0b3b2 --- /dev/null +++ b/src/utils/hooks/useGithubApi.tsx @@ -0,0 +1,23 @@ +import { RepoData } from '@ts/types/repos'; +import useSWR, { Fetcher } from 'swr'; + +const fetcher: Fetcher<RepoData, string> = (...args) => + fetch(...args).then((res) => res.json()); + +/** + * Retrieve data from Github API. + * @param repo The repo name. Format: "User/project-slug". + * @returns {object} The data and two booleans to determine if is loading/error. + */ +const useGithubApi = (repo: string) => { + const apiUrl = repo ? `https://api.github.com/repos/${repo}` : null; + const { data, error } = useSWR<RepoData>(apiUrl, fetcher); + + return { + data, + isLoading: !error && !data, + isError: error, + }; +}; + +export default useGithubApi; |
