import {z} from 'zod' export type GetApiV1ThreatLibraryFilesArgs = {limit?: number | undefined, offset?: number | undefined, type?: string | undefined, nameLike?: string | undefined}; export type GetApiV1ThreatLibraryFilesOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const fileApiOutput = z.object({affected_processes: z.array(z.object({arguments: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), path: z.string().optional(), process_type: z.string().optional()})).optional(), created_at: z.string().optional(), download_path: z.string().optional(), id: z.number().int().optional(), is_executable: z.boolean().optional(), name: z.object({base_name: z.string().optional(), extension: z.string().optional(), full_name: z.string().optional(), uuid: z.string().optional()}).optional(), skip_zip_extract: z.boolean().optional(), type: z.string().optional()}); export const pagination = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), total_count: z.number().int().optional()}); export const getApiV1ThreatLibraryFilesResponse = z.object({files: z.array(fileApiOutput).optional(), pages: pagination.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 }; /** * # Files List * * GET /v1/threat-library/files */ export const getApiV1ThreatLibraryFiles = async (args: GetApiV1ThreatLibraryFilesArgs, options?: GetApiV1ThreatLibraryFilesOptions) => { 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.type != null) query.append("type", String(args.type)) if (args.nameLike != null) query.append("nameLike", String(args.nameLike)) if (AccessToken !== undefined) headers.set("Authorization", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/threat-library/files${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(getApiV1ThreatLibraryFilesResponse, await res.json()) };