import {z} from 'zod' export type GetApiV1ThreatLibraryThreatsArgs = {limit?: number | undefined, offset?: number | undefined, severities?: Array | undefined, attack_category_ids?: Array | undefined, attack_module_ids?: Array | undefined, release_date_gte?: number | undefined, release_date_lte?: number | undefined, is_predefined?: boolean | undefined, affected_products?: Array | undefined, affected_os?: Array | undefined, unified_kill_chains?: Array | undefined, threat_actors?: Array | undefined, mitre_attack?: Array | undefined, attacker_objectives?: Array | undefined}; export type GetApiV1ThreatLibraryThreatsOptions = {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 attackCategory = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const attackModule = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const flowNode: z.ZodType = z.object({display_id: z.number().int().optional(), false_items: z.array(z.lazy(() => flowNode)).optional(), id: z.number().int().optional(), name: z.string().optional(), node_id: z.number().int().optional(), true_items: z.array(z.lazy(() => flowNode)).optional(), type: z.string().optional()}); export const objective = z.object({actions: z.array(flowNode).optional(), id: z.number().int().optional(), name: z.string().optional(), type: z.string().optional()}); export const mitreDTO = z.object({tactic: z.string().optional(), techniques: z.array(z.string()).optional()}); export const ukcDTO = z.object({phases: z.array(z.string()).optional(), stage: z.string().optional()}); export const threatsReturn = z.object({affected_os: z.array(z.string()).optional(), attack_categories: z.array(attackCategory).optional(), attack_modules: z.array(attackModule).optional(), created_at: z.number().int().optional(), description: z.string().optional(), display_id: z.number().int().optional(), flows: z.array(objective).optional(), id: z.number().int().optional(), is_predefined: z.boolean().optional(), mitre_attack: z.array(mitreDTO).optional(), name: z.string().optional(), release_date: z.number().int().optional(), severity: z.string().optional(), tags: z.array(z.string()).optional(), unified_kill_chains: z.array(ukcDTO).optional(), updated_at: z.number().int().optional()}); export const getApiV1ThreatLibraryThreatsResponse = z.object({pages: pagination.optional(), threats: z.array(threatsReturn).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 }; /** * # Threat List * * GET /v1/threat-library/threats */ export const getApiV1ThreatLibraryThreats = async (args: GetApiV1ThreatLibraryThreatsArgs, options?: GetApiV1ThreatLibraryThreatsOptions) => { 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)) for (const item of args.severities ?? []) query.append("severities", String(item)) for (const item of args.attack_category_ids ?? []) query.append("attack_category_ids", String(item)) for (const item of args.attack_module_ids ?? []) query.append("attack_module_ids", String(item)) if (args.release_date_gte != null) query.append("release_date_gte", String(args.release_date_gte)) if (args.release_date_lte != null) query.append("release_date_lte", String(args.release_date_lte)) if (args.is_predefined != null) query.append("is_predefined", String(args.is_predefined)) for (const item of args.affected_products ?? []) query.append("affected_products", String(item)) for (const item of args.affected_os ?? []) query.append("affected_os", String(item)) for (const item of args.unified_kill_chains ?? []) query.append("unified_kill_chains", String(item)) for (const item of args.threat_actors ?? []) query.append("threat_actors", String(item)) for (const item of args.mitre_attack ?? []) query.append("mitre_attack", String(item)) for (const item of args.attacker_objectives ?? []) query.append("attacker_objectives", String(item)) if (AccessToken !== undefined) headers.set("Authorization", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/threat-library/threats${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(getApiV1ThreatLibraryThreatsResponse, await res.json()) };