@travetto/email 7.0.0-rc.1 → 7.0.0-rc.3
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 +4 -4
- package/src/resource.ts +2 -2
- package/src/service.ts +18 -12
- package/src/template.ts +2 -2
- package/src/types.ts +1 -1
- package/src/util.ts +13 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/email",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.3",
|
|
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/config": "^7.0.0-rc.
|
|
27
|
-
"@travetto/di": "^7.0.0-rc.
|
|
28
|
-
"@travetto/runtime": "^7.0.0-rc.
|
|
26
|
+
"@travetto/config": "^7.0.0-rc.3",
|
|
27
|
+
"@travetto/di": "^7.0.0-rc.3",
|
|
28
|
+
"@travetto/runtime": "^7.0.0-rc.3",
|
|
29
29
|
"@types/mustache": "^4.2.6",
|
|
30
30
|
"mustache": "^4.2.0"
|
|
31
31
|
},
|
package/src/resource.ts
CHANGED
|
@@ -10,9 +10,9 @@ export class EmailResourceLoader extends FileLoader {
|
|
|
10
10
|
super([
|
|
11
11
|
...Env.TRV_RESOURCES.list ?? [],
|
|
12
12
|
`${module}#resources`,
|
|
13
|
-
...RuntimeIndex.getDependentModules(mod, 'children').map(
|
|
13
|
+
...RuntimeIndex.getDependentModules(mod, 'children').map(indexedMod => `${indexedMod.name}#resources`),
|
|
14
14
|
'@@#resources',
|
|
15
15
|
...globalResources ?? []
|
|
16
|
-
].map(
|
|
16
|
+
].map(name => Runtime.modulePath(name)));
|
|
17
17
|
}
|
|
18
18
|
}
|
package/src/service.ts
CHANGED
|
@@ -17,6 +17,7 @@ export class MailService {
|
|
|
17
17
|
#compiled = new Map<string, EmailCompiled>();
|
|
18
18
|
#transport: MailTransport;
|
|
19
19
|
#interpolator: MailInterpolator;
|
|
20
|
+
#cacheResults = true;
|
|
20
21
|
|
|
21
22
|
constructor(
|
|
22
23
|
transport: MailTransport,
|
|
@@ -26,18 +27,23 @@ export class MailService {
|
|
|
26
27
|
this.#transport = transport;
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
setCacheState(active: boolean): void {
|
|
31
|
+
this.#cacheResults = active;
|
|
32
|
+
}
|
|
33
|
+
|
|
29
34
|
/**
|
|
30
35
|
* Get compiled content by key
|
|
31
36
|
*/
|
|
32
37
|
async getCompiled(key: string): Promise<EmailCompiled> {
|
|
33
|
-
if (
|
|
38
|
+
if (!this.#compiled.has(key)) {
|
|
34
39
|
const [html, text, subject] = await Promise.all([
|
|
35
40
|
RuntimeResources.read(`${key}.compiled.html`),
|
|
36
41
|
RuntimeResources.read(`${key}.compiled.text`),
|
|
37
42
|
RuntimeResources.read(`${key}.compiled.subject`)
|
|
38
|
-
].map(
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
].map(file => file.then(MailUtil.purgeBrand)));
|
|
44
|
+
if (this.#cacheResults) {
|
|
45
|
+
this.#compiled.set(key, { html, text, subject });
|
|
46
|
+
}
|
|
41
47
|
}
|
|
42
48
|
return this.#compiled.get(key)!;
|
|
43
49
|
}
|
|
@@ -49,12 +55,12 @@ export class MailService {
|
|
|
49
55
|
* @returns
|
|
50
56
|
*/
|
|
51
57
|
async renderMessage(keyOrMessage: string | EmailCompiled | EmailOptions, ctx: Record<string, unknown>): Promise<EmailCompiled> {
|
|
52
|
-
const
|
|
58
|
+
const template = (typeof keyOrMessage === 'string' ? await this.getCompiled(keyOrMessage) : keyOrMessage);
|
|
53
59
|
|
|
54
60
|
const [html, text, subject] = await Promise.all([
|
|
55
|
-
this.#interpolator.render(
|
|
56
|
-
this.#interpolator.render(
|
|
57
|
-
this.#interpolator.render(
|
|
61
|
+
this.#interpolator.render(template.html, ctx),
|
|
62
|
+
this.#interpolator.render(template.text ?? '', ctx),
|
|
63
|
+
this.#interpolator.render(template.subject, ctx)
|
|
58
64
|
]);
|
|
59
65
|
|
|
60
66
|
return { html, text, subject };
|
|
@@ -107,13 +113,13 @@ export class MailService {
|
|
|
107
113
|
* Send multiple messages.
|
|
108
114
|
*/
|
|
109
115
|
async sendAll<S extends SentEmail = SentEmail>(messages: EmailOptions[], base: Partial<EmailOptions> = {}): Promise<S[]> {
|
|
110
|
-
return Promise.all(messages.map(
|
|
116
|
+
return Promise.all(messages.map(message => this.send<S>({
|
|
111
117
|
...base,
|
|
112
|
-
...
|
|
113
|
-
...(
|
|
118
|
+
...message,
|
|
119
|
+
...(message.context || base.context ? {
|
|
114
120
|
context: {
|
|
115
121
|
...(base.context || {}),
|
|
116
|
-
...(
|
|
122
|
+
...(message.context || {})
|
|
117
123
|
}
|
|
118
124
|
} : {})
|
|
119
125
|
})));
|
package/src/template.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -84,4 +84,4 @@ type EmailTemplateContent = Record<EmailContentType, () => (Promise<string> | st
|
|
|
84
84
|
|
|
85
85
|
export type EmailTemplateLocation = { file: string, module: string };
|
|
86
86
|
export type EmailTemplateModule = EmailTemplateResource & EmailTemplateContent;
|
|
87
|
-
export type EmailTemplateImport = { prepare(
|
|
87
|
+
export type EmailTemplateImport = { prepare(location: EmailTemplateLocation): Promise<EmailTemplateModule> };
|
package/src/util.ts
CHANGED
|
@@ -35,20 +35,20 @@ export class MailUtil {
|
|
|
35
35
|
html = html.replace(/data:(image\/[^;]{1,50});base64,([^"']{1,10000000})/g, (__, contentType, content) => {
|
|
36
36
|
// Ensure same data uris map to a single cid
|
|
37
37
|
if (!contentMap.has(content)) {
|
|
38
|
-
const
|
|
38
|
+
const contentId = `image-${idx += 1}`;
|
|
39
39
|
const ext = contentType.split('/')[1];
|
|
40
40
|
attachments.push({
|
|
41
|
-
cid,
|
|
42
|
-
filename: `${
|
|
41
|
+
cid: contentId,
|
|
42
|
+
filename: `${contentId}.${ext}`,
|
|
43
43
|
headers: {
|
|
44
|
-
'X-Attachment-Id': `${
|
|
44
|
+
'X-Attachment-Id': `${contentId}`
|
|
45
45
|
},
|
|
46
46
|
content: Buffer.from(content, 'base64'),
|
|
47
47
|
contentDisposition: 'inline',
|
|
48
48
|
contentType
|
|
49
49
|
});
|
|
50
|
-
contentMap.set(content,
|
|
51
|
-
return `cid:${
|
|
50
|
+
contentMap.set(content, contentId);
|
|
51
|
+
return `cid:${contentId}`;
|
|
52
52
|
} else {
|
|
53
53
|
return contentMap.get(content)!;
|
|
54
54
|
}
|
|
@@ -62,14 +62,14 @@ export class MailUtil {
|
|
|
62
62
|
/**
|
|
63
63
|
* Get the primary email, if set from an email identity or identity list
|
|
64
64
|
*/
|
|
65
|
-
static getPrimaryEmail(
|
|
66
|
-
if (!
|
|
65
|
+
static getPrimaryEmail(identity?: EmailIdentity | EmailIdentityList): string | undefined {
|
|
66
|
+
if (!identity) {
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
|
-
if (Array.isArray(
|
|
70
|
-
|
|
69
|
+
if (Array.isArray(identity)) {
|
|
70
|
+
identity = identity[0];
|
|
71
71
|
}
|
|
72
|
-
return (typeof
|
|
72
|
+
return (typeof identity === 'string') ? identity : identity.address;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
@@ -78,7 +78,7 @@ export class MailUtil {
|
|
|
78
78
|
static buildUniqueMessageId(message: EmailOptions): string {
|
|
79
79
|
const from = this.getPrimaryEmail(message.from)!;
|
|
80
80
|
const to = this.getPrimaryEmail(message.to)!;
|
|
81
|
-
const
|
|
82
|
-
return `<${
|
|
81
|
+
const uniqueId = BinaryUtil.hash(`${to}${from}${message.subject}${Date.now()}`, 12);
|
|
82
|
+
return `<${uniqueId}@${from.split('@')[1]}>`;
|
|
83
83
|
}
|
|
84
84
|
}
|