Skip to content

Commit

Permalink
feat: migrate from deprecated request package 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 d3048c4
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 285 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
2 changes: 1 addition & 1 deletion packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ client.setDefaultHeader('User-Agent', 'Some user agent string');

## Change Request Defaults
```js
client.setDefaultRequest('baseUrl', 'https://api.sendgrid.com/');
client.setDefaultRequest('baseURL', 'https://api.sendgrid.com/');
```

## Overwrite Promise Implementation
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
45 changes: 28 additions & 17 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,14 +29,15 @@ 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,
baseUrl: 'https://api.sendgrid.com/',
data: {},
baseURL: 'https://api.sendgrid.com/',
url: '',
method: 'GET',
headers: {},
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
var parsedResponse = {
statusCode: response.status,
body: response.data,
};
return 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
Loading

0 comments on commit d3048c4

Please sign in to comment.