import {z} from 'zod' export type CreateApiTenantsArgs = {body: {name: string, domain: string, website?: string | undefined, timezone: string, currency: string, region: string, enabled?: boolean | undefined, enable_video_recording?: boolean | undefined, enable_audio_recording?: boolean | undefined, enable_dual_track_recording?: boolean | undefined, enable_billing?: boolean | undefined, enable_feature_billing?: boolean | undefined, enable_feature_call_statistics?: boolean | undefined, enable_feature_contact_center?: boolean | undefined, enable_feature_message_channels?: boolean | undefined, enable_feature_microsoft_teams?: boolean | undefined, enable_feature_trunks?: boolean | undefined, enable_feature_whats_app?: boolean | undefined, custom_options?: string | undefined, max_extensions?: number | undefined, max_concurrent_calls?: number | undefined, max_ring_groups?: number | undefined, max_virtual_receptionists?: number | undefined, max_call_queues?: number | undefined, max_conference_rooms?: number | undefined, disk_quota?: string | undefined, im_disk_quota?: string | undefined, extension_im_disk_quota?: string | undefined, recording_retention?: number | undefined, call_report_retention?: number | undefined, log_retention?: number | undefined, temp_file_retention?: number | undefined, office_hours?: ({monday?: ({enabled?: boolean | undefined, ranges?: (Array<{from?: string | undefined, to?: string | undefined}>) | undefined}) | undefined, tuesday?: ({enabled?: boolean | undefined, ranges?: (Array<{from?: string | undefined, to?: string | undefined}>) | undefined}) | undefined, wednesday?: ({enabled?: boolean | undefined, ranges?: (Array<{from?: string | undefined, to?: string | undefined}>) | undefined}) | undefined, thursday?: ({enabled?: boolean | undefined, ranges?: (Array<{from?: string | undefined, to?: string | undefined}>) | undefined}) | undefined, friday?: ({enabled?: boolean | undefined, ranges?: (Array<{from?: string | undefined, to?: string | undefined}>) | undefined}) | undefined, saturday?: ({enabled?: boolean | undefined, ranges?: (Array<{from?: string | undefined, to?: string | undefined}>) | undefined}) | undefined, sunday?: ({enabled?: boolean | undefined, ranges?: (Array<{from?: string | undefined, to?: string | undefined}>) | undefined}) | undefined}) | undefined, e164?: ({enabled?: boolean | undefined, international_code?: string | undefined, country?: string | undefined, area_code?: string | undefined, national_code?: string | undefined, prefix?: string | undefined, remove_special_chars?: boolean | undefined, remove_duplicate_countries?: boolean | undefined, remove_duplicate_area_codes?: boolean | undefined}) | undefined, cdr_event?: ({enabled?: boolean | undefined, auth?: ('DISABLE' | 'BASIC' | 'DIGEST' | 'BEARER') | undefined, username?: string | undefined, password?: string | undefined, token?: string | undefined, url?: string | undefined}) | undefined, extension_event?: ({enabled?: boolean | undefined, auth?: ('DISABLE' | 'BASIC' | 'DIGEST' | 'BEARER') | undefined, username?: string | undefined, password?: string | undefined, token?: string | undefined, url?: string | undefined}) | undefined, contact_match_type?: ('DISABLE' | 'MATCH_EXACTLY' | 'MATCH_LENGTH') | undefined, contact_match_length?: number | undefined, contact_append_type?: ('DISABLE' | 'APPEND' | 'PREPEND') | undefined, contact_update_interval?: number | undefined}}; export type CreateApiTenantsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiTenantsResponse = z.object({id: z.string().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 tenant * * POST /tenants */ export const createApiTenants = async (args: CreateApiTenantsArgs, options?: CreateApiTenantsOptions) => { 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(/\/+$/, '')}/tenants${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(createApiTenantsResponse, await res.json()) };