import {z} from 'zod' export type GetApiApiPublicContactsArgs = {days?: number | undefined, created_after?: string | undefined, updated_after?: string | undefined, limit?: number | undefined, offset?: number | undefined, stage_id?: string | undefined, pipeline_id?: string | undefined, custom_field_key?: string | undefined, custom_field_value?: string | undefined, entity_type?: ('ticket' | 'customer') | undefined, with_utm_events?: boolean | undefined, 'x-api-key': string}; export type GetApiApiPublicContactsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPublicContactsResponse = z.array(z.object({id: z.string().optional(), name: z.string().optional(), phone: z.string().optional(), email: z.string().optional(), platform: z.string().optional(), last_message_at: z.string().optional(), chat_status: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), client_id: z.string().optional(), ai_customer_id: z.string().nullable().optional(), default_stage_id: z.string().nullable().optional(), blocked: z.boolean().optional(), is_chat_read: z.boolean().optional(), active_ticket_v2: z.object({id: z.string().optional(), current_stage_id: z.string().nullable().optional()}).nullable().optional(), utm_events: z.array(z.object({id: z.string().optional(), created_at: z.string().nullable().optional(), customer_id: z.string().nullable().optional(), integration_type: z.string().nullable().optional(), ad_id: z.string().nullable().optional(), referrer_url: z.string().nullable().optional(), search_keyword: z.string().nullable().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 }; /** * Get contacts by time filters * * GET /api/public/contacts */ export const getApiApiPublicContacts = async (args: GetApiApiPublicContactsArgs, options?: GetApiApiPublicContactsOptions) => { const {baseUrl = "https://api.vambe.me", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.days != null) query.append("days", String(args.days)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.stage_id != null) query.append("stage_id", String(args.stage_id)) if (args.pipeline_id != null) query.append("pipeline_id", String(args.pipeline_id)) if (args.custom_field_key != null) query.append("custom_field_key", String(args.custom_field_key)) if (args.custom_field_value != null) query.append("custom_field_value", String(args.custom_field_value)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args.with_utm_events != null) query.append("with_utm_events", String(args.with_utm_events)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/contacts${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(getApiApiPublicContactsResponse, await res.json()) };