swaf/src/mail/Mail.ts

40 lines
993 B
TypeScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
import {Options} from "nodemailer/lib/mailer";
import {ParsedUrlQueryInput} from "querystring";
import MailError from "./MailError";
import MailTemplate from "./MailTemplate";
2020-04-22 15:52:17 +02:00
export default class Mail {
private readonly options: Options = {};
public constructor(
private readonly template: MailTemplate,
private readonly data: ParsedUrlQueryInput = {},
) {
2020-04-22 15:52:17 +02:00
this.options.subject = this.template.getSubject(data);
this.verifyData();
}
private verifyData() {
for (const forbiddenField of [
'to',
]) {
if (this.data[forbiddenField] !== undefined) {
throw new MailError(`Can't use reserved data.${forbiddenField}.`);
}
}
}
public getTemplate(): MailTemplate {
return this.template;
2020-04-22 15:52:17 +02:00
}
public getData(): ParsedUrlQueryInput {
return {...this.data};
2020-04-22 15:52:17 +02:00
}
public getOptions(): Options {
return {...this.options};
2020-04-22 15:52:17 +02:00
}
}