import {z} from 'zod' export type Date = string; export type DateRangeMatch = {gte?: Date | undefined, lte?: Date | undefined}; export type ResourceId = string; export type RiskEventEntitiesMatch = {id: {any: Array}}; export type RiskEventDefinitionsMatch = {id: {any: Array}}; export type RiskEventMatch = {'first-reported-at'?: DateRangeMatch | undefined, entities?: RiskEventEntitiesMatch | undefined, 'event-definitions'?: RiskEventDefinitionsMatch | undefined}; export type RiskEventSearchQuery = {where: RiskEventMatch, size?: number | undefined, 'from-cursor'?: string | undefined}; export type CreateApiRiskEventsSearchArgs = {body: RiskEventSearchQuery}; export type CreateApiRiskEventsSearchOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceId = z.string(); export const riskEventDefinition = z.object({id: z.string(), name: z.string(), description: z.string().optional()}); export const date = z.string(); export const entityType = z.enum(["person", "organisation", "location", "substance", "disease", "product", "regulation"]); export const entity = z.object({id: resourceId, type: entityType, name: z.string()}); export const riskEventDocument = z.object({id: resourceId, title: z.string(), "signal-url": z.string().optional()}); export const riskEvent = z.object({id: resourceId, title: z.string(), "event-definition": riskEventDefinition, "first-reported": date, "first-detected": date, actors: z.array(entity), targets: z.array(entity), documents: z.array(riskEventDocument)}); export const riskEventSearchResponse = z.object({events: z.array(riskEvent), "next-cursor": z.string().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 }; /** * Risk Events Search * * POST /risk-events-search */ export const createApiRiskEventsSearch = async (args: CreateApiRiskEventsSearchArgs, options?: CreateApiRiskEventsSearchOptions) => { const {baseUrl = "https://api.signal-ai.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/risk-events-search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(riskEventSearchResponse, await res.json()) };