import {z} from 'zod' export type GetApiRoleAssignmentsArgs = {ids?: string | undefined, query?: string | undefined, limit?: number | undefined, offset?: number | undefined, role_id?: string | undefined, assignee_type?: ('User' | 'Team' | 'Guest') | undefined, assignee_id?: number | undefined, applies_to_type?: ('Organization' | 'Place' | 'Group') | undefined, applies_to_id?: number | undefined, scope?: ('organization' | 'place' | 'group' | 'team') | undefined, user_id?: (number | string) | undefined, place_id?: number | undefined, group_id?: number | undefined, sort?: ('user.name' | '-user.name') | undefined}; export type GetApiRoleAssignmentsOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRoleAssignmentsResponse = z.array(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 }; /** * Fetch role assignments * * GET /role_assignments * * Also accepts auth: OAuth2 */ export const getApiRoleAssignments = async (args: GetApiRoleAssignmentsArgs, options?: GetApiRoleAssignmentsOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.role_id != null) query.append("role_id", String(args.role_id)) if (args.assignee_type != null) query.append("assignee_type", String(args.assignee_type)) if (args.assignee_id != null) query.append("assignee_id", String(args.assignee_id)) if (args.applies_to_type != null) query.append("applies_to_type", String(args.applies_to_type)) if (args.applies_to_id != null) query.append("applies_to_id", String(args.applies_to_id)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.place_id != null) query.append("place_id", String(args.place_id)) if (args.group_id != null) query.append("group_id", String(args.group_id)) if (args.sort != null) query.append("sort", String(args.sort)) if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) const url = `${baseUrl.replace(/\/+$/, '')}/role_assignments${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(getApiRoleAssignmentsResponse, await res.json()) };