import {z} from 'zod' export type GetApiV1ScansHistoryArgs = {offset?: number | undefined, limit?: number | undefined, start_date?: string | undefined, end_date?: string | undefined, time_range?: ('current_month' | 'last_month' | 'last_3_months' | 'last_6_months' | 'last_12_months' | 'all_time') | undefined, 'X-Team-Id'?: string | undefined}; export type GetApiV1ScansHistoryOptions = {baseUrl?: string | undefined, XAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const scansHistoryItems = z.object({completed_request: z.number().int().optional(), name: z.string().optional(), scan_id: z.string().optional(), scan_time: z.string().optional(), severity: z.object({}).optional(), status: z.string().optional(), time: z.string().optional(), total_request: z.number().int().optional(), total_result: z.number().int().optional(), total_target: z.number().int().optional(), total_templates: z.number().int().optional(), error_details: z.string().optional()}); export const getApiV1ScansHistoryResponse = z.object({message: z.string(), current_page: z.number().int(), result_count: z.number().int(), total_results: z.number().int(), total_pages: z.number().int(), data: z.array(scansHistoryItems), total_completed_request: z.number().int().optional(), total_scan_run: z.number().int().optional(), total_unique_scan_run: z.number().int().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 }; /** * Get All Scans History * * GET /v1/scans/history */ export const getApiV1ScansHistory = async (args: GetApiV1ScansHistoryArgs, options?: GetApiV1ScansHistoryOptions) => { const {baseUrl = "https://api.projectdiscovery.io", fetch: fetchFn = fetch, XAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.time_range != null) query.append("time_range", String(args.time_range)) if (args['X-Team-Id'] != null) headers.set("X-Team-Id", String(args['X-Team-Id'])) if (XAPIKey !== undefined) headers.set("X-API-Key", XAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/scans/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(getApiV1ScansHistoryResponse, await res.json()) };