import {z} from 'zod' export type WatchlistDiffRequest = {domains: Array, since?: string | undefined}; export type CreateApiV1AgentWatchlistDiffArgs = {body: WatchlistDiffRequest}; export type CreateApiV1AgentWatchlistDiffOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const watchlistDiffResponse = z.object({changed: z.array(z.object({domain: z.string().optional(), org_id: z.number().int().optional(), name: z.string().optional(), new_events: 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()})).optional(), next_cursor: z.string().optional(), checked: z.number().int().optional(), unresolved: z.array(z.unknown()).optional(), served_from: z.string().optional(), not_checked: z.array(z.unknown()).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 }; /** * Only the companies in your book with new events since a cursor * * POST /v1/agent/watchlist_diff */ export const createApiV1AgentWatchlistDiff = async (args: CreateApiV1AgentWatchlistDiffArgs, options?: CreateApiV1AgentWatchlistDiffOptions) => { 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/watchlist_diff${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(watchlistDiffResponse, await res.json()) };