import {z} from 'zod' export type TypeaheadSearchType = 'PERSON' | 'COMPANY' | 'SCHOOL' | 'EMAIL_INTEGRATED_USER' | 'INVESTOR' | 'INVESTOR_COMPANY' | 'INVESTOR_PERSON' | 'LOCATION' | 'LOCATION_CITY' | 'LOCATION_STATE' | 'LOCATION_COUNTRY' | 'LOCATION_METRO_AREA'; export type TypeaheadSource = 'ENTERPRISE_APP'; export type GetApiSearchTypeaheadArgs = {query: string, search_type?: (TypeaheadSearchType | null) | undefined, source?: (TypeaheadSource | null) | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiSearchTypeaheadOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const typeaheadSearchType = z.enum(["PERSON", "COMPANY", "SCHOOL", "EMAIL_INTEGRATED_USER", "INVESTOR", "INVESTOR_COMPANY", "INVESTOR_PERSON", "LOCATION", "LOCATION_CITY", "LOCATION_STATE", "LOCATION_COUNTRY", "LOCATION_METRO_AREA"]); export const typeaheadSource = z.literal("ENTERPRISE_APP"); export const indexMethod = z.union([z.literal(1), z.literal(2)]); export const typeaheadSubtype = z.enum(["COMPANY", "PERSON", "CITY", "STATE", "COUNTRY", "METRO_AREA"]); export const typeaheadEntity = z.object({entity_urn: z.union([z.string(), z.string(), z.string(), z.string(), z.string(), z.string(), z.string()]), type: typeaheadSearchType, source: typeaheadSource, text: z.string().nullable().optional(), alt_text: z.string().nullable().optional(), index_field: z.string().nullable().optional(), index_id: z.string().nullable().optional(), ranking_score: z.number().nullable().optional(), index_method: indexMethod.nullable().optional(), subtype: typeaheadSubtype.nullable().optional(), linked_entity_urn: z.string().nullable().optional(), headcount: z.number().int().nullable().optional(), num_funding_rounds: z.number().int().nullable().optional(), primary_type: z.string().nullable().optional()}); export const typeaheadSearchOutput = z.object({count: z.number().int().nullable().optional(), total_count: z.number().int().nullable().optional(), results: z.array(typeaheadEntity)}); 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 }; /** * Typeahead Search * * GET /search/typeahead */ export const getApiSearchTypeahead = async (args: GetApiSearchTypeaheadArgs, options: GetApiSearchTypeaheadOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.search_type != null) query.append("search_type", String(args.search_type)) if (args.source != null) query.append("source", String(args.source)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) const url = `${baseUrl.replace(/\/+$/, '')}/search/typeahead${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(typeaheadSearchOutput, await res.json()) };