Clean-room Rust rewrite (written without reference to useSend source). Crates: mail-types, mail-core, mail-server, mail-auth, mail-submit, mail-hooks, mail-deliverability; sdks/ts (@scribase/mail); docs/. Each verified by running locally (see plans/scribase-mail-rust-2026-11-12.md). mail-mta, mail-inbound, mail-oci are still in progress and not in this commit. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
86 lines
3.3 KiB
TypeScript
86 lines
3.3 KiB
TypeScript
import { Http, query, snake } from "./http";
|
|
import type {
|
|
BatchSendResponse,
|
|
Email,
|
|
EmailSummary,
|
|
ListEmailsOptions,
|
|
ListResponse,
|
|
ObjectRef,
|
|
ReportEmailEventRequest,
|
|
RequestOptions,
|
|
Result,
|
|
SendEmailRequest,
|
|
SendEmailResponse,
|
|
UpdateEmailRequest,
|
|
} from "./types";
|
|
|
|
const SEND_ALIASES = { replyTo: "reply_to", scheduledAt: "scheduled_at" };
|
|
const ATTACHMENT_ALIASES = { contentType: "content_type" };
|
|
|
|
export function normalizeSend(payload: SendEmailRequest): Record<string, unknown> {
|
|
const body = snake(payload as unknown as Record<string, unknown>, SEND_ALIASES);
|
|
if (Array.isArray(payload.attachments)) {
|
|
body.attachments = payload.attachments.map((a) => snake(a as unknown as Record<string, unknown>, ATTACHMENT_ALIASES));
|
|
}
|
|
return body;
|
|
}
|
|
|
|
/** `/emails` */
|
|
export class Emails {
|
|
constructor(private readonly http: Http) {}
|
|
|
|
/** `POST /emails` */
|
|
send(payload: SendEmailRequest, options?: RequestOptions): Promise<Result<SendEmailResponse>> {
|
|
return this.http.post<SendEmailResponse>("/emails", normalizeSend(payload), options);
|
|
}
|
|
|
|
/** `POST /emails/batch`: up to 100 emails, no attachments. */
|
|
batch(payloads: SendEmailRequest[], options?: RequestOptions): Promise<Result<BatchSendResponse>> {
|
|
return this.http.post<BatchSendResponse>("/emails/batch", payloads.map(normalizeSend), options);
|
|
}
|
|
|
|
/** `GET /emails/{id}` */
|
|
get(id: string, options?: RequestOptions): Promise<Result<Email>> {
|
|
return this.http.get<Email>(`/emails/${encodeURIComponent(id)}`, options);
|
|
}
|
|
|
|
/** `GET /emails` (full-access keys only). */
|
|
list(params: ListEmailsOptions = {}, options?: RequestOptions): Promise<Result<ListResponse<EmailSummary>>> {
|
|
return this.http.get<ListResponse<EmailSummary>>(`/emails${query({ ...params })}`, options);
|
|
}
|
|
|
|
/**
|
|
* `PATCH /emails/{id}`: reschedule. Accepts Resend's
|
|
* `update({ id, scheduledAt })` or `update(id, { scheduled_at })`.
|
|
*/
|
|
update(idOrPayload: string | (UpdateEmailRequest & { id: string }), payload?: UpdateEmailRequest, options?: RequestOptions): Promise<Result<ObjectRef>> {
|
|
const id = typeof idOrPayload === "string" ? idOrPayload : idOrPayload.id;
|
|
const source = typeof idOrPayload === "string" ? (payload ?? {}) : idOrPayload;
|
|
const body = snake(source as unknown as Record<string, unknown>, SEND_ALIASES);
|
|
delete body.id;
|
|
return this.http.patch<ObjectRef>(`/emails/${encodeURIComponent(id)}`, body, options);
|
|
}
|
|
|
|
/** `POST /emails/{id}/cancel`: only scheduled or queued emails can be canceled. */
|
|
cancel(id: string, options?: RequestOptions): Promise<Result<ObjectRef>> {
|
|
return this.http.post<ObjectRef>(`/emails/${encodeURIComponent(id)}/cancel`, undefined, options);
|
|
}
|
|
|
|
/**
|
|
* `POST /emails/{id}/events` (Scribase extension): report a bounce,
|
|
* complaint or delivery from your own feedback processor.
|
|
*/
|
|
reportEvent(id: string, payload: ReportEmailEventRequest, options?: RequestOptions): Promise<Result<ObjectRef>> {
|
|
return this.http.post<ObjectRef>(`/emails/${encodeURIComponent(id)}/events`, payload, options);
|
|
}
|
|
}
|
|
|
|
/** Resend parity: `client.batch.send([...])`. */
|
|
export class Batch {
|
|
constructor(private readonly emails: Emails) {}
|
|
|
|
send(payloads: SendEmailRequest[], options?: RequestOptions): Promise<Result<BatchSendResponse>> {
|
|
return this.emails.batch(payloads, options);
|
|
}
|
|
}
|