import {z} from 'zod' export type CreateApiControllerInputConnectionsArgs = {body: {controller_input_connection: {controller_input_id: number, type: 'contact_sensor', enabled?: boolean | undefined, duration?: number | undefined, end_of_line_type?: ('cut_detect' | 'short_detect' | 'cut_and_short_detect' | 'null' | null) | undefined, end_of_line_main_resistor?: (number | null) | undefined, end_of_line_second_resistor?: (number | null) | undefined, name?: (string | null) | undefined, lock_id: number}} | {controller_input_connection: {controller_input_id: number, type: 'request_to_exit', enabled?: boolean | undefined, duration?: number | undefined, end_of_line_type?: ('cut_detect' | 'short_detect' | 'cut_and_short_detect' | 'null' | null) | undefined, end_of_line_main_resistor?: (number | null) | undefined, end_of_line_second_resistor?: (number | null) | undefined, name?: (string | null) | undefined, lock_id: number, action?: ('last_to_leave' | 'last_to_unlock' | 'unlock' | 'none') | undefined}} | {controller_input_connection: {controller_input_id: number, type: 'third_party_alarm', action: 'away' | 'stay' | 'disarmed' | 'violated', zone_id: number}} | {controller_input_connection: {controller_input_id: number, type: 'tamper_detection', enabled?: boolean | undefined, end_of_line_type?: ('cut_detect' | 'short_detect' | 'cut_and_short_detect' | 'null' | null) | undefined, end_of_line_main_resistor?: (number | null) | undefined, end_of_line_second_resistor?: (number | null) | undefined, name?: (string | null) | undefined}}}; export type CreateApiControllerInputConnectionsOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiControllerInputConnectionsResponse = z.union([z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), controller_input_id: z.number().int(), controller_input: 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()}), type: z.literal("contact_sensor"), enabled: z.boolean(), duration: z.number().gte(3).lte(600), end_of_line_type: z.enum(["cut_detect", "short_detect", "cut_and_short_detect", "null"]).nullable(), end_of_line_main_resistor: z.number().int().gte(0).lte(100000).nullable(), end_of_line_second_resistor: z.number().int().gte(0).lte(100000).nullable()}), z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), controller_input_id: z.number().int(), controller_input: 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()}), type: z.literal("request_to_exit"), enabled: z.boolean(), duration: z.number().gte(0.5).lte(600), end_of_line_type: z.enum(["cut_detect", "short_detect", "cut_and_short_detect", "null"]).nullable(), end_of_line_main_resistor: z.number().int().gte(0).lte(100000).nullable(), end_of_line_second_resistor: z.number().int().gte(0).lte(100000).nullable(), action: z.enum(["last_to_leave", "last_to_unlock", "unlock", "none"])}), z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), controller_input_id: z.number().int(), controller_input: 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()}), type: z.literal("third_party_alarm"), action: z.enum(["away", "stay", "disarmed", "violated"])}), z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), controller_input_id: z.number().int(), controller_input: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), type: z.literal("tamper_detection"), enabled: z.boolean(), end_of_line_type: z.enum(["cut_detect", "short_detect", "cut_and_short_detect", "null"]).nullable(), end_of_line_main_resistor: z.number().int().gte(0).lte(100000).nullable(), end_of_line_second_resistor: z.number().int().gte(0).lte(100000).nullable()})]); 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 input connection * * POST /controller_input_connections * * Also accepts auth: OAuth2 */ export const createApiControllerInputConnections = async (args: CreateApiControllerInputConnectionsArgs, options?: CreateApiControllerInputConnectionsOptions) => { 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_input_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(createApiControllerInputConnectionsResponse, await res.json()) };