Skip to content

Commit

Permalink
feat: migrate from deprecated request module to axios
Browse files Browse the repository at this point in the history
  • Loading branch information
eshanholtz committed Mar 9, 2020
1 parent e29d125 commit 8ad56ba
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"chai": "^4.0.1",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"eslint": "^4.19.1",
"eslint": "^6.8.0",
"istanbul": "^1.0.0-alpha.2",
"lerna": "^3.19.0",
"mocha": "^6.2.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/client/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ For more information, please see our [User Guide](http://sendgrid.com/docs/User_
const queryParams = {
'limit': 1
};
request.qs = queryParams;
request.params = queryParams;
request.method = 'GET';
request.url = '/v3/access_settings/activity';
client.request(request)
Expand Down Expand Up @@ -92,7 +92,7 @@ For more information, please see our [User Guide](http://sendgrid.com/docs/User_
}
]
};
request.body = data;
request.data = data;
request.method = 'POST';
request.url = '/v3/access_settings/whitelist';
client.request(request)
Expand Down Expand Up @@ -122,7 +122,7 @@ For more information, please see our [User Guide](http://sendgrid.com/docs/User_
3
]
};
request.body = data;
request.data = data;
request.method = 'DELETE';
request.url = '/v3/access_settings/whitelist';
client.request(request)
Expand Down
3 changes: 1 addition & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
},
"dependencies": {
"@sendgrid/helpers": "^6.5.3",
"@types/request": "^2.48.4",
"request": "^2.88.0"
"axios": "^0.19.2"
},
"tags": [
"http",
Expand Down
43 changes: 27 additions & 16 deletions packages/client/src/classes/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Dependencies
*/
const http = require('request');
const axios = require('axios');
const pkg = require('../../package.json');
const {
helpers: {
Expand All @@ -29,13 +29,14 @@ class Client {

//Default headers
this.defaultHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-agent': 'sendgrid/' + pkg.version + ';nodejs',
};

//Empty default request
this.defaultRequest = {
json: true,
data: {},
baseUrl: 'https://api.sendgrid.com/',
url: '',
method: 'GET',
Expand Down Expand Up @@ -94,6 +95,12 @@ class Client {
delete data.uri;
}

// Ensure backwards compatibility from request module
data.data = data.body ? data.body : undefined;
delete data.body;
data.params = data.qs ? data.qs : undefined;
delete data.qs;

//Merge data with empty request
const request = mergeData(this.defaultRequest, data);

Expand All @@ -112,21 +119,25 @@ class Client {

//Perform request
const promise = new Promise((resolve, reject) => {
http(request, (error, response, body) => {

//Request error
if (error) {
axios(request)
.then(response => {
// Successful response
const parsedResponse = {
statusCode: repsonse.status,
body: response.data,
};
resolve([parsedResponse, response.data]);
})
.catch(error => {
// Response error
if (error.response) {
if (error.response.status >= 400) {
return reject(new ResponseError(error.response));
}
}
// Request error
return reject(error);
}

//Response error
if (response.statusCode >= 400) {
return reject(new ResponseError(response));
}

//Successful response
resolve([response, body]);
});
});
});

// Throw and error incase function not passed
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('test_access_settings_activity_get', () => {
const queryParams = {
'limit': 1,
};
request.qs = queryParams;
request.params = queryParams;
request.method = 'GET';
request.url = '/v3/access_settings/activity';
it('should have the correct response code', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/request.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {OptionsWithUrl} from "request";
import {AxiosRequestConfig} from "axios";

export type ClientRequest = OptionsWithUrl;
export type ClientRequest = AxiosRequestConfig;
4 changes: 2 additions & 2 deletions packages/client/src/response.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {RequestResponse} from "request";
import {AxiosResponse} from "axios";

export type ClientResponse = RequestResponse;
export type ClientResponse = AxiosResponse;
14 changes: 7 additions & 7 deletions packages/helpers/classes/response-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class ResponseError extends Error {
super();

//Extract data from response
const {headers, statusCode, statusMessage, body} = response;
const {headers, status, statusText, data} = response;

//Set data
this.code = statusCode;
this.message = statusMessage;
this.response = {headers, body};
this.code = status;
this.message = statusText;
this.response = {headers, data};

//Capture stack trace
if (!this.stack) {
Expand All @@ -40,10 +40,10 @@ class ResponseError extends Error {
* Convert to string
*/
toString() {
const {body} = this.response;
const {data} = this.response;
let err = chalk.red(`${this.message} (${this.code})`);
if (body && Array.isArray(body.errors)) {
body.errors.forEach(error => {
if (data && Array.isArray(data.errors)) {
data.errors.forEach(error => {
const message = chalk.yellow(error.message);
const field = chalk.grey(error.field);
const help = chalk.grey(error.help);
Expand Down
8 changes: 5 additions & 3 deletions packages/mail/src/classes/mail-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
/**
* Dependencies
*/
const {Client} = require('@sendgrid/client');
const {classes: {Mail}} = require('@sendgrid/helpers');
// const {Client} = require('@sendgrid/client');
const {Client} = require('../../../client');
// const {classes: {Mail}} = require('@sendgrid/helpers');
const {classes: {Mail}} = require('../../../helpers');

/**
* Mail service class
Expand Down Expand Up @@ -192,7 +194,7 @@ class MailService {
const request = {
method: 'POST',
url: '/v3/mail/send',
body,
data: body,
};

//Send
Expand Down

0 comments on commit 8ad56ba

Please sign in to comment.