import {z} from 'zod' export type DatasetCapabilities = {[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 OrgCapabilities = {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 ViewCapabilities = {[key: string]: {query?: Array<'read'> | undefined, share?: (Array<'create' | 'read' | 'delete'>) | undefined}}; export type CreateAPIToken = {datasetCapabilities?: DatasetCapabilities | undefined, description?: string | undefined, expiresAt?: (string | null) | undefined, name: string, orgCapabilities?: OrgCapabilities | undefined, viewCapabilities?: ViewCapabilities | undefined}; export type CreateApiTokensArgs = {body: CreateAPIToken}; export type CreateApiTokensOptions = {baseUrl?: string | undefined, Auth?: string | undefined, fetch?: typeof fetch | undefined}; export const datasetCapabilities = 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 orgCapabilities = 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 viewCapabilities = z.record(z.string(), z.object({query: z.array(z.literal("read")).optional(), share: z.array(z.enum(["create", "read", "delete"])).optional()})); export const createAPITokenResponse = z.object({apiLastUsed: z.string().nullable().optional(), auditIdentity: z.string().optional(), datasetCapabilities: datasetCapabilities, description: z.string().optional(), expiresAt: z.string().nullable().optional(), id: z.string(), ingestLastUsed: z.string().nullable().optional(), name: z.string(), orgCapabilities: orgCapabilities, samlAuthenticated: z.boolean().optional(), viewCapabilities: viewCapabilities.optional(), token: z.string().optional()}); 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 }; /** * Creates an API token and returns its secret `token` value, which you can't retrieve again. You can only grant capabilities you have yourself. If you omit `expiresAt`, the token doesn't expire. * * POST /tokens */ export const createApiTokens = async (args: CreateApiTokensArgs, options?: CreateApiTokensOptions) => { 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(/\/+$/, '')}/tokens${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(createAPITokenResponse, await res.json()) };