// `node packages/email/scripts/integration.mjs` runs this: both providers' APIs against a scripted server, and a full message // printed for Python's `email` parser to read back. import { header, json as jsonResponse, serve, status } from "std/http" import type { Request } from "std/http" import { parse } from "std/json" import { log } from "std/log" import { exit, stdout } from "std/process" import { send } from "@weft/email" import type { EmailError, Message } from "@weft/email" import { toMime } from "@weft/email/mime" import { postmark } from "@weft/email/postmark" import { resend } from "@weft/email/resend" let mut passed = 0 let mut failed = 0 fn check(name: string, ok: boolean, detail: string = "") { if ok { passed += 1 } else { failed += 1 log(`FAIL ${name} ${detail}`) } } let mut seen: Request[] = [] let mut script: string[] = [] fn okResend(): json { return { id: "re_123" } } fn okPostmark(): json { return { To: "ana@example.com", SubmittedAt: "2026-11-17T10:00:00Z", MessageID: "pm-123", ErrorCode: 0, Message: "OK", } } fn refused(): json { return { statusCode: 422, name: "validation_error", message: "The domain is not verified." } } fn postmarkRefused(): json { return { ErrorCode: 406, Message: "The address is inactive." } } async fn handle(req: Request) { seen.push(req) let step = script.shift() ?? "ok" return match step { "resend-ok" => jsonResponse(okResend()), "postmark-ok" => jsonResponse(okPostmark()), "resend-422" => jsonResponse(refused(), { status: 422 }), "postmark-422" => jsonResponse(postmarkRefused(), { status: 422 }), "postmark-200-error" => jsonResponse(postmarkRefused()), "429" => header(jsonResponse(refused(), { status: 429 }), "retry-after", "12"), "500" => status(500, "broken"), "no-id" => jsonResponse(refused()), _ => status(404), } } fn kindOf(e: EmailError?): string { return e == null ? "none" : e.kind } fn message(): Message { return { from: { email: "info@example.com", name: "Vidni" }, to: ["ana@example.com"], subject: "Vaša vstopnica", html: "
Živjo
", text: "Živjo", attachments: [ { filename: "vstopnica.pdf", content: Bytes.of([37, 80, 68, 70]), type: "application/pdf" }, ], } } fn edit(change: (mut m: Message) => void): Message { let mut m = message() change(&m) return m } fn parsed(req: Request): json { return try parse(req.text()) else null } async fn main() { let server = try await serve({ port: 0, host: "127.0.0.1" }, handle) let base = `http://127.0.0.1:${server.port}` // ---- Resend ---- let viaResend = resend( { apiKey: "re_key", baseUrl: base, idempotencyKey: m => `order-${m.subject.length}` }, ) script = ["resend-ok"] seen = [] let sent1, e0 = try await send(viaResend, message()) check("Resend: sent", sent1?.id == "re_123" && sent1?.provider == "resend", kindOf(e0)) check("Resend: the endpoint", seen[0].path == "/emails" && seen[0].method == "POST", seen[0].path) check("Resend: the key", seen[0].headers.get("authorization") == "Bearer re_key") check("Resend: the idempotency key", seen[0].headers.get("idempotency-key") == "order-14") check("Resend: JSON", (seen[0].headers.get("content-type") ?? "").startsWith("application/json")) check( "Resend: the fields", parsed(seen[0]).subject == "Vaša vstopnica" && parsed(seen[0]).attachments[0].content == "JVBERg==", ) script = ["resend-422"] let _, e1 = try await send(viaResend, message()) match e1 { Rejected { status: s, code, message: m } => check( "Resend: a refusal", s == 422 && code == "validation_error" && m == "The domain is not verified.", ), _ => check("Resend: a refusal", false, kindOf(e1)), } script = ["429"] let _, e2 = try await send(viaResend, message()) match e2 { RateLimited { retryAfter } => check("Resend: rate limited", retryAfter == 12), _ => check("Resend: rate limited", false, kindOf(e2)), } script = ["500"] let _, e3 = try await send(viaResend, message()) check("Resend: a server error", kindOf(e3) == "ProviderFailure", kindOf(e3)) script = ["no-id"] let _, e4 = try await send(viaResend, message()) check("Resend: an answer without an id", kindOf(e4) == "BadResponse", kindOf(e4)) // An invalid message never reaches the provider. seen = [] let _, e5 = try await send( viaResend, edit((mut m) => { m.subject = "a\r\nBcc: v@x.y" }), ) check( "an invalid message isn't sent", kindOf(e5) == "InvalidMessage" && seen.length == 0, kindOf(e5), ) // ---- Postmark ---- let viaPostmark = postmark({ serverToken: "pm_token", baseUrl: base }) script = ["postmark-ok"] seen = [] let sent2, e10 = try await send(viaPostmark, message()) check("Postmark: sent", sent2?.id == "pm-123" && sent2?.provider == "postmark", kindOf(e10)) check( "Postmark: the endpoint", seen[0].path == "/email" && seen[0].method == "POST", seen[0].path, ) check("Postmark: the token", seen[0].headers.get("x-postmark-server-token") == "pm_token") check( "Postmark: the fields", parsed(seen[0]).Subject == "Vaša vstopnica" && parsed(seen[0]).MessageStream == "outbound" && parsed(seen[0]).Attachments[0].Name == "vstopnica.pdf", ) script = ["postmark-422"] let _, e6 = try await send(viaPostmark, message()) match e6 { Rejected { code, message: m } => check( "Postmark: a refusal", code == "406" && m == "The address is inactive.", ), _ => check("Postmark: a refusal", false, kindOf(e6)), } script = ["postmark-200-error"] let _, e7 = try await send(viaPostmark, message()) check("Postmark: an error code in a 200", kindOf(e7) == "Rejected", kindOf(e7)) script = ["500"] let _, e8 = try await send(viaPostmark, message()) check("Postmark: a server error", kindOf(e8) == "ProviderFailure", kindOf(e8)) // ---- nothing listening ---- let dead = resend({ apiKey: "k", baseUrl: "http://127.0.0.1:9", timeout: 2000 }) let _, e9 = try await send(dead, message()) check("no connection", kindOf(e9) == "ConnectionFailed", kindOf(e9)) await server.close() // ---- a full message for Python's parser ---- let long = "Pozdravljeni, to je dolgo sporočilo s črkami č, š in ž, ki sega čez mejo šestinsedemdesetih znakov v vrstici. = in pika.\n.vrstica s piko\nKonec " let full: Message = { from: { email: "info@example.com", name: "Žiga \"Vidni\" Novak" }, to: ["ana@example.com", { email: "bor@example.com", name: "Bor Šuštar" }], cc: ["cene@example.com"], bcc: ["skrito@example.com"], replyTo: { email: "lastnik@example.com", name: "Lastnik" }, subject: "Vaša vstopnica za dogodek: čudovit večer na gradu, ki se ga boste še dolgo spominjali", text: long, html: `${long}