import {z} from 'zod' export type CreateApiV1HostsFilterArgs = {offset?: number | undefined, limit?: number | undefined, body: {environments?: (Array | null) | undefined, hostnames?: (Array | null) | undefined, node_groups?: (Array | null) | undefined, operating_systems?: (Array | null) | undefined}}; export type CreateApiV1HostsFilterOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const compatiblebenchmark = z.object({id: z.number().int(), name: z.string()}); export const operatingSystem = z.object({compatible_benchmarks: z.array(compatiblebenchmark).nullable().optional(), id: z.number().int(), name: z.string()}); export const host = z.object({assessor_version: z.string().nullable().optional(), environment: z.string(), id: z.number().int(), name: z.string(), operating_system: operatingSystem}); export const createApiV1HostsFilterResponse = z.object({data: z.array(host), limit: z.number().int().optional(), next: z.string().nullable().optional(), offset: z.number().int().optional(), total: z.number().int()}); 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 hosts with filter * * POST /v1/hosts/filter */ export const createApiV1HostsFilter = async (args: CreateApiV1HostsFilterArgs, options: CreateApiV1HostsFilterOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/hosts/filter${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1HostsFilterResponse, await res.json()) };