Skip to content

Commit

Permalink
feat(AsyncTemplate): added async template with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Stradivario committed Nov 7, 2020
1 parent 722ed1f commit e1643e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
27 changes: 24 additions & 3 deletions packages/sendgrid/src/send-grid.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject, Service } from '@rxdi/core';

import { Mailer, Templates, TemplatesModel, TemplateTypes } from './tokens';
import { Mailer, Templates, TemplatesModel } from './tokens';

@Service()
export class SendGridHelperService {
Expand All @@ -9,12 +9,12 @@ export class SendGridHelperService {
@Inject(Mailer) private mailer: Mailer,
) {}

getTemplate(type: TemplateTypes) {
getTemplate<T extends string>(type: T) {
return this.templates.filter((t) => t.type === type)[0];
}

async send(from: string, to: string, template: TemplatesModel) {
const html = await (template.asyncHtml || template.html);
const html = await template.html;
return new Promise(async (resolve, reject) =>
resolve(
await this.mailer.send(
Expand All @@ -32,6 +32,27 @@ export class SendGridHelperService {
);
}

sendWithParams<T>(from: string, to: string) {
return async (template: TemplatesModel, params: T) => {
const html = await template.html(params);
return new Promise(async (resolve, reject) =>
resolve(
await this.mailer.send(
{
subject: template.subject,
text: template.text,
html,
from,
to,
},
null,
(e) => (e ? reject(e) : null),
),
),
);
};
}

async raw(
subject: string,
text: string,
Expand Down
18 changes: 16 additions & 2 deletions packages/sendgrid/src/send-grid.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Service } from '@rxdi/core';

import { SendGridHelperService } from './send-grid.helpers';
import { DefaultEmail, TemplateTypes } from './tokens';
import { DefaultEmail } from './tokens';

@Service()
export class SendGridService {
Expand All @@ -10,12 +10,26 @@ export class SendGridService {
@Inject(DefaultEmail) private defaultEmail: string,
) {}

async sendEmail(to: string, type: TemplateTypes, from?: string) {
async sendEmail<T extends string>(to: string, type: T, from?: string) {
const template = this.sendGridHelper.getTemplate(type);
await this.sendGridHelper.send(from || this.defaultEmail, to, template);
return { status: 'ok' };
}

async sendEmailWithParams<T extends string, P>(
to: string,
type: T,
from?: string,
) {
return async (params: P) => {
const template = this.sendGridHelper.getTemplate(type);
await this.sendGridHelper.sendWithParams(from || this.defaultEmail, to)(
template,
params,
);
return { status: 'ok' };
};
}
async sendTemplateString({
from,
html,
Expand Down
7 changes: 3 additions & 4 deletions packages/sendgrid/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ export const DefaultEmail = new InjectionToken<string>(
export type Mailer = typeof MailService;
export type Templates = TemplatesModel[];

export type TemplateTypes = 'subscribe' | 'forgotpassword' | 'profiling';
// export type TemplateTypes = 'subscribe' | 'forgotpassword' | 'profiling';

export class TemplatesModel implements MailData {
type?: TemplateTypes;
type?: string;
subject: string;
text: string;
html?: string;
asyncHtml?: Promise<string>;
html?: string & Promise<string> & (<T>(arg: T) => Promise<string>);
to: string;
from: string;
}

0 comments on commit e1643e8

Please sign in to comment.