import {z} from 'zod' export type CreateApiDectPhonesArgs = {body: {name: string, mac: string, template: string, vendor: string, model: string, password: string, user_password?: string | undefined, engineer_password?: string | undefined, timezone: string, region?: string | undefined, language: 'ENGLISH' | 'CHINESE' | 'DUTCH' | 'FRENCH' | 'GERMAN' | 'GREEK' | 'ITALIAN' | 'JAPANESE' | 'POLISH' | 'RUSSIAN' | 'SPANISH' | 'SWEDISH' | 'UKRAINIAN' | 'BULGARIAN', handset_language?: ('ENGLISH' | 'CHINESE' | 'DUTCH' | 'FRENCH' | 'GERMAN' | 'GREEK' | 'ITALIAN' | 'JAPANESE' | 'POLISH' | 'RUSSIAN' | 'SPANISH' | 'SWEDISH' | 'UKRAINIAN' | 'BULGARIAN') | undefined, tone: string, rps?: boolean | undefined, https?: boolean | undefined, codecs: Array, interface: 'WEB_DOMAIN' | 'PUBLIC_IPV4' | 'PUBLIC_IPV6' | 'PRIVATE_IPV4' | 'PRIVATE_IPV6' | 'SBC_DOMAIN', preferred_transport: 'UDP' | 'TCP' | 'TLS'}}; export type CreateApiDectPhonesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiDectPhonesResponse = 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 DECT phone * * POST /dect_phones */ export const createApiDectPhones = async (args: CreateApiDectPhonesArgs, options?: CreateApiDectPhonesOptions) => { 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(/\/+$/, '')}/dect_phones${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(createApiDectPhonesResponse, await res.json()) };