-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
46 lines (39 loc) · 1.1 KB
/
api.js
File metadata and controls
46 lines (39 loc) · 1.1 KB
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
import { getApiKey, getApiUrl } from './config.js';
function headers(auth = true) {
const h = { 'Content-Type': 'application/json' };
if (auth) {
const key = getApiKey();
if (!key) {
throw new Error('No API key configured. Run: bool auth login');
}
h['Authorization'] = `Bearer ${key}`;
}
return h;
}
async function request(method, path, body, auth = true) {
const url = `${getApiUrl()}${path}`;
const opts = { method, headers: headers(auth) };
if (body !== undefined) opts.body = JSON.stringify(body);
const res = await fetch(url, opts);
if (res.status === 204) return null;
const data = await res.json();
if (!res.ok) {
throw new Error(data.error || `API error: ${res.status}`);
}
return data;
}
export function get(path) {
return request('GET', path);
}
export function post(path, body) {
return request('POST', path, body);
}
export function patch(path, body) {
return request('PATCH', path, body);
}
export function del(path) {
return request('DELETE', path);
}
export function healthCheck() {
return request('GET', '/health/', undefined, false);
}