@travetto/email 3.1.11 → 3.1.12

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/email",
3
- "version": "3.1.11",
3
+ "version": "3.1.12",
4
4
  "description": "Email transmission module.",
5
5
  "keywords": [
6
6
  "email",
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 Compiled = { html: string, text?: string, subject: string };
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, Compiled>();
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<Compiled> {
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`).catch(() => ''),
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 | Compiled, ctx: Record<string, unknown>): Promise<Compiled> {
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
- tpl.html ? this.#tplEngine!.template(tpl.html, ctx) : undefined,
61
- tpl.text ? this.#tplEngine!.template(tpl.text, ctx) : undefined,
62
- tpl.subject ? this.#tplEngine!.template(tpl.subject, ctx) : undefined
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: html!, text, subject: subject! };
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 Compiled, context);
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,19 @@ 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 MessageCompilationSource = {
66
+ file?: string;
67
+ styles?: {
68
+ search: string[];
69
+ global?: string;
70
+ };
71
+ inlineImages?: boolean;
72
+ inlineStyles?: boolean;
73
+ html: () => Promise<string> | string;
74
+ text: () => Promise<string> | string;
75
+ subject: () => Promise<string> | string;
61
76
  };