import {z} from 'zod' export type RescheduleEmailMessageRequest = {scheduled_at: string}; export type PatchApiEmailMessagesEmailIdScheduleArgs = {email_id: string, body: RescheduleEmailMessageRequest}; export type PatchApiEmailMessagesEmailIdScheduleOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emailMessageStatus = z.enum(["queued", "scheduled", "cancelled", "sandbox", "sending", "sent", "failed", "deferred", "delivered", "bounced", "complained", "rejected", "opened", "clicked", "unsubscribed"]); export const emailAddress = z.object({email: z.string(), name: z.string().optional()}); export const attachmentResponse = z.object({url: z.string().nullable(), sha256: z.string().nullable(), size_bytes: z.number().int().nullable(), filename: z.string(), content_type: z.string(), disposition: z.string(), content_id: z.string().nullable()}); export const emailEventType = z.enum(["queued", "deferred", "scheduled", "cancelled", "sandbox", "sending", "sent", "failed", "delivered", "bounced", "complained", "suppressed", "rejected", "opened", "clicked", "unsubscribed", "daily_limit_exceeded", "scan_deferred", "quarantined", "quarantine_released", "quarantine_release_dispatched", "quarantine_rejected", "quarantine_expired", "gw_reject", "injection_timeout", "expired"]); export const embeddedMessageEvent = z.object({type: emailEventType, occurred_at: z.string(), payload: z.record(z.string(), z.unknown()).optional()}); export const suppressedRecipient = z.object({to: z.string(), reason: z.string(), scope: z.string(), override_allowed: z.boolean()}); export const emailMessageDetail = z.object({record_type: z.literal("email_message"), id: z.string(), status: emailMessageStatus, from: emailAddress, to: z.array(emailAddress), cc: z.array(emailAddress), bcc: z.array(emailAddress), reply_to: z.string().nullable(), subject: z.string(), template_id: z.string().nullable(), template_variables: z.record(z.string(), z.unknown()), tags: z.array(z.string()), metadata: z.record(z.string(), z.unknown()), attachments: z.array(attachmentResponse), events: z.array(embeddedMessageEvent), created_at: z.string(), scheduled_at: z.string().optional(), inline_css: z.boolean().optional(), sandbox: z.boolean().optional(), recipient_statuses: z.record(z.string(), z.number().int()).optional(), suppressed: z.array(suppressedRecipient).optional(), text_body: z.string().nullable(), html_body: z.string().nullable()}); export const emailMessageDetailResponse = z.object({data: emailMessageDetail}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Reschedule a scheduled email message * * PATCH /email_messages/{email_id}/schedule */ export const patchApiEmailMessagesEmailIdSchedule = async (args: PatchApiEmailMessagesEmailIdScheduleArgs, options?: PatchApiEmailMessagesEmailIdScheduleOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/email_messages/${encodeURIComponent(String(args.email_id))}/schedule${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(emailMessageDetailResponse, await res.json()) };