import {z} from 'zod' export type NodeUpdate = {node_type?: (string | null) | undefined, configuration?: (Record | null) | undefined, dependencies?: (Array | null) | undefined, is_output_node?: (boolean | null) | undefined}; export type EdgeConditional = {position: number, operator: '=' | '!=' | '<' | '>' | '<=' | '>=' | 'in' | 'not_in' | 'is_null' | 'is_not_null', left_config: Record, right_config: Record}; export type Edge = {source_node_name: string, target_node_name: string, is_and: boolean, conditionals: Array}; export type PatchWorkflow = {base_version?: (number | null) | undefined, commit_message?: (string | null) | undefined, nodes?: ({[key: string]: NodeUpdate | null} | null) | undefined, required_input_variables?: ({[key: string]: string} | null) | undefined, edges?: (Array | null) | undefined, release_labels?: (Array | null) | undefined}; export type PatchApiRestWorkflowsWorkflowIdOrNameArgs = {workflow_id_or_name: string, body: PatchWorkflow}; export type PatchApiRestWorkflowsWorkflowIdOrNameOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const externalId = z.object({source: z.string().min(1).max(128), external_id: z.string().min(1).max(512)}); export const createWorkflowResponse = z.object({success: z.boolean(), workflow_id: z.number().int(), workflow_name: z.string(), workflow_version_id: z.number().int(), version_number: z.number().int(), base_version: z.number().int().nullable().optional(), release_labels: z.array(z.string()).nullable().optional(), nodes: z.array(z.object({id: z.string().optional(), name: z.string().optional(), node_type: z.string().optional(), is_output_node: z.boolean().optional()})).optional(), required_input_variables: z.record(z.string(), z.string()).optional(), external_ids: z.array(externalId)}); 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 }; /** * Patch Workflow * * PATCH /rest/workflows/{workflow_id_or_name} */ export const patchApiRestWorkflowsWorkflowIdOrName = async (args: PatchApiRestWorkflowsWorkflowIdOrNameArgs, options: PatchApiRestWorkflowsWorkflowIdOrNameOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/rest/workflows/${encodeURIComponent(String(args.workflow_id_or_name))}${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(createWorkflowResponse, await res.json()) };