import {z} from 'zod' export type GetApiKapisResourcesKubesphereIoV1alpha2ComponenthealthOptions = {baseUrl: string, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v1alpha2ComponentStatusLabel = z.unknown(); export const v1alpha2ComponentStatus = z.object({healthyBackends: z.number().int(), label: v1alpha2ComponentStatusLabel, name: z.string(), namespace: z.string(), selfLink: z.string(), startedAt: z.string(), totalBackends: z.number().int()}); export const v1alpha2NodeStatus = z.object({healthyNodes: z.number().int(), totalNodes: z.number().int()}); export const v1alpha2HealthStatus = z.object({kubesphereStatus: z.array(v1alpha2ComponentStatus), nodeStatus: v1alpha2NodeStatus}); 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 the health status of system components * * GET /kapis/resources.kubesphere.io/v1alpha2/componenthealth */ export const getApiKapisResourcesKubesphereIoV1alpha2Componenthealth = async (options: GetApiKapisResourcesKubesphereIoV1alpha2ComponenthealthOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerToken !== undefined) headers.set("Authorization", BearerToken) const url = `${baseUrl.replace(/\/+$/, '')}/kapis/resources.kubesphere.io/v1alpha2/componenthealth${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(v1alpha2HealthStatus, await res.json()) };