import {z} from 'zod' export type CreateApiInvestorsLookupArgs = {body: {domains: Array, names?: Array | undefined} | {domains?: Array | undefined, names: Array}}; export type CreateApiInvestorsLookupOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const investorCard = z.object({id: z.string(), name: z.string(), slug: z.string(), logo_url: z.string().nullable().optional(), website: z.string().nullable().optional(), investor_type: z.enum(["vc", "pe", "growth_equity", "hedge_fund", "cvc", "family_office", "sovereign_wealth", "accelerator", "incubator", "angel_group", "angel", "individual", "bank", "other", "null"]).nullable().optional(), hq_city: z.string().nullable().optional(), hq_country: z.string().nullable().optional(), total_investments_count: z.number().int().nullable().optional(), _revealed: z.boolean()}); export const investorLookupItem = z.union([z.object({domain: z.string(), name: z.string().optional(), status: z.enum(["hit", "miss", "ambiguous", "insufficient_credits"]), credits_charged: z.number().int().gte(0), data: investorCard.optional(), coverage_requested: z.boolean().optional(), coverage_status: z.enum(["queued", "not_queued"]).optional(), total_matches: z.number().int().gte(2).optional(), has_more: z.boolean().optional(), next_action: z.literal("refine_domain_or_name").optional(), matches: z.array(z.object({id: z.string(), slug: z.string(), name: z.string(), investor_type: z.string().nullable(), hq_country: z.string().nullable(), profile_path: z.string()})).optional()}), z.object({domain: z.string().optional(), name: z.string(), status: z.enum(["hit", "miss", "ambiguous", "insufficient_credits"]), credits_charged: z.number().int().gte(0), data: investorCard.optional(), coverage_requested: z.boolean().optional(), coverage_status: z.enum(["queued", "not_queued"]).optional(), total_matches: z.number().int().gte(2).optional(), has_more: z.boolean().optional(), next_action: z.literal("refine_domain_or_name").optional(), matches: z.array(z.object({id: z.string(), slug: z.string(), name: z.string(), investor_type: z.string().nullable(), hq_country: z.string().nullable(), profile_path: z.string()})).optional()})]); export const investorLookupSummary = z.object({total_requested: z.number().int().gte(0).lte(100), total_hits: z.number().int().gte(0), total_misses: z.number().int().gte(0), total_ambiguous: z.number().int().gte(0), total_insufficient_credits: z.number().int().gte(0), total_credits_charged: z.number().int().gte(0)}); export const createApiInvestorsLookupResponse = z.object({data: z.array(investorLookupItem), summary: investorLookupSummary}); 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 }; /** * Resolve up to 100 investor domains or names * * POST /investors/lookup */ export const createApiInvestorsLookup = async (args: CreateApiInvestorsLookupArgs, options?: CreateApiInvestorsLookupOptions) => { const {baseUrl = "https://indexed.vc/api/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/investors/lookup${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(createApiInvestorsLookupResponse, await res.json()) };