import {z} from 'zod' export type GetApiApiPublicContactsSearchArgs = {phone?: string | undefined, email?: string | undefined, with_utm_events?: boolean | undefined, 'x-api-key': string}; export type GetApiApiPublicContactsSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPublicContactsSearchResponse = 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(), 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 }; /** * Search contacts by phone or email * * GET /api/public/contacts/search */ export const getApiApiPublicContactsSearch = async (args: GetApiApiPublicContactsSearchArgs, options?: GetApiApiPublicContactsSearchOptions) => { 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.phone != null) query.append("phone", String(args.phone)) if (args.email != null) query.append("email", String(args.email)) 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/search${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(getApiApiPublicContactsSearchResponse, await res.json()) };