import {z} from 'zod' export type GetApiWorkflowsIdArgs = {id: number}; export type GetApiWorkflowsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiWorkflowsIdResponse = z.object({id: z.number(), name: z.string().optional(), filter: z.object({ids: z.array(z.number()).optional()}).optional(), individual_record_filter: z.object({condition: z.enum(["AND", "OR"]), rules: z.array(z.object({id: z.string(), query_builder_id: z.string().optional(), customfield_id: z.string(), label: z.string().optional(), type: z.enum(["integer", "double", "boolean", "tri_state_boolean", "string", "long_string", "select", "date", "datetime", "file"]).optional(), input: z.string().optional(), operator: z.enum(["equal", "not_equal", "in", "not_in", "less", "less_or_equal", "greater", "greater_or_equal", "between", "not_between", "begins_with", "not_begins_with", "contains", "not_contains", "ends_with", "not_ends_with", "is_empty", "is_not_empty", "is_null", "is_not_null", "true", "false", "past_less", "past_exactly", "past_more_than", "future_less", "future_exactly", "future_more_than", "after", "on", "before", "is", "is_not"]), value: z.array(z.string()).optional(), entity: z.enum(["account", "person", "opportunity", "campaign", "tag", "task", "workflow", "workflow_record", "workflow_step_record", "lead"]).optional(), display_entity: z.enum(["Account", "Contact", "Opportunity", "Campaign", "Tag", "Task", "Workflow", "Lead"]).optional(), pipeline: z.number().optional(), enabled: z.string(), internal: z.boolean().optional(), archived: z.boolean().optional(), options: z.array(z.object({})).optional(), group: z.enum(["Account", "Contact", "Opportunity", "Campaign", "Tag", "Task", "Workflow", "Lead"]).optional(), raw_value: z.array(z.object({})).optional(), search_entity: z.enum(["account", "contact", "user", "tag", "pipeline", "workflow", "lead"]).optional(), sortable: z.boolean().optional()})), order_by: z.string().optional(), not: z.boolean().optional(), valid: z.boolean().optional(), search: z.string().optional()}).optional(), team: z.number(), status: z.enum(["draft", "live", "paused", "finished", "failed", "scheduled"]).optional(), status_message: z.string().optional(), record_type: z.literal("contact").optional(), goal: z.enum(["opened", "clicked", "replied"]).optional(), continuous: z.boolean().optional(), exit_after_days: z.number().optional(), schedule_date: z.string().optional(), schedule_time_start: z.string().optional(), schedule_time_end: z.string().optional(), schedule_days: z.array(z.number().gte(0).lte(6)).optional(), analytics: z.object({total: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), in: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), exited: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), met_goal: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional()}).optional(), archived: z.boolean().optional(), steps: z.array(z.object({name: z.string().optional(), order: z.number(), filter: z.object({condition: z.enum(["AND", "OR"]), rules: z.array(z.object({id: z.string(), query_builder_id: z.string().optional(), customfield_id: z.string(), label: z.string().optional(), type: z.enum(["integer", "double", "boolean", "tri_state_boolean", "string", "long_string", "select", "date", "datetime", "file"]).optional(), input: z.string().optional(), operator: z.enum(["equal", "not_equal", "in", "not_in", "less", "less_or_equal", "greater", "greater_or_equal", "between", "not_between", "begins_with", "not_begins_with", "contains", "not_contains", "ends_with", "not_ends_with", "is_empty", "is_not_empty", "is_null", "is_not_null", "true", "false", "past_less", "past_exactly", "past_more_than", "future_less", "future_exactly", "future_more_than", "after", "on", "before", "is", "is_not"]), value: z.array(z.string()).optional(), entity: z.enum(["account", "person", "opportunity", "campaign", "tag", "task", "workflow", "workflow_record", "workflow_step_record", "lead"]).optional(), display_entity: z.enum(["Account", "Contact", "Opportunity", "Campaign", "Tag", "Task", "Workflow", "Lead"]).optional(), pipeline: z.number().optional(), enabled: z.string(), internal: z.boolean().optional()})), order_by: z.string().optional(), not: z.boolean().optional(), valid: z.boolean().optional(), search: z.string().optional()}).optional(), trigger_step: z.number().optional(), trigger_step_index: z.number().optional(), trigger_after_days: z.number().optional(), trigger: z.enum(["sent", "opened", "clicked", "replied", "not_opened", "not_replied", "not_clicked"]).optional(), action_type: z.literal("send_email").optional(), action_config: z.object({}).optional(), action_status: z.string().optional(), action_source: z.number().optional(), action_source_email: z.string().optional(), analytics: z.object({total: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), sent: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), interacted: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), replied: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), failed: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional()}).optional(), metrics_data: z.object({}).optional(), id: z.number(), workflow: z.number().optional(), team: z.number().optional(), archived: z.boolean().optional(), archived_by: z.number().optional(), archived_date: z.string().optional(), created_by: z.object({id: z.number(), name: z.string().optional(), email: z.string().optional(), picture: z.string().optional()}).optional(), last_modified_by: z.number().optional(), creation_date: z.string().optional(), modification_date: z.string().optional()})).optional(), creator_name: z.string().optional(), created_by: z.number().optional(), last_modified_by: z.number().optional(), can_edit: z.boolean().optional(), creation_date: z.string().optional(), modification_date: z.string().optional(), step_amount: z.number().optional(), multi_step_before_pro_release: z.boolean().optional(), scheduled_before_pro_release: z.boolean().optional(), workflow_steps: z.array(z.object({name: z.string().optional(), order: z.number(), filter: z.object({condition: z.enum(["AND", "OR"]), rules: z.array(z.object({id: z.string(), query_builder_id: z.string().optional(), customfield_id: z.string(), label: z.string().optional(), type: z.enum(["integer", "double", "boolean", "tri_state_boolean", "string", "long_string", "select", "date", "datetime", "file"]).optional(), input: z.string().optional(), operator: z.enum(["equal", "not_equal", "in", "not_in", "less", "less_or_equal", "greater", "greater_or_equal", "between", "not_between", "begins_with", "not_begins_with", "contains", "not_contains", "ends_with", "not_ends_with", "is_empty", "is_not_empty", "is_null", "is_not_null", "true", "false", "past_less", "past_exactly", "past_more_than", "future_less", "future_exactly", "future_more_than", "after", "on", "before", "is", "is_not"]), value: z.array(z.string()).optional(), entity: z.enum(["account", "person", "opportunity", "campaign", "tag", "task", "workflow", "workflow_record", "workflow_step_record", "lead"]).optional(), display_entity: z.enum(["Account", "Contact", "Opportunity", "Campaign", "Tag", "Task", "Workflow", "Lead"]).optional(), pipeline: z.number().optional(), enabled: z.string(), internal: z.boolean().optional()})), order_by: z.string().optional(), not: z.boolean().optional(), valid: z.boolean().optional(), search: z.string().optional()}).optional(), trigger_step: z.number().optional(), trigger_step_index: z.number().optional(), trigger_after_days: z.number().optional(), trigger: z.enum(["sent", "opened", "clicked", "replied", "not_opened", "not_replied", "not_clicked"]).optional(), action_type: z.literal("send_email").optional(), action_config: z.object({}).optional(), action_status: z.string().optional(), action_source: z.number().optional(), action_source_email: z.string().optional(), analytics: z.object({total: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), sent: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), interacted: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), replied: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional(), failed: z.object({name: z.string().optional(), value: z.number().optional(), type: z.enum(["absolute", "percentage"]).optional()}).optional()}).optional(), metrics_data: z.object({}).optional(), id: z.number(), workflow: z.number().optional(), team: z.number().optional(), archived: z.boolean().optional(), archived_by: z.number().optional(), archived_date: z.string().optional(), created_by: z.object({id: z.number(), name: z.string().optional(), email: z.string().optional(), picture: z.string().optional()}).optional(), last_modified_by: z.number().optional(), creation_date: z.string().optional(), modification_date: z.string().optional()})).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 }; /** * Get workflow details * * GET /workflows/{id} */ export const getApiWorkflowsId = async (args: GetApiWorkflowsIdArgs, options?: GetApiWorkflowsIdOptions) => { const {baseUrl = "https://api.salesflare.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/workflows/${encodeURIComponent(String(args.id))}${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(getApiWorkflowsIdResponse, await res.json()) };