import {z} from 'zod' export type GetApiCrmLeadsArgs = {match_filter_dict?: string | undefined, eval_object_id?: boolean | undefined, sort_dict?: string | undefined, page_size?: number | undefined, page_num?: number | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined}; export type GetApiCrmLeadsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const leadResponse = z.object({id: z.string(), phone_list: z.array(z.unknown()).nullable(), email_list: z.array(z.unknown()).nullable(), first_name: z.string().nullable(), last_name: z.string().nullable(), name: z.string(), status: z.string(), marketing_source: z.string().nullable().optional(), created_date: z.string().nullable(), updated_date: z.string().nullable(), status_updated_date: z.string().nullable(), tags: z.array(z.string()).nullable().optional(), social_dict_list: z.array(z.record(z.string(), z.unknown())).nullable().optional(), is_converted: z.boolean().optional(), contact_id: z.string().nullable().optional(), has_deal: z.boolean().optional(), deal_stage: z.string().nullable().optional(), deal_id: z.string().nullable().optional(), value_of_sale: z.number().nullable().optional(), close_date: z.string().nullable().optional()}); export const leadResponseList = z.object({leads: z.array(leadResponse), page_size: z.number().int(), page_num: z.number().int()}); 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 }; /** * Get Leads * * GET /crm/leads */ export const getApiCrmLeads = async (args: GetApiCrmLeadsArgs, options: GetApiCrmLeadsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.match_filter_dict != null) query.append("match_filter_dict", String(args.match_filter_dict)) if (args.eval_object_id != null) query.append("eval_object_id", String(args.eval_object_id)) if (args.sort_dict != null) query.append("sort_dict", String(args.sort_dict)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_num != null) query.append("page_num", String(args.page_num)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/crm/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(leadResponseList, await res.json()) };