import {z} from 'zod' export type GetApiApiV2ThreatFeedEventsArgs = {search_key: string, page_number?: number | undefined, page_size?: (10 | 20 | 30 | 50 | 100 | 250) | undefined}; export type GetApiApiV2ThreatFeedEventsOptions = {baseUrl?: string | undefined, OAuthv2?: string | undefined, fetch?: typeof fetch | undefined}; export const threatFeedEvent = z.object({EventId: z.string().optional(), SchemaVersion: z.string().optional(), LifecycleState: z.enum(["Active", "Merged", "Retracted", "Superseded"]).optional(), MergedInto: z.string().nullable().optional(), EventType: z.enum(["Data Breach", "Ransomware", "Vulnerability", "Advisory", "Hacktivist", "Social", "Incident", "Credential Exposure", "Data Leak", "Threat Actor Activity", "Research Report"]).optional(), Severity: z.enum(["Informational", "Low", "Medium", "High", "Critical"]).nullable().optional(), Title: z.string().nullable().optional(), Companies: z.array(z.object({CompanyId: z.number().nullable().optional(), CompanyName: z.string().nullable().optional(), Domain: z.string().nullable().optional(), Role: z.enum(["Affected", "Producer", "Publisher", "Mentioned"]).optional(), CountryCode: z.string().nullable().optional(), Industry: z.string().nullable().optional(), NaicsCode: z.string().nullable().optional()})).optional(), Summary: z.string().nullable().optional(), Links: z.array(z.string()).optional(), FeedMentionCount: z.number().optional(), AffectedProducts: z.array(z.object({ProductName: z.string().optional(), ProductId: z.number().nullable().optional(), Cpes: z.array(z.string()).optional()})).optional(), Vulnerabilities: z.array(z.object({Cve: z.string().optional(), VulnerabilityId: z.number().nullable().optional()})).optional(), FocusTags: z.array(z.string()).optional(), ThreatActors: z.array(z.string()).optional(), Advisories: z.object({summary: z.string().nullable().optional(), cisa_advisory: z.string().nullable().optional(), recommendations: z.array(z.string()).optional(), other_advisories: z.array(z.string()).optional(), vendor_advisories: z.array(z.string()).optional()}).and(z.record(z.string(), z.unknown())).nullable().optional(), EventDate: z.string().optional(), FirstSeenDate: z.string().optional(), UpdateDate: z.string().optional()}); export const getApiApiV2ThreatFeedEventsResponse = z.array(threatFeedEvent); 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 }; /** * Get the list of Cyber Threat Feed events. * * GET /api/v2/threat-feed-events */ export const getApiApiV2ThreatFeedEvents = async (args: GetApiApiV2ThreatFeedEventsArgs, options?: GetApiApiV2ThreatFeedEventsOptions) => { const {baseUrl = "https://app.blackkitetech.com", fetch: fetchFn = fetch, OAuthv2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search_key != null) query.append("search_key", String(args.search_key)) if (args.page_number != null) query.append("page_number", String(args.page_number)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (OAuthv2 !== undefined) headers.set('authorization', `Bearer ${OAuthv2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/threat-feed-events${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(getApiApiV2ThreatFeedEventsResponse, await res.json()) };