api.ts 487 B

1234567891011121314151617
  1. import type { ApiPaths } from "../types/api"
  2. const apiBase = "https://api.joinmastodon.org/"
  3. const getApiUrl = (path: ApiPaths, params?: string | URLSearchParams) =>
  4. params ? `${apiBase}${path}?${params}` : `${apiBase}${path}`
  5. export const fetchEndpoint = async (
  6. endpoint: ApiPaths,
  7. params?: string | URLSearchParams
  8. ): Promise<any[] | any> => {
  9. const res = await fetch(getApiUrl(endpoint, params))
  10. if (!res.ok) throw new Error(res.statusText)
  11. return await res.json()
  12. }