import {z} from 'zod' export type GetApiCustomerLeadsLeadIdEngagementsArgs = {leadId: string, limit?: number | undefined, cursor?: string | undefined, engagementType?: ('LIKE' | 'COMMENT' | 'REPOST' | 'POST_AUTHOR') | undefined, monitoredProfileId?: string | undefined, postId?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined}; export type GetApiCustomerLeadsLeadIdEngagementsOptions = {baseUrl?: string | undefined, CustomerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pageEnvelope = z.object({data: z.array(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 safe engagement summaries for one lead. * * GET /customer/leads/{leadId}/engagements */ export const getApiCustomerLeadsLeadIdEngagements = async (args: GetApiCustomerLeadsLeadIdEngagementsArgs, options?: GetApiCustomerLeadsLeadIdEngagementsOptions) => { 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.engagementType != null) query.append("engagementType", String(args.engagementType)) if (args.monitoredProfileId != null) query.append("monitoredProfileId", String(args.monitoredProfileId)) if (args.postId != null) query.append("postId", String(args.postId)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (CustomerApiKey !== undefined) headers.set('authorization', `Bearer ${CustomerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/customer/leads/${encodeURIComponent(String(args.leadId))}/engagements${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(pageEnvelope, await res.json()) };