import useSWR, { SWRResponse } from "swr" const API_URL = import.meta.env.VITE_API_URL const fetcher = (path: string) => fetch(`${API_URL}${path}`) .then(res => res.json()) .catch((err) => { console.error(`An error occurred when fetching API path ${path}`) console.error(err) throw err }) interface Snippet { id: string title: string content: string } interface APISuccess { readonly type: "success" data: D } interface APILoading { readonly type: "loading" } interface APIFailed { readonly type: "failed" error: E } type APIResult = APISuccess | APILoading | APIFailed const mapSWRResult = ({ data, error }: SWRResponse): APIResult => { if (error !== undefined) { return { type: "failed", error } } const isLoading = data === undefined if (isLoading) { return { type: "loading" } } return { type: "success", data } } export const useSnippets = () => mapSWRResult( useSWR("/snippets", fetcher), )