import {z} from 'zod' export type CreateApiFilesSha256AnalyseArgs = {sha256: string, disable_sandbox?: string | undefined, enable_internet?: string | undefined, intercept_tls?: string | undefined, command_line?: string | undefined, interaction_sandbox?: string | undefined, interaction_timeout?: number | undefined, 'x-apikey': string}; export type CreateApiFilesSha256AnalyseOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiFilesSha256AnalyseResponse = z.object({}); 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 }; /** * Rescan a private file * * POST /files/{sha256}/analyse */ export const createApiFilesSha256Analyse = async (args: CreateApiFilesSha256AnalyseArgs, options?: CreateApiFilesSha256AnalyseOptions) => { const {baseUrl = "https://www.virustotal.com/api/v3/private", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.disable_sandbox != null) query.append("disable_sandbox", String(args.disable_sandbox)) if (args.enable_internet != null) query.append("enable_internet", String(args.enable_internet)) if (args.intercept_tls != null) query.append("intercept_tls", String(args.intercept_tls)) if (args.command_line != null) query.append("command_line", String(args.command_line)) if (args.interaction_sandbox != null) query.append("interaction_sandbox", String(args.interaction_sandbox)) if (args.interaction_timeout != null) query.append("interaction_timeout", String(args.interaction_timeout)) if (args['x-apikey'] != null) headers.set("x-apikey", String(args['x-apikey'])) const url = `${baseUrl.replace(/\/+$/, '')}/files/${encodeURIComponent(String(args.sha256))}/analyse${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiFilesSha256AnalyseResponse, await res.json()) };