import {z} from 'zod' export type InteractionChannelParticipantEnumType = 'supervisor' | 'customer' | 'external' | 'agent' | 'unknown'; export type CreateApiV1InteractionsInteractionSidChannelsChannelSidParticipantsArgs = {InteractionSid: string, ChannelSid: string, body: {Type: InteractionChannelParticipantEnumType, MediaProperties: unknown, RoutingProperties: unknown}}; export type CreateApiV1InteractionsInteractionSidChannelsChannelSidParticipantsOptions = {baseUrl?: string | undefined, accountSidAuthToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const interactionChannelParticipantEnumType = z.enum(["supervisor", "customer", "external", "agent", "unknown"]); export const flexV1InteractionInteractionChannelInteractionChannelParticipant = z.object({sid: z.string().min(34).max(34).nullable().optional(), type: interactionChannelParticipantEnumType.optional(), interaction_sid: z.string().min(34).max(34).nullable().optional(), channel_sid: z.string().min(34).max(34).nullable().optional(), url: z.string().nullable().optional(), routing_properties: z.unknown()}); 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 }; /** * Add a Participant to a Channel. * * POST /v1/Interactions/{InteractionSid}/Channels/{ChannelSid}/Participants */ export const createApiV1InteractionsInteractionSidChannelsChannelSidParticipants = async (args: CreateApiV1InteractionsInteractionSidChannelsChannelSidParticipantsArgs, options?: CreateApiV1InteractionsInteractionSidChannelsChannelSidParticipantsOptions) => { const {baseUrl = "https://flex-api.twilio.com", fetch: fetchFn = fetch, accountSidAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (accountSidAuthToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accountSidAuthToken.username}:${accountSidAuthToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Interactions/${encodeURIComponent(String(args.InteractionSid))}/Channels/${encodeURIComponent(String(args.ChannelSid))}/Participants${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(flexV1InteractionInteractionChannelInteractionChannelParticipant, await res.json()) };