import {z} from 'zod' export type GetApiRbacRolesIdArgs = {id: string}; export type GetApiRbacRolesIdOptions = {baseUrl?: string | undefined, Auth?: string | undefined, fetch?: typeof fetch | undefined}; export const roleDatasetCapabilities = z.record(z.string(), z.object({ingest: z.array(z.literal("create")).optional(), query: z.array(z.literal("read")).optional(), share: z.array(z.enum(["create", "read", "delete"])).optional(), starredQueries: z.array(z.enum(["create", "read", "update", "delete"])).optional(), trim: z.array(z.literal("update")).optional(), vacuum: z.array(z.literal("update")).optional(), virtualFields: z.array(z.enum(["create", "read", "update", "delete"])).optional()})); export const roleOrgCapabilities = z.object({annotations: z.array(z.enum(["create", "read", "update", "delete"])).optional(), apiTokens: z.array(z.enum(["create", "read", "update", "delete"])).optional(), auditLog: z.array(z.literal("read")).optional(), billing: z.array(z.enum(["read", "update"])).optional(), dashboards: z.array(z.enum(["create", "read", "update", "delete"])).optional(), datasets: z.array(z.enum(["create", "read", "update", "delete"])).optional(), endpoints: z.array(z.enum(["create", "read", "update", "delete"])).optional(), integrations: z.array(z.enum(["create", "read", "update", "delete"])).optional(), labels: z.array(z.enum(["create", "read", "update", "delete"])).optional(), monitors: z.array(z.enum(["create", "read", "update", "delete"])).optional(), notifiers: z.array(z.enum(["create", "read", "update", "delete"])).optional(), rbac: z.array(z.enum(["create", "read", "update", "delete"])).optional(), sharedAccessKeys: z.array(z.enum(["read", "update"])).optional(), users: z.array(z.enum(["create", "read", "update", "delete"])).optional(), views: z.array(z.enum(["create", "read", "update", "delete"])).optional()}); export const roleViewCapabilities = z.record(z.string(), z.object({query: z.array(z.literal("read")).optional(), share: z.array(z.enum(["create", "read", "delete"])).optional()})); export const roleWithID = z.object({datasetCapabilities: roleDatasetCapabilities.optional(), description: z.string().optional(), members: z.array(z.string()).optional(), name: z.string(), orgCapabilities: roleOrgCapabilities.optional(), viewCapabilities: roleViewCapabilities.optional(), 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 role by ID * * GET /rbac/roles/{id} */ export const getApiRbacRolesId = async (args: GetApiRbacRolesIdArgs, options?: GetApiRbacRolesIdOptions) => { const {baseUrl = "https://api.axiom.co/v2", fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/rbac/roles/${encodeURIComponent(String(args.id))}${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(roleWithID, await res.json()) };