wirejs-resources 0.1.176 → 0.1.178

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.
@@ -4,12 +4,18 @@ export type EmailMessage = {
4
4
  subject: string;
5
5
  body: string;
6
6
  html?: string;
7
+ from?: string;
8
+ };
9
+ export type EmailSenderOptions = {
10
+ from: string;
11
+ } | {
12
+ fromDomain: string;
7
13
  };
8
14
  export declare class EmailSender extends Resource {
9
15
  #private;
10
- constructor(scope: Resource | string, id: string, options: {
11
- from: string;
12
- });
13
- get from(): string;
16
+ constructor(scope: Resource | string, id: string, options: EmailSenderOptions);
17
+ get from(): string | undefined;
18
+ get fromDomain(): string | undefined;
19
+ get isDomainSender(): boolean;
14
20
  send(message: EmailMessage): Promise<void>;
15
21
  }
@@ -4,21 +4,55 @@ import path from 'path';
4
4
  import { Resource } from '../resource.js';
5
5
  export class EmailSender extends Resource {
6
6
  #from;
7
+ #fromDomain;
7
8
  constructor(scope, id, options) {
8
9
  super(scope, id);
9
- this.#from = options.from;
10
+ if ('from' in options) {
11
+ this.#from = options.from;
12
+ }
13
+ else if ('fromDomain' in options) {
14
+ this.#fromDomain = options.fromDomain;
15
+ }
16
+ else {
17
+ throw new Error('EmailSender requires either "from" (specific email address) or "fromDomain" options');
18
+ }
10
19
  }
11
20
  get from() {
12
21
  return this.#from;
13
22
  }
23
+ get fromDomain() {
24
+ return this.#fromDomain;
25
+ }
26
+ get isDomainSender() {
27
+ return this.#fromDomain !== undefined;
28
+ }
14
29
  async send(message) {
30
+ // Determine the sender address
31
+ let senderAddress;
32
+ if (this.#from) {
33
+ // Using specific from address
34
+ senderAddress = this.#from;
35
+ }
36
+ else if (this.#fromDomain && message.from) {
37
+ // Using domain with message-specific from address
38
+ if (!message.from.endsWith(`@${this.#fromDomain}`)) {
39
+ throw new Error(`Message from address "${message.from}" must use domain "@${this.#fromDomain}"`);
40
+ }
41
+ senderAddress = message.from;
42
+ }
43
+ else if (this.#fromDomain) {
44
+ throw new Error('Domain-based EmailSender requires message.from to specify the sender address');
45
+ }
46
+ else {
47
+ throw new Error('EmailSender configuration error: no valid sender address available');
48
+ }
15
49
  const outboxDir = path.join(process.cwd(), '.outbox');
16
50
  await fs.promises.mkdir(outboxDir, { recursive: true });
17
51
  const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
18
52
  const suffix = Math.random().toString(36).slice(2);
19
53
  const filename = `${timestamp}-${suffix}.json`;
20
54
  const email = {
21
- from: this.#from,
55
+ from: senderAddress,
22
56
  to: message.to,
23
57
  subject: message.subject,
24
58
  body: message.body,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.176",
3
+ "version": "0.1.178",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",