import {z} from 'zod' export type CRMActionType = 'note' | 'task' | 'post' | 'lead'; export type TaskPriority = 'LOW' | 'MEDIUM' | 'HIGH'; export type TaskOptions = {subject?: (string | null) | undefined, body?: (string | null) | undefined, due_date?: (string | null) | undefined, priority?: (TaskPriority | null) | undefined, owner_id?: (string | null) | undefined}; export type PostOptions = {title?: (string | null) | undefined, body?: (string | null) | undefined}; export type LeadOptions = {last_name: string, company: string, first_name?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, title?: (string | null) | undefined, status?: (string | null) | undefined, rating?: (string | null) | undefined, lead_source?: (string | null) | undefined, description?: (string | null) | undefined, owner_id?: (string | null) | undefined}; export type CRMActionRequest = {action_type: CRMActionType, task_options?: (TaskOptions | null) | undefined, post_options?: (PostOptions | null) | undefined, lead_options?: (LeadOptions | null) | undefined, subscription_id?: (string | null) | undefined}; export type CreateApiApiV3CrmIntegrationIdSignalsSignalIdArgs = {integration_id: string, signal_id: string, body: CRMActionRequest}; export type CreateApiApiV3CrmIntegrationIdSignalsSignalIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const cRMActionType = z.enum(["note", "task", "post", "lead"]); export const cRMActionResponse = z.object({success: z.boolean(), action_type: cRMActionType, crm_id: z.string(), company_id: z.string(), message: z.string()}); 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 }; /** * Push Signal to CRM * * POST /api/v3/crm/{integration_id}/signals/{signal_id} */ export const createApiApiV3CrmIntegrationIdSignalsSignalId = async (args: CreateApiApiV3CrmIntegrationIdSignalsSignalIdArgs, options: CreateApiApiV3CrmIntegrationIdSignalsSignalIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/crm/${encodeURIComponent(String(args.integration_id))}/signals/${encodeURIComponent(String(args.signal_id))}${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(cRMActionResponse, await res.json()) };