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 | 2x 2x 2x 2x 2x 7x 7x 7x 2x 2x 1x 1x 4x 4x | import { Inject, Injectable } from '@nestjs/common';
import {
send,
sendMultiple,
setApiKey,
setSubstitutionWrappers,
} from '@sendgrid/mail';
import { SENDGRID_MODULE_OPTIONS } from '../common/sendgrid.constants';
import { SendGridModuleOptions } from '../interfaces/sendgrid-options.interface';
import { MailDataRequired } from '@sendgrid/helpers/classes/mail';
import { ResponseError } from '@sendgrid/helpers/classes';
import { ClientResponse } from '@sendgrid/client/src/response';
import * as deepmerge from 'deepmerge';
@Injectable()
export class SendGridService {
constructor(
@Inject(SENDGRID_MODULE_OPTIONS)
private readonly options: SendGridModuleOptions,
) {
Iif (!(options && options.apiKey)) {
// console.log('options not found. Did you use SendGridModule.forRoot?');
return;
}
setApiKey(options.apiKey);
// console.log('api key set');
}
public async send(
data: Partial<MailDataRequired> | Partial<MailDataRequired>[],
isMultiple?: boolean,
cb?: (err: Error | ResponseError, result: [ClientResponse, {}]) => void,
): Promise<[ClientResponse, {}]> {
if (Array.isArray(data)) {
return send(data.map((d) => this.mergeWithDefaultMailData(d)) as MailDataRequired[], isMultiple, cb);
} else {
return send(this.mergeWithDefaultMailData(data), isMultiple, cb);
}
}
public async sendMultiple(
data: Partial<MailDataRequired>,
cb?: (error: Error | ResponseError, result: [ClientResponse, {}]) => void,
): Promise<[ClientResponse, {}]> {
return sendMultiple(this.mergeWithDefaultMailData(data) as MailDataRequired, cb);
}
private mergeWithDefaultMailData(data: Partial<MailDataRequired>): MailDataRequired {
Eif (!this.options.defaultMailData) {
return data as MailDataRequired;
}
return deepmerge(this.options.defaultMailData, data);
}
}
|