import {z} from 'zod' export type GetApiV1ScansVulnsHistoryArgs = {vuln_hash?: Array | undefined, history_limit?: number | undefined}; export type GetApiV1ScansVulnsHistoryOptions = {baseUrl?: string | undefined, XAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const vulnerabilityHistory = z.object({history_count: z.number().int(), history: z.array(z.object({severity: z.string().optional(), target: z.string().optional(), template_id: z.string().optional(), matched: z.boolean().optional(), timestamp: z.string().optional(), vuln_hash: z.string().optional(), event: z.string().optional()}))}); export const vulnerabilityHistoryDetail = z.record(z.string(), vulnerabilityHistory); 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 scan log stats of given scan id * * GET /v1/scans/vulns/history */ export const getApiV1ScansVulnsHistory = async (args: GetApiV1ScansVulnsHistoryArgs, options?: GetApiV1ScansVulnsHistoryOptions) => { const {baseUrl = "https://api.projectdiscovery.io", fetch: fetchFn = fetch, XAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.vuln_hash ?? []) query.append("vuln_hash", String(item)) if (args.history_limit != null) query.append("history_limit", String(args.history_limit)) if (XAPIKey !== undefined) headers.set("X-API-Key", XAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/scans/vulns/history${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(vulnerabilityHistoryDetail, await res.json()) };