import {z} from 'zod' export type PropertyCrmEventNote = {description?: string | undefined, title?: string | undefined}; export type PropertyCrmEventMeeting = {start_at?: string | undefined, end_at?: string | undefined, title?: string | undefined, description?: string | undefined}; export type PropertyCrmEventEmailTo = Array; export type PropertyCrmEventEmailCc = Array; export type PropertyCrmEventEmailAttachmentFileIds = Array; export type PropertyCrmEventEmail = {from?: string | undefined, to?: PropertyCrmEventEmailTo | undefined, cc?: PropertyCrmEventEmailCc | undefined, subject?: string | undefined, body?: string | undefined, attachment_file_ids?: PropertyCrmEventEmailAttachmentFileIds | undefined}; export type PropertyCrmEventCall = {duration?: number | undefined, description?: string | undefined, start_at?: string | undefined}; export type PropertyCrmEventTask = {name?: string | undefined, status?: ('COMPLETED' | 'NOT_STARTED' | 'WORK_IN_PROGRESS' | 'DEFERRED') | undefined, description?: string | undefined, due_at?: string | undefined, priority?: ('HIGH' | 'MEDIUM' | 'LOW') | undefined}; export type PropertyCrmEventMarketingEmailTo = Array; export type PropertyCrmEventMarketingEmailCc = Array; export type PropertyCrmEventMarketingEmailAttachmentFileIds = Array; export type PropertyCrmEventMarketingEmail = {name?: string | undefined, from?: string | undefined, to?: PropertyCrmEventMarketingEmailTo | undefined, cc?: PropertyCrmEventMarketingEmailCc | undefined, subject?: string | undefined, body?: string | undefined, attachment_file_ids?: PropertyCrmEventMarketingEmailAttachmentFileIds | undefined}; export type CrmEventFormOption = {label?: string | undefined, value?: string | undefined}; export type PropertyCrmEventFormFieldOptions = Array; export type CrmEventFormField = {name?: string | undefined, type?: ('TEXT' | 'NUMBER' | 'DATE' | 'BOOLEAN' | 'MULTIPLE_CHOICE' | 'FILE' | 'TEXTAREA' | 'SINGLE_SELECT' | 'MULTIPLE_SELECT' | 'EMAIL' | 'PHONE' | 'YES_NO' | 'CURRENCY' | 'URL') | undefined, options?: PropertyCrmEventFormFieldOptions | undefined, required?: boolean | undefined}; export type PropertyCrmEventFormFields = Array; export type PropertyCrmEventForm = {name?: string | undefined, archived_at?: string | undefined, fields?: PropertyCrmEventFormFields | undefined, redirect_url?: string | undefined}; export type PropertyCrmEventPageView = {count?: number | undefined, average?: number | undefined, url?: string | undefined}; export type PropertyCrmEventDealIds = Array; export type PropertyCrmEventCompanyIds = Array; export type PropertyCrmEventContactIds = Array; export type PropertyCrmEventLeadIds = 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 PropertyCrmEventMetadata = Array; export type CrmEvent = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, type?: ('NOTE' | 'EMAIL' | 'TASK' | 'MEETING' | 'CALL' | 'MARKETING_EMAIL' | 'FORM' | 'PAGE_VIEW') | undefined, note?: PropertyCrmEventNote | undefined, meeting?: PropertyCrmEventMeeting | undefined, email?: PropertyCrmEventEmail | undefined, call?: PropertyCrmEventCall | undefined, task?: PropertyCrmEventTask | undefined, marketing_email?: PropertyCrmEventMarketingEmail | undefined, form?: PropertyCrmEventForm | undefined, page_view?: PropertyCrmEventPageView | undefined, deal_ids?: PropertyCrmEventDealIds | undefined, company_ids?: PropertyCrmEventCompanyIds | undefined, contact_ids?: PropertyCrmEventContactIds | undefined, lead_ids?: PropertyCrmEventLeadIds | undefined, user_id?: string | undefined, metadata?: PropertyCrmEventMetadata | undefined, raw?: {[key: string]: unknown} | undefined}; export type CreateApiCrmConnectionIdEventArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'type' | 'note' | 'meeting' | 'email' | 'call' | 'task' | 'marketing_email' | 'form' | 'page_view' | 'deal_ids' | 'company_ids' | 'contact_ids' | 'lead_ids' | 'user_id' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, body: CrmEvent}; export type CreateApiCrmConnectionIdEventOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyCrmEventNote = z.object({description: z.string().optional(), title: z.string().optional()}); export const propertyCrmEventMeeting = z.object({start_at: z.string().optional(), end_at: z.string().optional(), title: z.string().optional(), description: z.string().optional()}); export const propertyCrmEventEmailTo = z.array(z.string()); export const propertyCrmEventEmailCc = z.array(z.string()); export const propertyCrmEventEmailAttachmentFileIds = z.array(z.string()); export const propertyCrmEventEmail = z.object({from: z.string().optional(), to: propertyCrmEventEmailTo.optional(), cc: propertyCrmEventEmailCc.optional(), subject: z.string().optional(), body: z.string().optional(), attachment_file_ids: propertyCrmEventEmailAttachmentFileIds.optional()}); export const propertyCrmEventCall = z.object({duration: z.number().optional(), description: z.string().optional(), start_at: z.string().optional()}); export const propertyCrmEventTask = z.object({name: z.string().optional(), status: z.enum(["COMPLETED", "NOT_STARTED", "WORK_IN_PROGRESS", "DEFERRED"]).optional(), description: z.string().optional(), due_at: z.string().optional(), priority: z.enum(["HIGH", "MEDIUM", "LOW"]).optional()}); export const propertyCrmEventMarketingEmailTo = z.array(z.string()); export const propertyCrmEventMarketingEmailCc = z.array(z.string()); export const propertyCrmEventMarketingEmailAttachmentFileIds = z.array(z.string()); export const propertyCrmEventMarketingEmail = z.object({name: z.string().optional(), from: z.string().optional(), to: propertyCrmEventMarketingEmailTo.optional(), cc: propertyCrmEventMarketingEmailCc.optional(), subject: z.string().optional(), body: z.string().optional(), attachment_file_ids: propertyCrmEventMarketingEmailAttachmentFileIds.optional()}); export const crmEventFormOption = z.object({label: z.string().optional(), value: z.string().optional()}); export const propertyCrmEventFormFieldOptions = z.array(crmEventFormOption); export const crmEventFormField = z.object({name: z.string().optional(), type: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "MULTIPLE_CHOICE", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "EMAIL", "PHONE", "YES_NO", "CURRENCY", "URL"]).optional(), options: propertyCrmEventFormFieldOptions.optional(), required: z.boolean().optional()}); export const propertyCrmEventFormFields = z.array(crmEventFormField); export const propertyCrmEventForm = z.object({name: z.string().optional(), archived_at: z.string().optional(), fields: propertyCrmEventFormFields.optional(), redirect_url: z.string().optional()}); export const propertyCrmEventPageView = z.object({count: z.number().optional(), average: z.number().optional(), url: z.string().optional()}); export const propertyCrmEventDealIds = z.array(z.string()); export const propertyCrmEventCompanyIds = z.array(z.string()); export const propertyCrmEventContactIds = z.array(z.string()); export const propertyCrmEventLeadIds = 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 propertyCrmEventMetadata = z.array(crmMetadata); export const crmEvent = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), type: z.enum(["NOTE", "EMAIL", "TASK", "MEETING", "CALL", "MARKETING_EMAIL", "FORM", "PAGE_VIEW"]).optional(), note: propertyCrmEventNote.optional(), meeting: propertyCrmEventMeeting.optional(), email: propertyCrmEventEmail.optional(), call: propertyCrmEventCall.optional(), task: propertyCrmEventTask.optional(), marketing_email: propertyCrmEventMarketingEmail.optional(), form: propertyCrmEventForm.optional(), page_view: propertyCrmEventPageView.optional(), deal_ids: propertyCrmEventDealIds.optional(), company_ids: propertyCrmEventCompanyIds.optional(), contact_ids: propertyCrmEventContactIds.optional(), lead_ids: propertyCrmEventLeadIds.optional(), user_id: z.string().optional(), metadata: propertyCrmEventMetadata.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 }; /** * Create an event * * POST /crm/{connection_id}/event */ export const createApiCrmConnectionIdEvent = async (args: CreateApiCrmConnectionIdEventArgs, options?: CreateApiCrmConnectionIdEventOptions) => { 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))}/event${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(crmEvent, await res.json()) };