import {z} from 'zod' export type ReputationSingleRequest = {resource?: string | undefined}; export type ReputationRequest = {request: Array}; export type CreateApiServiceRepServiceV20QueryArgs = {'Client-Key': string, token: string, service: 'url' | 'ip' | 'file', resource: string, body: ReputationRequest}; export type CreateApiServiceRepServiceV20QueryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reputationClassification = z.object({classification: z.string().optional(), severity: z.enum(["N/A", "Low", "Medium", "High", "Critical"]).optional(), Confidence: z.enum(["Low", "Medium", "High"]).optional()}); export const reputationStatus = z.object({code: z.union([z.literal(2001), z.literal(2002)]).optional(), label: z.enum(["SUCCESS", "PARTIAL_SUCCESS"]).optional(), message: z.enum(["Succeeded to generate reputation", "Some vendors are unavailable"]).optional()}); export const reputationResponseContext = z.object({asn: z.number().optional(), as_owner: z.string().optional(), safe: z.boolean().optional(), malware_family: z.string().optional(), protection_name: z.string().optional(), redirections: z.array(z.string()).optional(), malware_types: z.array(z.string()).optional(), categories: z.array(z.string()).optional(), location: z.object({region: z.string().optional(), city: z.string().optional(), postalCode: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), dma_code: z.number().int().optional(), area_code: z.number().int().optional(), metro_code: z.number().int().optional()}).optional(), related_resources: z.object({similar_files_found: z.number().optional(), similar_files_details: z.object({md5: z.string().optional(), ssdeep: z.string().optional(), ssdeep_similarity: z.string().optional(), file_type: z.string().optional(), classification: z.string().optional(), malware_family: z.string().optional(), confidence: z.string().optional()}).optional()}).optional()}); export const reputationResponse = z.object({risk: z.number().optional(), resource: z.string().optional(), reputation: reputationClassification.optional(), status: reputationStatus.optional(), context: reputationResponseContext.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 }; /** * Query your resource * * POST /{service}-rep/service/v2.0/query */ export const createApiServiceRepServiceV20Query = async (args: CreateApiServiceRepServiceV20QueryArgs, options?: CreateApiServiceRepServiceV20QueryOptions) => { const {baseUrl = "https://rep.checkpoint.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Client-Key'] != null) headers.set("Client-Key", String(args['Client-Key'])) if (args.token != null) headers.set("token", String(args.token)) if (args.resource != null) query.append("resource", String(args.resource)) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.service))}-rep/service/v2.0/query${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(reputationResponse, await res.json()) };