import {z} from 'zod' export type GetApiControllerWiegandConnectionsArgs = {controller_id: number}; export type GetApiControllerWiegandConnectionsOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiControllerWiegandConnectionsResponse = z.array(z.union([z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), controller_wiegand_id: z.number().int(), controller_wiegand: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), type: z.literal("legacy_reader"), lock_id: z.number().int().nullable(), lock: z.object({id: z.number().int(), resource_type: z.literal("Lock"), name: z.string()}).optional(), elevator_id: z.number().int().nullable(), elevator: z.object({id: z.number().int(), resource_type: z.literal("Elevator"), name: z.string()}).optional()}), z.object({id: z.number().int(), created_at: z.string(), updated_at: z.string(), name: z.string().nullable(), controller_wiegand_id: z.number().int(), controller_wiegand: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), type: z.literal("legacy_controller"), virtual_card_format_name: z.enum(["null", "hid_h10301", "hid_h10302", "hid_h10304", "em_410x_26", "em_410x_32_34", "keyscan_c15001", "hid_d10202"]).nullable(), virtual_card_card_number: z.number().int().nullable(), virtual_card_card_id: z.number().int().nullable(), virtual_card_oem: z.number().int().nullable(), virtual_card_facility_code: z.number().int().nullable(), virtual_card_uid: z.string().nullable(), virtual_card_length: z.number().int().nullable(), 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(), controller_wiegand_id: z.number().int(), controller_wiegand: z.object({id: z.number().int(), channel: z.number(), name: z.string()}), type: z.literal("legacy_keypad_reader"), master_pin_code: z.string().nullable(), facility_code: z.number().int().nullable(), keypad_type: z.enum(["single_key_wiegand_6_bit_rosslare_format", "single_key_wiegand_8_bit_nibbles_complemented", "four_keys_binary_plus_facility_code_wiegand_26_bit", "one_to_five_keys_plus_facility_code_wiegand_26_bit", "single_key_wiegand_4_bit"]), two_factor_enabled: z.boolean(), lock_id: z.number().int().nullable(), lock: z.object({id: z.number().int(), resource_type: z.literal("Lock"), name: z.string()}).optional(), elevator_id: z.number().int().nullable(), elevator: z.object({id: z.number().int(), resource_type: z.literal("Elevator"), name: 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 }; /** * Fetch controller wiegand connections * * GET /controller_wiegand_connections * * Also accepts auth: OAuth2 */ export const getApiControllerWiegandConnections = async (args: GetApiControllerWiegandConnectionsArgs, options?: GetApiControllerWiegandConnectionsOptions) => { 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 (args.controller_id != null) query.append("controller_id", String(args.controller_id)) if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/controller_wiegand_connections${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(getApiControllerWiegandConnectionsResponse, await res.json()) };