summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/api/index.js
blob: e6528fd95b7d89911df05db2bb3c84eb9ac6fd52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import config from '../config'
import { getAuthToken } from '../auth'

const apiUrl = config['API_BASE_URL']

export async function request(path, method = 'GET', body) {
    const res = await fetch(`${apiUrl}/v2/${path}`, {
        method: method,
        headers: {
            'auth-token': getAuthToken(),
            'Content-Type': 'application/json',
        },
        body: body && JSON.stringify(body),
    })
    const { status, content } = await res.json()

    if (status.code !== 200) {
        throw status
    }

    return content
}