import {z} from 'zod' export type CreateApiApiV1ContextIdKeysKeyNameRotateArgs = {context_id: string, key_name: string, ttlSeconds?: (number | null) | undefined}; export type CreateApiApiV1ContextIdKeysKeyNameRotateOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mintedKeyJson = z.object({id: z.string(), key: z.string(), validUntil: z.string().nullable().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 }; /** * Rotate the secret on the caller's own key (or, for a context administrator holding `grant:manage` over `/`, any key). The id stays stable (preserving the principal binding and attenuating grants); the previous secret stops validating immediately. A key the caller may not manage surfaces as 404. `ttlSeconds` is reset-or-inherit, matching the management rotate path. * * POST /api/v1/{context_id}/keys/{key_name}/rotate */ export const createApiApiV1ContextIdKeysKeyNameRotate = async (args: CreateApiApiV1ContextIdKeysKeyNameRotateArgs, options: CreateApiApiV1ContextIdKeysKeyNameRotateOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ttlSeconds != null) query.append("ttlSeconds", String(args.ttlSeconds)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/${encodeURIComponent(String(args.context_id))}/keys/${encodeURIComponent(String(args.key_name))}/rotate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(mintedKeyJson, await res.json()) };