import {z} from 'zod' export type GetApiRisksVendorsHostnamesArgs = {risk_id: string, vendor_id?: number | undefined, vendor_primary_hostname?: string | undefined}; export type GetApiRisksVendorsHostnamesOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const externalHostnameRisk = z.object({date_detected: z.string().optional(), hostname: z.string().optional(), is_custom: z.boolean().optional(), risk_id: z.string().optional(), scanned_at: z.string().optional(), score: z.number().int().optional()}); export const externalVendorHostnamesWithRiskResponse = z.object({hostnames_with_risk: z.array(z.string()).optional(), risk_details_by_hostname: z.record(z.string(), externalHostnameRisk).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 vendor hostnames affected by a risk * * GET /risks/vendors/hostnames */ export const getApiRisksVendorsHostnames = async (args: GetApiRisksVendorsHostnamesArgs, options?: GetApiRisksVendorsHostnamesOptions) => { 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.risk_id != null) query.append("risk_id", String(args.risk_id)) if (args.vendor_id != null) query.append("vendor_id", String(args.vendor_id)) if (args.vendor_primary_hostname != null) query.append("vendor_primary_hostname", String(args.vendor_primary_hostname)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/risks/vendors/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(externalVendorHostnamesWithRiskResponse, await res.json()) };