import {z} from 'zod' export type CreateApiEventSetsArgs = {body: {event_set: {interval?: string | undefined, event_actor_id?: (number | null) | undefined, event_actor_type?: (string | null) | undefined, event_authenticated_by_id?: (number | null) | undefined, event_authenticated_by_type?: (string | null) | undefined, event_object_id?: (number | null) | undefined, event_object_type?: (string | null) | undefined, event_place_id?: (number | null) | undefined, event_success?: (boolean | null) | undefined, event_sequence?: (string | null) | undefined, event_type?: (string | null) | undefined, event_uuid?: (string | null) | undefined, place_id?: (number | null) | undefined}}}; export type CreateApiEventSetsOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEventSetsResponse = z.object({id: z.number().int(), created_at: z.string(), status: z.enum(["in_progress", "finished", "failed"]), interval: z.string(), event_actor_id: z.number().int().nullable(), event_actor_type: z.string().nullable(), event_authenticated_by_id: z.number().int().nullable(), event_authenticated_by_type: z.string().nullable(), event_object_id: z.number().int().nullable(), event_object_type: z.string().nullable(), event_place_id: z.number().int().nullable(), event_success: z.boolean().nullable(), event_sequence: z.string().nullable(), event_type: z.string().nullable(), event_uuid: z.string().nullable(), events: z.array(z.object({uuid: z.string(), type: z.string(), actor_type: z.enum(["null", "GroupLink", "MarketplaceInstallation", "User", "Guest"]).nullable(), actor_id: z.number().int().nullable(), actor_name: z.string().nullable(), actor_email: z.string().nullable(), authenticated_by_type: z.enum(["null", "ApplePassInstance", "Card", "FaceCredential", "GroupLink", "Login", "OauthAccessToken", "AccessKey"]).nullable(), authenticated_by_id: z.number().int().nullable(), authorized_by_type: z.string().nullable(), authorized_by_id: z.number().int().nullable(), authorized_by_role_id: z.string().nullable(), object_type: z.string(), object_id: z.number().int(), object_name: z.string(), object: z.object({}).optional(), action: z.string(), created_at: z.string(), message: z.string().nullable(), success: z.boolean(), code: z.string().nullable(), sequence: z.string(), organization_id: z.number().int(), place_id: z.number().int().nullable(), error_code: z.string().nullable(), error_message: z.string().nullable(), details: z.object({}), via: z.object({})})).optional(), cursor: z.string().nullable(), place_id: z.number().int().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 event set * * POST /event_sets * * Also accepts auth: OAuth2 */ export const createApiEventSets = async (args: CreateApiEventSetsArgs, options?: CreateApiEventSetsOptions) => { 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(/\/+$/, '')}/event_sets${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(createApiEventSetsResponse, await res.json()) };