Typescript definitions for sendgrid

南笙酒味 提交于 2019-12-06 05:57:35

This should give you an idea on how to use SendGrid's typescript definitions.

import * as SendGrid from 'sendgrid';

export class SendGridMail extends SendGrid.mail.Mail {}
export class SendGridEmail extends SendGrid.mail.Email {}
export class SendGridContent extends SendGrid.mail.Content {}

export class SendGridService {
  private sendGrid;

  constructor(private sendgridApiKey: string) {
    this.sendGrid = SendGrid(sendgridApiKey);

  }

  send(mail: SendGridMail): Promise<any> {

    let request = this.sendGrid.emptyRequest({
      method: 'POST',
      path: '/v3/mail/send',
      body: mail.toJSON()
    });

    return this.sendGrid.API(request);
  }

}

Here is how I used the class above:

let mail = new SendGridMail(
      new SendGridEmail('from@example.com'),
      'Sending with SendGrid is Fun',
      new SendGridEmail('to@example.com'),
      new SendGridContent('text/plain', 'Email sent to to@example.com'));


return this.sendgridService.send(mail);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!