import useSWR, { responseInterface } from 'swr'; import { User, Poll, Feedback } from 'which-types'; import { get } from '../requests'; interface Response extends responseInterface { data: T; } const fetcher = (endpoint: string) => get(endpoint).then(response => response.data); const arrayOptions = { initialData: [], revalidateOnMount: true }; export const useUser = (username: string | null): Response => { return useSWR( username && `/users?username=${username}`, (url: string) => get(url).then(response => response.data[0]) ) as Response; }; export const useProfile = (id: string): Response => { return useSWR(id && `/profiles/${id}`, fetcher, arrayOptions) as Response; }; export const useFeed = (): Response => { return useSWR('/feed', fetcher, { ...arrayOptions, revalidateOnFocus: false }) as Response; }; export const useFeedback = (): Response => { return useSWR('/feedback', fetcher, arrayOptions) as Response; };