Skip to content

Commit

Permalink
Merge pull request #475 from davidpatrick/deprecate-request
Browse files Browse the repository at this point in the history
Deprecate Request lib
  • Loading branch information
davidpatrick authored Apr 14, 2020
2 parents 43b33a1 + 41962eb commit 32f0001
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 349 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
},
"homepage": "https://github.com/auth0/node-auth0",
"dependencies": {
"axios": "^0.19.2",
"bluebird": "^3.5.5",
"form-data": "^3.0.0",
"jsonwebtoken": "^8.5.1",
"jwks-rsa": "^1.7.0",
"jwks-rsa": "^1.8.0",
"lru-memoizer": "^2.1.0",
"object.assign": "^4.1.0",
"request": "^2.88.0",
"rest-facade": "^1.12.0",
"retry": "^0.12.0"
},
Expand Down
6 changes: 3 additions & 3 deletions src/auth/TokensManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var extend = require('util')._extend;
var getRequestPromise = require('../utils').getRequestPromise;
var axios = require('axios');

var ArgumentError = require('rest-facade').ArgumentError;

Expand Down Expand Up @@ -66,7 +66,7 @@ TokensManager.prototype.getInfo = function(idToken, cb) {
}

// Perform the request.
var promise = getRequestPromise({
var promise = axios({
method: 'POST',
url: this.baseUrl + '/tokeninfo',
data: { id_token: idToken },
Expand Down Expand Up @@ -156,7 +156,7 @@ TokensManager.prototype.getDelegationToken = function(data, cb) {
}

// Perform the request.
var promise = getRequestPromise({
var promise = axios({
method: 'POST',
url: this.baseUrl + '/delegation',
data: body,
Expand Down
9 changes: 4 additions & 5 deletions src/auth/UsersManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var extend = require('util')._extend;
var getRequestPromise = require('../utils').getRequestPromise;
var axios = require('axios');

var ArgumentError = require('rest-facade').ArgumentError;

Expand Down Expand Up @@ -69,11 +69,10 @@ UsersManager.prototype.getInfo = function(accessToken, cb) {
headers['Authorization'] = 'Bearer ' + accessToken;

// Perform the request.
var promise = getRequestPromise({
var promise = axios({
method: 'GET',
url: url,
headers: headers,
data: {}
headers: headers
});

// Use callback if given.
Expand Down Expand Up @@ -154,7 +153,7 @@ UsersManager.prototype.impersonate = function(userId, settings, cb) {
var data = extend({ client_id: settings.clientId || this.clientId }, settings);
var headers = extend({ Authorization: `Bearer ${settings.token}` }, this.headers);
// Perform the request.
var promise = getRequestPromise({
var promise = axios({
method: 'POST',
headers: headers,
data: data,
Expand Down
80 changes: 27 additions & 53 deletions src/management/JobsManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var request = require('request');
var axios = require('axios');
var extend = require('util')._extend;
var Promise = require('bluebird');
var FormData = require('form-data');
var fs = require('fs');

var ArgumentError = require('rest-facade').ArgumentError;
Expand Down Expand Up @@ -156,63 +157,36 @@ JobsManager.prototype.get = function(params, cb) {
*/
JobsManager.prototype.importUsers = function(data, cb) {
var options = this.options;
var headers = extend({}, options.headers);
var url = options.baseUrl + '/jobs/users-imports';
var userData = data.users_json ? Buffer.from(data.users_json) : fs.createReadStream(data.users);
var userFilename = data.users_json ? 'users.json' : data.users;

headers['Content-Type'] = 'multipart/form-data';
var form = new FormData();
form.append('users', userData, userFilename);
form.append('connection_id', data.connection_id);
form.append('upsert', data.upsert === true ? 'true' : 'false');
form.append('send_completion_email', data.send_completion_email === false ? 'false' : 'true');

var url = options.baseUrl + '/jobs/users-imports';
var method = 'POST';
var upsert = data.upsert === true ? 'true' : 'false';
var send_completion_email = data.send_completion_email === false ? 'false' : 'true';
var headers = { ...options.headers, ...form.getHeaders() };
headers['Content-Type'] = 'multipart/form-data';

var promise = options.tokenProvider.getAccessToken().then(function(access_token) {
return new Promise(function(resolve, reject) {
request(
{
url: url,
method: method,
headers: extend({ Authorization: `Bearer ${access_token}` }, headers),
formData: {
users: {
value: data.users_json
? Buffer.from(data.users_json)
: fs.createReadStream(data.users),
options: {
filename: data.users_json ? 'users.json' : data.users
}
},
connection_id: data.connection_id,
upsert: upsert,
send_completion_email: send_completion_email
}
},
function(err, res) {
if (err) {
reject(err);
return;
}
// `superagent` uses the error parameter in callback on http errors.
// the following code is intended to keep that behaviour (https://github.com/visionmedia/superagent/blob/master/lib/node/response.js#L170)
var type = (res.statusCode / 100) | 0;
var isErrorResponse = 4 === type || 5 === type;
if (isErrorResponse) {
var error = new Error('cannot ' + method + ' ' + url + ' (' + res.statusCode + ')');
error.status = res.statusCode;
error.method = method;
error.text = res.text;
try {
if (!error.text && res.body) {
error.text = JSON.parse(res.body).message;
}
} catch (ex) {
// Ignore the error.
}
reject(error);
}
resolve(res);
return axios
.post(url, form, { headers: { ...headers, Authorization: `Bearer ${access_token}` } })
.catch(function(err) {
if (!err.response) {
return Promise.reject(err);
}
);
});

var res = err.response;
// `superagent` uses the error parameter in callback on http errors.
// the following code is intended to keep that behaviour (https://github.com/visionmedia/superagent/blob/master/lib/node/response.js#L170)
var error = new Error('cannot POST' + ' ' + url + ' (' + res.status + ')');
error.status = res.status;
error.method = 'POST';
error.text = res.data.message || res.statusText || error.message;
return Promise.reject(error);
});
});

// Don't return a promise if a callback was given.
Expand Down
30 changes: 0 additions & 30 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Promise = require('bluebird');
var request = require('request');
var pkg = require('../package.json');

/**
Expand Down Expand Up @@ -62,35 +61,6 @@ utils.wrapPropertyMethod = function(Parent, name, propertyMethod) {
});
};

/**
* Perform a request with the given settings and return a promise that resolves
* when the request is successfull and rejects when there's an error.
*
* @method getRequestPromise
* @memberOf module:utils
*/
utils.getRequestPromise = function(settings) {
return new Promise(function(resolve, reject) {
request(
{
url: settings.url,
method: settings.method,
body: settings.data,
json: typeof settings.data === 'object',
headers: settings.headers
},
function(err, res, body) {
if (err) {
reject(err);
return;
}

resolve(res.body);
}
);
});
};

utils.containsUnsafeChars = s => {
const safeChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$-_.+!*'(),%";
return !!s.split('').find(c => !safeChars.includes(c));
Expand Down
8 changes: 3 additions & 5 deletions test/auth/tokens-manager.tests.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var expect = require('chai').expect;
var nock = require('nock');
var utilTypes = require('util').types;

var BASE_URL = 'https://tenant.auth0.com';

var Promise = require('bluebird');
var ArgumentError = require('rest-facade').ArgumentError;
var TokensManager = require('../../src/auth/TokensManager');

Expand Down Expand Up @@ -78,8 +78,7 @@ describe('TokensManager', function() {

it('should return a promise when no callback is provided', function() {
var returnValue = manager.getInfo('VALID_TOKEN');

expect(returnValue).to.be.an.instanceOf(Promise);
expect(utilTypes.isPromise(returnValue)).ok;
});

it('should not return a promise when a callback is provided', function() {
Expand Down Expand Up @@ -213,8 +212,7 @@ describe('TokensManager', function() {
grant_type: 'SAMPLE_GRANT_TYPE'
};
var returnValue = manager.getDelegationToken(data);

expect(returnValue).to.be.an.instanceOf(Promise);
expect(utilTypes.isPromise(returnValue)).ok;
});

it('should not return a promise when a callback is given', function() {
Expand Down
6 changes: 3 additions & 3 deletions test/auth/users-manager.tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var expect = require('chai').expect;
var nock = require('nock');
var Promise = require('bluebird');
var utilTypes = require('util').types;

var BASE_URL = 'https://tenant.auth0.com';
var CLIENT_ID = 'TEST_CLIENT_ID';
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('UsersManager', function() {
it('should return a promise when no callback is provided', function() {
var returnValue = manager.getInfo('ACCESS_TOKEN');

expect(returnValue).to.be.an.instanceOf(Promise);
expect(utilTypes.isPromise(returnValue)).ok;
});

it('should perform a GET request to ' + path, function(done) {
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('UsersManager', function() {
};
var returnValue = manager.impersonate(USER_ID, settings);

expect(returnValue).to.be.an.instanceOf(Promise);
expect(utilTypes.isPromise(returnValue)).ok;
});

it('should perform a POST request to ' + path, function(done) {
Expand Down
Loading

0 comments on commit 32f0001

Please sign in to comment.