import {z} from 'zod' export type GetApiV2ServicesServiceSidChannelsSidArgs = {ServiceSid: string, Sid: string}; export type GetApiV2ServicesServiceSidChannelsSidOptions = {baseUrl?: string | undefined, accountSidAuthToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const channelEnumChannelType = z.enum(["public", "private"]); export const ipMessagingV2ServiceChannel = z.object({sid: z.string().min(34).max(34).nullable().optional(), account_sid: z.string().min(34).max(34).nullable().optional(), service_sid: z.string().min(34).max(34).nullable().optional(), friendly_name: z.string().nullable().optional(), unique_name: z.string().nullable().optional(), attributes: z.string().nullable().optional(), type: channelEnumChannelType.optional(), date_created: z.string().nullable().optional(), date_updated: z.string().nullable().optional(), created_by: z.string().nullable().optional(), members_count: z.number().int().optional(), messages_count: z.number().int().optional(), url: z.string().nullable().optional(), links: z.object({}).nullable().optional()}); 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 }; /** * GET /v2/Services/{ServiceSid}/Channels/{Sid} */ export const getApiV2ServicesServiceSidChannelsSid = async (args: GetApiV2ServicesServiceSidChannelsSidArgs, options?: GetApiV2ServicesServiceSidChannelsSidOptions) => { const {baseUrl = "https://ip-messaging.twilio.com", fetch: fetchFn = fetch, accountSidAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (accountSidAuthToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accountSidAuthToken.username}:${accountSidAuthToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/Services/${encodeURIComponent(String(args.ServiceSid))}/Channels/${encodeURIComponent(String(args.Sid))}${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(ipMessagingV2ServiceChannel, await res.json()) };