Return

All tests / src/hooks useGithubStars.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 342x 2x 2x                 2x   2x   2x         1x         1x             2x  
import useSWR from 'swr';
import createFetcher from '@/helpers/createFetcher';
import { githubStarsSchema } from '../types/schemas';
 
/**
 * SDD-L07: this was `new URL(...)` at module scope, the only hook of seven that built its key that
 * way. Two consequences. The URL object became the SWR key, so the fetcher received an object where
 * every sibling passes a string; and with `NEXT_PUBLIC_DOMAIN` unset the constructor threw
 * `Invalid URL` while the module was still being imported — which fails `next build` outright rather
 * than failing the one widget. A template string like the other six does neither.
 */
const url = `${process.env.NEXT_PUBLIC_DOMAIN}/api/github-stars`;
 
const fetchGithubStars = createFetcher(githubStarsSchema, '/api/github-stars');
 
const useGithubStars = (): {
    data: number | undefined;
    error: Error | undefined;
    loading: boolean;
} => {
    const { data, error, isLoading } = useSWR<number, Error>(url, fetchGithubStars, {
        fallbackData: 0,
        dedupingInterval: 5000,
    });
 
    return {
        data,
        error,
        loading: isLoading,
    };
};
 
export default useGithubStars;