import {z} from 'zod' export type GetApiApiV1PhoneNumbersPhoneNumberIdTextsConversationsArgs = {phone_number_id: string, limit?: number | undefined, offset?: number | undefined, include_groups?: boolean | undefined, is_blocked?: (boolean | null) | undefined, participants?: (Array | null) | undefined, contact_id?: (string | null) | undefined, start_datetime?: (string | null) | undefined, end_datetime?: (string | null) | undefined, tz?: (string | null) | undefined}; export type GetApiApiV1PhoneNumbersPhoneNumberIdTextsConversationsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const textDirection = z.enum(["inbound", "outbound"]); export const textType = z.enum(["sms", "mms"]); export const textConversationSummary = z.object({remote_phone_number: z.string().nullable().optional(), id: z.string().nullable().optional(), participants: z.array(z.string()).nullable().optional(), is_group: z.boolean().nullable().optional(), latest_text: z.string().nullable(), latest_direction: textDirection, latest_type: textType, latest_has_media: z.boolean().optional(), latest_message_at: z.string(), unread_count: z.number().int(), total_count: z.number().int()}); export const getApiApiV1PhoneNumbersPhoneNumberIdTextsConversationsResponse = z.array(textConversationSummary); 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 Conversations * * GET /api/v1/phone/numbers/{phone_number_id}/texts/conversations * * Also accepts auth: ApiKeyAuth */ export const getApiApiV1PhoneNumbersPhoneNumberIdTextsConversations = async (args: GetApiApiV1PhoneNumbersPhoneNumberIdTextsConversationsArgs, options: GetApiApiV1PhoneNumbersPhoneNumberIdTextsConversationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.include_groups != null) query.append("include_groups", String(args.include_groups)) if (args.is_blocked != null) query.append("is_blocked", String(args.is_blocked)) for (const item of args.participants ?? []) query.append("participants", String(item)) if (args.contact_id != null) query.append("contact_id", String(args.contact_id)) if (args.start_datetime != null) query.append("start_datetime", String(args.start_datetime)) if (args.end_datetime != null) query.append("end_datetime", String(args.end_datetime)) if (args.tz != null) query.append("tz", String(args.tz)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/phone/numbers/${encodeURIComponent(String(args.phone_number_id))}/texts/conversations${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(getApiApiV1PhoneNumbersPhoneNumberIdTextsConversationsResponse, await res.json()) };