-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
64 lines (57 loc) · 1.6 KB
/
client.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fetch from 'node-fetch';
const apiBaseUrl = process.env.API_BASE_URL || 'http://localhost:8080/api/';
const appUser = process.env.APP_USER || 'admin';
const appPassword = process.env.APP_PASSWORD || 'root';
const get = async (url, qs) => {
let result;
if (url.startsWith('http')) {
result = await fetch(url,
{
headers: {
'Authorization': 'Basic ' + Buffer.from(appUser + ':' + appPassword)
.toString('base64'),
},
});
} else {
result = await fetch(apiBaseUrl+url+'?'+new URLSearchParams(qs),
{
headers: {
'Authorization': 'Basic ' + Buffer.from(appUser + ':' + appPassword)
.toString('base64'),
},
});
}
if (result.status === 404) {
return 'NOT_FOUND';
}
if (result.status > 399) {
return null;
}
return await result.json();
};
const post = async (url, json) => {
const opts = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + Buffer.from(appUser + ':' + appPassword)
.toString('base64'),
},
body: JSON.stringify(json)};
const result = await fetch(apiBaseUrl+url, opts);
return await result.json();
};
const put = async (url, json) => {
const opts = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + Buffer.from(appUser + ':' + appPassword)
.toString('base64'),
},
body: JSON.stringify(json),
};
const result = await fetch(url, opts);
return await result.json();
};
export {get, post, put};