import {z} from 'zod' export type GetApiApiV3CrmIntegrationIdContactsSearchArgs = {integration_id: string, query?: (string | null) | undefined, email?: (string | null) | undefined, firstname?: (string | null) | undefined, lastname?: (string | null) | undefined, phone?: (string | null) | undefined, company?: (string | null) | undefined, jobtitle?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, country?: (string | null) | undefined, owner_id?: (string | null) | undefined, limit?: number | undefined, after?: (string | null) | undefined, properties?: (string | null) | undefined}; export type GetApiApiV3CrmIntegrationIdContactsSearchOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const appRoutesV3CrmContactsContactResponse = z.object({id: z.string(), properties: z.record(z.string(), z.unknown()), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const contactSearchResponse = z.object({total: z.number().int(), results: z.array(appRoutesV3CrmContactsContactResponse), has_more: z.boolean().optional(), next_cursor: z.string().nullable().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 * * GET /api/v3/crm/{integration_id}/contacts/search */ export const getApiApiV3CrmIntegrationIdContactsSearch = async (args: GetApiApiV3CrmIntegrationIdContactsSearchArgs, options: GetApiApiV3CrmIntegrationIdContactsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.email != null) query.append("email", String(args.email)) if (args.firstname != null) query.append("firstname", String(args.firstname)) if (args.lastname != null) query.append("lastname", String(args.lastname)) if (args.phone != null) query.append("phone", String(args.phone)) if (args.company != null) query.append("company", String(args.company)) if (args.jobtitle != null) query.append("jobtitle", String(args.jobtitle)) if (args.city != null) query.append("city", String(args.city)) if (args.state != null) query.append("state", String(args.state)) if (args.country != null) query.append("country", String(args.country)) if (args.owner_id != null) query.append("owner_id", String(args.owner_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args.properties != null) query.append("properties", String(args.properties)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/crm/${encodeURIComponent(String(args.integration_id))}/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(contactSearchResponse, await res.json()) };