import {z} from 'zod' export type WhyNowRequest = {domain?: string | undefined, org_id?: number | undefined, lookback_days?: number | undefined}; export type CreateApiV1AgentWhyNowArgs = {body: WhyNowRequest}; export type CreateApiV1AgentWhyNowOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whyNowResponse = z.object({company: z.object({org_id: z.number().int().nullable().optional(), name: z.string().optional(), domain: z.string().optional(), name_agreement: z.number().int().optional()}).optional(), evidence: 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(), sec_items: z.array(z.string()).optional()})).optional(), last_event: z.object({type: z.string().optional(), date: z.string().optional(), summary: z.string().optional(), source_url: z.string().optional(), source_kind: z.string().optional(), sec_items: z.array(z.string()).optional(), within_lookback: z.boolean().optional()}).nullable().optional(), last_event_at: z.string().nullable().optional(), narrative: z.unknown(), coverage_note: z.string().nullable().optional(), resolved: z.boolean().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 }; /** * Dated evidence for one company * * POST /v1/agent/why_now */ export const createApiV1AgentWhyNow = async (args: CreateApiV1AgentWhyNowArgs, options?: CreateApiV1AgentWhyNowOptions) => { 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/why_now${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(whyNowResponse, await res.json()) };