@travetto/email 7.0.0-rc.1 → 7.0.0-rc.2
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 +9 -9
- 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.2",
|
|
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.2",
|
|
27
|
+
"@travetto/di": "^7.0.0-rc.2",
|
|
28
|
+
"@travetto/runtime": "^7.0.0-rc.2",
|
|
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
|
@@ -35,7 +35,7 @@ export class MailService {
|
|
|
35
35
|
RuntimeResources.read(`${key}.compiled.html`),
|
|
36
36
|
RuntimeResources.read(`${key}.compiled.text`),
|
|
37
37
|
RuntimeResources.read(`${key}.compiled.subject`)
|
|
38
|
-
].map(
|
|
38
|
+
].map(file => file.then(MailUtil.purgeBrand)));
|
|
39
39
|
|
|
40
40
|
this.#compiled.set(key, { html, text, subject });
|
|
41
41
|
}
|
|
@@ -49,12 +49,12 @@ export class MailService {
|
|
|
49
49
|
* @returns
|
|
50
50
|
*/
|
|
51
51
|
async renderMessage(keyOrMessage: string | EmailCompiled | EmailOptions, ctx: Record<string, unknown>): Promise<EmailCompiled> {
|
|
52
|
-
const
|
|
52
|
+
const template = (typeof keyOrMessage === 'string' ? await this.getCompiled(keyOrMessage) : keyOrMessage);
|
|
53
53
|
|
|
54
54
|
const [html, text, subject] = await Promise.all([
|
|
55
|
-
this.#interpolator.render(
|
|
56
|
-
this.#interpolator.render(
|
|
57
|
-
this.#interpolator.render(
|
|
55
|
+
this.#interpolator.render(template.html, ctx),
|
|
56
|
+
this.#interpolator.render(template.text ?? '', ctx),
|
|
57
|
+
this.#interpolator.render(template.subject, ctx)
|
|
58
58
|
]);
|
|
59
59
|
|
|
60
60
|
return { html, text, subject };
|
|
@@ -107,13 +107,13 @@ export class MailService {
|
|
|
107
107
|
* Send multiple messages.
|
|
108
108
|
*/
|
|
109
109
|
async sendAll<S extends SentEmail = SentEmail>(messages: EmailOptions[], base: Partial<EmailOptions> = {}): Promise<S[]> {
|
|
110
|
-
return Promise.all(messages.map(
|
|
110
|
+
return Promise.all(messages.map(message => this.send<S>({
|
|
111
111
|
...base,
|
|
112
|
-
...
|
|
113
|
-
...(
|
|
112
|
+
...message,
|
|
113
|
+
...(message.context || base.context ? {
|
|
114
114
|
context: {
|
|
115
115
|
...(base.context || {}),
|
|
116
|
-
...(
|
|
116
|
+
...(message.context || {})
|
|
117
117
|
}
|
|
118
118
|
} : {})
|
|
119
119
|
})));
|
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
|
}
|