import {z} from 'zod' export type GetApiCompaniesArgs = {q?: string | undefined, industries?: string | undefined, countries?: string | undefined, cities?: string | undefined, states?: string | undefined, employees?: string | undefined, operatingStatus?: string | undefined, minFunding?: number | undefined, maxFunding?: number | undefined, fundedAfter?: string | undefined, fundedBefore?: string | undefined, domain?: string | undefined, minCompleteness?: number | undefined, updated_since?: string | undefined, techStack?: string | undefined, appStack?: string | undefined, stackMatch?: ('any' | 'all') | undefined, sort?: ('total_funding_raised' | 'name' | 'last_funding_date' | 'last_funding_amount' | 'founded_year' | 'data_completeness_score') | undefined, order?: ('asc' | 'desc') | undefined, page?: number | undefined, limit?: number | undefined, cursor?: string | undefined, reveal_status?: ('all' | 'revealed' | 'unrevealed') | undefined, 'X-Search-Session'?: string | undefined}; export type GetApiCompaniesOptions = {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 searchPageMeta = z.object({total: z.number().int(), total_is_estimate: z.boolean().optional(), page: z.number().int(), limit: z.number().int(), hasMore: z.boolean(), revealed_count: z.number().int(), unrevealed_count: z.number().int(), all_revealed: z.boolean().optional(), next_cursor: z.string().nullable().optional(), search_quota: z.object({per_day: z.number().int(), used_today: z.number().int(), remaining_today: z.number().int(), resets_at: z.string(), note: z.string().optional()}), depth: z.object({endpoint: z.string(), cost_credits: z.number().int(), unlocks: z.array(z.string()), docs: z.string(), free_plan: z.object({credits_per_month: z.number().int(), full_records_per_month: z.number().int()}).optional()}).optional(), alternate_entities: z.array(z.object({entity_type: z.literal("investor"), id: z.string(), name: z.string(), slug: z.string(), website: z.string(), investor_type: z.string().nullable(), profile_path: z.string()})).optional(), alternate_entities_truncated: z.boolean().optional(), investor_lookup: z.string().optional(), resolved_filters: z.record(z.string(), z.unknown()).optional()}); export const getApiCompaniesResponse = z.object({data: z.array(companyCard), meta: searchPageMeta, coverage_requested: z.boolean().optional(), coverage_status: z.enum(["queued", "not_queued", "pending_enrichment", "scope_review", "not_in_coverage_scope", "not_found"]).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 }; /** * Search companies * * GET /companies */ export const getApiCompanies = async (args: GetApiCompaniesArgs, options?: GetApiCompaniesOptions) => { const {baseUrl = "https://indexed.vc/api/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.industries != null) query.append("industries", String(args.industries)) if (args.countries != null) query.append("countries", String(args.countries)) if (args.cities != null) query.append("cities", String(args.cities)) if (args.states != null) query.append("states", String(args.states)) if (args.employees != null) query.append("employees", String(args.employees)) if (args.operatingStatus != null) query.append("operatingStatus", String(args.operatingStatus)) if (args.minFunding != null) query.append("minFunding", String(args.minFunding)) if (args.maxFunding != null) query.append("maxFunding", String(args.maxFunding)) if (args.fundedAfter != null) query.append("fundedAfter", String(args.fundedAfter)) if (args.fundedBefore != null) query.append("fundedBefore", String(args.fundedBefore)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.minCompleteness != null) query.append("minCompleteness", String(args.minCompleteness)) if (args.updated_since != null) query.append("updated_since", String(args.updated_since)) if (args.techStack != null) query.append("techStack", String(args.techStack)) if (args.appStack != null) query.append("appStack", String(args.appStack)) if (args.stackMatch != null) query.append("stackMatch", String(args.stackMatch)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.reveal_status != null) query.append("reveal_status", String(args.reveal_status)) if (args['X-Search-Session'] != null) headers.set("X-Search-Session", String(args['X-Search-Session'])) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/companies${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiCompaniesResponse, await res.json()) };