import {z} from 'zod' export type ScanFilters = {environments?: (Array | null) | undefined, hostnames?: (Array | null) | undefined, node_groups?: (Array | null) | undefined, operating_systems?: (Array | null) | undefined}; export type CreateApiV1CustomScanArgs = {body: {benchmark_id: number, custom_profile_id?: (number | null) | undefined, profile_id?: (number | null) | undefined, scan_filters?: ScanFilters | undefined, scan_type: 'custom'}}; export type CreateApiV1CustomScanOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1CustomScanResponse = z.object({created_at: z.string(), errored: z.number().int(), failed: z.number().int(), finished: z.number().int(), finished_at: z.string().nullable().optional(), id: z.string(), name: z.string(), nodes_scanned: z.number().int(), status: z.enum(["started", "finished", "failed"]), type: z.string()}); 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 }; /** * Create an ad-hoc custom scan * * POST /v1/custom-scan */ export const createApiV1CustomScan = async (args: CreateApiV1CustomScanArgs, options: CreateApiV1CustomScanOptions) => { 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 (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/custom-scan${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(createApiV1CustomScanResponse, await res.json()) };