import {z} from 'zod' export type IamV2JwksSpec = {jwks_status?: string | undefined}; export type IamV2JwksStatus = {jwks_status?: string | undefined, jwks_last_refresh_at?: string | undefined}; export type IamV2Jwks = {api_version?: 'iam/v2' | undefined, kind?: 'Jwks' | undefined, spec?: IamV2JwksSpec | undefined, status?: IamV2JwksStatus | undefined}; export type PatchApiIamV2IdentityProvidersProviderIdJwksArgs = {provider_id: string, body: IamV2Jwks}; export type PatchApiIamV2IdentityProvidersProviderIdJwksOptions = {baseUrl?: string | undefined, cloudApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const iamV2JwksStatus = z.object({jwks_status: z.string().optional(), jwks_last_refresh_at: z.string().optional()}); export const patchApiIamV2IdentityProvidersProviderIdJwksResponse = z.object({api_version: z.literal("iam/v2"), kind: z.literal("Jwks"), spec: z.object({jwks_status: z.string()}), status: iamV2JwksStatus.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 }; /** * Refresh a provider's JWKS * * PATCH /iam/v2/identity-providers/{provider_id}/jwks * * Also accepts auth: confluent-sts-access-token */ export const patchApiIamV2IdentityProvidersProviderIdJwks = async (args: PatchApiIamV2IdentityProvidersProviderIdJwksArgs, options?: PatchApiIamV2IdentityProvidersProviderIdJwksOptions) => { const {baseUrl = "https://api.confluent.cloud", fetch: fetchFn = fetch, cloudApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (cloudApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${cloudApiKey.username}:${cloudApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/iam/v2/identity-providers/${encodeURIComponent(String(args.provider_id))}/jwks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiIamV2IdentityProvidersProviderIdJwksResponse, await res.json()) };