import {z} from 'zod' export type GetApiEmailInboxesInboxIdMessagesArgs = {inbox_id: string, 'filter[from]'?: string | undefined, 'filter[subject]'?: string | undefined, 'filter[received_after]'?: string | undefined, 'filter[received_before]'?: string | undefined, 'filter[read]'?: boolean | undefined, 'filter[unread]'?: boolean | undefined, 'filter[label]'?: string | undefined, 'filter[search]'?: string | undefined, 'page[size]'?: number | undefined, 'page[after]'?: string | undefined}; export type GetApiEmailInboxesInboxIdMessagesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const inboundEmailAddress = z.object({email: z.string(), name: z.string().optional()}); export const inboundMessage = z.object({id: z.string(), record_type: z.literal("email_message"), direction: z.literal("inbound"), status: z.literal("received"), inbox_id: z.string(), thread_id: z.string(), message_id: z.string().nullable(), in_reply_to: z.string().nullable(), references: z.array(z.string()), from: inboundEmailAddress, to: z.array(inboundEmailAddress), cc: z.array(inboundEmailAddress), bcc: z.array(inboundEmailAddress), reply_to: z.array(inboundEmailAddress), subject: z.string().nullable(), text_body_url: z.string().nullable(), html_body_url: z.string().nullable(), reply_text: z.string().nullable(), has_quoted_text: z.boolean(), headers: z.record(z.string(), z.unknown()), inline_files: z.array(z.record(z.string(), z.unknown())), attachments: z.array(z.record(z.string(), z.unknown())), labels: z.array(z.string().max(255)), read_at: z.string().nullable(), received_at: z.string().nullable(), sent_at: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const emailPaginationMeta = z.object({page_size: z.number().int().gte(1).lte(100), page_cursor: z.string().optional()}); export const inboundMessageListResponse = z.object({data: z.array(inboundMessage), meta: emailPaginationMeta}); 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 and search messages in an inbox * * GET /email_inboxes/{inbox_id}/messages */ export const getApiEmailInboxesInboxIdMessages = async (args: GetApiEmailInboxesInboxIdMessagesArgs, options?: GetApiEmailInboxesInboxIdMessagesOptions) => { 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['filter[from]'] != null) query.append("filter[from]", String(args['filter[from]'])) if (args['filter[subject]'] != null) query.append("filter[subject]", String(args['filter[subject]'])) if (args['filter[received_after]'] != null) query.append("filter[received_after]", String(args['filter[received_after]'])) if (args['filter[received_before]'] != null) query.append("filter[received_before]", String(args['filter[received_before]'])) if (args['filter[read]'] != null) query.append("filter[read]", String(args['filter[read]'])) if (args['filter[unread]'] != null) query.append("filter[unread]", String(args['filter[unread]'])) if (args['filter[label]'] != null) query.append("filter[label]", String(args['filter[label]'])) if (args['filter[search]'] != null) query.append("filter[search]", String(args['filter[search]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/email_inboxes/${encodeURIComponent(String(args.inbox_id))}/messages${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(inboundMessageListResponse, await res.json()) };