@opencoredev/email-sdk3.1.02.9 kB
import { EmailAdapterError } from "./errors.js";
import { jsonString, responseJson } from "./internal/decode.js";
import { sendAtIso } from "./payloads.js";
import { builtInAdapterDefinition, attachmentToBase64, formatAddress, formatAddresses, headersToObject, httpErrorMessage, httpErrorDelivery, isRetryableStatus, readErrorBody, assertSupportedMessageFields, SUPPORTED_MESSAGE_FIELDS, mapAttachments, } from "./utils.js";
export function resend(options) {
const baseUrl = options.baseUrl ?? "https://api.resend.com";
const fetcher = options.fetch ?? fetch;
return {
name: "resend",
...builtInAdapterDefinition("resend"),
raw: { baseUrl },
async send(message, context) {
// Read per send so a rotated apiKey or edited headers apply to the next request.
const headers = new Headers({
Authorization: `Bearer ${options.apiKey}`,
"Content-Type": "application/json",
...options.headers,
});
if (context.idempotencyKey) {
headers.set("Idempotency-Key", context.idempotencyKey);
}
const response = await fetcher(`${baseUrl}/emails`, {
method: "POST",
signal: context.signal,
headers,
body: JSON.stringify(await toResendPayload(message)),
});
if (!response.ok) {
const body = await readErrorBody(response);
throw new EmailAdapterError(httpErrorMessage("Resend", response.status, body), {
adapter: "resend",
status: response.status,
retryable: isRetryableStatus(response.status),
delivery: httpErrorDelivery(response.status),
});
}
const body = await responseJson(response);
return {
adapter: "resend",
id: jsonString(body, "id"),
raw: body,
};
},
};
}
async function toResendPayload(message) {
assertSupportedMessageFields("resend", message, SUPPORTED_MESSAGE_FIELDS.resend);
return {
from: formatAddress(message.from),
to: formatAddresses(message.to),
subject: message.subject,
html: message.html,
text: message.text,
cc: formatAddresses(message.cc),
bcc: formatAddresses(message.bcc),
reply_to: formatAddresses(message.replyTo),
headers: headersToObject(message.headers),
attachments: await mapAttachments(message.attachments, toResendAttachment),
tags: message.tags,
scheduled_at: sendAtIso(message),
};
}
async function toResendAttachment(attachment) {
return {
filename: attachment.filename,
content: await attachmentToBase64(attachment),
content_type: attachment.contentType,
content_id: attachment.contentId,
};
}
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.