@@ -11,7 +11,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
1111var superagent = require ( 'superagent' ) ;
1212
1313var 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 () {
112120function 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 */
173189function 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 ;
0 commit comments