diff --git a/packages/client/src/classes/client.js b/packages/client/src/classes/client.js index 713dcc31..c0c051fe 100644 --- a/packages/client/src/classes/client.js +++ b/packages/client/src/classes/client.js @@ -24,6 +24,7 @@ class Client { constructor() { this.auth = ''; this.impersonateSubuser = ''; + this.sendgrid_region = ''; this.defaultHeaders = { Accept: 'application/json', @@ -43,8 +44,10 @@ class Client { setApiKey(apiKey) { this.auth = 'Bearer ' + apiKey; - this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL); - + // this means that region was never set before + if (this.sendgrid_region == '') { + this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL); + } if (!this.isValidApiKey(apiKey)) { console.warn(`API key does not start with "${API_KEY_PREFIX}".`); } @@ -108,6 +111,7 @@ class Client { if (!REGION_HOST_MAP.hasOwnProperty(region)) { console.warn('Region can only be "global" or "eu".'); } else { + this.sendgrid_region = region; this.setDefaultRequest('baseUrl', REGION_HOST_MAP[region]); } return this; diff --git a/packages/client/src/client.spec.js b/packages/client/src/client.spec.js index 2dfc8a4a..b2be9286 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'); +let testClient = require('./client'); const testRequest = (request, statusCode) => { const sgClient = require('./client'); sgClient.setApiKey('SG.API Key'); @@ -3094,16 +3094,18 @@ describe('test_whitelabel_links__link_id__subuser_post', () => { }); describe('setDataResidency', () => { - const testClient = require('./client'); let consoleWarnSpy; - beforeEach(() => { + testClient = require('./client'); consoleWarnSpy = sinon.spy(console, 'warn'); }); afterEach(() => { console.warn.restore(); }); - + it('should have default value of hostname as https://api.sendgrid.com/', () => { + expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/'); + expect(testClient.sendgrid_region).to.equal(''); + }); it('should send to host EU', () => { testClient.setDataResidency('eu'); expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/'); @@ -3111,6 +3113,7 @@ describe('setDataResidency', () => { it('should send to host Global/default', () => { testClient.setDataResidency('global'); expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/'); + expect(testClient.sendgrid_region).to.equal('global'); }); it('should override the existing set hostname, if data residency setter is called after', () => { testClient.setApiKey('SG.1234567890'); @@ -3125,13 +3128,19 @@ describe('setDataResidency', () => { testClient.setDataResidency(null); expect(consoleWarnSpy.calledOnce).to.equal(true); }); - it('should give precedence to the order of execution', () => { + it('setting the API Key wont reset the region set', () => { testClient.setDataResidency('eu'); testClient.setApiKey('SG.1234567890'); - expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/'); + expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/'); + expect(testClient.sendgrid_region).to.equal('eu'); }); - it('should have default value of hostname as https://api.sendgrid.com/', () => { + it('should send to host global and then call setApiKey', () => { + testClient.setDataResidency('global'); + testClient.setApiKey('SG.1234567890'); expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/'); + expect(testClient.sendgrid_region).to.equal('global'); + + }); });