import {z} from 'zod' export type IntegerOrPublicId = number | string; export type WorkflowsStepParametersUpdate = {workflows_step: {name?: string | undefined, parent_step_id?: IntegerOrPublicId | undefined, branch?: string | undefined, position?: number | undefined, after_step_id?: IntegerOrPublicId | undefined, step_type_settings?: Record | undefined}}; export type PatchApiWorkflowsStepsIdArgs = {id: string, body: WorkflowsStepParametersUpdate}; export type PatchApiWorkflowsStepsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowsStepAttributes = z.object({id: z.number().int().optional(), public_id: z.string().nullable().optional(), workflow_id: z.number().int().optional(), step_type: z.string().optional(), name: z.string().optional(), sort_order: z.number().int().optional(), state: z.enum(["active", "notSetup", "notDefined"]).optional(), parent_step_id: z.number().int().nullable().optional(), branch: z.string().nullable().optional(), convergence_step_id: z.number().int().nullable().optional(), step_type_settings: z.object({}).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 }; /** * Update Step * * PATCH /workflows/steps/{id} */ export const patchApiWorkflowsStepsId = async (args: PatchApiWorkflowsStepsIdArgs, options?: PatchApiWorkflowsStepsIdOptions) => { 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/steps/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(workflowsStepAttributes, await res.json()) };