From b8eb008dd5927fb736e56699637f5f8549965eae Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 6 Dec 2023 18:20:54 +0100 Subject: refactor(hooks): replace useGithubApi with useGithubRepoMeta * use GraphQL API instead of REST (the inconvenient however is that we now need an authorization token...) * move fetcher in services * add tests * mock response using MSW --- .../use-github-repo-meta/use-github-repo-meta.ts | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts (limited to 'src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts') diff --git a/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts b/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts new file mode 100644 index 0000000..888682e --- /dev/null +++ b/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts @@ -0,0 +1,37 @@ +import useSWR from 'swr'; +import { + type FetchGithubRepoMetaInput, + fetchGithubRepoMeta, +} from '../../../services/github'; +import type { GithubRepositoryMeta, Maybe } from '../../../types'; + +export type UseGithubRepoMetaReturn> = { + isError: boolean; + isLoading: boolean; + isValidating: boolean; + meta: T extends undefined + ? Maybe + : GithubRepositoryMeta; +}; + +export const useGithubRepoMeta = >( + input: FetchGithubRepoMetaInput, + fallback?: T +) => { + const { data, error, isLoading, isValidating } = useSWR( + input, + fetchGithubRepoMeta, + { + fallbackData: fallback, + } + ); + + if (error) console.error(error); + + return { + isError: !!error, + isLoading, + isValidating, + meta: data, + } as UseGithubRepoMetaReturn; +}; -- cgit v1.2.3