-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvanaApi.js
42 lines (36 loc) · 921 Bytes
/
vanaApi.js
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
import config from "./config";
/**
* Helper function to make Vana API calls
*/
const vanaApiFetch = async (path, options = {}) => {
const authToken = localStorage?.authToken ?? undefined;
if (authToken) {
options.headers = {
...options.headers,
Authorization: `Bearer ${authToken}`,
};
}
const response = await fetch(`${config.VANA_API_URL}/${path}`, options);
const data = await response.json();
if (response.ok && data.success === true) {
return data;
} else {
console.error(`Error calling ${path}`, data.message);
}
};
/**
* Vana API POST request
*/
const vanaApiPost = async (path, body) =>
vanaApiFetch(path, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(body),
});
/**
* Vana API GET request
*/
const vanaApiGet = async (path) => vanaApiFetch(path, {});
export { vanaApiGet, vanaApiPost };