summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/api/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-web/opendc-web-ui/src/api/index.js')
-rw-r--r--opendc-web/opendc-web-ui/src/api/index.js31
1 files changed, 20 insertions, 11 deletions
diff --git a/opendc-web/opendc-web-ui/src/api/index.js b/opendc-web/opendc-web-ui/src/api/index.js
index cefcb2c5..e6528fd9 100644
--- a/opendc-web/opendc-web-ui/src/api/index.js
+++ b/opendc-web/opendc-web-ui/src/api/index.js
@@ -1,13 +1,22 @@
-import { sendSocketRequest } from './socket'
-
-export function sendRequest(request) {
- return new Promise((resolve, reject) => {
- sendSocketRequest(request, (response) => {
- if (response.status.code === 200) {
- resolve(response.content)
- } else {
- reject(response)
- }
- })
+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
}