@travetto/email 3.1.11 → 3.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/service.ts +11 -12
- package/src/types.ts +15 -0
package/package.json
CHANGED
package/src/service.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { GlobalEnv } from '@travetto/base';
|
|
2
2
|
import { Injectable } from '@travetto/di';
|
|
3
3
|
|
|
4
|
-
import { MessageOptions, SentMessage } from './types';
|
|
4
|
+
import { MessageCompiled, MessageOptions, SentMessage } from './types';
|
|
5
5
|
import { MailTransport } from './transport';
|
|
6
6
|
import { MailTemplateEngine } from './template';
|
|
7
7
|
import { MailUtil } from './util';
|
|
8
8
|
import { EmailResource } from './resource';
|
|
9
9
|
|
|
10
|
-
type
|
|
11
|
-
type MessageWithoutBody = Omit<MessageOptions, keyof Compiled>;
|
|
10
|
+
type MessageWithoutBody = Omit<MessageOptions, keyof MessageCompiled>;
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* Email service for sending and templating emails
|
|
@@ -16,7 +15,7 @@ type MessageWithoutBody = Omit<MessageOptions, keyof Compiled>;
|
|
|
16
15
|
@Injectable()
|
|
17
16
|
export class MailService {
|
|
18
17
|
|
|
19
|
-
#compiled = new Map<string,
|
|
18
|
+
#compiled = new Map<string, MessageCompiled>();
|
|
20
19
|
#transport: MailTransport;
|
|
21
20
|
#tplEngine: MailTemplateEngine;
|
|
22
21
|
#resources: EmailResource;
|
|
@@ -34,11 +33,11 @@ export class MailService {
|
|
|
34
33
|
/**
|
|
35
34
|
* Get compiled content by key
|
|
36
35
|
*/
|
|
37
|
-
async getCompiled(key: string): Promise<
|
|
36
|
+
async getCompiled(key: string): Promise<MessageCompiled> {
|
|
38
37
|
if (GlobalEnv.dynamic || !this.#compiled.has(key)) {
|
|
39
38
|
const [html, text, subject] = await Promise.all([
|
|
40
39
|
this.#resources.read(`${key}.compiled.html`),
|
|
41
|
-
this.#resources.read(`${key}.compiled.text`)
|
|
40
|
+
this.#resources.read(`${key}.compiled.text`),
|
|
42
41
|
this.#resources.read(`${key}.compiled.subject`)
|
|
43
42
|
]);
|
|
44
43
|
|
|
@@ -53,16 +52,16 @@ export class MailService {
|
|
|
53
52
|
* @param ctx
|
|
54
53
|
* @returns
|
|
55
54
|
*/
|
|
56
|
-
async templateMessage(keyOrMessage: string |
|
|
55
|
+
async templateMessage(keyOrMessage: string | MessageCompiled, ctx: Record<string, unknown>): Promise<MessageCompiled> {
|
|
57
56
|
const tpl = (typeof keyOrMessage === 'string' ? await this.getCompiled(keyOrMessage) : keyOrMessage);
|
|
58
57
|
|
|
59
58
|
const [html, text, subject] = await Promise.all([
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
this.#tplEngine!.template(tpl.html, ctx),
|
|
60
|
+
this.#tplEngine!.template(tpl.text, ctx),
|
|
61
|
+
this.#tplEngine!.template(tpl.subject, ctx)
|
|
63
62
|
]);
|
|
64
63
|
|
|
65
|
-
return { html
|
|
64
|
+
return { html, text, subject };
|
|
66
65
|
}
|
|
67
66
|
|
|
68
67
|
/**
|
|
@@ -84,7 +83,7 @@ export class MailService {
|
|
|
84
83
|
const keyOrMessage = key ?? ('html' in message ? message : '') ?? '';
|
|
85
84
|
const context = ctx ?? (('context' in message) ? message.context : {}) ?? {};
|
|
86
85
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
87
|
-
const compiled = await this.templateMessage(keyOrMessage as
|
|
86
|
+
const compiled = await this.templateMessage(keyOrMessage as MessageCompiled, context);
|
|
88
87
|
|
|
89
88
|
const final = { ...base, ...message, ...compiled, context };
|
|
90
89
|
|
package/src/types.ts
CHANGED
|
@@ -58,4 +58,19 @@ export interface MessageOptions {
|
|
|
58
58
|
|
|
59
59
|
export type SentMessage = {
|
|
60
60
|
messageId?: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type MessageCompiled = { html: string, text: string, subject: string };
|
|
64
|
+
|
|
65
|
+
export type MessageCompilationSource = {
|
|
66
|
+
file?: string;
|
|
67
|
+
styles?: {
|
|
68
|
+
search: string[];
|
|
69
|
+
global?: string;
|
|
70
|
+
};
|
|
71
|
+
inlineImages?: boolean;
|
|
72
|
+
inlineStyles?: boolean;
|
|
73
|
+
html: () => Promise<string> | string;
|
|
74
|
+
text: () => Promise<string> | string;
|
|
75
|
+
subject: () => Promise<string> | string;
|
|
61
76
|
};
|