@steve31415/baselib 3.59.0 → 3.59.1
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/dist/email.js +25 -20
- package/package.json +1 -1
package/dist/email.js
CHANGED
|
@@ -28,6 +28,27 @@
|
|
|
28
28
|
import { serializeError, truncate } from './log-core.js';
|
|
29
29
|
export const RESEND_API_URL = 'https://api.resend.com/emails';
|
|
30
30
|
export const DEFAULT_EMAIL_TIMEOUT_MS = 30_000;
|
|
31
|
+
/** The JSON body of Resend's `POST /emails`: optional fields are omitted
|
|
32
|
+
* rather than sent empty. */
|
|
33
|
+
function resendRequestBody(msg) {
|
|
34
|
+
return {
|
|
35
|
+
from: msg.from,
|
|
36
|
+
to: [msg.to],
|
|
37
|
+
subject: msg.subject,
|
|
38
|
+
...(msg.text !== undefined ? { text: msg.text } : {}),
|
|
39
|
+
...(msg.html !== undefined ? { html: msg.html } : {}),
|
|
40
|
+
...(msg.headers && Object.keys(msg.headers).length ? { headers: msg.headers } : {}),
|
|
41
|
+
...(msg.attachments?.length
|
|
42
|
+
? {
|
|
43
|
+
attachments: msg.attachments.map((attachment) => ({
|
|
44
|
+
filename: attachment.filename,
|
|
45
|
+
content: attachment.content,
|
|
46
|
+
...(attachment.contentType ? { content_type: attachment.contentType } : {}),
|
|
47
|
+
})),
|
|
48
|
+
}
|
|
49
|
+
: {}),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
31
52
|
/**
|
|
32
53
|
* Send one email through Resend. Never throws: every failure is an ERROR
|
|
33
54
|
* log (with enough of the message to redo the send by hand) and an
|
|
@@ -69,30 +90,14 @@ export async function sendEmail(msg, deps) {
|
|
|
69
90
|
...(deps.idempotencyKey !== undefined ? { 'Idempotency-Key': deps.idempotencyKey } : {}),
|
|
70
91
|
},
|
|
71
92
|
signal: AbortSignal.timeout(timeoutMs),
|
|
72
|
-
body: JSON.stringify(
|
|
73
|
-
from: msg.from,
|
|
74
|
-
to: [msg.to],
|
|
75
|
-
subject: msg.subject,
|
|
76
|
-
...(msg.text !== undefined ? { text: msg.text } : {}),
|
|
77
|
-
...(msg.html !== undefined ? { html: msg.html } : {}),
|
|
78
|
-
...(msg.headers && Object.keys(msg.headers).length ? { headers: msg.headers } : {}),
|
|
79
|
-
...(msg.attachments?.length
|
|
80
|
-
? {
|
|
81
|
-
attachments: msg.attachments.map((attachment) => ({
|
|
82
|
-
filename: attachment.filename,
|
|
83
|
-
content: attachment.content,
|
|
84
|
-
...(attachment.contentType ? { content_type: attachment.contentType } : {}),
|
|
85
|
-
})),
|
|
86
|
-
}
|
|
87
|
-
: {}),
|
|
88
|
-
}),
|
|
93
|
+
body: JSON.stringify(resendRequestBody(msg)),
|
|
89
94
|
});
|
|
90
95
|
const elapsedMs = Date.now() - start;
|
|
91
96
|
const body = await res.text().catch(() => '');
|
|
92
97
|
if (!res.ok) {
|
|
93
|
-
const
|
|
94
|
-
logger.error('email_send_failed', { ...base, status: res.status, body:
|
|
95
|
-
return { ok: false, error };
|
|
98
|
+
const bodyPreview = truncate(body, 500);
|
|
99
|
+
logger.error('email_send_failed', { ...base, status: res.status, body: bodyPreview, elapsedMs, ...preview });
|
|
100
|
+
return { ok: false, error: `Resend API ${res.status}: ${bodyPreview}` };
|
|
96
101
|
}
|
|
97
102
|
let emailId;
|
|
98
103
|
try {
|
package/package.json
CHANGED