aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/useGithubApi.tsx
blob: 4b0b3b232e7f6e416990258b0ce6774f48ed23ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;