import {z} from 'zod' export type EventsForIcpRequest = {industries?: Array | undefined, states?: Array | undefined, min_employees?: number | undefined, max_employees?: number | undefined, lookback_days?: number | undefined, min_signals?: number | undefined, limit?: number | undefined, cursor?: string | undefined}; export type CreateApiV1AgentEventsForIcpArgs = {body: EventsForIcpRequest}; export type CreateApiV1AgentEventsForIcpOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eventsForIcpResponse = z.object({results: z.array(z.object({company: z.object({org_id: z.number().int().optional(), name: z.string().optional(), domain: z.string().optional(), name_agreement: z.number().int().optional()}).optional(), signals: z.array(z.object({type: z.string().optional(), date: z.string().optional(), summary: z.string().optional(), source_url: z.string().optional(), source_kind: z.string().optional()})).optional(), signal_count: z.number().int().optional(), last_event_at: z.string().optional(), why_now_short: z.string().optional()})).optional(), next_cursor: z.string().optional(), _usage: z.object({units_charged: z.string().optional(), units_month_to_date: z.string().optional()}).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 }; /** * Ranked companies matching an ICP, each with evidence * * POST /v1/agent/events_for_icp */ export const createApiV1AgentEventsForIcp = async (args: CreateApiV1AgentEventsForIcpArgs, options?: CreateApiV1AgentEventsForIcpOptions) => { const {baseUrl = "https://api.fundz.net", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agent/events_for_icp${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(eventsForIcpResponse, await res.json()) };