import {z} from 'zod' export type SegmentationCondition = {attribute: 'about.businessType' | 'about.industries' | 'about.industry' | 'about.name' | 'about.totalEmployees' | 'about.yearFounded' | 'ai.search' | 'analytics.monthlyVisitors' | 'apps' | 'codes.naics' | 'codes.sic' | 'contacts' | 'domain.domain' | 'domain.tld' | 'finances.revenue' | 'finances.stockExchange' | 'locations.headquarters.city.code' | 'locations.headquarters.continent.code' | 'locations.headquarters.country.code' | 'locations.headquarters.county.code' | 'locations.headquarters.state.code' | 'meta.listIds' | 'meta.score' | 'meta.syncedAt' | 'socials' | 'socials.linkedin.id' | 'technologies.active' | 'technologies.categories' | 'urls', blockedOperator?: boolean | undefined, operator: 'and' | 'or', sign: 'equals' | 'exactEquals' | 'greater' | 'lower' | 'notEquals', values: Array}; export type GetApiV2CompaniesCountArgs = {actionId?: number | undefined, query?: Array | undefined, search?: string | undefined, searchFields?: (Array<'about.name' | 'domain.domain'>) | undefined}; export type GetApiV2CompaniesCountOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2CompaniesCountResponse = z.object({count: z.number()}); 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 }; /** * Get the count of companies using a segmentation query. * * GET /v2/companies/count */ export const getApiV2CompaniesCount = async (args: GetApiV2CompaniesCountArgs, options?: GetApiV2CompaniesCountOptions) => { const {baseUrl = "https://api.thecompaniesapi.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.actionId != null) query.append("actionId", String(args.actionId)) for (const item of args.query ?? []) query.append("query", String(item)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.searchFields ?? []) query.append("searchFields", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/companies/count${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(getApiV2CompaniesCountResponse, await res.json()) };