@avelonjs/resend 0.8.4 → 0.8.6
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 +1 -1
- package/package.json +1 -1
- package/src/driver.ts +24 -2
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
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
|
|
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'] } },
|