@travetto/email 3.1.11 → 3.1.13
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/README.md +1 -1
- package/package.json +4 -4
- package/src/service.ts +11 -12
- package/src/types.ts +22 -0
package/README.md
CHANGED
|
@@ -37,4 +37,4 @@ By design, sending an email requires the sender to specify the html, text option
|
|
|
37
37
|
* `resources/<key>.compiled.html`
|
|
38
38
|
* `resources/<key>.compiled.text`
|
|
39
39
|
* `resources/<key>.compiled.subject`
|
|
40
|
-
With `.html` being the only required field. The [Email
|
|
40
|
+
With `.html` being the only required field. The [Email Compilation Support](https://github.com/travetto/travetto/tree/main/module/email-compiler#readme "Email compiling module") module supports this format, and will generate files accordingly. Also, note that `<key>` can include slashes, allowing for nesting folders.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/email",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.13",
|
|
4
4
|
"description": "Email transmission module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"email",
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"directory": "module/email"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@travetto/base": "^3.1.
|
|
27
|
-
"@travetto/config": "^3.1.
|
|
28
|
-
"@travetto/di": "^3.1.
|
|
26
|
+
"@travetto/base": "^3.1.3",
|
|
27
|
+
"@travetto/config": "^3.1.7",
|
|
28
|
+
"@travetto/di": "^3.1.4",
|
|
29
29
|
"@types/mustache": "^4.2.2",
|
|
30
30
|
"mustache": "^4.2.0"
|
|
31
31
|
},
|
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,26 @@ 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 MessageCompilationStyles = {
|
|
66
|
+
search?: string[];
|
|
67
|
+
global?: string;
|
|
68
|
+
inline?: boolean;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type MessageCompilationImages = {
|
|
72
|
+
search?: string[];
|
|
73
|
+
inline?: boolean;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type MessageCompilationSource = {
|
|
77
|
+
file?: string;
|
|
78
|
+
styles?: MessageCompilationStyles;
|
|
79
|
+
images?: MessageCompilationImages;
|
|
80
|
+
html: () => Promise<string> | string;
|
|
81
|
+
text: () => Promise<string> | string;
|
|
82
|
+
subject: () => Promise<string> | string;
|
|
61
83
|
};
|