import {z} from 'zod' export type AutocompleteType = 'country' | 'country_code' | 'region_country_code' | 'google_category' | 'naics_category' | 'linkedin_category' | 'company_tech_stack_tech' | 'company_tech_stack_categories' | 'job_title' | 'company_size' | 'company_revenue' | 'number_of_locations' | 'company_age' | 'job_department' | 'job_level' | 'city_region_country' | 'company_name' | 'business_intent_topics' | 'city_region'; export type GetApiV1ProspectsAutocompleteArgs = {field: AutocompleteType, query?: string | undefined, semantic_search?: boolean | undefined, tenant?: string | undefined}; export type GetApiV1ProspectsAutocompleteOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const autoCompleteItem = z.object({query: z.string(), label: z.string(), value: z.string()}); export const getApiV1ProspectsAutocompleteResponse = z.array(autoCompleteItem); 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 }; /** * Autocomplete Prospects * * GET /v1/prospects/autocomplete * * Also accepts auth: APIKeyHeader */ export const getApiV1ProspectsAutocomplete = async (args: GetApiV1ProspectsAutocompleteArgs, options?: GetApiV1ProspectsAutocompleteOptions) => { const {baseUrl = "https://api.explorium.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.field != null) query.append("field", String(args.field)) if (args.query != null) query.append("query", String(args.query)) if (args.semantic_search != null) query.append("semantic_search", String(args.semantic_search)) if (args.tenant != null) headers.set("tenant", String(args.tenant)) if (APIKeyHeader !== undefined) headers.set("api_key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/prospects/autocomplete${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(getApiV1ProspectsAutocompleteResponse, await res.json()) };