import {z} from 'zod' export type CrmEmail = {email?: string | undefined, type?: ('WORK' | 'HOME' | 'OTHER') | undefined}; export type PropertyCrmContactEmails = Array; export type CrmTelephone = {telephone: string, type?: ('WORK' | 'HOME' | 'OTHER' | 'FAX' | 'MOBILE') | undefined}; export type PropertyCrmContactTelephones = Array; export type PropertyCrmContactDealIds = Array; export type PropertyCrmContactCompanyIds = Array; export type PropertyCrmContactAddress = {address1?: string | undefined, address2?: string | undefined, city?: string | undefined, region?: string | undefined, region_code?: string | undefined, postal_code?: string | undefined, country?: string | undefined, country_code?: string | undefined}; export type PropertyCrmContactLinkUrls = Array; export type CrmMetadata = {id?: string | undefined, slug?: string | undefined, value?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined, namespace?: string | undefined, format?: ('TEXT' | 'NUMBER' | 'DATE' | 'BOOLEAN' | 'FILE' | 'TEXTAREA' | 'SINGLE_SELECT' | 'MULTIPLE_SELECT' | 'MEASUREMENT' | 'PRICE' | 'YES_NO' | 'CURRENCY' | 'URL') | undefined, extra_data?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined}; export type PropertyCrmContactMetadata = Array; export type CrmContact = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, name?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, title?: string | undefined, company?: string | undefined, emails?: PropertyCrmContactEmails | undefined, telephones?: PropertyCrmContactTelephones | undefined, deal_ids?: PropertyCrmContactDealIds | undefined, company_ids?: PropertyCrmContactCompanyIds | undefined, address?: PropertyCrmContactAddress | undefined, user_id?: string | undefined, link_urls?: PropertyCrmContactLinkUrls | undefined, metadata?: PropertyCrmContactMetadata | undefined, department?: string | undefined, image_url?: string | undefined, raw?: {[key: string]: unknown} | undefined}; export type UpdateApiCrmConnectionIdContactIdArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'name' | 'first_name' | 'last_name' | 'title' | 'company' | 'emails' | 'telephones' | 'deal_ids' | 'company_ids' | 'address' | 'user_id' | 'link_urls' | 'metadata' | 'department' | 'image_url' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, id: string, body: CrmContact}; export type UpdateApiCrmConnectionIdContactIdOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const crmEmail = z.object({email: z.string().optional(), type: z.enum(["WORK", "HOME", "OTHER"]).optional()}); export const propertyCrmContactEmails = z.array(crmEmail); export const crmTelephone = z.object({telephone: z.string(), type: z.enum(["WORK", "HOME", "OTHER", "FAX", "MOBILE"]).optional()}); export const propertyCrmContactTelephones = z.array(crmTelephone); export const propertyCrmContactDealIds = z.array(z.string()); export const propertyCrmContactCompanyIds = z.array(z.string()); export const propertyCrmContactAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional()}); export const propertyCrmContactLinkUrls = z.array(z.string()); export const crmMetadata = z.object({id: z.string().optional(), slug: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional()}); export const propertyCrmContactMetadata = z.array(crmMetadata); export const crmContact = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), title: z.string().optional(), company: z.string().optional(), emails: propertyCrmContactEmails.optional(), telephones: propertyCrmContactTelephones.optional(), deal_ids: propertyCrmContactDealIds.optional(), company_ids: propertyCrmContactCompanyIds.optional(), address: propertyCrmContactAddress.optional(), user_id: z.string().optional(), link_urls: propertyCrmContactLinkUrls.optional(), metadata: propertyCrmContactMetadata.optional(), department: z.string().optional(), image_url: z.string().optional(), raw: z.record(z.string(), z.unknown()).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 }; /** * Update a contact * * PUT /crm/{connection_id}/contact/{id} */ export const updateApiCrmConnectionIdContactId = async (args: UpdateApiCrmConnectionIdContactIdArgs, options?: UpdateApiCrmConnectionIdContactIdOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/crm/${encodeURIComponent(String(args.connection_id))}/contact/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(crmContact, await res.json()) };