import {z} from 'zod' export type BetaAwsExternalKeyConfig = {kms_arn: string, region?: (string | null) | undefined, role_arn?: (string | null) | undefined, type: 'aws'}; export type BetaGcpExternalKeyConfig = {key_name: string, type: 'gcp'}; export type BetaAzureExternalKeyConfigParams = {client_id?: (string | null) | undefined, key_name: string, tenant_id: string, type: 'azure', vault_uri: string}; export type BetaExternalKeyUpdateParams = {display_name?: (string | null) | undefined, geo?: ('us' | null) | undefined, provider_config?: (BetaAwsExternalKeyConfig | BetaGcpExternalKeyConfig | BetaAzureExternalKeyConfigParams | null) | undefined}; export type CreateApiV1OrganizationsExternalKeysExternalKeyIdBetaTrueArgs = {external_key_id: string, 'anthropic-version'?: string | undefined, body: BetaExternalKeyUpdateParams}; export type CreateApiV1OrganizationsExternalKeysExternalKeyIdBetaTrueOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const betaAttachedAttachment = z.object({type: z.literal("attached")}); export const betaUnattachedAttachment = z.object({type: z.literal("unattached")}); export const betaAwsExternalKeyConfig = z.object({kms_arn: z.string().max(2048), region: z.string().nullable().optional(), role_arn: z.string().nullable().optional(), type: z.literal("aws")}); export const betaGcpExternalKeyConfig = z.object({key_name: z.string(), type: z.literal("gcp")}); export const betaAzureExternalKeyConfig = z.object({client_id: z.string().nullable().optional(), key_name: z.string(), tenant_id: z.string(), type: z.literal("azure"), vault_uri: z.string()}); export const betaExternalKey = z.object({attachment: z.discriminatedUnion("type", [betaAttachedAttachment, betaUnattachedAttachment]), created_at: z.string(), display_name: z.string().nullable(), geo: z.string(), id: z.string(), provider_config: z.discriminatedUnion("type", [betaAwsExternalKeyConfig, betaGcpExternalKeyConfig, betaAzureExternalKeyConfig]), type: z.literal("external_key"), updated_at: z.string(), user_profile_id: z.string().nullable()}); 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 }; /** * Update External Key * * POST /v1/organizations/external_keys/{external_key_id}?beta=true */ export const createApiV1OrganizationsExternalKeysExternalKeyIdBetaTrue = async (args: CreateApiV1OrganizationsExternalKeysExternalKeyIdBetaTrueArgs, options?: CreateApiV1OrganizationsExternalKeysExternalKeyIdBetaTrueOptions) => { const {baseUrl = "https://api.anthropic.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['anthropic-version'] != null) headers.set("anthropic-version", String(args['anthropic-version'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organizations/external_keys/${encodeURIComponent(String(args.external_key_id))}?beta=true${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(betaExternalKey, await res.json()) };