import {z} from 'zod' export type CreateApiControllerRelayConnectionsArgs = {body: {controller_relay_connection: {type: 'lock', controller_relay_id: number, lock_id: number, duration?: number | undefined, name?: (string | null) | undefined}} | {controller_relay_connection: {type: 'elevator_stop', controller_relay_id: number, elevator_stop_id: number, duration?: number | undefined, name?: (string | null) | undefined}} | {controller_relay_connection: {type: 'siren', controller_relay_id: number, zone_id: number, name?: (string | null) | undefined}} | {controller_relay_connection: {type: 'third_party_alarm', controller_relay_id: number, zone_id: number, duration_for_away_mode?: (number | null) | undefined, duration_for_stay_mode?: (number | null) | undefined, name?: (string | null) | undefined}}}; export type CreateApiControllerRelayConnectionsOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiControllerRelayConnectionsResponse = z.union([z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), duration: z.number().gte(0.5).lte(600), type: z.literal("lock"), controller_relay_id: z.number().int(), controller_relay: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), lock_id: z.number().int(), lock: z.object({id: z.number().int(), resource_type: z.literal("Lock"), name: z.string()})}), z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), duration: z.number().gte(0.5).lte(600), type: z.literal("elevator_stop"), controller_relay_id: z.number().int(), controller_relay: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), elevator_stop_id: z.number().int(), elevator_stop: z.object({id: z.number().int(), name: z.string()})}), z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), type: z.literal("siren"), controller_relay_id: z.number().int(), controller_relay: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), zone_id: z.number().int(), zone: z.object({id: z.number().int(), name: z.string()})}), z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), duration_for_away_mode: z.number().gte(0).lte(600).nullable(), duration_for_stay_mode: z.number().gte(0).lte(600).nullable(), type: z.literal("third_party_alarm"), controller_relay_id: z.number().int(), controller_relay: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), zone_id: z.number().int(), zone: z.object({id: z.number().int(), name: z.string()})})]); 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 controller relay connection * * POST /controller_relay_connections * * Also accepts auth: OAuth2 */ export const createApiControllerRelayConnections = async (args: CreateApiControllerRelayConnectionsArgs, options?: CreateApiControllerRelayConnectionsOptions) => { const {baseUrl = "https://api.kisi.io", fetch: fetchFn = fetch, KisiLogin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/controller_relay_connections${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(createApiControllerRelayConnectionsResponse, await res.json()) };