import {z} from 'zod' export type RoleDatasetCapabilities = {[key: string]: {ingest?: Array<'create'> | undefined, query?: Array<'read'> | undefined, share?: (Array<'create' | 'read' | 'delete'>) | undefined, starredQueries?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, trim?: Array<'update'> | undefined, vacuum?: Array<'update'> | undefined, virtualFields?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined}}; export type RoleOrgCapabilities = {annotations?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, apiTokens?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, auditLog?: Array<'read'> | undefined, billing?: (Array<'read' | 'update'>) | undefined, dashboards?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, datasets?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, endpoints?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, integrations?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, labels?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, monitors?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, notifiers?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, rbac?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, sharedAccessKeys?: (Array<'read' | 'update'>) | undefined, users?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined, views?: (Array<'create' | 'read' | 'update' | 'delete'>) | undefined}; export type RoleViewCapabilities = {[key: string]: {query?: Array<'read'> | undefined, share?: (Array<'create' | 'read' | 'delete'>) | undefined}}; export type Role = {datasetCapabilities?: RoleDatasetCapabilities | undefined, description?: string | undefined, members?: Array | undefined, name: string, orgCapabilities?: RoleOrgCapabilities | undefined, viewCapabilities?: RoleViewCapabilities | undefined}; export type CreateApiRbacRolesArgs = {body: Role}; export type CreateApiRbacRolesOptions = {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 }; /** * Create role * * POST /rbac/roles */ export const createApiRbacRoles = async (args: CreateApiRbacRolesArgs, options?: CreateApiRbacRolesOptions) => { const {baseUrl = "https://api.axiom.co/v2", fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/rbac/roles${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) 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()) };