aboutsummaryrefslogtreecommitdiffstats
path: root/thallium-frontend/src/api/client.ts
blob: 1e23110bb2c042d9c1533a77e1131c81a5815359 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
TODO: Someday these methods should try pick out authentication from the Redux stores.
*/

const BASE_URL = THALLIUM_BASE_URL;

export class APIMissingTokenError extends Error {
    constructor() {
        super("No token available");
    }
}

interface APIErrorResponse {
    detail?: string;
}

export type APIRequestBody = Record<string, unknown>;

type APIResponseBody = Record<string, unknown>;

export class APIError extends Error {
    constructor(message: string, public status: number, public data: APIErrorResponse | null) {
        super(message);
    }
}

const request = async (url: string, options: RequestInit = {}): Promise<APIResponseBody> => {
    const response = await fetch(`${BASE_URL}${url}`, options);
    if (!response.ok) {
        let maybeData: APIErrorResponse | null;

        try {
            maybeData = await response.json() as APIErrorResponse;
        } catch (e) {
            maybeData = null;
        }

        throw new APIError(response.statusText, response.status, maybeData);
    }
    return response.json() as Promise<APIResponseBody>;
};

export const get = async (url: string, options: RequestInit = {}): Promise<APIResponseBody> => {
    return request(url, {
        ...options,
        method: "GET",
    });
};

export const post = async (url: string, data: APIRequestBody, options: RequestInit = {}): Promise<APIResponseBody> => {
    return request(url, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(data),
        ...options,
    });
};