From 8ad56ba6a40a655570044c950a886af3aa17c798 Mon Sep 17 00:00:00 2001 From: Elise Shanholtz Date: Mon, 9 Mar 2020 14:03:13 -0700 Subject: [PATCH] feat: migrate from deprecated request module to axios --- package.json | 2 +- packages/client/USAGE.md | 6 +-- packages/client/package.json | 3 +- packages/client/src/classes/client.js | 43 ++++++++++++++-------- packages/client/src/client.spec.js | 2 +- packages/client/src/request.d.ts | 4 +- packages/client/src/response.d.ts | 4 +- packages/helpers/classes/response-error.js | 14 +++---- packages/mail/src/classes/mail-service.js | 8 ++-- 9 files changed, 49 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index ca3d2224b..ffcaf1f73 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/client/USAGE.md b/packages/client/USAGE.md index da2963bb0..4ec3b1292 100644 --- a/packages/client/USAGE.md +++ b/packages/client/USAGE.md @@ -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) @@ -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) @@ -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) diff --git a/packages/client/package.json b/packages/client/package.json index de69de7de..ff0c8ec09 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -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", diff --git a/packages/client/src/classes/client.js b/packages/client/src/classes/client.js index a76cbbbdd..1298d24e3 100644 --- a/packages/client/src/classes/client.js +++ b/packages/client/src/classes/client.js @@ -3,7 +3,7 @@ /** * Dependencies */ -const http = require('request'); +const axios = require('axios'); const pkg = require('../../package.json'); const { helpers: { @@ -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', @@ -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); @@ -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 diff --git a/packages/client/src/client.spec.js b/packages/client/src/client.spec.js index a8503c1af..6e6846a15 100644 --- a/packages/client/src/client.spec.js +++ b/packages/client/src/client.spec.js @@ -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', () => { diff --git a/packages/client/src/request.d.ts b/packages/client/src/request.d.ts index 5ddb74dee..4c487ea77 100644 --- a/packages/client/src/request.d.ts +++ b/packages/client/src/request.d.ts @@ -1,3 +1,3 @@ -import {OptionsWithUrl} from "request"; +import {AxiosRequestConfig} from "axios"; -export type ClientRequest = OptionsWithUrl; \ No newline at end of file +export type ClientRequest = AxiosRequestConfig; diff --git a/packages/client/src/response.d.ts b/packages/client/src/response.d.ts index 25ddfdcba..73ffdc214 100644 --- a/packages/client/src/response.d.ts +++ b/packages/client/src/response.d.ts @@ -1,3 +1,3 @@ -import {RequestResponse} from "request"; +import {AxiosResponse} from "axios"; -export type ClientResponse = RequestResponse; \ No newline at end of file +export type ClientResponse = AxiosResponse; diff --git a/packages/helpers/classes/response-error.js b/packages/helpers/classes/response-error.js index d422b6efd..38f8d2f33 100644 --- a/packages/helpers/classes/response-error.js +++ b/packages/helpers/classes/response-error.js @@ -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) { @@ -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); diff --git a/packages/mail/src/classes/mail-service.js b/packages/mail/src/classes/mail-service.js index ec4e455e0..edc14c2bb 100644 --- a/packages/mail/src/classes/mail-service.js +++ b/packages/mail/src/classes/mail-service.js @@ -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 @@ -192,7 +194,7 @@ class MailService { const request = { method: 'POST', url: '/v3/mail/send', - body, + data: body, }; //Send