api.ts 482 B

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