Skip to content

Commit 7154516

Browse files
authored
feat: Add subuser impersonation to client (#600)
Sendgrid API has an (as yet) undocumented feature allowing parent accounts to impersonate subusers by including an HTTP header "On-Behalf-Of" in each API request. This commit enables setting the value for impersonation when creating the Sendgrid API Client instance.
1 parent fe835e1 commit 7154516

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

packages/client/src/classes/client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const TWILIO_BASE_URL = 'https://email.twilio.com/';
1818
class Client {
1919
constructor() {
2020
this.auth = '';
21+
this.impersonateSubuser = '';
2122

2223
this.defaultHeaders = {
2324
Accept: 'application/json',
@@ -65,6 +66,10 @@ class Client {
6566
return typeof value === 'string' || value instanceof String;
6667
}
6768

69+
setImpersonateSubuser(subuser) {
70+
this.impersonateSubuser = subuser;
71+
}
72+
6873
setDefaultHeader(key, value) {
6974
this.defaultHeaders[key] = value;
7075
return this;
@@ -84,6 +89,10 @@ class Client {
8489
headers.Authorization = this.auth;
8590
}
8691

92+
if (this.impersonateSubuser) {
93+
headers['On-Behalf-Of'] = this.impersonateSubuser;
94+
}
95+
8796
return headers;
8897
}
8998

packages/client/src/client.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ declare class Client {
1515
*/
1616
setTwilioEmailAuth(username: string, password: string): void;
1717

18+
/**
19+
* Set client requests to impersonate a subuser
20+
*/
21+
setImpersonateSubuser(subuser: string): void;
22+
1823
/**
1924
* Set default header
2025
*/

packages/client/src/client.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ describe('client', () => {
7676
});
7777
});
7878

79+
describe('setImpersonateSubuser', () => {
80+
const impersonateSubuser = 'abcxyz@this.is.a.test.subuser';
81+
const sgClient = require('./client');
82+
sgClient.setImpersonateSubuser(impersonateSubuser);
83+
84+
it('should set the imperonate subuser header', () => {
85+
expect(sgClient.impersonateSubuser).to.equal(impersonateSubuser);
86+
});
87+
});
88+
7989
describe('test_access_settings_activity_get', () => {
8090
const request = {};
8191
request.qs = {

0 commit comments

Comments
 (0)