import {z} from 'zod' export type CreateApiEnrichArgs = {body: {domains?: Array | undefined, slugs?: Array | undefined}}; export type CreateApiEnrichOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const domainMatch = z.object({queried: z.string(), canonical_domain: z.string().nullable(), matched_via: z.enum(["canonical_domain", "alias_domain", "former_website"])}); export const profileLoadState = z.object({status: z.enum(["ok", "partial", "unavailable", "failed"]), cause: z.enum(["outage", "defect"]).optional()}); export const nullableCompanyReference = z.object({id: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), logo_url: z.string().nullable().optional()}).nullable(); export const companyRelationship = z.object({relationship_type: z.enum(["subsidiary", "brand", "division", "joint_venture"]).optional(), ownership_pct: z.number().nullable().optional(), started_on: z.string().nullable().optional(), company: z.object({id: z.string().optional(), slug: z.string().optional(), name: z.string().optional(), logo_url: z.string().nullable().optional()}).optional()}); export const companyProfile = z.object({id: z.string(), name: z.string(), slug: z.string(), data_as_of: z.string().nullable().optional(), logo_url: z.string().nullable().optional(), website: z.string().nullable().optional(), short_description: z.string().nullable().optional(), long_description: z.string().nullable().optional(), industries: z.array(z.string()).nullable().optional(), hq_city: z.string().nullable().optional(), hq_state: z.string().nullable().optional(), hq_country: z.string().nullable().optional(), employee_count_range: z.string().nullable().optional(), total_funding_raised: z.number().int().nullable().optional(), operating_status: z.string().nullable().optional(), founded_year: z.number().int().nullable().optional(), last_valuation: z.number().int().nullable().optional(), last_valuation_date: z.string().nullable().optional(), load_states: z.object({relationships: profileLoadState.optional(), acquisitions: profileLoadState.optional(), acquirer: profileLoadState.optional(), parent_company: profileLoadState.optional(), valuation_events: profileLoadState.optional(), technologies: profileLoadState.optional(), app_stack: profileLoadState.optional()}).optional(), acquirer: nullableCompanyReference.optional(), parent_company: nullableCompanyReference.optional(), owners: z.array(companyRelationship).optional(), subsidiaries: z.array(companyRelationship).optional(), acquisitions: z.array(z.object({id: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), logo_url: z.string().nullable().optional(), website: z.string().nullable().optional(), short_description: z.string().nullable().optional(), industries: z.array(z.string()).nullable().optional(), hq_city: z.string().nullable().optional(), hq_country: z.string().nullable().optional(), acquisition_date: z.string().nullable().optional(), acquisition_price_usd: z.number().int().nullable().optional()})).optional(), valuation_events: z.array(z.object({id: z.string().optional(), event_type: z.string().optional(), amount_raised_usd: z.number().int().nullable().optional(), announced_date: z.string().nullable().optional(), participants: z.array(z.object({is_lead: z.boolean().optional(), investor: z.object({id: z.string().optional(), name: z.string().optional(), slug: z.string().optional(), investor_type: z.string().nullable().optional()}).optional()})).optional()})).optional(), technologies: z.array(z.object({technology_name: z.string().optional(), category: z.string().nullable().optional(), stack_type: z.literal("tech").optional()})).optional(), app_stack: z.array(z.object({technology_name: z.string().optional(), category: z.string().nullable().optional(), stack_type: z.literal("app").optional()})).optional()}); export const enrichSummary = z.object({total_requested: z.number().int().gte(0), total_matched: z.number().int().gte(0), total_revealed: z.number().int().gte(0), total_cached: z.number().int().gte(0), total_unmatched: z.number().int().gte(0), total_credits_charged: z.number().int().gte(0)}); export const createApiEnrichResponse = z.object({data: z.array(z.object({query: z.string(), matched: z.boolean(), status: z.enum(["revealed", "cached", "insufficient_credits", "not_found"]), credits_charged: z.number().int(), domain_match: domainMatch.optional(), coverage_status: z.enum(["not_in_coverage_scope", "scope_review", "pending_enrichment", "not_found", "invalid_domain"]).optional(), data: companyProfile.optional()})), summary: enrichSummary}); 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 }; /** * Bulk enrich companies by domain or slug * * POST /enrich */ export const createApiEnrich = async (args: CreateApiEnrichArgs, options?: CreateApiEnrichOptions) => { 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(/\/+$/, '')}/enrich${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(createApiEnrichResponse, await res.json()) };