import {z} from 'zod' export type GetApiLocksArgs = {ids?: string | undefined, query?: string | undefined, floor_id?: number | undefined, place_id?: number | undefined, limit?: number | undefined, offset?: number | undefined, configured?: boolean | undefined, online?: boolean | undefined, unlocked?: boolean | undefined, locked_down?: boolean | undefined, favorite?: ('true' | 'false' | '*') | undefined, sort?: ('favorite' | 'name' | '-name') | undefined}; export type GetApiLocksOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiLocksResponse = z.array(z.union([z.object({id: z.number().int(), configured: z.boolean(), description: z.string().nullable(), locked_down_since: z.string().nullable(), locked_down: z.boolean(), name: z.string(), online: z.boolean().nullable(), open: z.boolean(), unlocked_until: z.string().nullable(), unlocked: z.boolean(), place_id: z.number().int(), place: z.object({id: z.number().int(), name: z.string(), latitude: z.number().nullable(), longitude: z.number().nullable()})}), z.object({id: z.number().int(), resource_type: z.literal("Lock"), created_at: z.string(), updated_at: z.string(), configured: z.boolean(), description: z.string().nullable(), first_to_arrive_required_until: z.string().nullable(), first_to_arrive_satisfied: z.boolean(), latitude: z.number().nullable(), locked_down: z.boolean(), locked_down_since: z.string().nullable(), longitude: z.number().nullable(), name: z.string(), on_scheduled_unlock: z.boolean(), online: z.boolean().nullable(), open: z.boolean(), order_id: z.number().int().nullable(), unlocked: z.boolean(), geofence_restriction_enabled: z.boolean().nullable(), geofence_restriction_radius: z.number(), reader_restriction_enabled: z.boolean().nullable(), time_restriction_enabled: z.boolean().nullable(), time_restriction_time_zone: z.string().nullable(), favorite: z.boolean().optional(), place_id: z.number().int(), place: z.object({id: z.number().int(), name: z.string(), latitude: z.number().nullable(), longitude: z.number().nullable()}), floor_id: z.number().int().nullable(), groups_count: z.number().int(), unlocked_until: z.string().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 }; /** * Fetch locks * * GET /locks * * Also accepts auth: OAuth2, Kisi-Group-Link, Kisi-Access-Key */ export const getApiLocks = async (args: GetApiLocksArgs, options?: GetApiLocksOptions) => { 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.floor_id != null) query.append("floor_id", String(args.floor_id)) if (args.place_id != null) query.append("place_id", String(args.place_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.configured != null) query.append("configured", String(args.configured)) if (args.online != null) query.append("online", String(args.online)) if (args.unlocked != null) query.append("unlocked", String(args.unlocked)) if (args.locked_down != null) query.append("locked_down", String(args.locked_down)) if (args.favorite != null) query.append("favorite", String(args.favorite)) if (args.sort != null) query.append("sort", String(args.sort)) if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/locks${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(getApiLocksResponse, await res.json()) };