import {z} from 'zod' export type GetApiV1ThreatLibraryThreatsThreatIdArgs = {ThreatId: number}; export type GetApiV1ThreatLibraryThreatsThreatIdOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; 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 threatReturn = z.object({affected_os: z.array(z.string()).optional(), affected_platforms: z.array(z.string()).optional(), affected_products: z.array(z.string()).optional(), applicable_agent_types: z.array(z.number().int()).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(), threat_actors: z.array(z.string()).optional(), unified_kill_chains: z.array(ukcDTO).optional(), updated_at: z.number().int().optional()}); export const getApiV1ThreatLibraryThreatsThreatIdResponse = z.object({threat: threatReturn.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 Details * * GET /v1/threat-library/threats/{ThreatId} */ export const getApiV1ThreatLibraryThreatsThreatId = async (args: GetApiV1ThreatLibraryThreatsThreatIdArgs, options?: GetApiV1ThreatLibraryThreatsThreatIdOptions) => { 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 (AccessToken !== undefined) headers.set("Authorization", AccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/threat-library/threats/${encodeURIComponent(String(args.ThreatId))}${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(getApiV1ThreatLibraryThreatsThreatIdResponse, await res.json()) };