import {z} from 'zod' export type CreateApiLocksArgs = {body: {lock: {name: string, description?: (string | null) | undefined, latitude?: (number | null) | undefined, longitude?: (number | null) | undefined, geofence_restriction_enabled?: (boolean | null) | undefined, reader_restriction_enabled?: (boolean | null) | undefined, time_restriction_enabled?: (boolean | null) | undefined, order_id?: (number | null) | undefined, floor_id?: (number | null) | undefined, place_id: number, favorite?: boolean | undefined}}}; export type CreateApiLocksOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiLocksResponse = 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 }; /** * Create lock * * POST /locks * * Also accepts auth: OAuth2 */ export const createApiLocks = async (args: CreateApiLocksArgs, options?: CreateApiLocksOptions) => { const {baseUrl = "https://api.kisi.io", fetch: fetchFn = fetch, KisiLogin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/locks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiLocksResponse, await res.json()) };