Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
chore(release): 1.13.2 [skip ci]
Browse files Browse the repository at this point in the history
## [1.13.2](v1.13.1...v1.13.2) (2018-09-05)

### Performance Improvements

* **profiling:** add flame graph generation to npm scripts ([88a9284](88a9284))
  • Loading branch information
Ryan committed Sep 5, 2018
1 parent 88a9284 commit 748dfe3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 81 deletions.
148 changes: 68 additions & 80 deletions lib/rest-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,151 +7,139 @@
* @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',
Expand All @@ -161,7 +149,7 @@ RestApiClient.prototype.DEFAULT_CONFIG = {
password: 'password',
base: '',
// oauth: undefined,
strictSSL: true,
}
strictSSL: true
};

module.exports = RestApiClient
module.exports = RestApiClient;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gh",
"description": "GitHub command line tools.",
"version": "1.13.1",
"version": "1.13.2",
"homepage": "http://nodegh.io",
"author": {
"name": "Eduardo Lundgren",
Expand Down

0 comments on commit 748dfe3

Please sign in to comment.