@travetto/email 3.1.6 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/service.ts +26 -41
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/email",
3
- "version": "3.1.6",
3
+ "version": "3.1.8",
4
4
  "description": "Email transmission module.",
5
5
  "keywords": [
6
6
  "email",
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
  */
@@ -79,13 +48,13 @@ export class MailService {
79
48
 
80
49
  /**
81
50
  * Build message from key/context
82
- * @param key
83
- * @param ctx
84
- * @returns
51
+ * @param key
52
+ * @param ctx
53
+ * @returns
85
54
  */
86
- async buildMessage(key: string | MessageOptions, ctx?: Record<string, unknown>): Promise<MessageOptions> {
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
- ctx ??= (typeof key === 'string' ? {} : key.context ?? {});
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,
@@ -96,7 +65,7 @@ export class MailService {
96
65
  html: rawHtml ?? '',
97
66
  text,
98
67
  subject
99
- }
68
+ };
100
69
 
101
70
  if (msg.html) {
102
71
  const { html, attachments } = await MailUtil.extractImageAttachments(msg.html);
@@ -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
- let msg = await this.buildMessage(keyOrMessage);
117
- msg = this.#forceContentToAlternative(msg);
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
  }