import {z} from 'zod' export type FullNameWithCompanyDto = {first_name?: string | undefined, last_name?: string | undefined, company_domain?: string | undefined, company_website?: string | undefined}; export type LeadEnrichmentDto = {linkedin_url?: Array | undefined, lead_id?: Array | undefined, reveal_phone?: boolean | undefined, profile_only?: boolean | undefined, email_type?: ('work_email' | 'personal_email') | undefined, webhook_url?: string | undefined, full_name_with_company?: Array | undefined}; export type CreateApiV1EnrichContactArgs = {body: LeadEnrichmentDto}; export type CreateApiV1EnrichContactOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1EnrichContactResponse = z.object({message: z.string().optional(), payload: z.object({requestId: z.string().optional()}).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 }; /** * Enrich people by LinkedIn URL or full name + company or Lead Ids. Maximum 100 allowed * * POST /v1/enrich/contact */ export const createApiV1EnrichContact = async (args: CreateApiV1EnrichContactArgs, options: CreateApiV1EnrichContactOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/enrich/contact${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(createApiV1EnrichContactResponse, await res.json()) };