import {z} from 'zod' export type GetApiHostIdArgs = {'X-Auth-Token': string, id: string}; export type GetApiHostIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rESTIPAddr = z.object({ip: z.string(), ip_prefix: z.number().int(), gateway: z.string()}); export const rESTScanBrief = z.object({status: z.string(), high: z.number().int(), medium: z.number().int(), result: z.string(), scanned_timestamp: z.number().int(), scanned_at: z.string(), base_os: z.string(), scanner_version: z.string(), cvedb_create_time: z.string()}); export const rESTHost = z.object({name: z.string(), id: z.string(), runtime: z.string(), runtime_version: z.string(), runtime_api_version: z.string(), platform: z.string(), os: z.string(), kernel: z.string(), cpus: z.number().int(), memory: z.number().int(), cgroup_version: z.number().int(), containers: z.number().int(), interfaces: z.object({key: z.string().optional(), value: z.array(rESTIPAddr).optional()}), state: z.string(), cap_docker_bench: z.boolean(), cap_kube_bench: z.boolean(), docker_bench_status: z.string().optional(), kube_bench_status: z.string().optional(), policy_mode: z.string(), profile_mode: z.string(), scan_summary: rESTScanBrief, storage_driver: z.string(), labels: z.object({key: z.array(z.string()).optional(), value: z.string().optional()}), annotations: z.object({key: z.array(z.string()).optional(), value: z.string().optional()})}); export const rESTHostData = z.object({host: rESTHost}); 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 }; /** * Show host * * GET /host/{id} */ export const getApiHostId = async (args: GetApiHostIdArgs, options: GetApiHostIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Auth-Token'] != null) headers.set("X-Auth-Token", String(args['X-Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/host/${encodeURIComponent(String(args.id))}${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(rESTHostData, await res.json()) };