import {z} from 'zod' export type VoipGatewayType = 0 | 1 | 2 | 3 | 4 | 5; export type VoipProvider = {id?: number | undefined, departmentId?: number | undefined, name: string, gatewayType: VoipGatewayType, maxChannelCount?: number | undefined, ipAddress: string, port?: number | undefined, outboundProxyIp?: (string | null) | undefined, outboundProxyPort?: number | undefined, externalNumber: string, virtualExtNumber: string, voipUserName?: (string | null) | undefined, voipPassword?: (string | null) | undefined, registerTimeout?: number | undefined, codecList?: (string | null) | undefined, registerType?: number | undefined, sendEmailOffHourCalls?: number | undefined, toAddress?: (string | null) | undefined, active?: number | undefined, registered?: (boolean | null) | undefined, sipServerId?: number | undefined}; export type CreateApiV1CallcenterProvidersArgs = {body: VoipProvider}; export type CreateApiV1CallcenterProvidersOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const voipGatewayType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]); export const voipProvider = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), name: z.string().min(1).max(50), gatewayType: voipGatewayType, maxChannelCount: z.number().int().optional(), ipAddress: z.string().min(1), port: z.number().int().optional(), outboundProxyIp: z.string().nullable().optional(), outboundProxyPort: z.number().int().optional(), externalNumber: z.string().min(1), virtualExtNumber: z.string().min(1), voipUserName: z.string().nullable().optional(), voipPassword: z.string().nullable().optional(), registerTimeout: z.number().int().optional(), codecList: z.string().nullable().optional(), registerType: z.number().int().optional(), sendEmailOffHourCalls: z.number().int().optional(), toAddress: z.string().nullable().optional(), active: z.number().int().optional(), registered: z.boolean().nullable().optional(), sipServerId: z.number().int().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 VoIP provider * * POST /v1/callcenter/providers * * Also accepts auth: API Key */ export const createApiV1CallcenterProviders = async (args: CreateApiV1CallcenterProvidersArgs, options: CreateApiV1CallcenterProvidersOptions) => { const {baseUrl, 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(/\/+$/, '')}/v1/callcenter/providers${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(voipProvider, await res.json()) };