@sendgrid/client
Advanced tools
Comparing version 7.0.1 to 7.1.0
{ | ||
"name": "@sendgrid/client", | ||
"description": "Twilio SendGrid NodeJS API client", | ||
"version": "7.0.1", | ||
"version": "7.1.0", | ||
"author": "Twilio SendGrid <help@twilio.com> (sendgrid.com)", | ||
@@ -33,2 +33,5 @@ "contributors": [ | ||
}, | ||
"devDependencies": { | ||
"nock": "^10.0.6" | ||
}, | ||
"tags": [ | ||
@@ -41,3 +44,3 @@ "http", | ||
], | ||
"gitHead": "2641271a05b2dcc05b3ba13d10ea577102c97e4e" | ||
"gitHead": "1246402c49ea95c742663a62df082de73e9b040e" | ||
} |
@@ -15,6 +15,9 @@ 'use strict'; | ||
const API_KEY_PREFIX = 'SG.'; | ||
const SENDGRID_BASE_URL = 'https://api.sendgrid.com/'; | ||
const TWILIO_BASE_URL = 'https://email.twilio.com/'; | ||
class Client { | ||
constructor() { | ||
this.apiKey = ''; | ||
this.auth = ''; | ||
this.impersonateSubuser = ''; | ||
@@ -28,3 +31,3 @@ this.defaultHeaders = { | ||
this.defaultRequest = { | ||
baseUrl: 'https://api.sendgrid.com/', | ||
baseUrl: SENDGRID_BASE_URL, | ||
url: '', | ||
@@ -37,3 +40,4 @@ method: 'GET', | ||
setApiKey(apiKey) { | ||
this.apiKey = apiKey; | ||
this.auth = 'Bearer ' + apiKey; | ||
this.setDefaultRequest('baseUrl', SENDGRID_BASE_URL); | ||
@@ -45,2 +49,12 @@ if (!this.isValidApiKey(apiKey)) { | ||
setTwilioEmailAuth(username, password) { | ||
const b64Auth = Buffer.from(username + ':' + password).toString('base64'); | ||
this.auth = 'Basic ' + b64Auth; | ||
this.setDefaultRequest('baseUrl', TWILIO_BASE_URL); | ||
if (!this.isValidTwilioAuth(username, password)) { | ||
console.warn('Twilio Email credentials must be non-empty strings.'); | ||
} | ||
} | ||
isValidApiKey(apiKey) { | ||
@@ -50,2 +64,7 @@ return this.isString(apiKey) && apiKey.trim().startsWith(API_KEY_PREFIX); | ||
isValidTwilioAuth(username, password) { | ||
return this.isString(username) && username | ||
&& this.isString(password) && password; | ||
} | ||
isString(value) { | ||
@@ -55,2 +74,6 @@ return typeof value === 'string' || value instanceof String; | ||
setImpersonateSubuser(subuser) { | ||
this.impersonateSubuser = subuser; | ||
} | ||
setDefaultHeader(key, value) { | ||
@@ -70,7 +93,11 @@ this.defaultHeaders[key] = value; | ||
// Add API key, but don't overwrite if header already set. | ||
if (typeof headers.Authorization === 'undefined' && this.apiKey) { | ||
headers.Authorization = 'Bearer ' + this.apiKey; | ||
// Add auth, but don't overwrite if header already set. | ||
if (typeof headers.Authorization === 'undefined' && this.auth) { | ||
headers.Authorization = this.auth; | ||
} | ||
if (this.impersonateSubuser) { | ||
headers['On-Behalf-Of'] = this.impersonateSubuser; | ||
} | ||
return headers; | ||
@@ -77,0 +104,0 @@ } |
@@ -7,4 +7,5 @@ import {ResponseError} from "@sendgrid/helpers/classes"; | ||
constructor(); | ||
/** | ||
* Set API key | ||
* Set the SendGrid API key. | ||
*/ | ||
@@ -14,2 +15,12 @@ setApiKey(apiKey: string): void; | ||
/** | ||
* Set the Twilio Email credentials. | ||
*/ | ||
setTwilioEmailAuth(username: string, password: string): void; | ||
/** | ||
* Set client requests to impersonate a subuser | ||
*/ | ||
setImpersonateSubuser(subuser: string): void; | ||
/** | ||
* Set default header | ||
@@ -16,0 +27,0 @@ */ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
318605
2995
1