import {z} from 'zod' export type GetApiApiGovernanceProvidersOptions = {baseUrl?: string | undefined, ManagementBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const budget = z.object({id: z.string(), max_limit: z.number(), reset_duration: z.string(), calendar_aligned: z.boolean().optional(), last_reset: z.string(), current_usage: z.number(), team_id: z.string().optional(), virtual_key_id: z.string().optional(), provider_config_id: z.number().int().optional(), config_hash: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const rateLimit = z.object({id: z.string(), token_max_limit: z.number().int().optional(), token_reset_duration: z.string().optional(), token_current_usage: z.number().int(), token_last_reset: z.string(), request_max_limit: z.number().int().nullable().optional(), request_reset_duration: z.string().nullable().optional(), request_current_usage: z.number().int(), request_last_reset: z.string(), config_hash: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const providerGovernanceResponse = z.object({provider: z.string().optional(), budgets: z.array(budget).optional(), budget: budget.optional(), rate_limit: rateLimit.optional(), calendar_aligned: z.boolean().optional()}); export const listProviderGovernanceResponse = z.object({providers: z.array(providerGovernanceResponse).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 provider governance * * GET /api/governance/providers */ export const getApiApiGovernanceProviders = async (options?: GetApiApiGovernanceProvidersOptions) => { 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/governance/providers${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(listProviderGovernanceResponse, await res.json()) };