@forge/api

8.2.1-next.18.2.1-next.1-experimental-e03cc85
out/sendEmail.js
+out/sendEmail.jsNew file
+91
Index: package/out/sendEmail.js
===================================================================
--- package/out/sendEmail.js
+++ package/out/sendEmail.js
@@ -0,0 +1,91 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.SendEmailError = void 0;
+exports.sendEmail = sendEmail;
+const node_stream_1 = require("node:stream");
+const errors_1 = require("./api/errors");
+Object.defineProperty(exports, "SendEmailError", { enumerable: true, get: function () { return errors_1.SendEmailError; } });
+const fetch_1 = require("./api/fetch");
+async function createEmailRequest(options) {
+    const form = await createAttachmentForm(options);
+    const request = new Response(form);
+    const contentType = request.headers.get('content-type');
+    if (contentType === null) {
+        throw new Error('Unable to determine the multipart email content type');
+    }
+    const body = Buffer.from(await request.arrayBuffer());
+    return { headers: { 'Content-Type': contentType }, body };
+}
+async function createAttachmentForm(options) {
+    const form = new FormData();
+    const attachments = [];
+    for (const [index, attachment] of (options.attachments ?? []).entries()) {
+        const { content, encoding, ...metadata } = attachment;
+        let bytes;
+        if (typeof content === 'string') {
+            const hasValidEncoding = encoding === undefined || Buffer.isEncoding(encoding);
+            if (!hasValidEncoding) {
+                throw new TypeError('Invalid attachment encoding. Supply a supported Buffer encoding');
+            }
+            bytes = Buffer.from(content, encoding ?? 'utf8');
+        }
+        else if (Buffer.isBuffer(content)) {
+            bytes = content;
+        }
+        else if (content instanceof node_stream_1.Readable) {
+            const chunks = [];
+            for await (const chunk of content) {
+                const hasValidChunk = typeof chunk === 'string' || chunk instanceof Uint8Array;
+                if (!hasValidChunk) {
+                    throw new TypeError('Attachment streams must emit bytes or strings, not objects');
+                }
+                chunks.push(Buffer.from(chunk));
+            }
+            bytes = Buffer.concat(chunks);
+        }
+        else {
+            throw new TypeError('Attachment content must either be a string, Buffer, or Node.js Readable stream');
+        }
+        const attachmentIndex = `attachment-${index}`;
+        attachments.push({ ...metadata, part: attachmentIndex });
+        form.append(attachmentIndex, new Blob([new Uint8Array(bytes)]), attachmentIndex);
+    }
+    const metadata = JSON.stringify({ ...options, attachments });
+    form.append('metadata', metadata);
+    return form;
+}
+async function parseSmtpErrorDetails(response) {
+    const body = await response.json().catch(() => undefined);
+    if (typeof body !== 'object' || body === null || Array.isArray(body)) {
+        return {};
+    }
+    const { code, responseCode, command } = body;
+    const details = {};
+    if (typeof code === 'string')
+        details.code = code;
+    if (typeof responseCode === 'number')
+        details.responseCode = responseCode;
+    if (typeof command === 'string')
+        details.command = command;
+    return details;
+}
+async function sendEmail(options) {
+    const request = await createEmailRequest(options);
+    const response = await global.__forge_fetch__({
+        type: 'emails'
+    }, '/emails', {
+        method: 'POST',
+        ...request
+    });
+    try {
+        (0, fetch_1.handleProxyResponseErrors)(response);
+    }
+    catch (error) {
+        if (!(error instanceof errors_1.ProxyRequestError)) {
+            throw error;
+        }
+        const { code, responseCode, command } = await parseSmtpErrorDetails(response);
+        throw new errors_1.SendEmailError(error.status, error.errorCode, code, responseCode, command);
+    }
+    return (await response.json());
+}