import {z} from 'zod' export type GetApiEmailDomainsDomainIdWebhooksArgs = {domain_id: string, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined, sort?: ('created_at' | '-created_at') | undefined}; export type GetApiEmailDomainsDomainIdWebhooksOptions = {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 offsetPaginationMeta = z.object({page_number: z.number().int().gte(1), page_size: z.number().int().gte(1), total_pages: z.number().int().gte(0), total_results: z.number().int().gte(0)}); export const emailWebhookListResponse = z.object({data: z.array(emailWebhook), meta: offsetPaginationMeta}); 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 }; /** * List webhooks for an email domain * * GET /email_domains/{domain_id}/webhooks */ export const getApiEmailDomainsDomainIdWebhooks = async (args: GetApiEmailDomainsDomainIdWebhooksArgs, options?: GetApiEmailDomainsDomainIdWebhooksOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.sort != null) query.append("sort", String(args.sort)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(emailWebhookListResponse, await res.json()) };