import {z} from 'zod' export type ProspectMatchInput = {business_id?: (string | null) | undefined, full_name?: (string | null) | undefined, company_name?: (string | null) | undefined, email?: (string | null) | undefined, phone_number?: (string | null) | undefined, linkedin?: (string | null) | undefined}; export type ProspectsMatchRequest = {request_context?: (Record | null) | undefined, prospects_to_match: Array}; export type CreateApiV1ProspectsMatchArgs = {tenant?: string | undefined, body: ProspectsMatchRequest}; export type CreateApiV1ProspectsMatchOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const requestStatus = z.enum(["success", "miss", "failure"]); export const responseContext = z.object({correlation_id: z.string(), request_status: requestStatus, time_took_in_seconds: z.number()}); export const prospectMatchInput = z.object({business_id: z.string().nullable().optional(), full_name: z.string().nullable().optional(), company_name: z.string().nullable().optional(), email: z.string().nullable().optional(), phone_number: z.string().nullable().optional(), linkedin: z.string().nullable().optional()}); export const prospectMatchOutputWithError = z.object({input: prospectMatchInput, prospect_id: z.string().optional(), error: z.string(), error_type: z.string()}); export const prospectMatchOutput = z.object({input: prospectMatchInput, prospect_id: z.string().optional()}); export const prospectsMatchResponse = z.object({response_context: responseContext, total_results: z.number().int().gt(0), total_matches: z.number().int().gte(0), matched_prospects: z.array(z.union([prospectMatchOutputWithError, prospectMatchOutput])).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 }; /** * Match Prospects * * POST /v1/prospects/match * * Also accepts auth: APIKeyHeader */ export const createApiV1ProspectsMatch = async (args: CreateApiV1ProspectsMatchArgs, options?: CreateApiV1ProspectsMatchOptions) => { const {baseUrl = "https://api.explorium.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.tenant != null) headers.set("tenant", String(args.tenant)) if (APIKeyHeader !== undefined) headers.set("api_key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/prospects/match${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(prospectsMatchResponse, await res.json()) };