@avelonjs/resend 0.8.5 → 0.8.7

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/README.md CHANGED
@@ -50,7 +50,7 @@ await mail.sendTemplate('welcome-message', ['reader@example.test'], { actorName:
50
50
  | `createResendMail` | `(options?: ResendMailOptions) => ResendMail` | Constructs a Resend HTTP mail driver. |
51
51
  | `resendMailCapabilities` | `{ templates: true }` | Exact capability declaration for the Resend driver. |
52
52
  | `ResendMailOptions` | `interface` | API key, endpoint, instance, and default sender. |
53
- | `ResendMail.send` | `(message: MailMessage) => Promise<MailReceipt>` | Sends one transactional message. |
53
+ | `ResendMail.send` | `(message: MailMessage) => Promise<MailReceipt>` | Sends one transactional message. An empty recipient list is invalid. |
54
54
  | `ResendMail.sendTemplate` | `(template: string, to: string \| readonly string[], variables: Readonly<Record<string, unknown>>) => Promise<MailReceipt>` | Sends a hosted template. |
55
55
  | `ResendMail.raw` | `() => { endpoint: string }` | Returns the API root at the vendor boundary. |
56
56
  | `ResendMail.sent` | `readonly MailMessage[]` | Copies of messages this driver accepted. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avelonjs/resend",
3
- "version": "0.8.5",
3
+ "version": "0.8.7",
4
4
  "private": false,
5
5
  "description": "Resend HTTP mail driver for Avelon.",
6
6
  "license": "MIT",
@@ -37,10 +37,10 @@
37
37
  "typecheck": "tsc --noEmit"
38
38
  },
39
39
  "dependencies": {
40
- "@avelonjs/core": "workspace:*"
40
+ "@avelonjs/core": "0.8.7"
41
41
  },
42
42
  "devDependencies": {
43
- "@avelonjs/conformance": "workspace:*",
43
+ "@avelonjs/conformance": "0.8.7",
44
44
  "@types/bun": "1.3.14",
45
45
  "typescript": "5.9.3"
46
46
  },
package/src/driver.ts CHANGED
@@ -32,6 +32,13 @@ function validAddress(address: string): boolean {
32
32
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(address)
33
33
  }
34
34
 
35
+ function requireRecipients(field: string, addresses: readonly string[]): void {
36
+ if (addresses.length > 0) return
37
+ throw new Invalid(`A message needs at least one ${field} address.`, {
38
+ metadata: { fields: { [field]: ['Add a recipient.'] } },
39
+ })
40
+ }
41
+
35
42
  function validateAddresses(
36
43
  message: Pick<MailMessage, 'to' | 'from' | 'replyTo' | 'cc' | 'bcc'>,
37
44
  ): void {
@@ -118,6 +125,7 @@ export class ResendMail
118
125
 
119
126
  async send(message: MailMessage): Promise<MailReceipt> {
120
127
  const normalized = copyMessage(message, this.#defaultFrom)
128
+ requireRecipients('to', addressList(normalized.to))
121
129
  validateAddresses(normalized)
122
130
  const receipt = await this.#post('/emails', {
123
131
  from: normalized.from,
@@ -150,8 +158,9 @@ export class ResendMail
150
158
  to: string | readonly string[],
151
159
  variables: Readonly<Record<string, unknown>>,
152
160
  ): Promise<MailReceipt> {
153
- validateAddresses({ to, from: this.#defaultFrom })
154
161
  const recipients = addressList(to)
162
+ requireRecipients('to', recipients)
163
+ validateAddresses({ to, from: this.#defaultFrom })
155
164
  const receipt = await this.#post('/emails', {
156
165
  from: this.#defaultFrom,
157
166
  to: recipients,
@@ -176,7 +185,20 @@ export class ResendMail
176
185
  },
177
186
  body: JSON.stringify(body),
178
187
  })
179
- const parsed = (await response.json()) as { id?: string; message?: string }
188
+ const text = await response.text()
189
+ let parsed: { id?: string; message?: string } = {}
190
+ if (text.trim().length > 0) {
191
+ try {
192
+ const value: unknown = JSON.parse(text)
193
+ if (typeof value === 'object' && value !== null) {
194
+ parsed = value as { id?: string; message?: string }
195
+ }
196
+ } catch {
197
+ throw new Invalid('Resend returned a non-JSON response.', {
198
+ metadata: { fields: { mail: [`HTTP ${response.status}`] } },
199
+ })
200
+ }
201
+ }
180
202
  if (!response.ok || typeof parsed.id !== 'string') {
181
203
  throw new Invalid(parsed.message ?? 'Resend rejected the message.', {
182
204
  metadata: { fields: { mail: [parsed.message ?? 'send failed'] } },