Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutiburman committed Nov 30, 2023
1 parent b356716 commit 319754e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
11 changes: 5 additions & 6 deletions docs/use-cases/data-residency-set-hostname.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Choosing a hostname to send messages to
# Choosing a data-residency to send messages to

Use the `setDataResidency` setter to specify which host to send to:

Send to EU (hostname: `https://api.eu.sendgrid.com/`)
Send to EU (data-residency: `https://api.eu.sendgrid.com/`)
```js
const sgMail = require('@sendgrid/mail');
sgMail.setDataResidency('eu');
Expand All @@ -16,7 +16,7 @@ const msg = {
sgMail.send(msg);
```
Send to Global region, this is also the default host, if the setter is not used
(hostname: `https://api.sendgrid.com/`)
(data-residency: `https://api.sendgrid.com/`)
```js
const sgMail = require('@sendgrid/mail');
sgMail.setDataResidency('global');
Expand All @@ -32,7 +32,6 @@ 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.
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/
1. 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 data-residency is https://api.sendgrid.com/
3. The valid values for `region` in `client.setDataResidency(region)` are only `eu` and `global`. Case-sensitive.
10 changes: 4 additions & 6 deletions packages/client/src/classes/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
const API_KEY_PREFIX = 'SG.';
const SENDGRID_BASE_URL = 'https://api.sendgrid.com/';
const TWILIO_BASE_URL = 'https://email.twilio.com/';

const SENDGRID_REGION = 'global';
// Initialize the allowed regions and their corresponding hosts
const REGION_HOST_MAP = {
eu: 'https://api.eu.sendgrid.com/',
Expand All @@ -24,7 +24,7 @@ class Client {
constructor() {
this.auth = '';
this.impersonateSubuser = '';
this.sendgrid_region = '';
this.sendgrid_region = SENDGRID_REGION;

this.defaultHeaders = {
Accept: 'application/json',
Expand All @@ -44,10 +44,8 @@ class Client {

setApiKey(apiKey) {
this.auth = 'Bearer ' + apiKey;
// this means that region was never set before
if (this.sendgrid_region == '') {
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL);
}
this.setDefaultRequest('baseUrl', REGION_HOST_MAP[this.sendgrid_region]);

if (!this.isValidApiKey(apiKey)) {
console.warn(`API key does not start with "${API_KEY_PREFIX}".`);
}
Expand Down

0 comments on commit 319754e

Please sign in to comment.