-
Notifications
You must be signed in to change notification settings - Fork 783
feat: Add data residency for eu and global regions #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
91b4cb6
50a8691
2f7c9ea
a246759
1c79ce7
a21b169
b356716
8bfe5cd
3f90c9f
d0cc9e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Choosing a hostname to send messages to | ||
|
||
Use the `setDataResidency` setter to specify which host to send to: | ||
|
||
Send to EU (hostname: `https://api.eu.sendgrid.com/`) | ||
```js | ||
const sgMail = require('@sendgrid/mail'); | ||
sgMail.setDataResidency('eu'); | ||
const msg = { | ||
to: '[email protected]', | ||
from: '[email protected]', | ||
subject: 'Hello world', | ||
text: 'Hello plain world!', | ||
html: '<p>Hello HTML world!</p>', | ||
}; | ||
sgMail.send(msg); | ||
``` | ||
Send to Global region, this is also the default host, if the setter is not used | ||
(hostname: `https://api.sendgrid.com/`) | ||
```js | ||
const sgMail = require('@sendgrid/mail'); | ||
sgMail.setDataResidency('global'); | ||
const msg = { | ||
to: '[email protected]', | ||
from: '[email protected]', | ||
subject: 'Hello world', | ||
text: 'Hello plain world!', | ||
html: '<p>Hello HTML world!</p>', | ||
}; | ||
sgMail.send(msg); | ||
``` | ||
|
||
## Limitations | ||
|
||
1. Setting the API Key (via `client.setApiKey()`) or Twilio Authentication (via `client.setTwilioEmailAuth()`) will override the hostname to default value. Use the setter call after this set-up. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this something we should figure out a better path for? This is not an ideal scenario. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tbischel @gladysmae08 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this limitation is no longer necessary after the changes that were just added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this won't introduce breaking changes for existing customers, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if customers don't have to change their code to achieve the same functionality, I think I'm good. if they do, we might want to look into a major version bump? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wont introduce a breaking change, as the |
||
2. Emails can only be sent to two hosts for now; 'eu' (https://api.eu.sendgrid.com/) and 'global' (https://api.eu.sendgrid.com/) | ||
2. The default hostname is https://api.sendgrid.com/ | ||
3. The valid values for `region` in `client.setDataResidency(region)` are only `eu` and `global`. Case-sensitive. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,16 @@ const API_KEY_PREFIX = 'SG.'; | |
const SENDGRID_BASE_URL = 'https://api.sendgrid.com/'; | ||
const TWILIO_BASE_URL = 'https://email.twilio.com/'; | ||
|
||
// Initialize the allowed regions and their corresponding hosts | ||
const REGION_HOST_MAP = { | ||
eu: 'https://api.eu.sendgrid.com/', | ||
global: 'https://api.sendgrid.com/', | ||
}; | ||
class Client { | ||
constructor() { | ||
this.auth = ''; | ||
this.impersonateSubuser = ''; | ||
this.sendgrid_region = ''; | ||
shrutiburman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
this.defaultHeaders = { | ||
Accept: 'application/json', | ||
|
@@ -38,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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This technically works but I think it is unnecessary. We can just always reset the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, I have set the default region as 'global', we can reset based on that now. Thanks. |
||
if (!this.isValidApiKey(apiKey)) { | ||
console.warn(`API key does not start with "${API_KEY_PREFIX}".`); | ||
} | ||
|
@@ -94,6 +102,21 @@ class Client { | |
return this; | ||
} | ||
|
||
/** | ||
* Global is the default residency (or region) | ||
* Global region means the message will be sent through https://api.sendgrid.com | ||
* EU region means the message will be sent through https://api.eu.sendgrid.com | ||
**/ | ||
setDataResidency(region) { | ||
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; | ||
} | ||
|
||
createHeaders(data) { | ||
// Merge data with default headers. | ||
const headers = mergeData(this.defaultHeaders, data); | ||
|
Uh oh!
There was an error while loading. Please reload this page.