import {z} from 'zod' export type GetApiBulkHostnamesArgs = {page_token?: string | undefined, page_size?: number | undefined, sort_desc?: boolean | undefined, omit_scan_info?: boolean | undefined, omit_vendor?: boolean | undefined, omit_labels?: boolean | undefined, exclude_active?: boolean | undefined, exclude_inactive?: boolean | undefined, labels?: Array | undefined}; export type GetApiBulkHostnamesOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkRisk = z.object({category: z.enum(["website_sec", "email_sec", "network_sec", "ip_domain_reputation", "brand_protect", "attack_surface", "dns", "vulnerability_management", "data_leakage", "encryption", "questionnaire_risks", "vulns", "typosquatting", "emailexposures", "dataleaks", "additional_evidence"]).optional(), description: z.string().optional(), finding: z.string().optional(), id: z.string().optional(), risk: z.string().optional(), riskSubtype: z.string().optional(), riskType: z.string().optional(), severity: z.enum(["pass", "info", "low", "medium", "high", "critical"]).optional()}); export const bulkVendor = z.object({id: z.number().int().optional(), name: z.string().optional(), primary_hostname: z.string().optional()}); export const bulkHostname = z.object({active: z.boolean().optional(), automated_score: z.number().int().gte(0).lte(950).optional(), hostname: z.string().optional(), labels: z.array(z.string()).optional(), last_scanned_at: z.string().optional(), risks: z.array(bulkRisk).optional(), vendor: bulkVendor.optional()}); export const bulkListHostnamesV1ResponseBody = z.object({hostnames: z.array(bulkHostname).optional(), next_page_token: z.string().optional(), total_results: 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 }; /** * List hostnames and their risks * * GET /bulk/hostnames */ export const getApiBulkHostnames = async (args: GetApiBulkHostnamesArgs, options?: GetApiBulkHostnamesOptions) => { const {baseUrl = "https://cyber-risk.upguard.com/api/public", fetch: fetchFn = fetch, APIKeyInHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.sort_desc != null) query.append("sort_desc", String(args.sort_desc)) if (args.omit_scan_info != null) query.append("omit_scan_info", String(args.omit_scan_info)) if (args.omit_vendor != null) query.append("omit_vendor", String(args.omit_vendor)) if (args.omit_labels != null) query.append("omit_labels", String(args.omit_labels)) if (args.exclude_active != null) query.append("exclude_active", String(args.exclude_active)) if (args.exclude_inactive != null) query.append("exclude_inactive", String(args.exclude_inactive)) for (const item of args.labels ?? []) query.append("labels", String(item)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/bulk/hostnames${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(bulkListHostnamesV1ResponseBody, await res.json()) };