import {z} from 'zod' export type GetApiIamRoleBindingsArgs = {user_id?: string | undefined, role_id?: string | undefined, scope_type?: ('org' | 'project') | undefined, scope_object_id?: string | undefined}; export type GetApiIamRoleBindingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const roleBinding = z.object({customer_id: z.string(), id: z.string(), role_id: z.string(), role_name: z.string(), scope_object_id: z.string(), scope_type: z.string()}); export const listRoleBindingsResponse = z.object({role_bindings: z.array(roleBinding)}); 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 }; /** * Lists role bindings visible to the caller. * * GET /iam/role-bindings */ export const getApiIamRoleBindings = async (args: GetApiIamRoleBindingsArgs, options?: GetApiIamRoleBindingsOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.role_id != null) query.append("role_id", String(args.role_id)) if (args.scope_type != null) query.append("scope_type", String(args.scope_type)) if (args.scope_object_id != null) query.append("scope_object_id", String(args.scope_object_id)) const url = `${baseUrl.replace(/\/+$/, '')}/iam/role-bindings${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(listRoleBindingsResponse, await res.json()) };