-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (114 loc) · 3.32 KB
/
index.js
File metadata and controls
129 lines (114 loc) · 3.32 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const OAuth = require("oauth-1.0a");
const { createHmac } = require("crypto");
const got = require("got");
const querystring = require("querystring");
class NetSuiteRest {
constructor({ consumerKey, consumerSecret, token, tokenSecret, accountId }) {
this.accountId = accountId;
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.token = token;
this.tokenSecret = tokenSecret;
}
getAuthorizationHeader({ url, method }) {
const oauth = OAuth({
consumer: { key: this.consumerKey, secret: this.consumerSecret },
signature_method: "HMAC-SHA256",
realm: this.accountId,
hash_function(base_string, key) {
return createHmac("sha256", key).update(base_string).digest("base64");
},
});
const signedData = oauth.authorize(
{
url,
method,
},
{
key: this.token,
secret: this.tokenSecret,
}
);
return oauth.toHeader(signedData).Authorization;
}
getAccountPath() {
return this.accountId.toLowerCase().replace("_", "-");
}
async request({ body, path, method = "GET", options = {} }) {
const accountPath = this.getAccountPath();
const url = `https://${accountPath}.suitetalk.api.netsuite.com/services/rest/record/v1${path}`;
const authorization = this.getAuthorizationHeader({ url, method });
try {
const res = await got(url, {
method,
json: body,
responseType: "json",
headers: { Authorization: authorization },
...options,
});
return res.body;
} catch (err) {
if (err.response) {
console.log(err.response.body);
return err.response.body;
} else {
throw err;
}
}
}
async restlet({
body,
method = "GET",
deployId = "1",
scriptId,
options = {},
}) {
const accountPath = this.accountId.toLowerCase().replace("_", "-");
const url = `https://${accountPath}.restlets.api.netsuite.com/app/site/hosting/restlet.nl?deploy=${deployId}&script=${scriptId}`;
const authorization = this.getAuthorizationHeader({ url, method });
try {
const res = await got(url, {
method,
json: body,
responseType: "json",
headers: { Authorization: authorization },
...options,
});
return res.body;
} catch (err) {
if (err.response) {
console.log(err.response.body);
return err.response.body;
} else {
throw err;
}
}
}
async suiteql(query, options = {}) {
const { query: urlQuery, ...fetchOptions } = options;
const accountPath = this.getAccountPath();
const method = "POST";
const url = `https://${accountPath}.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql${
urlQuery ? `?${querystring.stringify(urlQuery)}` : ""
}`;
const authorization = this.getAuthorizationHeader({ url, method });
try {
const res = await got(url, {
method,
json: { q: query },
responseType: "json",
headers: { Authorization: authorization, Prefer: "transient" },
...fetchOptions,
});
return res.body;
} catch (err) {
if (err.response) {
console.log(err.response.body);
return err.response.body;
} else {
throw err;
}
}
}
}
module.exports = NetSuiteRest;