import {z} from 'zod' export type GetApiApiV1InfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const serverDeploymentType = z.enum(["local", "docker", "kubernetes", "aws", "gcp", "azure", "alpha", "other", "hf_spaces", "sandbox", "cloud"]); export const serverDatabaseType = z.enum(["sqlite", "mysql", "other"]); export const secretsStoreType = z.enum(["none", "sql", "aws", "gcp", "azure", "hashicorp", "custom"]); export const authScheme = z.enum(["NO_AUTH", "HTTP_BASIC", "OAUTH2_PASSWORD_BEARER", "EXTERNAL"]); export const serverModel = z.object({id: z.string().optional(), version: z.string(), active: z.boolean().optional(), debug: z.boolean().optional(), deployment_type: serverDeploymentType.optional(), database_type: serverDatabaseType.optional(), secrets_store_type: secretsStoreType.optional(), auth_scheme: authScheme, server_url: z.string().optional(), dashboard_url: z.string().optional(), analytics_enabled: z.boolean().optional(), metadata: z.record(z.string(), z.string()).optional(), use_legacy_dashboard: z.boolean().optional(), last_user_activity: 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 }; /** * Server Info * * GET /api/v1/info */ export const getApiApiV1Info = async (options: GetApiApiV1InfoOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/info${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(serverModel, await res.json()) };