packages/ at markup-kit dd1e6eb5270e
A snapshot of packages/ in the Weft repository; the official packages are published from these commits. Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
This commit is contained in:
commit
306b143104
72 changed files with 10915 additions and 0 deletions
131
email/postmark.weft
Normal file
131
email/postmark.weft
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
// Sending through Postmark (https://postmarkapp.com/developer/api/email-api).
|
||||
import { fetch } from "std/http"
|
||||
import type { Address, EmailError, Message, Transport } from "./email"
|
||||
import { formatAddress } from "./mime"
|
||||
|
||||
/// `messageStream` is Postmark's stream to send on (`outbound` unless given; broadcasts need `broadcast`). `baseUrl` is for tests.
|
||||
export type PostmarkOptions = {
|
||||
serverToken: string,
|
||||
messageStream?: string,
|
||||
baseUrl?: string,
|
||||
timeout?: float,
|
||||
}
|
||||
|
||||
fn text(value: json): string? {
|
||||
return value is string ? value : null
|
||||
}
|
||||
|
||||
fn list(addresses: Address[]): string {
|
||||
return addresses.map(formatAddress).join(", ")
|
||||
}
|
||||
|
||||
/// The JSON Postmark is sent for a message.
|
||||
export fn payload(message: Message, messageStream: string = "outbound"): json {
|
||||
let mut body: json = {
|
||||
From: formatAddress(message.from),
|
||||
To: list(message.to),
|
||||
Subject: message.subject,
|
||||
MessageStream: messageStream,
|
||||
}
|
||||
let cc = message.cc ?? []
|
||||
if cc.length > 0 {
|
||||
body["Cc"] = list(cc)
|
||||
}
|
||||
let bcc = message.bcc ?? []
|
||||
if bcc.length > 0 {
|
||||
body["Bcc"] = list(bcc)
|
||||
}
|
||||
let replyTo = message.replyTo
|
||||
if replyTo != null {
|
||||
body["ReplyTo"] = formatAddress(replyTo)
|
||||
}
|
||||
let html = message.html
|
||||
if html != null && html != "" {
|
||||
body["HtmlBody"] = html
|
||||
}
|
||||
let plainText = message.text
|
||||
if plainText != null && plainText != "" {
|
||||
body["TextBody"] = plainText
|
||||
}
|
||||
let headers = message.headers ?? []
|
||||
if headers.length > 0 {
|
||||
let fields: json[] = headers.map(h => {
|
||||
let field: json = { Name: h[0], Value: h[1] }
|
||||
return field
|
||||
})
|
||||
body["Headers"] = fields
|
||||
}
|
||||
let attachments = message.attachments ?? []
|
||||
if attachments.length > 0 {
|
||||
let files: json[] = attachments.map(a => {
|
||||
let mut file: json = {
|
||||
Name: a.filename,
|
||||
Content: a.content.toBase64(),
|
||||
ContentType: a.type ?? "application/octet-stream",
|
||||
}
|
||||
let cid = a.contentId
|
||||
if cid != null {
|
||||
file["ContentID"] = `cid:${cid}`
|
||||
}
|
||||
return file
|
||||
})
|
||||
body["Attachments"] = files
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
/// The error for an answer that isn't a success.
|
||||
export fn failureOf(status: float, body: json): EmailError {
|
||||
let message = text(body.Message) ?? `Postmark answered ${status}`
|
||||
let code = body.ErrorCode
|
||||
if status == 429 {
|
||||
return RateLimited { message, retryAfter: null }
|
||||
}
|
||||
if status >= 500 {
|
||||
return ProviderFailure { status, message }
|
||||
}
|
||||
return Rejected { status, code: code is float ? `${code}` : null, message }
|
||||
}
|
||||
|
||||
/// A transport that sends through Postmark. It needs `http.fetch`.
|
||||
///
|
||||
/// ```weft
|
||||
/// let mail = postmark({ serverToken: env("POSTMARK_TOKEN")! })
|
||||
/// ```
|
||||
export fn postmark(options: PostmarkOptions): Transport {
|
||||
let url = `${options.baseUrl ?? "https://api.postmarkapp.com"}/email`
|
||||
let timeout = options.timeout ?? 30000
|
||||
let token = options.serverToken
|
||||
let stream = options.messageStream ?? "outbound"
|
||||
return {
|
||||
name: "postmark",
|
||||
send: async message => {
|
||||
let headers: [string, string][] = [
|
||||
["x-postmark-server-token", token],
|
||||
["accept", "application/json"],
|
||||
["user-agent", "weft-email/0.1.0"],
|
||||
]
|
||||
let res, err = try await fetch(
|
||||
url,
|
||||
{ method: "POST", headers, json: payload(message, stream), timeout },
|
||||
)
|
||||
if err != null {
|
||||
fail ConnectionFailed { message: `could not reach Postmark: ${err.message}` }
|
||||
}
|
||||
let answer = try await res.json() else null
|
||||
if !res.ok {
|
||||
fail failureOf(res.status, answer)
|
||||
}
|
||||
// Postmark can answer 200 with an error code of its own.
|
||||
let code = answer.ErrorCode
|
||||
if code is float && code != 0 {
|
||||
fail failureOf(422, answer)
|
||||
}
|
||||
let id = text(answer.MessageID)
|
||||
if id == null {
|
||||
fail BadResponse { message: "Postmark's answer has no MessageID" }
|
||||
}
|
||||
return { id, provider: "postmark", skipped: false }
|
||||
},
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue