@opencoredev/email-sdk3.1.015.9 kB
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __hasOwnProp = Object.prototype.hasOwnProperty;
function __accessProp(key) {
return this[key];
}
var __toCommonJS = (from) => {
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
if (entry)
return entry;
entry = __defProp({}, "__esModule", { value: true });
if (from && typeof from === "object" || typeof from === "function") {
for (var key of __getOwnPropNames(from))
if (!__hasOwnProp.call(entry, key))
__defProp(entry, key, {
get: __accessProp.bind(from, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
__moduleCache.set(from, entry);
return entry;
};
var __moduleCache;
var __returnValue = (v) => v;
function __exportSetter(name, newValue) {
this[name] = __returnValue.bind(null, newValue);
}
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: true,
configurable: true,
set: __exportSetter.bind(all, name)
});
};
// ../../../../../../../tmp/email-sdk-cjs-jSjbIi/resend.ts
var exports_resend = {};
__export(exports_resend, {
resend: () => resend
});
module.exports = __toCommonJS(exports_resend);
// src/resend.ts
var import_errors2 = require("./errors.cjs");
// src/internal/decode.ts
var FUNCTION_TAGS = new Set([
"[object Function]",
"[object AsyncFunction]",
"[object GeneratorFunction]",
"[object AsyncGeneratorFunction]"
]);
function tagOf(value) {
return Object.prototype.toString.call(value);
}
function isPrimitive(value) {
return Object(value) !== value;
}
function isStringMember(value) {
return isPrimitive(value) && tagOf(value) === "[object String]";
}
function isJsonObject(value) {
return value !== undefined && value !== null && !Array.isArray(value) && Object(value) === value;
}
function jsonField(value, key) {
return isJsonObject(value) ? value[key] : undefined;
}
function jsonString(value, key) {
const field = jsonField(value, key);
return isJsonString(field) ? field : undefined;
}
function isJsonString(value) {
return isPrimitive(value) && tagOf(value) === "[object String]";
}
function parseJsonStrict(text) {
return JSON.parse(text);
}
function parseJson(text) {
try {
return parseJsonStrict(text);
} catch {
return;
}
}
async function readJson(response) {
const text = await response.text().catch(() => "");
return text ? parseJson(text) : undefined;
}
async function responseJson(response) {
return await response.json();
}
// src/utils.ts
var import_promises = require("node:fs/promises");
var import_errors = require("./errors.cjs");
function arrayify(value) {
if (value === undefined) {
return [];
}
return isList(value) ? [...value] : [value];
}
function isList(value) {
return Array.isArray(value);
}
function formatAddress(address) {
if (isStringMember(address)) {
return address;
}
if (!address.name) {
return address.email;
}
return `${address.name} <${address.email}>`;
}
function formatAddresses(addresses) {
if (addresses === undefined)
return [];
return isList(addresses) ? addresses.map(formatAddress) : [formatAddress(addresses)];
}
function isHeaderList(headers) {
return Array.isArray(headers);
}
function headersToObject(headers) {
if (!headers) {
return;
}
if (!isHeaderList(headers)) {
return { ...headers };
}
return Object.fromEntries(headers.map((header) => [header.name, header.value]));
}
function headersToArray(headers) {
if (!headers) {
return;
}
if (!isHeaderList(headers)) {
return Object.entries(headers).map(([name, value]) => ({
name,
value
}));
}
return [...headers];
}
function assertMessage(message) {
if (!message.from) {
throw new import_errors.EmailValidationError("Email message requires a from address.");
}
if (arrayify(message.to).length === 0) {
throw new import_errors.EmailValidationError("Email message requires at least one recipient.");
}
if (!message.subject) {
throw new import_errors.EmailValidationError("Email message requires a subject.");
}
if (!message.html && !message.text) {
throw new import_errors.EmailValidationError("Email message requires either html or text content.");
}
for (const attachment of message.attachments ?? []) {
const hasContent = attachment.content !== undefined;
const hasPath = isStringMember(attachment.path) && attachment.path.length > 0;
if (hasContent === hasPath) {
throw new import_errors.EmailValidationError(`Attachment "${attachment.filename}" requires exactly one of content or path.`);
}
}
if (message.sendAt !== undefined) {
toSendAtDate(message.sendAt);
}
}
var RFC_3339_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
function toSendAtDate(sendAt) {
if (isStringMember(sendAt) && !RFC_3339_TIMESTAMP.test(sendAt)) {
throw new import_errors.EmailValidationError(`Email message sendAt must be an RFC 3339 timestamp with Z or an explicit offset: "${sendAt}".`, { sendAt });
}
const date = sendAt instanceof Date ? sendAt : new Date(sendAt);
if (Number.isNaN(date.getTime())) {
throw new import_errors.EmailValidationError(`Email message sendAt is not a valid date: "${String(sendAt)}".`, { sendAt });
}
return date;
}
async function readErrorBody(response) {
const contentType = response.headers.get("content-type") ?? "";
if (/application\/(?:[\w.-]+\+)?json/i.test(contentType)) {
return readJson(response).catch(() => {
return;
});
}
return response.text().catch(() => {
return;
});
}
function httpErrorMessage(provider, status, body) {
if (isJsonObject(body)) {
const message = body.message ?? body.Message ?? body.error ?? body.ErrorCode;
if (isJsonString(message)) {
return `${provider} failed with ${status}: ${message}`;
}
const errors = body.errors;
if (Array.isArray(errors)) {
const nestedMessage = errors.map((error) => isJsonObject(error) ? error.message : undefined).find(isJsonString);
if (nestedMessage) {
return `${provider} failed with ${status}: ${nestedMessage}`;
}
}
}
return `${provider} failed with HTTP ${status}.`;
}
function isRetryableStatus(status) {
return status === 408 || status === 409 || status === 425 || status === 429 || status >= 500;
}
function httpErrorDelivery(status) {
if (status === 408 || status === 409 || status === 429)
return "unknown";
return status >= 400 && status < 500 ? "not_sent" : "unknown";
}
async function attachmentContentToString(content, encoding = "raw") {
if (content === undefined) {
return content;
}
if (isStringMember(content)) {
return encoding === "base64" ? content : Buffer.from(content).toString("base64");
}
if (content instanceof Blob) {
const arrayBuffer = await content.arrayBuffer();
return Buffer.from(arrayBuffer).toString("base64");
}
if (content instanceof ArrayBuffer) {
return Buffer.from(new Uint8Array(content)).toString("base64");
}
return Buffer.from(content).toString("base64");
}
function mapAttachments(attachments, map) {
if (!attachments?.length)
return [];
return Promise.all(attachments.map(map));
}
async function attachmentToBase64(attachment) {
if (attachment.path) {
return (await import_promises.readFile(attachment.path)).toString("base64");
}
return attachmentContentToString(attachment.content, attachment.contentEncoding);
}
var SUPPORTED_MESSAGE_FIELDS = {
resend: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
sendAt: true
},
postmark: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true
},
sendgrid: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true,
sendAt: true
},
cloudflare: { cc: true, bcc: true, replyTo: true, headers: true, attachments: true },
unosend: { cc: true, bcc: true, replyTo: true, headers: true, attachments: true, tags: true },
ses: { cc: true, bcc: true, replyTo: true, headers: true, attachments: true, tags: true },
mailgun: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true,
sendAt: true
},
mailersend: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
sendAt: true
},
brevo: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true,
sendAt: true
},
mailchimp: {
cc: true,
bcc: true,
headers: true,
attachments: true,
tags: true,
metadata: true,
sendAt: true
},
sparkpost: {
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true,
sendAt: true
},
iterable: { metadata: true },
loops: { attachments: true, metadata: true },
sequenzy: { replyTo: true, attachments: true, metadata: true },
jetemail: { cc: true, bcc: true, replyTo: true, headers: true, attachments: true },
lettermint: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true
},
lettr: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true,
sendAt: true
},
primitive: { attachments: true },
plunk: { replyTo: true, headers: true, attachments: true, metadata: true },
mailtrap: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true
},
scaleway: { cc: true, bcc: true, replyTo: true, headers: true, attachments: true },
zeptomail: { cc: true, bcc: true, replyTo: true, attachments: true },
mailpace: { cc: true, bcc: true, replyTo: true },
smtp: { cc: true, bcc: true, replyTo: true, headers: true, attachments: true },
graph: { cc: true, bcc: true, replyTo: true, headers: true, attachments: true },
sendheron: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
sendAt: true
},
helo: {
cc: true,
bcc: true,
replyTo: true,
headers: true,
attachments: true,
tags: true,
metadata: true
}
};
var NATIVE_IDEMPOTENCY = new Set(["resend", "jetemail", "lettermint", "primitive", "sendheron", "helo"]);
var REPEATED_HEADERS = new Set(["mailgun", "postmark", "scaleway", "ses", "smtp", "graph"]);
var NATIVE_PERSONALIZED = new Set(["mailgun", "sendgrid"]);
var BUILT_IN_ADAPTER_CAPABILITIES = Object.fromEntries(Object.entries(SUPPORTED_MESSAGE_FIELDS).map(([name, fields]) => [
name,
{
repeatedHeaders: REPEATED_HEADERS.has(name),
idempotency: NATIVE_IDEMPOTENCY.has(name) ? "native" : name === "smtp" ? "message_id" : "none",
scheduling: "sendAt" in fields && fields.sendAt === true,
personalized: NATIVE_PERSONALIZED.has(name) ? "native" : "expanded"
}
]));
function builtInAdapterDefinition(name, ...rules) {
const checks = rules;
return {
capabilities: BUILT_IN_ADAPTER_CAPABILITIES[name],
validate(message) {
validateBuiltInMessage(name, message, checks[0]);
}
};
}
var BUILT_IN_ADAPTER_MARK = Symbol.for("@opencoredev/email-sdk/built-in-adapter");
for (const capabilities of Object.values(BUILT_IN_ADAPTER_CAPABILITIES)) {
Object.defineProperty(capabilities, BUILT_IN_ADAPTER_MARK, { value: true });
}
function validateBuiltInMessage(adapter, message, rules) {
assertMessage(message);
assertSupportedMessageFields(adapter, message, SUPPORTED_MESSAGE_FIELDS[adapter]);
if (!BUILT_IN_ADAPTER_CAPABILITIES[adapter].repeatedHeaders) {
assertUniqueHeaderNames(adapter, message.headers);
}
rules?.(message);
}
var NO_HEADERS = [];
function headerList(headers) {
if (!headers)
return NO_HEADERS;
return isHeaderList(headers) ? headers : headersToArray(headers) ?? NO_HEADERS;
}
function assertUniqueHeaderNames(adapter, headers) {
const list = headerList(headers);
if (list.length < 2)
return;
const names = new Set;
for (const header of list) {
const normalized = header.name.toLowerCase();
if (names.has(normalized)) {
throw new import_errors.EmailValidationError(`${adapter} does not support repeated email header names: ${header.name}.`);
}
names.add(normalized);
}
}
function assertSupportedMessageFields(adapter, message, supported) {
const unsupported = [];
if (hasOptionalRecipients(message.cc) && !supported.cc)
unsupported.push("cc");
if (hasOptionalRecipients(message.bcc) && !supported.bcc)
unsupported.push("bcc");
if (hasOptionalRecipients(message.replyTo) && !supported.replyTo)
unsupported.push("replyTo");
if (hasValues(message.headers) && !supported.headers)
unsupported.push("headers");
if (message.attachments?.length && !supported.attachments)
unsupported.push("attachments");
if (message.tags?.length && !supported.tags)
unsupported.push("tags");
if (hasValues(message.metadata) && !supported.metadata)
unsupported.push("metadata");
if (message.sendAt !== undefined && !supported.sendAt)
unsupported.push("sendAt");
if (unsupported.length > 0) {
throw new import_errors.EmailValidationError(`${adapter} does not support these EmailMessage fields: ${unsupported.join(", ")}.`, { adapter, unsupported });
}
}
function hasValues(value) {
if (!value) {
return false;
}
if (Array.isArray(value)) {
return value.length > 0;
}
return Object.keys(value).length > 0;
}
function hasOptionalRecipients(value) {
return Array.isArray(value) ? value.length > 0 : value !== undefined;
}
// src/payloads.ts
function sendAtDate(message) {
return message.sendAt === undefined ? undefined : toSendAtDate(message.sendAt);
}
function sendAtIso(message) {
return sendAtDate(message)?.toISOString();
}
// src/resend.ts
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) {
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 body2 = await readErrorBody(response);
throw new import_errors2.EmailAdapterError(httpErrorMessage("Resend", response.status, body2), {
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.