import {z} from 'zod' export type CreateApiUserMeetingsArgs = {body: {mode: 'AUDIO' | 'VIDEO', control?: ('FREE' | 'MASTER') | undefined, height?: number | undefined, width?: number | undefined, bitrate?: number | undefined, framerate?: number | undefined, layout?: ('LAYOUT0' | 'LAYOUT1' | 'LAYOUT2' | 'LAYOUT3' | 'LAYOUT4' | 'LAYOUT6' | 'LAYOUT9') | undefined, subject: string, language?: string | undefined, admin_pin?: string | undefined, room_pin?: string | undefined, enable_recording?: boolean | undefined, enable_prompt?: boolean | undefined, timezone: string, scheduled_start_at?: unknown | undefined, scheduled_end_at?: unknown | undefined, internal_invitees?: Array | undefined, external_invitees?: string | undefined, close_on_nobody?: boolean | undefined, outbound_caller_ids?: (Array<{provider_id?: string | undefined, caller_id?: string | undefined, description?: string | undefined}>) | undefined, custom_options?: string | undefined}}; export type CreateApiUserMeetingsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiUserMeetingsResponse = z.object({id: z.string().optional(), extension_number: z.string().min(3).max(64).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 }; /** * Create a Meeting * * POST /user/meetings */ export const createApiUserMeetings = async (args: CreateApiUserMeetingsArgs, options?: CreateApiUserMeetingsOptions) => { const {baseUrl = "https://HOSTNAME:8887/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/user/meetings${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiUserMeetingsResponse, await res.json()) };