import { confirm, log } from "@clack/prompts" import { defineCommand } from "../lib/command" import { globalOptions, globalOptionsSchema } from "../lib/global-options" import z from "zod" import { apiClient } from "../lib/api-client" import { accent, dimmed } from "../lib/io" import { clearSpinner, startSpinner } from "../lib/spinner" import { API_KEY_OPT, apiKeyOptsSchema, ORG_OPT, output, requireSession, safelyPrompt, sessionIntro, useOrganization, useOrganizationApiKey, ORG_OPTION_DESCRIPTIONS, API_KEY_OPTION_DESCRIPTIONS, } from "../utils" /** Validates API key selection and destructive confirmation options. */ const deleteApiKeyOptsSchema = apiKeyOptsSchema.extend({ yes: z.boolean().default(false), }) export const orgApiKeyDeleteCommand = defineCommand({ name: "delete", description: "Delete an organization API key", options: { ...globalOptions, ...API_KEY_OPT, ...ORG_OPT, yes: { type: "boolean", short: "y", default: false, }, }, optionDescriptions: { ...API_KEY_OPTION_DESCRIPTIONS, ...ORG_OPTION_DESCRIPTIONS, yes: "Skip confirmation prompt", }, schema: globalOptionsSchema.extend(deleteApiKeyOptsSchema.shape), run: deleteOrganizationApiKey, }) /** * Permanently revokes an organization API key after confirmation. * * @param opts - Organization, API key, and confirmation options. */ export async function deleteOrganizationApiKey( opts: z.infer, ) { const session = await requireSession("delete an organization API key") output.normal(() => sessionIntro(session)) const organization = await useOrganization(opts) const apiKey = await useOrganizationApiKey(organization.id, opts) if ( !opts.yes && !(await safelyPrompt( () => confirm({ message: `Are you sure you want to delete ${accent(apiKey.name ?? apiKey.start ?? apiKey.id)}? This cannot be undone.`, initialValue: false, }), "--yes", )) ) { output.json({ cancelled: true }) return } startSpinner("Deleting API key") await apiClient.apiKey.delete({ keyId: apiKey.id, }) clearSpinner() output .normal(() => log.success( `Deleted ${accent(apiKey.name ?? "API key")} ${dimmed(apiKey.id)}`, ), ) .json({ apiKey, deleted: true }) }