import {z} from 'zod' export type CreateDekRequest = {subject?: string | undefined, version?: number | undefined, algorithm?: ('AES128_GCM' | 'AES256_GCM' | 'AES256_SIV') | undefined, encryptedKeyMaterial?: string | undefined, deleted?: boolean | undefined}; export type CreateApiDekRegistryV1KeksNameDeksArgs = {name: string, body: CreateDekRequest}; export type CreateApiDekRegistryV1KeksNameDeksOptions = {baseUrl?: string | undefined, resourceApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const dek = z.object({kekName: z.string().optional(), subject: z.string().optional(), version: z.number().int().optional(), algorithm: z.enum(["AES128_GCM", "AES256_GCM", "AES256_SIV"]).optional(), encryptedKeyMaterial: z.string().optional(), keyMaterial: z.string().optional(), ts: z.number().int().optional(), deleted: z.boolean().optional()}); 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 a dek * * POST /dek-registry/v1/keks/{name}/deks * * Also accepts auth: external-access-token */ export const createApiDekRegistryV1KeksNameDeks = async (args: CreateApiDekRegistryV1KeksNameDeksArgs, options?: CreateApiDekRegistryV1KeksNameDeksOptions) => { const {baseUrl = "https://api.confluent.cloud", fetch: fetchFn = fetch, resourceApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.schemaregistry.v1+json") if (resourceApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${resourceApiKey.username}:${resourceApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/dek-registry/v1/keks/${encodeURIComponent(String(args.name))}/deks${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(dek, await res.json()) };