import {z} from 'zod' export type GetApiTokensOptions = {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 aPIToken = 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()}); export const getApiTokensResponse = z.array(aPIToken); 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 }; /** * Lists the organization's active API tokens. Expired tokens, personal access tokens, and token secret values aren't included. * * GET /tokens */ export const getApiTokens = async (options?: GetApiTokensOptions) => { 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(/\/+$/, '')}/tokens${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(getApiTokensResponse, await res.json()) };