import {z} from 'zod' export type IntegerOrPublicId = number | string; export type WorkflowsTriggerParametersCreate = {workflows_trigger: {event_type_key: '$appointments.appointment_scheduled' | '$calendar_event.contact_registered' | '$community.join' | '$community.group_join' | '$community.topic_join' | '$community.post' | '$community.ban' | '$community.unban' | '$community.post_view' | '$contact.tag_applied' | '$contact.tag_removed' | '$course.complete' | '$course.enroll' | '$course.unenroll' | '$course.lesson_complete' | '$course.lesson_view' | '$course.section_complete' | '$email.bounce' | '$email.click' | '$email.open' | '$email.sent' | '$email.spam_complaint' | '$send_email_step.bounce' | '$send_email_step.click' | '$send_email_step.open' | '$send_email_step.sent' | '$send_email_step.spam_complaint' | '$order.successful_purchase' | '$order.cancelled' | '$order.churned' | '$subscription.upgraded' | '$subscription.downgraded' | '$transaction.renewal_failed' | '$sales.contact_entered_stage' | '$sales.contact_exited_stage' | '$optin' | '$view' | '$message_hub_event.contact_conversation_started' | '$message_hub_event.agent_conversation_started' | '$message_hub_event.contact_responded' | '$message_hub_event.agent_responded' | '$survey.complete', active?: boolean | undefined, allow_anonymous_contacts?: boolean | undefined, subscription_cancelation_reason?: string | undefined, contacts_tag_id?: IntegerOrPublicId | undefined, funnel_id?: IntegerOrPublicId | undefined, page_id?: IntegerOrPublicId | undefined, product_id?: IntegerOrPublicId | undefined, cancellation_product_id?: IntegerOrPublicId | undefined, product_variant_id?: IntegerOrPublicId | undefined, cancellation_product_variant_id?: IntegerOrPublicId | undefined, subscription_variant_to_id?: IntegerOrPublicId | undefined, sales_pipelines_id?: IntegerOrPublicId | undefined, current_sales_pipelines_stage_id?: IntegerOrPublicId | undefined, appointments_calendar_id?: IntegerOrPublicId | undefined, appointments_event_type_id?: IntegerOrPublicId | undefined, course_id?: IntegerOrPublicId | undefined, courses_lesson_id?: IntegerOrPublicId | undefined, courses_section_id?: IntegerOrPublicId | undefined, community_id?: IntegerOrPublicId | undefined, communities_space_id?: IntegerOrPublicId | undefined, communities_space_group_id?: IntegerOrPublicId | undefined, emails_broadcasts_id?: IntegerOrPublicId | undefined, calendar_event_id?: IntegerOrPublicId | undefined, survey_workflow_id?: IntegerOrPublicId | undefined, workflows_step_id?: IntegerOrPublicId | undefined}}; export type CreateApiWorkflowsWorkflowIdTriggersArgs = {workflow_id: string, body: WorkflowsTriggerParametersCreate}; export type CreateApiWorkflowsWorkflowIdTriggersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowsTriggerAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), workflow_id: z.number().int().optional(), event_type_key: z.string().nullable().optional(), event_type_name: z.string().nullable().optional(), active: z.boolean().optional(), allow_anonymous_contacts: z.boolean().optional(), subscription_cancelation_reason: z.string().nullable().optional(), contacts_tag_id: z.number().int().nullable().optional(), funnel_id: z.number().int().nullable().optional(), page_id: z.number().int().nullable().optional(), product_id: z.number().int().nullable().optional(), cancellation_product_id: z.number().int().nullable().optional(), product_variant_id: z.number().int().nullable().optional(), cancellation_product_variant_id: z.number().int().nullable().optional(), subscription_variant_to_id: z.number().int().nullable().optional(), sales_pipelines_id: z.number().int().nullable().optional(), current_sales_pipelines_stage_id: z.number().int().nullable().optional(), appointments_calendar_id: z.number().int().nullable().optional(), appointments_event_type_id: z.number().int().nullable().optional(), course_id: z.number().int().nullable().optional(), courses_lesson_id: z.number().int().nullable().optional(), courses_section_id: z.number().int().nullable().optional(), community_id: z.number().int().nullable().optional(), communities_space_id: z.number().int().nullable().optional(), communities_space_group_id: z.number().int().nullable().optional(), emails_broadcasts_id: z.number().int().nullable().optional(), calendar_event_id: z.number().int().nullable().optional(), survey_workflow_id: z.number().int().nullable().optional(), workflows_step_id: z.number().int().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().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 Trigger * * POST /workflows/{workflow_id}/triggers */ export const createApiWorkflowsWorkflowIdTriggers = async (args: CreateApiWorkflowsWorkflowIdTriggersArgs, options?: CreateApiWorkflowsWorkflowIdTriggersOptions) => { const {baseUrl = "https://myworkspace.myclickfunnels.com/api/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/workflows/${encodeURIComponent(String(args.workflow_id))}/triggers${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(workflowsTriggerAttributes, await res.json()) };