Functional Weave
Code in Rust

email.send@1.0.0

adapters/typescript/log.ts

893 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { io } from "./funeio.ts";
import type { EmailMessage, SentEmail } from "./email_send_types.ts";
import { hexEncode } from "./encoding_hex.ts";  ← from encoding.hex ^1.0.0 · built alongside by fune

/**
 * The log provider: one line per message, nothing sent. For development and
 * previews, where a real inbox would be a nuisance, and for an application's
 * own tests, which can read the line back.
 */
export async function sendWithLog(message: EmailMessage, from: string): Promise<SentEmail> {
  const id = `log-${hexEncode(io().randomBytes(6))}`;
  const to = [...message.to, ...(message.cc ?? []).map((a) => `cc ${a}`), ...(message.bcc ?? []).map((a) => `bcc ${a}`)].join(", ");
  const body = message.text ?? message.html ?? "";
  const preview = body.replace(/\s+/g, " ").trim().slice(0, 60);
  io().log(`email ${id} from ${from} to ${to}: ${message.subject}${preview ? ` | ${preview}` : ""}`);
  return { provider: "log", id };
}