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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 1x 1x 1x 1x 1x 2x 1x 1x 1x 2x 1x 1x 1x | import sgClient = require("@sendgrid/client"); import get = require("lodash.get"); 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 } from "@sendgrid/helpers/classes/mail"; import { PersonalizationData } from "@sendgrid/helpers/classes/personalization"; import { Mail } from "../../types"; import Configuration, { ConfigurationData } from "../configuration"; export default class Email { protected configuration: Configuration; protected client: any; protected _mail: Mail; constructor(configurationData?: ConfigurationData) { this.configuration = new Configuration(configurationData); this.client = sgClient; this.client.setApiKey(this.configuration.apiKey); this.mail.setFrom({ email: get( this.configuration, ["from", "email"], process.env.SENDGRID_FROM_EMAIL ), name: get( this.configuration, ["from", "name"], process.env.SENDGRID_FROM_NAME ) }); } public get mail(): Mail { if (typeof this._mail !== "undefined") { return this._mail; } this._mail = new Mail(); return this.mail; } public get content(): MailContent[] { return this.mail.content; } public addContent(content: MailContent) { return this.mail.addContent(content); } public set from(from: EmailJSON) { this.mail.setFrom(from); } public get personalization(): Personalization { if (this.mail.personalizations.length < 1) { return undefined; } return this.mail.personalizations[0]; } public addTo(to: EmailData): void { this.personalization.addTo(to); } public addSubstitution(key: string, value: string): void { this.personalization.addSubstitution(key, value); } protected pre(): void {} protected post(response: ClientResponse): void { // We can confirm the validity or invalidity of the action by checking the response statusCode switch (response.statusCode) { case 202: { return this.post(response); } case 400: case 401: case 413: default: { // 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." ); } } } public send(): Promise<[ClientResponse, any]> { this.pre(); return this.client .request({ method: "POST", url: "/v3/mail/send", body: this.mail.toJSON() }) .then((resp: [ClientResponse]) => { this.post(resp[0]); }); } } |