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 RiskScoreMatch = {'first-reported-at': DateRangeMatch, entities: RiskEventEntitiesMatch}; export type RiskPillar = {name: string, 'event-definition-ids': Array}; export type RiskScoreBy = {'entity-ids'?: Array | undefined, pillars?: Array | undefined}; export type RiskEventScoreQuery = {where: RiskScoreMatch, 'score-by': RiskScoreBy}; export type CreateApiRiskEventsScoresArgs = {body: RiskEventScoreQuery}; export type CreateApiRiskEventsScoresOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const riskEventDefinition = z.object({id: z.string(), name: z.string(), description: z.string().optional()}); export const resourceId = 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 riskScore = z.object({name: z.string(), "event-definitions": z.array(riskEventDefinition), entity: entity.optional(), "document-count": z.number(), "event-count": z.number(), likelihood: z.number().gte(1).lte(5), impact: z.number().gte(1).lte(5), score: z.number().gte(1).lte(25)}); export const riskEventScoreResponse = z.object({scores: z.array(riskScore)}); 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 Scores * * POST /risk-events-scores */ export const createApiRiskEventsScores = async (args: CreateApiRiskEventsScoresArgs, options?: CreateApiRiskEventsScoresOptions) => { 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-scores${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(riskEventScoreResponse, await res.json()) };