Skip to content

Commit eed8ba2

Browse files
authored
feat: Add data residency for eu and global regions (#1390)
1 parent b1c831f commit eed8ba2

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Choosing a data-residency to send messages to
2+
3+
Use the `setDataResidency` setter to specify which host to send to:
4+
5+
Send to EU (data-residency: `https://api.eu.sendgrid.com/`)
6+
```js
7+
const sgMail = require('@sendgrid/mail');
8+
sgMail.setDataResidency('eu');
9+
const msg = {
10+
11+
12+
subject: 'Hello world',
13+
text: 'Hello plain world!',
14+
html: '<p>Hello HTML world!</p>',
15+
};
16+
sgMail.send(msg);
17+
```
18+
Send to Global region, this is also the default host, if the setter is not used
19+
(data-residency: `https://api.sendgrid.com/`)
20+
```js
21+
const sgMail = require('@sendgrid/mail');
22+
sgMail.setDataResidency('global');
23+
const msg = {
24+
25+
26+
subject: 'Hello world',
27+
text: 'Hello plain world!',
28+
html: '<p>Hello HTML world!</p>',
29+
};
30+
sgMail.send(msg);
31+
```
32+
33+
## Limitations
34+
35+
1. Emails can only be sent to two hosts for now; 'eu' (https://api.eu.sendgrid.com/) and 'global' (https://api.eu.sendgrid.com/)
36+
2. The default data-residency is https://api.sendgrid.com/
37+
3. The valid values for `region` in `client.setDataResidency(region)` are only `eu` and `global`. Case-sensitive.

packages/client/src/classes/client.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ const {
1414
const API_KEY_PREFIX = 'SG.';
1515
const SENDGRID_BASE_URL = 'https://api.sendgrid.com/';
1616
const TWILIO_BASE_URL = 'https://email.twilio.com/';
17-
17+
const SENDGRID_REGION = 'global';
18+
// Initialize the allowed regions and their corresponding hosts
19+
const REGION_HOST_MAP = {
20+
eu: 'https://api.eu.sendgrid.com/',
21+
global: 'https://api.sendgrid.com/',
22+
};
1823
class Client {
1924
constructor() {
2025
this.auth = '';
2126
this.impersonateSubuser = '';
27+
this.sendgrid_region = SENDGRID_REGION;
2228

2329
this.defaultHeaders = {
2430
Accept: 'application/json',
@@ -38,7 +44,7 @@ class Client {
3844

3945
setApiKey(apiKey) {
4046
this.auth = 'Bearer ' + apiKey;
41-
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL);
47+
this.setDefaultRequest('baseUrl', REGION_HOST_MAP[this.sendgrid_region]);
4248

4349
if (!this.isValidApiKey(apiKey)) {
4450
console.warn(`API key does not start with "${API_KEY_PREFIX}".`);
@@ -94,6 +100,21 @@ class Client {
94100
return this;
95101
}
96102

103+
/**
104+
* Global is the default residency (or region)
105+
* Global region means the message will be sent through https://api.sendgrid.com
106+
* EU region means the message will be sent through https://api.eu.sendgrid.com
107+
**/
108+
setDataResidency(region) {
109+
if (!REGION_HOST_MAP.hasOwnProperty(region)) {
110+
console.warn('Region can only be "global" or "eu".');
111+
} else {
112+
this.sendgrid_region = region;
113+
this.setDefaultRequest('baseUrl', REGION_HOST_MAP[region]);
114+
}
115+
return this;
116+
}
117+
97118
createHeaders(data) {
98119
// Merge data with default headers.
99120
const headers = mergeData(this.defaultHeaders, data);

packages/client/src/client.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ declare class Client {
3030
*/
3131
setDefaultRequest<K extends keyof ClientRequest>(key: K | ClientRequest, value ?: ClientRequest[K]): this;
3232

33+
/**
34+
* Sets the data residency as per region provided
35+
*/
36+
setDataResidency(region: string): this;
37+
3338
/**
3439
* Create headers for request
3540
*/

packages/client/src/client.spec.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const nock = require('nock');
3-
3+
const sgClient = require('./client');
4+
let testClient = require('./client');
45
const testRequest = (request, statusCode) => {
56
const sgClient = require('./client');
67
sgClient.setApiKey('SG.API Key');
@@ -3091,3 +3092,55 @@ describe('test_whitelabel_links__link_id__subuser_post', () => {
30913092
return testRequest(request, 200);
30923093
});
30933094
});
3095+
3096+
describe('setDataResidency', () => {
3097+
let consoleWarnSpy;
3098+
beforeEach(() => {
3099+
testClient = require('./client');
3100+
consoleWarnSpy = sinon.spy(console, 'warn');
3101+
});
3102+
afterEach(() => {
3103+
console.warn.restore();
3104+
});
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('global');
3108+
});
3109+
it('should send to host EU', () => {
3110+
testClient.setDataResidency('eu');
3111+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
3112+
});
3113+
it('should send to host Global/default', () => {
3114+
testClient.setDataResidency('global');
3115+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3116+
expect(testClient.sendgrid_region).to.equal('global');
3117+
});
3118+
it('should override the existing set hostname, if data residency setter is called after', () => {
3119+
testClient.setApiKey('SG.1234567890');
3120+
testClient.setDataResidency('eu');
3121+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
3122+
});
3123+
it('should give a warning if the provided value is not allowed', () => {
3124+
testClient.setDataResidency('');
3125+
expect(consoleWarnSpy.calledOnce).to.equal(true);
3126+
});
3127+
it('should give a warning if the provided value is null', () => {
3128+
testClient.setDataResidency(null);
3129+
expect(consoleWarnSpy.calledOnce).to.equal(true);
3130+
});
3131+
it('setting the API Key wont reset the region set', () => {
3132+
testClient.setDataResidency('eu');
3133+
testClient.setApiKey('SG.1234567890');
3134+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
3135+
expect(testClient.sendgrid_region).to.equal('eu');
3136+
});
3137+
it('should send to host global and then call setApiKey', () => {
3138+
testClient.setDataResidency('global');
3139+
testClient.setApiKey('SG.1234567890');
3140+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3141+
expect(testClient.sendgrid_region).to.equal('global');
3142+
3143+
3144+
});
3145+
});
3146+

0 commit comments

Comments
 (0)