import {z} from 'zod' export type GetApiCustomerLeadsArgs = {limit?: number | undefined, cursor?: string | undefined, tier?: ('UNQUALIFIED' | 'NURTURE' | 'QUALIFIED' | 'PRIORITY') | undefined, leadTiers?: string | undefined, disposition?: ('ACTIVE' | 'CLIENT' | 'BAD_LEAD') | undefined, dispositions?: string | undefined, minIcpMatchPercent?: number | undefined, maxIcpMatchPercent?: number | undefined, monitoredProfileId?: string | undefined, monitoredProfileIds?: string | undefined, postId?: string | undefined, engagementType?: ('LIKE' | 'COMMENT' | 'REPOST' | 'POST_AUTHOR') | undefined, engagementTypes?: string | undefined, ownerId?: string | undefined, ownerIds?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, query?: string | undefined, companyNames?: string | undefined, origin?: ('agent' | 'watchlist' | 'all') | undefined}; export type GetApiCustomerLeadsOptions = {baseUrl?: string | undefined, CustomerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const leadSignal = z.object({summary: z.string().max(300), whyRelevant: z.string().max(500), topic: z.string().max(160).nullable(), type: z.enum(["COMPETITOR", "PAIN", "TRIGGER", "TOPIC", "CATEGORY_INTEREST", "CUSTOM", "GENERAL_ENGAGEMENT"]), strength: z.enum(["DIRECT", "CONTEXTUAL", "ACTIVITY"]), sentiment: z.enum(["NEGATIVE", "POSITIVE", "MIXED", "NEUTRAL", "UNKNOWN"]), occurredAt: z.string(), source: z.object({name: z.string().nullable(), title: z.string().nullable(), url: z.string().nullable()})}); export const leadPage = z.object({data: z.array(z.object({id: z.string(), icpMatchPercent: z.number().int().nullable().optional(), intentScore: z.number().optional(), leadTier: z.string().nullable().optional(), profile: z.object({displayName: z.string().nullable(), headline: z.string().nullable(), location: z.string().nullable(), currentCompany: z.string().nullable(), profileUrl: z.string().nullable()}), contact: z.object({workEmail: z.string().nullable(), workEmailStatus: z.string().nullable(), personalEmail: z.string().nullable(), personalEmailStatus: z.string().nullable(), contactPhone: z.string().nullable(), contactPhoneStatus: z.string().nullable()}).nullable(), signal: leadSignal.nullable()}).and(z.record(z.string(), z.unknown()))), page: z.object({limit: z.number().int(), cursor: z.string().nullable().optional(), nextCursor: z.string().nullable()})}); 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 }; /** * List API-visible leads. * * GET /customer/leads */ export const getApiCustomerLeads = async (args: GetApiCustomerLeadsArgs, options?: GetApiCustomerLeadsOptions) => { const {baseUrl = "https://api.traxy.ai/api/v1", fetch: fetchFn = fetch, CustomerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.tier != null) query.append("tier", String(args.tier)) if (args.leadTiers != null) query.append("leadTiers", String(args.leadTiers)) if (args.disposition != null) query.append("disposition", String(args.disposition)) if (args.dispositions != null) query.append("dispositions", String(args.dispositions)) if (args.minIcpMatchPercent != null) query.append("minIcpMatchPercent", String(args.minIcpMatchPercent)) if (args.maxIcpMatchPercent != null) query.append("maxIcpMatchPercent", String(args.maxIcpMatchPercent)) if (args.monitoredProfileId != null) query.append("monitoredProfileId", String(args.monitoredProfileId)) if (args.monitoredProfileIds != null) query.append("monitoredProfileIds", String(args.monitoredProfileIds)) if (args.postId != null) query.append("postId", String(args.postId)) if (args.engagementType != null) query.append("engagementType", String(args.engagementType)) if (args.engagementTypes != null) query.append("engagementTypes", String(args.engagementTypes)) if (args.ownerId != null) query.append("ownerId", String(args.ownerId)) if (args.ownerIds != null) query.append("ownerIds", String(args.ownerIds)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.query != null) query.append("query", String(args.query)) if (args.companyNames != null) query.append("companyNames", String(args.companyNames)) if (args.origin != null) query.append("origin", String(args.origin)) if (CustomerApiKey !== undefined) headers.set('authorization', `Bearer ${CustomerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/customer/leads${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(leadPage, await res.json()) };