import {z} from 'zod' export type GetApiV1ThreatLibraryProcessesArgs = {limit?: number | undefined, offset?: number | undefined, process_type?: string | undefined}; export type GetApiV1ThreatLibraryProcessesOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), total_count: z.number().int().optional()}); export const affectedAction = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const processFilesSummary = z.object({FileSize: z.number().int().optional(), FileType: z.string().optional(), Id: z.number().int().optional(), IsDownloaded: z.boolean().optional(), IsExecutable: z.boolean().optional(), Name: z.string().optional(), Path: z.string().optional(), SkipZipExtract: z.boolean().optional(), Url: z.string().optional()}); export const process = z.object({affected_actions: z.array(affectedAction).optional(), argument: z.string().optional(), created_at: z.string().optional(), id: z.number().int().optional(), is_predefined: z.boolean().optional(), path: z.string().optional(), process_files_summary: z.array(processFilesSummary).optional()}); export const getApiV1ThreatLibraryProcessesResponse = z.object({pages: pagination.optional(), processes: z.array(process).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 }; /** * # Process List * * GET /v1/threat-library/processes */ export const getApiV1ThreatLibraryProcesses = async (args: GetApiV1ThreatLibraryProcessesArgs, options?: GetApiV1ThreatLibraryProcessesOptions) => { const {baseUrl = "https://api.picussecurity.com", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.process_type != null) query.append("process_type", String(args.process_type)) if (AccessToken !== undefined) headers.set("Authorization", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/threat-library/processes${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(getApiV1ThreatLibraryProcessesResponse, await res.json()) };