-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetsuite.js
170 lines (147 loc) · 4.24 KB
/
netsuite.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* NetSuite connection module
*
* @author Jaime Ramos
* @version 0.8.0
*/
'use strict';
let request = require('request');
const OAuth = require('oauth-1.0a');
const crypto = require('crypto');
module.exports = NetSuiteRestlet;
/**
* NetSuiteRestlet constructor
*
* @param config
* @constructor
*/
function NetSuiteRestlet(config) {
let mode = config.mode;
if (!mode || mode === 'nlauth') {
this.authorization = 'NLAuth nlauth_account=' +
config.account + ', nlauth_email=' +
config.username + ', nlauth_signature=' +
config.password + ', nlauth_role=' +
config.role;
this.contentType = 'application/json';
this.headers = {
'Authorization': this.authorization,
'Content-Type': this.contentType
}
}
else {
if (mode === 'tba') {
const oauth = OAuth({
consumer: {
key: config.consumerKey,
secret: config.consumerSecret
},
signature_method: 'HMAC-SHA1',
realm: config.account,
hash_function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
});
const request_data = {
url: 'https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=404&deploy=1&action=getRecordTypes',
method: 'GET'
};
const token = {
key: 'c27d903a481bfe375c649553a7b480c3a99f2e6e3f48d7460b2ed8921547a9af',
secret: '608d1210df17644c58d0f71c5819f61a47865d8c45f1c1a3b8d6502cfb63d5f8'
};
let headers = oauth.toHeader(oauth.authorize(request_data, token));
headers['Content-Type'] = 'application/json';
this.headers = headers;
}
else {
throw 'Invalid authentication mode. You must select tba or nlauth';
}
}
}
/**
* GET Method
*
* @param parameters
* @param url
* @returns {Promise<any>}
*/
NetSuiteRestlet.prototype.get = function (parameters, url) {
return new Promise((resolve, reject) => {
for (let key in parameters) {
url += '&';
url += key;
url += '=';
url += parameters[key];
}
let options = {
headers: this.headers,
uri: url,
method: 'GET',
};
request(options, function (err, response, body) {
if (err || response.statusCode.toString()[0] != 2) {
console.log('Body data:', body);
reject(body || err);
}
else {
if (typeof body == 'string') body = JSON.parse(body);
resolve(body || err);
}
});
});
};
/**
* POST Method
*
* @param body
* @param url
* @returns {Promise<any>}
*/
NetSuiteRestlet.prototype.post = function (body, url) {
return new Promise((resolve, reject) => {
let options = {
headers: this.headers,
uri: url,
method: 'POST',
json: body
};
request(options, function (err, response, body) {
if (err || response.statusCode.toString()[0] != 2) {
console.log('Body data:', body);
reject(body || err);
}
else {
if (typeof body == 'string') body = JSON.parse(body);
resolve(body || err);
}
});
});
};
/**
* PUT Method
*
* @param body
* @param url
* @returns {Promise<any>}
*/
NetSuiteRestlet.prototype.put = function (body, url) {
return new Promise((resolve, reject) => {
let options = {
headers: this.headers,
uri: url,
method: 'PUT',
json: body
};
request(options, function (err, response, body) {
if (err || response.statusCode.toString()[0] != 2) {
console.log('Body data:', body);
reject(body || err);
}
else {
if (typeof body == 'string') body = JSON.parse(body);
resolve(body || err);
}
});
});
};