-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapiClient.js
More file actions
45 lines (35 loc) · 917 Bytes
/
Copy pathapiClient.js
File metadata and controls
45 lines (35 loc) · 917 Bytes
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
var fetch = require('node-fetch');
function ApiClient(options) {
options = options || {};
var authKey = options.authKey;
var endpoint = options.endpoint || 'https://api.strem.io';
this.endpoint = endpoint;
this.request = function(method, params) {
var fetchOptions = {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(Object.assign({ authKey: authKey }, params))
};
return fetch(endpoint + '/api/' + method, fetchOptions)
.then(function(resp) {
if (resp.status !== 200) {
throw new Error('request failed with status code ' + resp.status);
}
return resp.json();
})
.then(function(body) {
if (body.error) {
throw body.error;
}
if (!body.result) {
throw new Error('response has no result');
}
return body.result;
});
};
Object.freeze(this);
return this;
}
module.exports = ApiClient;