Skip to content
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

Make SendGrid a factory that enables multiple prototype instances #277

Merged
merged 1 commit into from
Sep 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions lib/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,55 +64,60 @@ function makeHeaders(apiKey, globalHeaders) {
* SendGrid allows for quick and easy access to the v3 Web API
*/
function SendGrid(apiKey, host, globalHeaders) {
return new SendGridInstance(apiKey, host, globalHeaders);
}

/**
* SendGrid allows for quick and easy access to the v3 Web API
*/
function SendGridInstance(apiKey, host, globalHeaders) {
//Create global request
var globalRequest = getEmptyRequest({
this.globalRequest = getEmptyRequest({
host: host || 'api.sendgrid.com',
headers: makeHeaders(apiKey, globalHeaders),
});

//Initialize new client
var client = new Client(globalRequest);
this.client = new Client(this.globalRequest);
}

//Interact with the API with this function
SendGrid.API = function(request, callback) {
//Interact with the API with this function
SendGridInstance.prototype.API = function(request, callback) {
var self = this;

//If no callback provided, we will return a promise
if (!callback) {
if (!SendGrid.Promise) {
throw new SendGridError('Promise API not supported');
}
return new SendGrid.Promise(function(resolve, reject) {
client.API(request, function(response) {
if (isValidResponse(response)) {
resolve(response);
}
else {
var error = new SendGridError('Response error');
error.response = response;
reject(error);
}
});
});
//If no callback provided, we will return a promise
if (!callback) {
if (!SendGrid.Promise) {
throw new SendGridError('Promise API not supported');
}

//Use callback
client.API(request, function(response) {
if (isValidResponse(response)) {
callback(null, response);
}
else {
var error = new SendGridError('Response error');
callback(error, response);
}
return new SendGrid.Promise(function(resolve, reject) {
self.client.API(request, function(response) {
if (isValidResponse(response)) {
resolve(response);
}
else {
var error = new SendGridError('Response error');
error.response = response;
reject(error);
}
});
});
};
}

//Set requests
SendGrid.emptyRequest = getEmptyRequest;
SendGrid.globalRequest = globalRequest;
return SendGrid;
}
//Use callback
self.client.API(request, function(response) {
if (isValidResponse(response)) {
callback(null, response);
}
else {
var error = new SendGridError('Response error');
callback(error, response);
}
});
};

//Set requests
SendGridInstance.prototype.emptyRequest = getEmptyRequest;

//Try to use native promises by default
if (typeof Promise !== 'undefined') {
Expand Down