import {z} from 'zod' export type EmailWebhookEvent = 'email.scheduled' | 'email.sandbox' | 'email.queued' | 'email.sending' | 'email.sent' | 'email.delivered' | 'email.deferred' | 'email.bounced' | 'email.failed' | 'email.complained' | 'email.opened' | 'email.clicked' | 'email.unsubscribed' | 'email.received' | 'email.cancelled' | 'email.daily_limit_exceeded' | 'email_domain.created' | 'email_domain.verified' | 'email_domain.degraded' | 'email_domain.suspended' | 'email_domain.deleted' | 'email_domain.dkim_rotated'; export type CreateEmailWebhookRequest = {url: string, events: Array}; export type CreateApiEmailDomainsDomainIdWebhooksArgs = {domain_id: string, body: CreateEmailWebhookRequest}; export type CreateApiEmailDomainsDomainIdWebhooksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emailWebhookEvent = z.enum(["email.scheduled", "email.sandbox", "email.queued", "email.sending", "email.sent", "email.delivered", "email.deferred", "email.bounced", "email.failed", "email.complained", "email.opened", "email.clicked", "email.unsubscribed", "email.received", "email.cancelled", "email.daily_limit_exceeded", "email_domain.created", "email_domain.verified", "email_domain.degraded", "email_domain.suspended", "email_domain.deleted", "email_domain.dkim_rotated"]); export const emailWebhook = z.object({id: z.string(), record_type: z.literal("email_webhook"), url: z.string(), events: z.array(emailWebhookEvent), domain_id: z.string(), created_at: z.string(), updated_at: z.string()}); export const emailWebhookResponse = z.object({data: emailWebhook}); 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 }; /** * Create a webhook for an email domain * * POST /email_domains/{domain_id}/webhooks */ export const createApiEmailDomainsDomainIdWebhooks = async (args: CreateApiEmailDomainsDomainIdWebhooksArgs, options?: CreateApiEmailDomainsDomainIdWebhooksOptions) => { 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_domains/${encodeURIComponent(String(args.domain_id))}/webhooks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(emailWebhookResponse, await res.json()) };