forked from SendSafely/JavaScript-Node-Client-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchRequest.js
More file actions
98 lines (84 loc) · 3.31 KB
/
FetchRequest.js
File metadata and controls
98 lines (84 loc) · 3.31 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
const fetch = require('make-fetch-happen');
const crypto = require("crypto");
const URL = require('url').URL;
function FetchRequest(param) {
if(!param.hasOwnProperty('signedRequest')) {
if(typeof param !== 'object') {
throw new Error('FetchRequest: Invalid parameters');
}
if(!param.hasOwnProperty('url')) {
throw new Error('FetchRequest: url is missing');
}
if(!param.hasOwnProperty('apiKey')) {
throw new Error('FetchRequest: apiKey is missing');
}
if(!param.hasOwnProperty('apiKeySecret')) {
throw new Error('FetchRequest: apiKeySecret is missing');
}
}
let myself = this;
myself.url = param.url; // scheme + domain + port for signed request, full url for non signed request
myself.apiPrefix = '/api/v2.0';
myself.apiKey = param.apiKey;
myself.apiKeySecret = param.apiKeySecret;
myself.requestAPI = param.hasOwnProperty('requestAPI') ? param.requestAPI: 'NODE_API';
myself.options = {};
myself.sendRequest = function (url, options) {
return fetch(url, options);
}
myself.sendSignedRequest = function (endpoint, data) {
buildHttpsOptions(endpoint, data);
return fetch(myself.url + myself.apiPrefix + endpoint.url, myself.options);
};
let buildHttpsOptions = function(endpoint, data) {
if(typeof endpoint !== 'object') {
throw new Error('FetchRequest: Invalid endpoint parameters');
}
if(!endpoint.hasOwnProperty('url')) {
throw new Error('FetchRequest: url is missing');
}
if(!endpoint.hasOwnProperty('HTTPMethod')) {
throw new Error('FetchRequest: HTTPMethod is missing');
}
if(!endpoint.hasOwnProperty('mimetype')) {
throw new Error('FetchRequest: mimetype is missing');
}
let timestamp = dateString();
let signature = myself.apiKey + myself.apiPrefix + endpoint.url.split("?")[0] + timestamp;
if(endpoint.hasOwnProperty('messageData')) {
signature += JSON.stringify(endpoint.messageData);
} else if(data !== '' && data !== null) {
signature += JSON.stringify(data);
}
signature = signMessage(signature);
let method = endpoint.HTTPMethod;
let headers = {
'Content-Type': endpoint.mimetype,
'ss-api-key':myself.apiKey,
'ss-request-timestamp': timestamp,
'ss-request-signature': signature,
'ss-request-api': myself.requestAPI,
};
let options = {
headers: headers,
method: method,
}
if(data !== null) {
if(endpoint.mimetype.includes('multipart/form-data')) {
options.headers['Content-Length'] = Buffer.from(data).length;
options.body = data;
} else {
options.body = JSON.stringify(data);
}
}
myself.options = options;
}
let dateString = function() {
let time = new Date().toISOString();
return time.substr(0, 19) + "+0000";
}
let signMessage = function(messageString) {
return crypto.createHmac('sha256', myself.apiKeySecret).update(messageString).digest('hex');
}
}
module.exports = {FetchRequest};