import {z} from 'zod' export type GetApiControllerRelayConnectionsIdArgs = {id: number}; export type GetApiControllerRelayConnectionsIdOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiControllerRelayConnectionsIdResponse = 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 }; /** * Fetch a controller relay connection * * GET /controller_relay_connections/{id} * * Also accepts auth: OAuth2 */ export const getApiControllerRelayConnectionsId = async (args: GetApiControllerRelayConnectionsIdArgs, options?: GetApiControllerRelayConnectionsIdOptions) => { const {baseUrl = "https://api.kisi.io", fetch: fetchFn = fetch, KisiLogin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/controller_relay_connections/${encodeURIComponent(String(args.id))}${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(getApiControllerRelayConnectionsIdResponse, await res.json()) };