import {z} from 'zod' export type AdminCreateAPIKeyIN = {name?: (string | null) | undefined, expiration?: (string | null) | undefined, workspace_uuid: string, user_id: string}; export type CreateApiV1AdminApiKeysArgs = {body: AdminCreateAPIKeyIN}; export type CreateApiV1AdminApiKeysOptions = {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 aPIKeyOUT = 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}); 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 Api Key * * POST /v1/admin/api-keys */ export const createApiV1AdminApiKeys = async (args: CreateApiV1AdminApiKeysArgs, options?: CreateApiV1AdminApiKeysOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, AdminApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(aPIKeyOUT, await res.json()) };