This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(release): add npm publish to release pipeline
- Loading branch information
Ryan Garant
committed
Sep 5, 2018
1 parent
748dfe3
commit e4e1f17
Showing
2 changed files
with
82 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,139 +7,151 @@ | |
* @author Eduardo Lundgren <[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
'use strict' | ||
|
||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError('Cannot call a class as a function'); | ||
throw new TypeError('Cannot call a class as a function') | ||
} | ||
} | ||
|
||
var _request = require('request'); | ||
var http = require('http'); | ||
var _url = require('url'); | ||
var _ = require('lodash'); | ||
var logger = require('./logger'); | ||
var _request = require('request') | ||
var http = require('http') | ||
var _url = require('url') | ||
var _ = require('lodash') | ||
var logger = require('./logger') | ||
|
||
var RestApiClient = function () { | ||
var RestApiClient = (function() { | ||
function RestApiClient(options) { | ||
_classCallCheck(this, RestApiClient); | ||
_classCallCheck(this, RestApiClient) | ||
|
||
options = _.merge(this.DEFAULT_CONFIG, options); | ||
this.options = options; | ||
options = _.merge(this.DEFAULT_CONFIG, options) | ||
this.options = options | ||
} | ||
|
||
RestApiClient.prototype.encode = function encode() { | ||
return encodeURIComponent.apply(this, arguments); | ||
}; | ||
return encodeURIComponent.apply(this, arguments) | ||
} | ||
|
||
RestApiClient.prototype.url = function url(pathname, query) { | ||
var options = this.options; | ||
var options = this.options | ||
var uri = _url.format({ | ||
protocol: options.protocol, | ||
hostname: options.host, | ||
port: options.port, | ||
pathname: options.base + pathname, | ||
query: query | ||
}); | ||
query: query, | ||
}) | ||
|
||
return decodeURIComponent(uri); | ||
}; | ||
return decodeURIComponent(uri) | ||
} | ||
|
||
RestApiClient.prototype.authorize = function authorize(p) { | ||
var options = this.options; | ||
var options = this.options | ||
|
||
if (p.oauth) { | ||
p.oauth = options.oauth; | ||
return; | ||
p.oauth = options.oauth | ||
return | ||
} | ||
|
||
if (typeof options.user === 'string') { | ||
p.auth = { | ||
user: options.user, | ||
pass: options.password | ||
}; | ||
pass: options.password, | ||
} | ||
} | ||
}; | ||
} | ||
|
||
RestApiClient.prototype.request = function request(method, path, params) { | ||
if (typeof path === 'object') { | ||
var args = Array.from(path); | ||
args.unshift(method); | ||
return this.request.apply(this, args); | ||
var args = Array.from(path) | ||
args.unshift(method) | ||
return this.request.apply(this, args) | ||
} | ||
|
||
var options = this.options; | ||
var options = this.options | ||
|
||
var p = { | ||
strictSSL: options.strictSSL, | ||
method: method, | ||
uri: this.url(path), | ||
json: true, | ||
followAllRedirects: true | ||
}; | ||
followAllRedirects: true, | ||
} | ||
|
||
if (params) { | ||
p = _.merge(p, params); | ||
p = _.merge(p, params) | ||
} | ||
|
||
this.authorize(p); | ||
|
||
var id = Math.floor(Math.random() * 10000000); | ||
var begin = new Date().getTime(); | ||
|
||
return new Promise(function (resolve, reject) { | ||
logger.debug('New request #' + id + ' started at ' + begin + ':\n' + method + ' ' + p.uri); | ||
logger.insane(p); | ||
_request(p, function (error, response) { | ||
var end = new Date().getTime(); | ||
logger.debug('End of request #' + id + ' at ' + end + ' (' + (end - begin) + 'ms)' + ' with status code: ' + (response && response.statusCode)); | ||
this.authorize(p) | ||
|
||
var id = Math.floor(Math.random() * 10000000) | ||
var begin = new Date().getTime() | ||
|
||
return new Promise(function(resolve, reject) { | ||
logger.debug( | ||
'New request #' + id + ' started at ' + begin + ':\n' + method + ' ' + p.uri | ||
) | ||
logger.insane(p) | ||
_request(p, function(error, response) { | ||
var end = new Date().getTime() | ||
logger.debug( | ||
'End of request #' + | ||
id + | ||
' at ' + | ||
end + | ||
' (' + | ||
(end - begin) + | ||
'ms)' + | ||
' with status code: ' + | ||
(response && response.statusCode) | ||
) | ||
|
||
if (response) { | ||
logger.insane('Response headers:'); | ||
logger.insane(response.headers); | ||
logger.debug('Response body'); | ||
logger.debug(response.body); | ||
logger.insane('Response headers:') | ||
logger.insane(response.headers) | ||
logger.debug('Response body') | ||
logger.debug(response.body) | ||
} | ||
|
||
if (error) { | ||
reject(error); | ||
return; | ||
reject(error) | ||
return | ||
} | ||
|
||
if (response.statusCode < 200 || response.statusCode > 399) { | ||
reject({ | ||
error: response.statusCode + ' ' + http.STATUS_CODES[response.statusCode], | ||
code: response.statusCode, | ||
msg: http.STATUS_CODES[response.statusCode], | ||
response: response | ||
}); | ||
return; | ||
response: response, | ||
}) | ||
return | ||
} | ||
|
||
resolve(response); | ||
}); | ||
}); | ||
}; | ||
resolve(response) | ||
}) | ||
}) | ||
} | ||
|
||
RestApiClient.prototype.get = function get() { | ||
return this.request('GET', arguments); | ||
}; | ||
return this.request('GET', arguments) | ||
} | ||
|
||
RestApiClient.prototype.post = function post() { | ||
return this.request('POST', arguments); | ||
}; | ||
return this.request('POST', arguments) | ||
} | ||
|
||
RestApiClient.prototype.put = function put() { | ||
return this.request('PUT', arguments); | ||
}; | ||
return this.request('PUT', arguments) | ||
} | ||
|
||
RestApiClient.prototype['delete'] = function _delete() { | ||
return this.request('DELETE', arguments); | ||
}; | ||
return this.request('DELETE', arguments) | ||
} | ||
|
||
return RestApiClient; | ||
}(); | ||
return RestApiClient | ||
})() | ||
|
||
RestApiClient.prototype.DEFAULT_CONFIG = { | ||
protocol: 'https', | ||
|
@@ -149,7 +161,7 @@ RestApiClient.prototype.DEFAULT_CONFIG = { | |
password: 'password', | ||
base: '', | ||
// oauth: undefined, | ||
strictSSL: true | ||
}; | ||
strictSSL: true, | ||
} | ||
|
||
module.exports = RestApiClient; | ||
module.exports = RestApiClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters