-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathutils.js
70 lines (68 loc) · 2.61 KB
/
utils.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
65
66
67
68
69
70
var https = require('https');
function validateJson(json) {
if (!json.hasOwnProperty("insert") && !json.hasOwnProperty("update") && !json.hasOwnProperty("delete") && !json.hasOwnProperty("select")) {
throw new Error("JSON should have insert or update or delete or select key");
}
if (json.hasOwnProperty("filter") && json.hasOwnProperty("insert")) {
throw new Error("JSON should have not have filter with insert");
}
if (json.hasOwnProperty("limit") || json.hasOwnProperty("having") || json.hasOwnProperty("groupby")) {
if (!json.hasOwnProperty("select")) {
throw new Error("Cannot use limit,having and groupby without select");
}
}
if (json.hasOwnProperty("insert") && (json.hasOwnProperty("update") || json.hasOwnProperty("delete") || json.hasOwnProperty("select"))) {
throw new Error("Cannot use insert with update,delete or select");
}
if (json.hasOwnProperty("update") && (json.hasOwnProperty("insert") || json.hasOwnProperty("delete") || json.hasOwnProperty("select"))) {
throw new Error("Cannot use update with insert,delete or select");
}
if (json.hasOwnProperty("delete") && (json.hasOwnProperty("update") || json.hasOwnProperty("insert") || json.hasOwnProperty("select"))) {
throw new Error("Cannot use delete with update,insert or select");
}
if (json.hasOwnProperty("select") && (json.hasOwnProperty("update") || json.hasOwnProperty("delete") || json.hasOwnProperty("insert"))) {
throw new Error("Cannot use select with update,delete or insert");
} else {
return "";
}
}
function getAccessToken(json,clientSecret) {
return new Promise((resolve, reject) => {
const options = {
hostname: json.extraparam.url,
path: "/" + json.extraparam.path,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
},
};
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
const oauthResponse = JSON.parse(data);
const accessToken = oauthResponse.access_token;
resolve(accessToken);
});
});
req.on("error", (error) => {
console.error("Error obtaining access token:", error.message);
reject(error);
});
req.write(
new URLSearchParams({
client_id: json.extraparam.clientId,
client_secret: clientSecret,
grant_type: json.extraparam.grantType,
scope: json.extraparam.scope,
}).toString()
);
req.end();
});
}
module.exports={
validateJson:validateJson,
getAccessToken:getAccessToken
}