import {z} from 'zod' export type ChimeId = string; export type CameraId = string; export type RingtoneId = string; export type RingSettings = {cameraId: CameraId, repeatTimes: number, ringtoneId: RingtoneId, volume: number}; export type PatchApiV1ChimesIdArgs = {id: ChimeId, body: {name?: string | undefined, cameraIds?: Array | undefined, ringSettings?: Array | undefined}}; export type PatchApiV1ChimesIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const chimeId = z.string(); export const chimeModelKey = z.literal("chime"); export const deviceState = z.enum(["CONNECTED", "CONNECTING", "DISCONNECTED"]); export const name = z.string(); export const mac = z.string(); export const cameraId = z.string(); export const ringtoneId = z.string(); export const ringSettings = z.object({cameraId: cameraId, repeatTimes: z.number().gte(1).lte(10), ringtoneId: ringtoneId, volume: z.number().gte(0).lte(100)}); export const chime = z.object({id: chimeId, modelKey: chimeModelKey, state: deviceState, name: name.nullable(), mac: mac, cameraIds: z.array(cameraId), ringSettings: z.array(ringSettings)}); 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 }; /** * Patch chime settings * * PATCH /v1/chimes/{id} */ export const patchApiV1ChimesId = async (args: PatchApiV1ChimesIdArgs, options?: PatchApiV1ChimesIdOptions) => { const {baseUrl = "https://api.ui.com/v1/connector/consoles/942A6FF664C0000000000964970E0000000009E710560000000068A9873F:9999999999/proxy/protect/integration", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/chimes/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(chime, await res.json()) };