Skip to content

Commit b356716

Browse files
committed
updates to baseUrl setter
1 parent a21b169 commit b356716

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

packages/client/src/classes/client.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Client {
2424
constructor() {
2525
this.auth = '';
2626
this.impersonateSubuser = '';
27+
this.sendgrid_region = '';
2728

2829
this.defaultHeaders = {
2930
Accept: 'application/json',
@@ -43,8 +44,10 @@ class Client {
4344

4445
setApiKey(apiKey) {
4546
this.auth = 'Bearer ' + apiKey;
46-
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL);
47-
47+
// this means that region was never set before
48+
if (this.sendgrid_region == '') {
49+
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL);
50+
}
4851
if (!this.isValidApiKey(apiKey)) {
4952
console.warn(`API key does not start with "${API_KEY_PREFIX}".`);
5053
}
@@ -108,6 +111,7 @@ class Client {
108111
if (!REGION_HOST_MAP.hasOwnProperty(region)) {
109112
console.warn('Region can only be "global" or "eu".');
110113
} else {
114+
this.sendgrid_region = region;
111115
this.setDefaultRequest('baseUrl', REGION_HOST_MAP[region]);
112116
}
113117
return this;

packages/client/src/client.spec.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const nock = require('nock');
33
const sgClient = require('./client');
4-
const testClient = require('./client');
4+
let testClient = require('./client');
55
const testRequest = (request, statusCode) => {
66
const sgClient = require('./client');
77
sgClient.setApiKey('SG.API Key');
@@ -3094,23 +3094,26 @@ describe('test_whitelabel_links__link_id__subuser_post', () => {
30943094
});
30953095

30963096
describe('setDataResidency', () => {
3097-
const testClient = require('./client');
30983097
let consoleWarnSpy;
3099-
31003098
beforeEach(() => {
3099+
testClient = require('./client');
31013100
consoleWarnSpy = sinon.spy(console, 'warn');
31023101
});
31033102
afterEach(() => {
31043103
console.warn.restore();
31053104
});
3106-
3105+
it('should have default value of hostname as https://api.sendgrid.com/', () => {
3106+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3107+
expect(testClient.sendgrid_region).to.equal('');
3108+
});
31073109
it('should send to host EU', () => {
31083110
testClient.setDataResidency('eu');
31093111
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
31103112
});
31113113
it('should send to host Global/default', () => {
31123114
testClient.setDataResidency('global');
31133115
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3116+
expect(testClient.sendgrid_region).to.equal('global');
31143117
});
31153118
it('should override the existing set hostname, if data residency setter is called after', () => {
31163119
testClient.setApiKey('SG.1234567890');
@@ -3125,13 +3128,19 @@ describe('setDataResidency', () => {
31253128
testClient.setDataResidency(null);
31263129
expect(consoleWarnSpy.calledOnce).to.equal(true);
31273130
});
3128-
it('should give precedence to the order of execution', () => {
3131+
it('setting the API Key wont reset the region set', () => {
31293132
testClient.setDataResidency('eu');
31303133
testClient.setApiKey('SG.1234567890');
3131-
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3134+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
3135+
expect(testClient.sendgrid_region).to.equal('eu');
31323136
});
3133-
it('should have default value of hostname as https://api.sendgrid.com/', () => {
3137+
it('should send to host global and then call setApiKey', () => {
3138+
testClient.setDataResidency('global');
3139+
testClient.setApiKey('SG.1234567890');
31343140
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3141+
expect(testClient.sendgrid_region).to.equal('global');
3142+
3143+
31353144
});
31363145
});
31373146

0 commit comments

Comments
 (0)