import {z} from 'zod' export type ProjectRelation = 'project_admin' | 'security_admin' | 'data_admin' | 'role_creator' | 'describe' | 'select' | 'create' | 'modify'; export type GetApiManagementV1PermissionsProjectAssignmentsArgs = {relations?: Array | undefined}; export type GetApiManagementV1PermissionsProjectAssignmentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const projectAssignment = z.union([z.object({user: z.string(), type: z.literal("project_admin")}), z.object({role: z.string(), type: z.literal("project_admin")}), z.object({user: z.string(), type: z.literal("security_admin")}), z.object({role: z.string(), type: z.literal("security_admin")}), z.object({user: z.string(), type: z.literal("data_admin")}), z.object({role: z.string(), type: z.literal("data_admin")}), z.object({user: z.string(), type: z.literal("role_creator")}), z.object({role: z.string(), type: z.literal("role_creator")}), z.object({user: z.string(), type: z.literal("describe")}), z.object({role: z.string(), type: z.literal("describe")}), z.object({user: z.string(), type: z.literal("select")}), z.object({role: z.string(), type: z.literal("select")}), z.object({user: z.string(), type: z.literal("create")}), z.object({role: z.string(), type: z.literal("create")}), z.object({user: z.string(), type: z.literal("modify")}), z.object({role: z.string(), type: z.literal("modify")})]); export const getProjectAssignmentsResponse = z.object({assignments: z.array(projectAssignment), "project-id": z.string()}); 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 }; /** * Get user and role assignments of a project * * GET /management/v1/permissions/project/assignments */ export const getApiManagementV1PermissionsProjectAssignments = async (args: GetApiManagementV1PermissionsProjectAssignmentsArgs, options?: GetApiManagementV1PermissionsProjectAssignmentsOptions) => { const {baseUrl = "https://localhost", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.relations ?? []) query.append("relations", String(item)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/management/v1/permissions/project/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(getProjectAssignmentsResponse, await res.json()) };