All files email.ts

100% Statements 26/26
100% Branches 6/6
100% Functions 8/8
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 901x 1x 1x               1x 1x   1x           4x   3x 3x   3x           3x 2x     2x 2x         7x       1x       2x 1x         1x   1x             2x 1x         2x 2x               2x       1x              
import sgClient = require("@sendgrid/client");
import get = require("lodash.get");
import fs = require("fs");
 
import { ClientResponse } from "@sendgrid/client/src/response";
import { EmailAddress, Personalization } from "@sendgrid/helpers/classes";
import { EmailJSON, EmailData } from "@sendgrid/helpers/classes/email-address";
import { MailContent, MailData } from "@sendgrid/helpers/classes/mail";
import { PersonalizationData } from "@sendgrid/helpers/classes/personalization";
 
import Configuration, { ConfigurationData } from "./configuration";
import Mail from "./mail";
 
export default class Email {
  protected configuration: Configuration;
  protected client: any;
  public mail: Mail;
 
  constructor(configurationData: ConfigurationData) {
    this.configuration = new Configuration(configurationData);
 
    this.client = sgClient;
    this.client.setApiKey(this.configuration.apiKey);
 
    this.mail = new Mail({
      from: this.configuration.from,
      to: this.configuration.to,
      subject: this.configuration.subject
    });
 
    if (this.configuration.template) {
      this.personalization.setSubstitutionWrappers(
        get(this.configuration, ["template", "wrappers"], ["{{", "}}"]) // default to {{ }}
      );
      this.setTemplateData(get(this.configuration, ["template", "data"], {}));
      this.setTemplateFile(get(this.configuration, ["template", "filePath"]));
    }
  }
 
  public get personalization(): Personalization {
    return this.mail.personalizations[0];
  }
 
  private addSubstitution(key: string, value: string): void {
    this.personalization.addSubstitution(key, value);
  }
 
  private setTemplateFile(filePath: string): void {
    if (!fs.existsSync(filePath)) {
      throw new Error(
        `[Email.addTemplate] The template file does not exist {path: ${filePath}}.`
      );
    }
 
    const value = fs.readFileSync(filePath).toString();
 
    this.mail.addContent({
      type: "text/html",
      value
    });
  }
 
  private setTemplateData(templateData: { [key: string]: string }): void {
    Object.keys(templateData).forEach(key => {
      this.addSubstitution(key, templateData[key]);
    });
  }
 
  public send(): Promise<[ClientResponse, any]> {
    console.log(JSON.stringify(this.mail.toJSON()));
    return this.client
      .request({
        method: "POST",
        url: "/v3/mail/send",
        body: this.mail.toJSON()
      })
      .then(([response]: [ClientResponse]) => {
        // We can confirm the validity or invalidity of the action by checking the response statusCode
        if (response.statusCode !== 202) {
          // TODO: determine the different types of errors we may receive here
          // and throw specific error if we can figure out what went wrong
          // https://sendgrid.com/docs/API_Reference/api_v3.html
          throw new Error(
            "[Email post] received an error response from the sendgrid API."
          );
        }
      });
  }
}