srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/api.js
blob: 36510674b021beba5bd7991ea640d5b3b3cbc67a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Thin fetch wrapper for the typerpunk-server backend. Base URL is
// same-origin by default (a reverse proxy is expected to route /api/* to the
// backend in that setup); window.TYPERPUNK_API_URL overrides it for local
// dev or any split frontend/backend hosting, mirroring the existing
// window.TYPERPUNK_TEXTS_URL override pattern already used for the dataset.
const BASE_URL = (typeof window !== 'undefined' && window.TYPERPUNK_API_URL) || '';

export class ApiError extends Error {
    constructor(status, message) {
        super(message);
        this.status = status;
    }
}

async function request(method, path, body) {
    const res = await fetch(`${BASE_URL}${path}`, {
        method,
        credentials: 'include',
        headers: body !== undefined ? { 'Content-Type': 'application/json' } : undefined,
        body: body !== undefined ? JSON.stringify(body) : undefined,
    });

    let data = null;
    try { data = await res.json(); } catch { /* empty body, e.g. logout */ }

    if (!res.ok) {
        throw new ApiError(res.status, (data && data.error) || `request failed (${res.status})`);
    }
    return data;
}

export const api = {
    get: path => request('GET', path),
    post: (path, body) => request('POST', path, body ?? {}),
    delete: path => request('DELETE', path),
};

// For the rare case a caller needs a real page navigation instead of a
// fetch - the Spotify OAuth login link has to be an actual top-level
// navigation so the browser can load Spotify's own consent screen.
export function apiUrl(path) {
    return `${BASE_URL}${path}`;
}