import {z} from 'zod' export type GetApiApiCircuitBreakerPoliciesOptions = {baseUrl?: string | undefined, ManagementBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiCircuitBreakerPoliciesResponse = z.object({policies: z.array(z.object({name: z.string(), enabled: z.boolean().optional(), primary_provider: z.string(), primary_model: z.string(), primary_key_ids: z.array(z.string()).optional(), fallback_provider: z.string(), fallback_model: z.string(), condition: z.object({operator: z.enum(["OR", "AND"]).optional(), signals: z.array(z.object({source: z.literal("response_header"), header_name: z.string(), header_value: z.string().optional(), header_contains: z.string().optional()}))}), default_cooldown: z.union([z.string(), z.number().int()]).optional(), cooldown_header: z.string().optional()})).optional(), count: z.number().int().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 }; /** * List circuit breaker policies * * GET /api/circuit-breaker/policies */ export const getApiApiCircuitBreakerPolicies = async (options?: GetApiApiCircuitBreakerPoliciesOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, ManagementBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ManagementBearerAuth !== undefined) headers.set('authorization', `Bearer ${ManagementBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/circuit-breaker/policies${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiCircuitBreakerPoliciesResponse, await res.json()) };