import {z} from 'zod' export type V3ContactSearchItem = {clientReferenceId?: string | undefined, id?: string | undefined, linkedinUrl?: string | undefined, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, companyName?: string | undefined, companyDomain?: string | undefined}; export type V3SearchOptions = {includePartialProfiles?: boolean | undefined}; export type V3ContactSignalsDto = {types: Array<'allSignals' | 'promotion' | 'companyChange'>, startDate?: string | undefined, maxResultsPerSignal?: number | undefined}; export type V3ContactsSearchRequest = {contacts: Array, options?: V3SearchOptions | undefined, signals?: V3ContactSignalsDto | undefined}; export type CreateApiV3ContactsSearchArgs = {body: V3ContactsSearchRequest}; export type CreateApiV3ContactsSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v3CanRevealItem = z.object({field: z.enum(["emails", "phones"]).optional(), credits: z.number().int().optional()}); export const v3ItemError = z.object({code: z.enum(["NOT_FOUND", "COMPLIANCE_RESTRICTED", "ENRICH_FAILED", "NO_SCORE"]).optional(), message: z.string().optional()}); export const v3ContactPreview = z.object({clientReferenceId: z.string().optional(), id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), jobTitle: z.object({title: z.string().optional(), departments: z.array(z.string()).optional(), seniority: z.string().optional()}).optional(), company: z.object({id: z.string().optional(), name: z.string().optional(), domain: z.string().optional()}).optional(), location: z.object({country: z.string().optional(), state: z.string().optional(), city: z.string().optional()}).optional(), socialLinks: z.object({linkedin: z.string().optional()}).optional(), has: z.array(z.string()).optional(), canReveal: z.array(v3CanRevealItem).optional(), signalTypes: z.array(z.string()).optional(), error: v3ItemError.optional()}); export const v3Billing = z.object({creditsCharged: z.number().int().optional(), resultsReturned: z.number().int().optional()}); export const v3ContactsSearchResponse = z.object({requestId: z.string().optional(), results: z.array(v3ContactPreview).optional(), billing: v3Billing.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 Contacts * * POST /v3/contacts/search */ export const createApiV3ContactsSearch = async (args: CreateApiV3ContactsSearchArgs, options?: CreateApiV3ContactsSearchOptions) => { const {baseUrl = "https://api.lusha.com", 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("api_key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/contacts/search${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(v3ContactsSearchResponse, await res.json()) };