import {z} from 'zod' export type CreateApiRoleAssignmentsArgs = {body: {role_assignment: {applies_to_type: 'Organization', applies_to_id: number, assignee_type: 'User' | 'Team' | 'Guest', assignee_id: number, role_id: string, user_id?: (number | null) | undefined, organization_id?: number | undefined, notify?: boolean | undefined}} | {role_assignment: {applies_to_type: 'Place', applies_to_id: number, assignee_type: 'User' | 'Team' | 'Guest', assignee_id: number, role_id: string, user_id?: (number | null) | undefined, place_id?: number | undefined, notify?: boolean | undefined}} | {role_assignment: {applies_to_type: 'Group', applies_to_id: number, assignee_type: 'User' | 'Team' | 'Guest', assignee_id: number, role_id: string, user_id?: (number | null) | undefined, group_id?: number | undefined, notify?: boolean | undefined, valid_from?: (string | null) | undefined, valid_until?: (string | null) | undefined}}}; export type CreateApiRoleAssignmentsOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiRoleAssignmentsResponse = z.union([z.object({id: z.number().int(), resource_type: z.literal("RoleAssignment"), created_at: z.string(), updated_at: z.string(), role_id: z.string(), notify: z.boolean(), applies_to_type: z.literal("Organization"), applies_to_id: z.number().int(), applies_to: z.union([z.object({id: z.number().int(), resource_type: z.literal("Organization"), name: z.string(), domain: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Group"), name: z.string()})]), assignee_type: z.enum(["User", "Team", "Guest"]), assignee_id: z.number().int(), assignee: 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("Team"), 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()})]), created_by_type: z.enum(["null", "User", "MarketplaceInstallation"]).nullable(), created_by_id: z.number().int().nullable(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), assignee_team_id: z.number().int().nullable(), assignee_team: z.object({id: z.number().int(), resource_type: z.literal("Team"), name: z.string()}).optional(), guest_id: z.number().int().nullable(), guest: z.object({id: z.number().int(), resource_type: z.literal("Guest"), name: z.string(), email: z.string().nullable(), phone_number: z.string().nullable()}).optional(), issued_by_id: z.number().int().nullable(), type: z.literal("organization"), scope: z.literal("organization"), organization_id: z.number().int(), organization: z.object({id: z.number().int(), resource_type: z.literal("Organization"), name: z.string(), domain: z.string()})}), z.object({id: z.number().int(), resource_type: z.literal("RoleAssignment"), created_at: z.string(), updated_at: z.string(), role_id: z.string(), notify: z.boolean(), applies_to_type: z.literal("Place"), applies_to_id: z.number().int(), applies_to: z.union([z.object({id: z.number().int(), resource_type: z.literal("Organization"), name: z.string(), domain: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Group"), name: z.string()})]), assignee_type: z.enum(["User", "Team", "Guest"]), assignee_id: z.number().int(), assignee: 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("Team"), 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()})]), created_by_type: z.enum(["null", "User", "MarketplaceInstallation"]).nullable(), created_by_id: z.number().int().nullable(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), assignee_team_id: z.number().int().nullable(), assignee_team: z.object({id: z.number().int(), resource_type: z.literal("Team"), name: z.string()}).optional(), guest_id: z.number().int().nullable(), guest: z.object({id: z.number().int(), resource_type: z.literal("Guest"), name: z.string(), email: z.string().nullable(), phone_number: z.string().nullable()}).optional(), issued_by_id: z.number().int().nullable(), type: z.literal("place"), scope: z.literal("place"), place_id: z.number().int(), place: z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()})}), z.object({id: z.number().int(), resource_type: z.literal("RoleAssignment"), created_at: z.string(), updated_at: z.string(), role_id: z.string(), notify: z.boolean(), applies_to_type: z.literal("Group"), applies_to_id: z.number().int(), applies_to: z.union([z.object({id: z.number().int(), resource_type: z.literal("Organization"), name: z.string(), domain: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Place"), name: z.string()}), z.object({id: z.number().int(), resource_type: z.literal("Group"), name: z.string()})]), assignee_type: z.enum(["User", "Team", "Guest"]), assignee_id: z.number().int(), assignee: 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("Team"), 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()})]), created_by_type: z.enum(["null", "User", "MarketplaceInstallation"]).nullable(), created_by_id: z.number().int().nullable(), user_id: z.number().int().nullable(), user: z.object({id: z.number().int(), resource_type: z.literal("User"), email: z.string(), name: z.string()}).optional(), assignee_team_id: z.number().int().nullable(), assignee_team: z.object({id: z.number().int(), resource_type: z.literal("Team"), name: z.string()}).optional(), guest_id: z.number().int().nullable(), guest: z.object({id: z.number().int(), resource_type: z.literal("Guest"), name: z.string(), email: z.string().nullable(), phone_number: z.string().nullable()}).optional(), issued_by_id: z.number().int().nullable(), type: z.literal("group"), scope: z.literal("group"), group_id: z.number().int(), group: z.object({id: z.number().int(), resource_type: z.literal("Group"), name: z.string()}), valid_from: z.string().nullable(), valid_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 role assignment * * POST /role_assignments * * Also accepts auth: OAuth2 */ export const createApiRoleAssignments = async (args: CreateApiRoleAssignmentsArgs, options?: CreateApiRoleAssignmentsOptions) => { 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(/\/+$/, '')}/role_assignments${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(createApiRoleAssignmentsResponse, await res.json()) };