import {z} from 'zod' export type GetApiApi3ScansIdArgs = {id: number}; export type GetApiApi3ScansIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const engineIDSchema = z.object({id: z.number().int().optional(), newScanEngine: z.boolean().optional(), scope: z.enum(["global", "silo"]).optional()}); export const link = z.object({deprecation: z.string().optional(), href: z.string().optional(), hreflang: z.string().optional(), media: z.string().optional(), rel: z.string().optional(), title: z.string().optional(), type: z.string().optional()}); export const scanStatus = z.enum(["aborted", "unknown", "running", "finished", "stopped", "error", "paused", "dispatched", "integrating"]); export const vulnerabilities = z.object({critical: z.number().int().optional(), moderate: z.number().int().optional(), severe: z.number().int().optional(), total: z.number().int().optional()}); export const scan = z.object({assets: z.number().int().optional(), duration: z.string().optional(), endTime: z.string().optional(), engineId: z.object({}).optional(), engineIds: engineIDSchema.optional(), engineName: z.string().optional(), id: z.object({}).optional(), links: z.array(link), message: z.string().optional(), scanName: z.string().optional(), scanType: z.string().optional(), startTime: z.string().optional(), startedBy: z.string().optional(), startedByUsername: z.string().optional(), status: scanStatus.optional(), vulnerabilities: vulnerabilities.optional()}); 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 }; /** * Scan * * GET /api/3/scans/{id} */ export const getApiApi3ScansId = async (args: GetApiApi3ScansIdArgs, options?: GetApiApi3ScansIdOptions) => { const {baseUrl = "https://:", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;charset=UTF-8") const url = `${baseUrl.replace(/\/+$/, '')}/api/3/scans/${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(scan, await res.json()) };