import {z} from 'zod' export type GetApiControllersArgs = {ids?: string | undefined, query?: string | undefined, place_id?: number | undefined, lock_id?: number | undefined, limit?: number | undefined, offset?: number | undefined, status?: ('online' | 'offline') | undefined, sort?: ('name' | '-name') | undefined}; export type GetApiControllersOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiControllersResponse = z.array(z.object({id: z.number().int(), resource_type: z.literal("Controller"), 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(["router-8.0", "controller-pro-1.1", "controller-pro-2.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(), zone_controller: z.boolean(), place_id: z.number().int(), place: z.object({id: z.number().int(), resource_type: z.literal("Place"), 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 controllers * * GET /controllers * * Also accepts auth: OAuth2 */ export const getApiControllers = async (args: GetApiControllersArgs, options?: GetApiControllersOptions) => { 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.ids != null) query.append("ids", String(args.ids)) if (args.query != null) query.append("query", String(args.query)) if (args.place_id != null) query.append("place_id", String(args.place_id)) if (args.lock_id != null) query.append("lock_id", String(args.lock_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.status != null) query.append("status", String(args.status)) if (args.sort != null) query.append("sort", String(args.sort)) if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/controllers${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(getApiControllersResponse, await res.json()) };