import {z} from 'zod' export type PrivateTemplates = {name: string, contents: string}; export type EnumerationIds = {id: string}; export type TimeRangeQueryParameter = 'last_day' | 'last_week' | 'last_month' | 'last_3_months' | 'last_6_months' | 'last_12_months' | 'all_time'; export type AssetFilters = {is_tech?: boolean | undefined, is_favicon?: boolean | undefined, is_new?: boolean | undefined, labels?: string | undefined, host?: string | undefined, port?: string | undefined, status_code?: string | undefined, content_length?: string | undefined, title?: string | undefined, domain?: Array | undefined, cname?: string | undefined, technologies?: string | undefined, ip?: string | undefined, is_screenshot?: boolean | undefined, time?: TimeRangeQueryParameter | undefined, start_date?: string | undefined, end_date?: string | undefined, custom_filter?: string | undefined, search?: string | undefined, enumeration_ids?: Array | undefined, only_dns?: boolean | undefined, only_ip?: boolean | undefined, not_seen_for_days?: number | undefined}; export type CreateApiV1ScansArgs = {'X-Team-Id'?: string | undefined, body: {targets?: Array | undefined, templates?: Array | undefined, assets?: Array | undefined, private_templates?: Array | undefined, name?: string | undefined, recommended?: boolean | undefined, all?: boolean | undefined, alerting_config_ids?: Array | undefined, scan_config_ids?: Array | undefined, reporting_config_ids?: Array | undefined, early_templates?: Array | undefined, exclude_targets?: Array | undefined, enumeration_ids?: Array | undefined, schedule_only?: boolean | undefined, disable_global_alert_config?: boolean | undefined, disable_global_report_config?: boolean | undefined, disable_global_scan_config?: boolean | undefined, agent_id?: string | undefined, agent_tags?: Array | undefined, agent_networks?: Array | undefined, agent_behavior?: ('distribute' | 'mirror') | undefined, automatic_scan?: boolean | undefined, scan_all_assets?: boolean | undefined, exclusions?: Array | undefined, asset_filters?: AssetFilters | undefined}}; export type CreateApiV1ScansOptions = {baseUrl?: string | undefined, XAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1ScansResponse = z.object({id: z.string().optional(), message: z.string(), "truncated-scan-targets": 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 }; /** * Create Scan * * POST /v1/scans */ export const createApiV1Scans = async (args: CreateApiV1ScansArgs, options?: CreateApiV1ScansOptions) => { const {baseUrl = "https://api.projectdiscovery.io", fetch: fetchFn = fetch, XAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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${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(createApiV1ScansResponse, await res.json()) };