@travetto/email 3.1.7 → 3.1.8
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 +22 -37
package/package.json
CHANGED
package/src/service.ts
CHANGED
|
@@ -30,37 +30,6 @@ export class MailService {
|
|
|
30
30
|
this.#resources = resources;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
/**
|
|
34
|
-
* Force content into alternative slots, satisfies node mailer
|
|
35
|
-
*/
|
|
36
|
-
#forceContentToAlternative(msg: MessageOptions): MessageOptions {
|
|
37
|
-
for (const [key, mime] of [['text', 'text/plain'], ['html', 'text/html']] as const) {
|
|
38
|
-
if (msg[key]) {
|
|
39
|
-
(msg.alternatives ??= []).push({
|
|
40
|
-
content: msg[key], contentDisposition: 'inline', contentTransferEncoding: '7bit', contentType: `${mime}; charset=utf-8`
|
|
41
|
-
});
|
|
42
|
-
delete msg[key];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return msg;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Send multiple messages.
|
|
50
|
-
*/
|
|
51
|
-
async sendAll<S extends SentMessage = SentMessage>(messages: MessageOptions[], base: Partial<MessageOptions> = {}): Promise<S[]> {
|
|
52
|
-
return Promise.all(messages.map(msg => this.send<S>({
|
|
53
|
-
...base,
|
|
54
|
-
...msg,
|
|
55
|
-
...(msg.context || base.context ? {
|
|
56
|
-
context: {
|
|
57
|
-
...(base.context || {}),
|
|
58
|
-
...(msg.context || {})
|
|
59
|
-
}
|
|
60
|
-
} : {})
|
|
61
|
-
})));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
33
|
/**
|
|
65
34
|
* Get compiled content by key
|
|
66
35
|
*/
|
|
@@ -83,9 +52,9 @@ export class MailService {
|
|
|
83
52
|
* @param ctx
|
|
84
53
|
* @returns
|
|
85
54
|
*/
|
|
86
|
-
async buildMessage(key: string | MessageOptions, ctx
|
|
55
|
+
async buildMessage(key: string | MessageOptions, ctx: Record<string, unknown>): Promise<MessageOptions> {
|
|
87
56
|
const tpl = (typeof key === 'string' ? await this.getCompiled(key) : key);
|
|
88
|
-
|
|
57
|
+
|
|
89
58
|
const [rawHtml, text, subject] = await Promise.all([
|
|
90
59
|
tpl.html ? this.#tplEngine!.template(tpl.html, ctx) : undefined,
|
|
91
60
|
tpl.text ? this.#tplEngine!.template(tpl.text, ctx) : undefined,
|
|
@@ -110,11 +79,27 @@ export class MailService {
|
|
|
110
79
|
/**
|
|
111
80
|
* Send a single message
|
|
112
81
|
*/
|
|
113
|
-
async send<S extends SentMessage = SentMessage>(key: string, base?: MessageWithoutBody): Promise<S>;
|
|
82
|
+
async send<S extends SentMessage = SentMessage>(key: string, ctx?: Record<string, unknown>, base?: MessageWithoutBody): Promise<S>;
|
|
114
83
|
async send<S extends SentMessage = SentMessage>(message: MessageOptions): Promise<S>;
|
|
115
|
-
async send<S extends SentMessage = SentMessage>(keyOrMessage: MessageOptions | string, base?: MessageWithoutBody): Promise<S> {
|
|
116
|
-
|
|
117
|
-
msg = this
|
|
84
|
+
async send<S extends SentMessage = SentMessage>(keyOrMessage: MessageOptions | string, ctx?: Record<string, unknown>, base?: MessageWithoutBody): Promise<S> {
|
|
85
|
+
ctx ??= (typeof keyOrMessage === 'string' ? {} : keyOrMessage.context) ?? {};
|
|
86
|
+
const msg = await this.buildMessage(keyOrMessage, ctx);
|
|
118
87
|
return this.#transport.send<S>({ ...base, ...msg });
|
|
119
88
|
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Send multiple messages.
|
|
92
|
+
*/
|
|
93
|
+
async sendAll<S extends SentMessage = SentMessage>(messages: MessageOptions[], base: Partial<MessageOptions> = {}): Promise<S[]> {
|
|
94
|
+
return Promise.all(messages.map(msg => this.send<S>({
|
|
95
|
+
...base,
|
|
96
|
+
...msg,
|
|
97
|
+
...(msg.context || base.context ? {
|
|
98
|
+
context: {
|
|
99
|
+
...(base.context || {}),
|
|
100
|
+
...(msg.context || {})
|
|
101
|
+
}
|
|
102
|
+
} : {})
|
|
103
|
+
})));
|
|
104
|
+
}
|
|
120
105
|
}
|