Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mastodon
GitHub Repository: mastodon/joinmastodon
Path: blob/main/utils/api.ts
1006 views
1
import type { ApiPaths } from "../types/api"
2
3
const apiBase = "https://api.joinmastodon.org/"
4
5
const getApiUrl = (path: ApiPaths, params?: string | URLSearchParams) =>
6
params ? `${apiBase}${path}?${params}` : `${apiBase}${path}`
7
8
export const fetchEndpoint = async <Result = any | any[]>(
9
endpoint: ApiPaths,
10
params?: string | URLSearchParams
11
): Promise<Result> => {
12
const res = await fetch(getApiUrl(endpoint, params))
13
if (!res.ok) throw new Error(res.statusText)
14
15
return await res.json()
16
}
17
18