import {z} from 'zod' export type CreateApiInvitesArgs = {body: {invite: {notify?: boolean | undefined, invites_to_type: 'Place', invites_to_id: number, invitee: {resource_type: 'User', email: string, name: string} | {resource_type: 'Guest', name: string, email: string | null, phone_number: string | null}, access_key?: ({link_enabled?: boolean | undefined, qr_code_enabled?: boolean | undefined}) | undefined, role_assignment: {role_id?: string | undefined}}} | {invite: {notify?: boolean | undefined, invites_to_type: 'Group', invites_to_id: number, invitee: {resource_type: 'User', email: string, name: string} | {resource_type: 'Guest', name: string, email: string | null, phone_number: string | null}, access_key?: ({link_enabled?: boolean | undefined, qr_code_enabled?: boolean | undefined}) | undefined, role_assignment: {role_id?: string | undefined, valid_from?: (string | null) | undefined, valid_until?: (string | null) | undefined}}} | {invite: {notify?: boolean | undefined, invites_to_type: 'Team', invites_to_id: number, invitee: {resource_type: 'User', email: string, name: string} | {resource_type: 'Guest', name: string, email: string | null, phone_number: string | null}, access_key?: ({link_enabled?: boolean | undefined, qr_code_enabled?: boolean | undefined}) | undefined}}}; export type CreateApiInvitesOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiInvitesResponse = z.union([z.object({id: z.number().int(), resource_type: z.literal("Invite"), notify: z.boolean(), created_at: z.string(), updated_at: z.string(), invites_to_type: z.literal("Place"), invites_to_id: z.number().int(), invitee_type: z.enum(["User", "Guest"]), invitee_id: z.number().int(), invitee: z.union([z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Guest"), name: z.string(), email: z.string().nullable(), phone_number: z.string().nullable()})]), access_key: z.object({id: z.number().int(), resource_type: z.literal("AccessKey"), link_enabled: z.boolean(), qr_code_enabled: z.boolean()}).optional(), role_assignment_id: z.number().int(), role_assignment: z.object({id: z.number().int(), resource_type: z.literal("RoleAssignment"), role_id: z.string()})}), z.object({id: z.number().int(), resource_type: z.literal("Invite"), notify: z.boolean(), created_at: z.string(), updated_at: z.string(), invites_to_type: z.literal("Group"), invites_to_id: z.number().int(), invitee_type: z.enum(["User", "Guest"]), invitee_id: z.number().int(), invitee: z.union([z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Guest"), name: z.string(), email: z.string().nullable(), phone_number: z.string().nullable()})]), access_key: z.object({id: z.number().int(), resource_type: z.literal("AccessKey"), link_enabled: z.boolean(), qr_code_enabled: z.boolean()}).optional(), role_assignment_id: z.number().int(), role_assignment: z.object({id: z.number().int(), resource_type: z.literal("RoleAssignment"), role_id: z.string(), valid_from: z.string().nullable(), valid_until: z.string().nullable()})}), z.object({id: z.number().int(), resource_type: z.literal("Invite"), notify: z.boolean(), created_at: z.string(), updated_at: z.string(), invites_to_type: z.literal("Team"), invites_to_id: z.number().int(), invitee_type: z.enum(["User", "Guest"]), invitee_id: z.number().int(), invitee: z.union([z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Guest"), name: z.string(), email: z.string().nullable(), phone_number: z.string().nullable()})]), access_key: z.object({id: z.number().int(), resource_type: z.literal("AccessKey"), link_enabled: z.boolean(), qr_code_enabled: z.boolean()}).optional(), team_membership_id: z.number().int()})]); 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 invite * * POST /invites * * Also accepts auth: OAuth2 */ export const createApiInvites = async (args: CreateApiInvitesArgs, options?: CreateApiInvitesOptions) => { 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(/\/+$/, '')}/invites${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(createApiInvitesResponse, await res.json()) };