import {z} from 'zod' export type GetApiV1OrganizationRoleArgs = {organization: string, return_permission_grants?: boolean | undefined, id?: Array | undefined, name?: Array | undefined, 'x-mongo-cluster-name'?: (string | null) | undefined, 'Sec-WebSocket-Protocol'?: Array | undefined}; export type GetApiV1OrganizationRoleOptions = {baseUrl?: string | undefined, BearerAuthorization?: string | undefined, BearerAuthorizationOrganization?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const frontendView = z.enum(["client", "admin"]); export const equalCondition = z.object({type: z.literal("Equals").optional(), value: z.unknown()}); export const inCondition = z.object({type: z.literal("In").optional(), values: z.array(z.unknown())}); export const notEqualCondition = z.object({type: z.literal("NotEquals").optional(), value: z.unknown()}); export const condition = z.discriminatedUnion("type", [equalCondition, inCondition, notEqualCondition]); export const permissionGrantOutput = z.object({permission_name: z.string(), conditions: z.record(z.string(), condition)}); export const roleInstance = z.object({id: z.string(), name: z.string(), description: z.string(), frontend_view: frontendView, permission_grants: z.array(permissionGrantOutput)}); export const srcAppEndpointsRoleGetRolesResponse = z.object({roles: z.array(roleInstance)}); 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 roles * * GET /v1/{organization}/role/ */ export const getApiV1OrganizationRole = async (args: GetApiV1OrganizationRoleArgs, options?: GetApiV1OrganizationRoleOptions) => { const {baseUrl = "https://api.amigo.ai", fetch: fetchFn = fetch, BearerAuthorization, BearerAuthorizationOrganization, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.return_permission_grants != null) query.append("return_permission_grants", String(args.return_permission_grants)) for (const item of args.id ?? []) query.append("id", String(item)) for (const item of args.name ?? []) query.append("name", String(item)) if (args['x-mongo-cluster-name'] != null) headers.set("x-mongo-cluster-name", String(args['x-mongo-cluster-name'])) if (args['Sec-WebSocket-Protocol'] != null) headers.set("Sec-WebSocket-Protocol", args['Sec-WebSocket-Protocol'].join(',')) if (BearerAuthorization !== undefined) headers.set('authorization', `Bearer ${BearerAuthorization}`) if (BearerAuthorizationOrganization !== undefined) headers.set("X-ORG-ID", BearerAuthorizationOrganization) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.organization))}/role/${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(srcAppEndpointsRoleGetRolesResponse, await res.json()) };