import {z} from 'zod' export type ContactItem = {firstName: string, lastName: string, companyDomain?: string | undefined, companyName?: string | undefined}; export type ContactIdentifiersBatch = {linkedinUrls?: Array | undefined, contacts?: Array | undefined, emails?: Array | undefined, ids?: Array | undefined, contactIds?: Array | undefined}; export type ContactLookalikesRequest = {dedupeSessionId?: string | undefined, seeds: ContactIdentifiersBatch, exclude?: ContactIdentifiersBatch | undefined, limit?: number | undefined, tableId?: string | undefined}; export type CreateApiV3ContactsLookalikeArgs = {body: ContactLookalikesRequest}; export type CreateApiV3ContactsLookalikeOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v3LookAlikeContactResult = z.object({id: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), socialLinks: z.object({linkedin: z.string().optional()}).optional(), company: z.object({id: z.string().optional(), name: z.string().optional(), domain: z.string().optional()}).optional(), jobTitle: z.object({title: z.string().optional(), departments: z.array(z.string()).optional(), seniority: z.string().optional()}).optional(), location: z.object({country: z.string().optional(), state: z.string().optional(), city: z.string().optional()}).optional()}); export const contactLookalikesMeta = z.object({returned: z.number().int(), hasMore: z.boolean()}); export const tableWrite = z.object({tableId: z.string().optional(), added: z.number().int().optional(), alreadyPresent: z.number().int().optional(), columnsCreated: z.number().int().optional(), rowsProcessed: z.number().int().optional(), rowsCharged: z.number().int().optional(), rowsAlreadyPaidInTable: z.number().int().optional(), creditsCharged: z.number().int().optional()}); export const v3Billing = z.object({creditsCharged: z.number().int().optional(), resultsReturned: z.number().int().optional()}); export const contactLookalikesResponse = z.object({dedupeSessionId: z.string().nullable(), results: z.array(v3LookAlikeContactResult), meta: contactLookalikesMeta, tableWrite: tableWrite.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 }; /** * Contact Lookalikes * * POST /v3/contacts/lookalike */ export const createApiV3ContactsLookalike = async (args: CreateApiV3ContactsLookalikeArgs, options?: CreateApiV3ContactsLookalikeOptions) => { 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/lookalike${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(contactLookalikesResponse, await res.json()) };