import useSWR, { responseInterface } from 'swr'; import { User, Poll, Feedback } from 'which-types'; import { get } from '../requests'; type Response = responseInterface; const fetcher = (endpoint: string) => get(endpoint).then(response => response.data); 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) as Response; }; export const useFeed = (): Response => { return useSWR('/feed', fetcher, { revalidateOnFocus: false }) as Response; }; export const useFeedback = (): Response => { return useSWR('/feedback', fetcher) as Response; };