import {z} from 'zod' export type GetApiV1SipServersArgs = {isActive?: boolean | undefined, status?: string | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiV1SipServersOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sipServer = z.object({id: z.number().int().optional(), serverId: z.number().int().optional(), serverName: z.string().nullable().optional(), queueName: z.string().nullable().optional(), ipAddress: z.string().nullable().optional(), isActive: z.boolean().optional(), status: z.string().nullable().optional(), maxChannels: z.number().int().optional(), currentChannels: z.number().int().optional(), httpBaseUrl: z.string().nullable().optional(), httpPort: z.number().int().optional()}); export const sipServerQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(sipServer).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 /v1/sip-servers * * Also accepts auth: API Key */ export const getApiV1SipServers = async (args: GetApiV1SipServersArgs, options: GetApiV1SipServersOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.isActive != null) query.append("isActive", String(args.isActive)) if (args.status != null) query.append("status", String(args.status)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sip-servers${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(sipServerQueryResult, await res.json()) };