import {z} from 'zod' export type Date = string; export type ResourceId = string; export type EventTopicsMatch = {id: {any: Array, all: unknown} | {all: Array, any: unknown}}; export type EventEntitiesMatch = {id: {any: Array, all?: Array | undefined} | {all: Array, any?: Array | undefined}}; export type EventMatch = {date?: ({gt?: Date | undefined, gte?: Date | undefined, lt?: Date | undefined, lte?: Date | undefined}) | undefined, topics?: EventTopicsMatch | undefined, entities?: EventEntitiesMatch | undefined, labels?: ({any?: Array | undefined} | {all?: Array | undefined}) | undefined, sources?: ({id: {any: Array, all: unknown} | {all: Array, any: unknown}}) | undefined}; export type EventExclude = {entities?: {id: {any: Array}} | undefined, topics?: {id: {any: Array}} | undefined, labels?: ({any?: Array | undefined}) | undefined, sources?: {id: {any: Array}} | undefined}; export type Base64String = string; export type EventSearchQuery = {where: EventMatch, exclude?: EventExclude | undefined, size?: number | undefined, 'from-cursor'?: Base64String | undefined}; export type CreateApiEventsArgs = {body: EventSearchQuery}; export type CreateApiEventsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const date = z.string(); export const eventHash = z.string(); export const resourceId = z.string(); export const entityType = z.enum(["person", "organisation", "location", "substance", "disease", "product", "regulation"]); export const eventEntity = z.object({id: resourceId, type: entityType, name: z.string(), "story-count": z.number().int().optional(), "prominence-score": z.number().optional()}); export const eventTopic = z.object({id: resourceId, name: z.string(), "story-count": z.number().int().optional(), "prominence-score": z.number().optional()}); export const event = z.object({labels: z.array(z.string()), date: date, "document-count-entitlements": z.number().int(), hash: eventHash, "story-count-global": z.number().int(), "story-ids": z.array(resourceId), "document-count-global": z.number().int(), entities: z.array(eventEntity), topics: z.array(eventTopic), "source-count-global": z.number().int().nullable().optional()}); export const base64String = z.string(); export const eventSearchResponse = z.object({events: z.array(event), "event-hash-expiry": z.string(), "next-cursor": base64String.nullable().optional(), stats: z.object({total: z.array(z.unknown())})}); 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 }; /** * Events Search * * POST /events */ export const createApiEvents = async (args: CreateApiEventsArgs, options?: CreateApiEventsOptions) => { 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(/\/+$/, '')}/events${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(eventSearchResponse, await res.json()) };