-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (66 loc) · 2.15 KB
/
index.js
File metadata and controls
70 lines (66 loc) · 2.15 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
var assert = require('assert');
var request = require('superagent');
var assign = require('object-assign');
var url = require('url');
var prototype = {
search: function search (params) {
return Promise.resolve()
.then(function () {
var type = typeof params;
var path = type === 'string' ? '/search/transactions/' + params : '/search/transactions';
var method = type === 'string' ? 'get' : 'post';
var endpoint = url.format(assign({}, this.endpoint, {pathname: path}));
return new Promise(function (resolve, reject) {
var req = request[method](endpoint)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + this.apiKey);
if (method === 'post') {
req.send(params);
}
req.end(function (err, response) {
if (err) {
reject(err);
} else {
resolve(response.body);
}
});
}.bind(this));
}.bind(this));
},
saveTransactions: function saveTransactions (transactions) {
return Promise.resolve()
.then(function () {
var endpoint = url.format(assign({}, this.endpoint, {pathname: '/transactions'}));
return new Promise(function (resolve, reject) {
request
.post(endpoint, transactions)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + this.apiKey)
.end(function (err, response) {
if (err) {
reject(err);
} else {
resolve(response.body);
}
});
}.bind(this));
}.bind(this));
}
};
module.exports = function factory (options) {
options = options || {};
assert(options.API_KEY, 'you must provide an API_KEY in your options');
return Object.create(prototype, {
endpoint: {
value: assign({
protocol: 'https',
hostname: 'reporting.funnels.io'
}, options.endpoint || {})
},
apiKey: {
value: options.API_KEY
}
});
};