summaryrefslogtreecommitdiffstats
path: root/lib/sendgrid.js
blob: 2c5cb6b8cc592b8a44cab24eed5c834f54a915cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* eslint dot-notation: 'off' */
'use strict';
var package_json = require('./../package.json');
var emptyRequest = require('sendgrid-rest').emptyRequest;

// SendGrid allows for quick and easy access to the v3 Web API
function SendGrid(apiKey, host, globalHeaders) {
  var Client = require('sendgrid-rest').Client;
  var globalRequest = this.emptyRequest();
  globalRequest.host = host || 'api.sendgrid.com';
  globalRequest.headers['Authorization'] = 'Bearer '.concat(apiKey);
  globalRequest.headers['Accept'] = 'application/json';
  globalRequest.headers['User-Agent'] =
    'sendgrid/' + package_json.version + ';nodejs';
  if (globalHeaders) {
    for (var obj in globalHeaders) {
      for (var key in globalHeaders[obj]) {
        globalRequest.headers[key] = globalHeaders[obj][key];
      }
    }
  }
  var client = new Client(globalRequest);

  this.emptyRequest = function() {
    return JSON.parse(JSON.stringify(emptyRequest));
  };

  // Interact with the API with this function
  this.API = function(request, callback) {
    client.API(request, function(response) {
      callback(response);
    });
  };

  this.globalRequest = globalRequest;
  return this;
}

module.exports =
{
  SendGrid: SendGrid,
  emptyRequest: SendGrid.emptyRequest(),
};