import {z} from 'zod' export type GetApiV1AdminApiKeysArgs = {limit?: number | undefined, offset?: number | undefined, name?: (string | null) | undefined}; export type GetApiV1AdminApiKeysOptions = {baseUrl?: string | undefined, AdminApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const actionAvailable = z.object({status: z.literal("available").optional()}); export const rotateUnavailableReason = z.enum(["unsupported_scope", "key_expired", "not_allowed"]); export const actionUnavailableRotateUnavailableReason = z.object({status: z.literal("unavailable").optional(), reason: rotateUnavailableReason.nullable().optional()}); export const deleteUnavailableReason = z.literal("not_allowed"); export const actionUnavailableDeleteUnavailableReason = z.object({status: z.literal("unavailable").optional(), reason: deleteUnavailableReason.nullable().optional()}); export const aPIKeyActions = z.object({rotate: z.union([actionAvailable, actionUnavailableRotateUnavailableReason]).nullable().optional(), delete: z.union([actionAvailable, actionUnavailableDeleteUnavailableReason]).nullable().optional()}); export const aPIKeyProduct = z.enum(["API", "Mistral Code", "Vibe"]); export const aPIKeyExtendedOUT = z.object({key_id: z.string(), key: z.string().nullable(), name: z.string().nullable(), hidden_key: z.string(), created_at: z.string().nullable(), expiration_date: z.string().nullable(), actions: aPIKeyActions, workspace_id: z.string().nullable(), workspace_name: z.string().nullable(), created_by: z.string().nullable(), product: aPIKeyProduct.nullable(), last_used: z.string().nullable(), can_delete: z.boolean().nullable()}); export const aPIKeysExtendedOUT = z.object({keys: z.array(aPIKeyExtendedOUT)}); 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 Api Keys * * GET /v1/admin/api-keys */ export const getApiV1AdminApiKeys = async (args: GetApiV1AdminApiKeysArgs, options?: GetApiV1AdminApiKeysOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, AdminApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.name != null) query.append("name", String(args.name)) if (AdminApiKey !== undefined) headers.set('authorization', `Bearer ${AdminApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/api-keys${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(aPIKeysExtendedOUT, await res.json()) };