import {z} from 'zod' export type GetApiBulkHostnamesHostnameArgs = {hostname: string, omit_scan_info?: boolean | undefined, omit_vendor?: boolean | undefined, omit_labels?: boolean | undefined}; export type GetApiBulkHostnamesHostnameOptions = {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 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 the details for a hostname * * GET /bulk/hostnames/{hostname} */ export const getApiBulkHostnamesHostname = async (args: GetApiBulkHostnamesHostnameArgs, options?: GetApiBulkHostnamesHostnameOptions) => { 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.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 (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/bulk/hostnames/${encodeURIComponent(String(args.hostname))}${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(bulkHostname, await res.json()) };