-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (53 loc) · 1.45 KB
/
index.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
const {fetch} = require('cross-fetch');
const checkEnvironment = (logging) => {
// URL
const url = process.env['ONTRACK_URL'];
if (!url) {
if (logging) console.log("ONTRACK_URL environment variable is not defined.")
return false;
}
// Token
const token = process.env['ONTRACK_TOKEN'];
if (!token) {
if (logging) console.log("ONTRACK_TOKEN secret is not defined.")
return false;
}
// OK
return {
url: url,
token: token,
};
};
const graphQL = async (clientEnvironment, query, variables, logging) => {
if (logging) {
console.log("Query: ", query);
console.log("Variables: ", variables);
}
const result = await fetch(`${clientEnvironment.url}/graphql`, {
method: "POST",
headers: {
'X-Ontrack-Token': clientEnvironment.token,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
credentials: "omit",
body: JSON.stringify({
query: query,
variables: variables
})
});
if (result.status >= 200 && result.status < 300) {
const json = await result.json();
if (logging) {
console.log("Answer: ", json);
}
return json;
} else {
throw Error(`HTTP ${result.status}`);
}
};
const client = {
checkEnvironment: checkEnvironment,
graphQL: graphQL
}
module.exports = client;