import {z} from 'zod' export type GetApiIpArgs = {ip: string}; export type GetApiIpOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const property = z.object({property: z.string().optional(), value: z.string().optional()}); export const category = z.string(); export const severity = z.number().int(); export const checkResult = z.object({actual: z.array(property).optional(), category: category.optional(), checked_at: z.string().optional(), description: z.string().optional(), expected: z.array(property).optional(), id: z.string().optional(), pass: z.boolean().optional(), riskSubtype: z.string().optional(), riskType: z.string().optional(), severity: severity.optional(), severityName: z.enum(["pass", "info", "low", "medium", "high", "critical"]).optional(), sources: z.array(z.string()).optional(), title: z.string().optional()}); export const domainScore = z.object({automated_score: z.number().int().optional(), hostname: z.string().optional()}); export const iPSource = z.string(); export const getIpDetailsV1RespBody = z.object({as_name: z.string().optional(), asn: z.number().int().optional(), automated_score: z.number().int().optional(), check_results: z.array(checkResult).optional(), country: z.string().optional(), domains: z.array(domainScore).optional(), ip: z.string().optional(), labels: z.array(z.string()).optional(), owner: z.string().optional(), services: z.array(z.string()).optional(), sources: z.array(iPSource).optional(), waived_check_results: z.array(checkResult).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 }; /** * Retrieve details for an IP address * * GET /ip */ export const getApiIp = async (args: GetApiIpArgs, options?: GetApiIpOptions) => { 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.ip != null) query.append("ip", String(args.ip)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/ip${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(getIpDetailsV1RespBody, await res.json()) };