Skip to content

Commit 0c8c027

Browse files
authored
Merge pull request #5 from jabyrd3/master
optional lifecycle hooks
2 parents b96acbd + 15561a2 commit 0c8c027

File tree

17 files changed

+2734
-2778
lines changed

17 files changed

+2734
-2778
lines changed

dist/ns1api_client.js

Lines changed: 2669 additions & 2748 deletions
Large diffs are not rendered by default.

lib/NS1_request.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
1111
var superagent = require('superagent');
1212

1313
var api_key = void 0,
14-
api_url = 'https://api.nsone.net/v1/';
14+
api_url = 'https://api.nsone.net/v1/',
15+
errorCb = void 0,
16+
successCb = void 0,
17+
startCb = void 0;
1518

1619
/**
1720
* Class representing all HTTP requests to the NS1 API. Uses the superagent
@@ -29,7 +32,6 @@ var NS1Request = function () {
2932
* @param {Object/FormData} files - Key / value mapped object containing file paths for uploads, or a FormData object if on the browser.
3033
* @return {Promise} an ES2015 promise w/ then and catch methods for continuation handling.
3134
*/
32-
3335
function NS1Request(method, uri, query, files) {
3436
_classCallCheck(this, NS1Request);
3537

@@ -62,12 +64,18 @@ var NS1Request = function () {
6264
/**
6365
* Sets the API key used by the class.
6466
* @param {String} key - The API key supplied by the user
67+
* @param {Function} start - function reference to be called whenever a request is made.
68+
* @param {Function} error - function reference to be called when a request has an error.
69+
* @param {Function} success - function reference to be called whenever a request completes successfully.
6570
*/
6671

6772
}, {
6873
key: 'set_api_key',
69-
value: function set_api_key(key) {
74+
value: function set_api_key(key, start, error, success) {
7075
api_key = key;
76+
errorCb = error ? error : undefined;
77+
startCb = start ? start : undefined;
78+
successCb = success ? success : undefined;
7179
}
7280

7381
/**
@@ -112,6 +120,9 @@ var NS1Request = function () {
112120
function apply_data(query, files) {
113121
var _this = this;
114122

123+
if (startCb) {
124+
startCb();
125+
}
115126
if (query !== undefined) {
116127
if (this.method === 'get') {
117128
this.request = this.request.query(query);
@@ -143,7 +154,13 @@ function create_promise() {
143154

144155
return new Promise(function (resolve, reject) {
145156
_this2.request.end(function (err, response) {
146-
if (err) reject(handle_error.call(_this2, err, response));
157+
if (err) {
158+
reject(handle_error.call(_this2, err, response));
159+
} else {
160+
if (successCb) {
161+
successCb(response);
162+
}
163+
}
147164

148165
if (_this2.is_json_response && _this2.method != 'del' && response.text !== '') {
149166
try {
@@ -154,7 +171,6 @@ function create_promise() {
154171
} else {
155172
// TODO: Determine if there's a need to filter/reject empty response bodies on 200's
156173
}
157-
158174
return resolve(parsed || true);
159175
});
160176
});
@@ -171,6 +187,9 @@ function create_promise() {
171187
* @private
172188
*/
173189
function handle_error(err, response) {
190+
if (errorCb) {
191+
errorCb(err, response);
192+
}
174193
if (response && response.text) {
175194
var final_message = void 0;
176195
try {
@@ -190,7 +209,7 @@ var NS1Error = function (_Error) {
190209
function NS1Error(message, raw) {
191210
_classCallCheck(this, NS1Error);
192211

193-
var _this3 = _possibleConstructorReturn(this, Object.getPrototypeOf(NS1Error).call(this, message));
212+
var _this3 = _possibleConstructorReturn(this, (NS1Error.__proto__ || Object.getPrototypeOf(NS1Error)).call(this, message));
194213

195214
_this3.raw = raw;
196215
return _this3;

lib/account/api_key.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var ApiKey = function (_RestResource) {
1717
function ApiKey() {
1818
_classCallCheck(this, ApiKey);
1919

20-
return _possibleConstructorReturn(this, Object.getPrototypeOf(ApiKey).apply(this, arguments));
20+
return _possibleConstructorReturn(this, (ApiKey.__proto__ || Object.getPrototypeOf(ApiKey)).apply(this, arguments));
2121
}
2222

2323
_createClass(ApiKey, [{

lib/account/payment_method.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var PaymentMethod = function (_RestResource) {
1717
function PaymentMethod() {
1818
_classCallCheck(this, PaymentMethod);
1919

20-
return _possibleConstructorReturn(this, Object.getPrototypeOf(PaymentMethod).apply(this, arguments));
20+
return _possibleConstructorReturn(this, (PaymentMethod.__proto__ || Object.getPrototypeOf(PaymentMethod)).apply(this, arguments));
2121
}
2222

2323
_createClass(PaymentMethod, [{

lib/account/team.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var Team = function (_RestResource) {
1717
function Team() {
1818
_classCallCheck(this, Team);
1919

20-
return _possibleConstructorReturn(this, Object.getPrototypeOf(Team).apply(this, arguments));
20+
return _possibleConstructorReturn(this, (Team.__proto__ || Object.getPrototypeOf(Team)).apply(this, arguments));
2121
}
2222

2323
_createClass(Team, [{

lib/account/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var User = function (_RestResource) {
1717
function User() {
1818
_classCallCheck(this, User);
1919

20-
return _possibleConstructorReturn(this, Object.getPrototypeOf(User).apply(this, arguments));
20+
return _possibleConstructorReturn(this, (User.__proto__ || Object.getPrototypeOf(User)).apply(this, arguments));
2121
}
2222

2323
_createClass(User, [{

lib/data_feed.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var DataFeed = function (_RestResource) {
2929
var data_source_id = attributes.data_source_id;
3030
delete attributes.data_source_id;
3131

32-
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(DataFeed).call(this, attributes, from_server));
32+
var _this = _possibleConstructorReturn(this, (DataFeed.__proto__ || Object.getPrototypeOf(DataFeed)).call(this, attributes, from_server));
3333

3434
_this.data_source_id = data_source_id;
3535
return _this;
@@ -52,7 +52,7 @@ var DataFeed = function (_RestResource) {
5252

5353
var data_source_id = this.data_source_id;
5454
return new Promise(function (resolve, reject) {
55-
return _get(Object.getPrototypeOf(DataFeed.prototype), 'save', _this2).call(_this2).then(function (results) {
55+
return _get(DataFeed.prototype.__proto__ || Object.getPrototypeOf(DataFeed.prototype), 'save', _this2).call(_this2).then(function (results) {
5656
resolve(_this2.constructor.add_data_source_to_objects(results, data_source_id));
5757
}).catch(function (err) {
5858
reject(err);
@@ -71,7 +71,7 @@ var DataFeed = function (_RestResource) {
7171

7272
var data_source_id = id.split('/')[0];
7373
return new Promise(function (resolve, reject) {
74-
return _get(Object.getPrototypeOf(DataFeed), 'find', _this3).call(_this3, id).then(function (results) {
74+
return _get(DataFeed.__proto__ || Object.getPrototypeOf(DataFeed), 'find', _this3).call(_this3, id).then(function (results) {
7575
resolve(_this3.add_data_source_to_objects(results, data_source_id));
7676
}).catch(function (err) {
7777
reject(err);

lib/data_source.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var DataSource = function (_RestResource) {
2424
function DataSource() {
2525
_classCallCheck(this, DataSource);
2626

27-
return _possibleConstructorReturn(this, Object.getPrototypeOf(DataSource).apply(this, arguments));
27+
return _possibleConstructorReturn(this, (DataSource.__proto__ || Object.getPrototypeOf(DataSource)).apply(this, arguments));
2828
}
2929

3030
_createClass(DataSource, [{

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ var NS1 = {
2121
Search: require('./search'),
2222

2323
// convenience methods
24-
set_api_key: function set_api_key(key) {
25-
return NS1.NS1Request.set_api_key(key);
24+
set_api_key: function set_api_key(key, start, err, success) {
25+
return NS1.NS1Request.set_api_key(key, start, err, success);
2626
},
2727
set_api_url: function set_api_url(url) {
2828
return NS1.NS1Request.set_api_url(url);

lib/monitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var Monitor = function (_RestResource) {
2424
function Monitor() {
2525
_classCallCheck(this, Monitor);
2626

27-
return _possibleConstructorReturn(this, Object.getPrototypeOf(Monitor).apply(this, arguments));
27+
return _possibleConstructorReturn(this, (Monitor.__proto__ || Object.getPrototypeOf(Monitor)).apply(this, arguments));
2828
}
2929

3030
_createClass(Monitor, [{

0 commit comments

Comments
 (0)