-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnetsuite-rest.js
84 lines (81 loc) · 2.16 KB
/
netsuite-rest.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const OAuth = require("oauth-1.0a");
const crypto = require("crypto");
var got = require("got");
class NetsuiteRest {
constructor(options) {
this.consumer_key = options.consumer_key;
this.consumer_secret_key = options.consumer_secret_key;
this.token = options.token;
this.token_secret = options.token_secret;
this.version = "1.0";
this.algorithm = "HMAC-SHA256";
this.realm = options.realm;
this.base_url = options.base_url;
}
getAuthorizationHeader(options) {
const oauth = OAuth({
consumer: {
key: this.consumer_key,
secret: this.consumer_secret_key,
},
realm: this.realm,
signature_method: this.algorithm,
hash_function(base_string, key) {
return crypto
.createHmac("sha256", key)
.update(base_string)
.digest("base64");
},
});
return oauth.toHeader(
oauth.authorize(
{
url: options.url,
method: options.method,
},
{
key: this.token,
secret: this.token_secret,
}
)
);
}
request(opts) {
const { path = "*", method = "GET", body = "", heads = {} } = opts;
// Setup the Request URI
let uri;
if (this.base_url) uri = `${this.base_url}/services/rest/${path}`;
else {
// as suggested by dylbarne in #15: sanitize url to enhance overall usability
uri = `https://${this.realm
.toLowerCase()
.replace("_", "-")}.suitetalk.api.netsuite.com/services/rest/${path}`;
}
const options = {
url: uri,
method,
throwHttpErrors: true,
decompress: true,
hooks: {
afterResponse: [
(response) => {
return {
...response,
data: response.body ? JSON.parse(response.body) : null,
};
},
],
},
};
options.headers = this.getAuthorizationHeader(options);
if (Object.keys(heads).length > 0) {
options.headers = { ...options.headers, ...heads };
}
if (body) {
options.body = body;
options.headers.prefer = "transient";
}
return got(options);
}
}
module.exports = NetsuiteRest;