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 90 91 92 93 94 95 96 | 1x 1x 1x 1x 1x 1x 4x 3x 3x 3x 3x 2x 2x 2x 4x 1x 2x 1x 1x 1x 2x 1x 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 Mail from "../types/mail"; import Configuration, { ConfigurationData } from "./configuration"; 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.mail.setSubstitutionWrappers( get(this.configuration, ["template", "wrappers"], ["{{", "}}"]) // default to {{ }} ); this.setTemplateData(get(this.configuration, ["template", "data"], {})); this.setTemplateFile(get(this.configuration, ["template", "filePath"])); } } public setSubject(subject: string): void {} public get personalization(): Personalization { return this.mail.personalizations[0]; } public addSubstitution(key: string, value: string): void { this.personalization.addSubstitution( `${this.mail.substitutionWrappers[0]}${key}${ this.mail.substitutionWrappers[1] }`, value ); } public 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 }); } public setTemplateData(templateData: { [key: string]: string }): void { Object.keys(templateData).forEach(key => { this.addSubstitution(key, templateData[key]); }); } public send(): Promise<[ClientResponse, any]> { 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." ); } }); } } |