import {z} from 'zod' export type GetApiReadersIdArgs = {id: number}; export type GetApiReadersIdOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiReadersIdResponse = z.object({id: z.number().int(), resource_type: z.literal("Reader"), name: z.string(), description: z.string().nullable(), created_at: z.string(), updated_at: z.string(), broadcast_ip_address: z.string().nullable(), configured: z.boolean(), device_id: z.string().min(16).max(16), ethernet_mac: z.string(), gateway_ip_address: z.string().nullable(), ip_address: z.string().nullable(), model: z.enum(["reader-1.0", "reader-2.0", "reader-pro-2.1", "reader-pro-2.1-hf", "reader-pro-3.0"]), network_interface: z.enum(["ethernet", "wifi", "null"]).nullable(), network: z.string().nullable(), online: z.boolean(), revision: z.string(), token: z.string(), wifi_mac: z.string(), backplate: z.enum(["standard", "keypad_terminal", "quick_response_code_terminal", "keypad_and_quick_response_code_terminal"]), buzzer_enabled: z.boolean(), cards_enabled: z.boolean(), checkout: z.boolean(), device_unlock_restriction_enabled: z.boolean(), held_open_alarm_enabled: z.boolean(), quick_response_code_enabled: z.boolean(), tampered: z.boolean(), tamper_enabled: z.boolean(), wave_to_unlock_enabled: z.boolean(), elevator_id: z.number().int().nullable(), elevator: z.object({id: z.number().int(), resource_type: z.literal("Elevator"), name: z.string()}).optional(), lock_id: z.number().int().nullable(), lock: z.object({id: z.number().int(), resource_type: z.literal("Lock"), name: z.string()}).optional(), place_id: z.number().int(), place: z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}), zone_id: z.number().int().nullable(), zone: z.object({id: z.number().int(), name: z.string()}).optional(), terminal_id: z.number().int().nullable(), terminal: z.object({id: z.number().int(), resource_type: z.literal("Terminal"), 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 reader * * GET /readers/{id} * * Also accepts auth: OAuth2 */ export const getApiReadersId = async (args: GetApiReadersIdArgs, options?: GetApiReadersIdOptions) => { 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(/\/+$/, '')}/readers/${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(getApiReadersIdResponse, await res.json()) };