diff --git a/.github/workflows/test-and-deploy.yml b/.github/workflows/test-and-deploy.yml index b0b5ee81b..aeebb9267 100644 --- a/.github/workflows/test-and-deploy.yml +++ b/.github/workflows/test-and-deploy.yml @@ -17,7 +17,7 @@ jobs: timeout-minutes: 20 strategy: matrix: - node: [ '6', '7', '8', '10', '12', '14', '16', 'lts' ] + node: [10, 14, 16, lts] env: version: ${{ matrix.node }} DOCKER_LOGIN: ${{ secrets.DOCKER_USERNAME && secrets.DOCKER_AUTH_TOKEN }} diff --git a/package.json b/package.json index 628a4aced..bb189642e 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "moment": "^2.19.3", "sinon": "^2.3.2", "sinon-chai": "^2.10.0", - "typescript": "^3.7.4" + "typescript": "^4.0.0" }, "scripts": { "lint": "if [ `node --version | cut -d'.' -f1 | cut -c 2` -ge \"8\" ]; then eslint . --fix; else echo \"eslint is not available for node < 8.0\"; fi", diff --git a/packages/client/src/classes/client.js b/packages/client/src/classes/client.js index 8d756df3a..c1ec0ed41 100644 --- a/packages/client/src/classes/client.js +++ b/packages/client/src/classes/client.js @@ -17,8 +17,8 @@ const TWILIO_BASE_URL = 'https://email.twilio.com/'; // Initialize the allowed regions and their corresponding hosts const REGION_HOST_MAP = { - eu: 'api.eu.sendgrid.com', - global: 'api.sendgrid.com', + eu: 'https://api.eu.sendgrid.com/', + global: 'https://api.sendgrid.com/', }; class Client { constructor() { @@ -105,6 +105,7 @@ class Client { } else { this.setDefaultRequest('baseUrl', REGION_HOST_MAP[region]); } + return this; } createHeaders(data) { diff --git a/packages/client/src/client.d.ts b/packages/client/src/client.d.ts index fb84f7cb1..84075d1f9 100644 --- a/packages/client/src/client.d.ts +++ b/packages/client/src/client.d.ts @@ -30,6 +30,11 @@ declare class Client { */ setDefaultRequest(key: K | ClientRequest, value ?: ClientRequest[K]): this; + /** + * Sets the data residency as per region provided + */ + setDataResidency(region: string): this; + /** * Create headers for request */ diff --git a/packages/client/src/client.spec.js b/packages/client/src/client.spec.js index 536515615..2dfc8a4a9 100644 --- a/packages/client/src/client.spec.js +++ b/packages/client/src/client.spec.js @@ -1,7 +1,7 @@ 'use strict'; const nock = require('nock'); const sgClient = require('./client'); - +const testClient = require('./client'); const testRequest = (request, statusCode) => { const sgClient = require('./client'); sgClient.setApiKey('SG.API Key'); @@ -3094,10 +3094,44 @@ describe('test_whitelabel_links__link_id__subuser_post', () => { }); describe('setDataResidency', () => { - const sgClient = require('./client'); - sgClient.setDataResidency('eu'); + const testClient = require('./client'); + let consoleWarnSpy; - it('should have host as eu', () => { - expect(sgClient.baseUrl).to.equal('api.eu.sendgrid.com'); + beforeEach(() => { + consoleWarnSpy = sinon.spy(console, 'warn'); + }); + afterEach(() => { + console.warn.restore(); + }); + + it('should send to host EU', () => { + testClient.setDataResidency('eu'); + expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/'); + }); + it('should send to host Global/default', () => { + testClient.setDataResidency('global'); + expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/'); + }); + it('should override the existing set hostname, if data residency setter is called after', () => { + testClient.setApiKey('SG.1234567890'); + testClient.setDataResidency('eu'); + expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/'); + }); + it('should give a warning if the provided value is not allowed', () => { + testClient.setDataResidency(''); + expect(consoleWarnSpy.calledOnce).to.equal(true); + }); + it('should give a warning if the provided value is null', () => { + testClient.setDataResidency(null); + expect(consoleWarnSpy.calledOnce).to.equal(true); + }); + it('should give precedence to the order of execution', () => { + testClient.setDataResidency('eu'); + testClient.setApiKey('SG.1234567890'); + expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/'); + }); + it('should have default value of hostname as https://api.sendgrid.com/', () => { + expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/'); }); }); +