import {z} from 'zod' export type GetApiApiV1SystemInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const modelProjectRef = z.object({name: z.string(), roles: z.array(z.string())}); export const v1StatisticInfo = z.object({appCount: z.string().optional(), clusterCount: z.string().optional(), componentDefinitionTopList: z.array(z.string()).optional(), enableAddonList: z.record(z.string(), z.string()).optional(), policyDefinitionTopList: z.array(z.string()).optional(), traitDefinitionTopList: z.array(z.string()).optional(), updateTime: z.string().optional(), workflowDefinitionTopList: z.array(z.string()).optional()}); export const v1SystemVersion = z.object({gitVersion: z.string(), velaVersion: z.string()}); export const v1SystemInfoResponse = z.object({dexUserDefaultPlatformRoles: z.array(z.string()).optional(), dexUserDefaultProjects: z.array(modelProjectRef).optional(), enableCollection: z.boolean(), installTime: z.string().optional(), loginType: z.string(), platformID: z.string(), statisticInfo: v1StatisticInfo.optional(), systemVersion: v1SystemVersion}); 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 }; /** * GET /api/v1/system_info */ export const getApiApiV1SystemInfo = async (options: GetApiApiV1SystemInfoOptions) => { 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/system_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(v1SystemInfoResponse, await res.json()) };