import {z} from 'zod' export type CreateApiCompaniesLookupArgs = {body: {domains: Array}}; export type CreateApiCompaniesLookupOptions = {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 companyCard = z.object({id: z.string(), name: z.string(), slug: z.string(), 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(), employee_count_range: z.enum(["1-10", "11-50", "51-200", "201-500", "501-1000", "1001-5000", "5001-10000", "10001+", "null"]).nullable().optional(), total_funding_raised: z.number().int().nullable().optional(), operating_status: z.enum(["active", "acquired", "ipo", "closed", "null"]).nullable().optional(), _revealed: z.boolean(), domain_match: domainMatch.optional()}); export const companyDomainLookupItem = z.union([z.object({domain: z.string(), status: z.literal("hit"), credits_charged: z.literal(1), data: companyCard}), z.object({domain: z.string(), status: z.literal("miss"), coverage_requested: z.boolean(), coverage_status: z.enum(["queued", "not_queued", "pending_enrichment", "scope_review", "not_in_coverage_scope"]).optional(), coverage_eta_days: z.number().int().nullable(), credits_charged: z.literal(0)}), z.object({domain: z.string(), status: z.literal("insufficient_credits"), credits_charged: z.literal(0)})]); export const companyDomainLookupSummary = z.object({total_requested: z.number().int(), total_hits: z.number().int(), total_misses: z.number().int(), total_insufficient_credits: z.number().int(), total_credits_charged: z.number().int()}); export const createApiCompaniesLookupResponse = z.object({data: z.array(companyDomainLookupItem), summary: companyDomainLookupSummary}); 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 }; /** * Look up up to 100 company domains * * POST /companies/lookup */ export const createApiCompaniesLookup = async (args: CreateApiCompaniesLookupArgs, options?: CreateApiCompaniesLookupOptions) => { 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(/\/+$/, '')}/companies/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(createApiCompaniesLookupResponse, await res.json()) };