-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
144 lines (124 loc) · 4.07 KB
/
index.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
var client = {};
client.version = "3.2.2";
module['exports'] = client;
client.createClient = function createClient (opts) {
var c = new Client(opts);
return c;
};
var thenify;
var Cron = require('./lib/cron');
var DS = require('./lib/datastore');
var Domains = require('./lib/domains');
var Env = require('./lib/env');
var Events = require('./lib/events');
var Files = require('./lib/files');
var Hook = require('./lib/hook');
var Keys = require('./lib/keys');
var Logs = require('./lib/logs');
var config = require('./config');
function Client (opts) {
var self = this;
opts = opts || {};
self.host = opts.host || config.host || "hook.io";
self.port = opts.port || config.port || 443;
self.protocol = opts.protocol || config.protocol || "https";
self.additionalRequestParams = {};
if (opts.files) {
self.additionalRequestParams.files = opts.files;
}
// set allowUnauthorizedTLS option to true for local testing with unauthorized / local SSL certificates
if (opts.allowUnauthorizedTLS === true) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
}
self.cron = new Cron(self);
self.datastore = new DS(self);
self.env = new Env(self);
self.events = new Events(self);
self.files = new Files(self);
self.hook = new Hook(self);
self.keys = new Keys(self);
self.logs = new Logs(self);
self.domains = new Domains(self);
if (config.hook_private_key) {
self.hook_private_key = config.hook_private_key;
self.attemptAuth = true;
}
if (opts.hook_private_key) {
self.hook_private_key = opts.hook_private_key;
self.attemptAuth = true;
}
// extends all core sdk methods with ES6 Promise API
// should be non-instrusive and not affect Callback API...
function extendWithPromiseApi (obj) {
for (var p in obj) {
if (typeof obj[p] === "function") {
obj[p] = thenify(obj[p]);
}
}
}
if (opts.promises === true) {
thenify = require('thenify');
extendWithPromiseApi(self.datastore);
extendWithPromiseApi(self.domains);
extendWithPromiseApi(self.env);
extendWithPromiseApi(self.events);
extendWithPromiseApi(self.files);
extendWithPromiseApi(self.hook);
extendWithPromiseApi(self.keys);
extendWithPromiseApi(self.logs);
// leave out core API methods
// extendWithPromiseApi(self);
}
return self;
};
Client.prototype.request = function (url, opts, cb) {
var self = this;
url = self.protocol + self.host + ":" + self.port + url;
opts.json = opts.json || {};
if (self.attemptAuth === true) {
if (opts.json === true) {
opts.json = {};
}
opts.json.hook_private_key = opts.json.hook_private_key || self.hook_private_key;
}
// console.log('making request', url, opts, typeof cb)
// TODO: add ability to extend other endpoints besides files
if (self.additionalRequestParams && typeof self.additionalRequestParams.files === "object") {
for (var k in self.additionalRequestParams.files) {
opts.json[k] = self.additionalRequestParams.files[k];
}
}
opts.headers = opts.headers || {};
opts.headers["hookio-private-key"] = self.hook_private_key;
if (opts.stream === true) {
var hyper = require('hyperquest');
var _url = url;
opts.method = opts.method || "POST";
var stream = hyper(_url, { method: opts.method, headers: opts.headers });
return stream;
} else {
var request = require('request');
// set max concurrency for requests to host
// Note: We could use Infinity value as default, but we will assume most users won't need to create so many concurrency connections
request.defaults({
pool: { maxSockets: 10 }
})
request(url, opts, function (err, res, body){
if (err) {
return cb(err);
}
if (res.statusCode === 400) {
return cb(new Error('Validation error: ' + url), res, body)
}
if (res.statusCode === 404) {
return cb(new Error('Could not find requested URI: ' + url), res, body)
}
cb(err, res, body);
});
}
};
Client.prototype.error = function (err, context, cb) {
var self = this;
err.message += JSON.stringify(self);
cb(err);
};