import {z} from 'zod' export type WorkflowNode = {name: string, node_type: string, configuration: {[key: string]: unknown}, dependencies?: Array | undefined, is_output_node: boolean}; 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 ExternalId = {source: string, external_id: string}; export type CreateWorkflow = {name?: (string | null) | undefined, workflow_id?: (number | null) | undefined, workflow_name?: (string | null) | undefined, folder_id?: (number | null) | undefined, commit_message?: (string | null) | undefined, nodes: Array, required_input_variables?: {[key: string]: string} | undefined, edges?: (Array | null) | undefined, release_labels?: (Array | null) | undefined, external_ids?: Array | undefined}; export type CreateApiRestWorkflowsArgs = {body: CreateWorkflow}; export type CreateApiRestWorkflowsOptions = {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 }; /** * Create Workflow * * POST /rest/workflows */ export const createApiRestWorkflows = async (args: CreateApiRestWorkflowsArgs, options: CreateApiRestWorkflowsOptions) => { 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${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(createWorkflowResponse, await res.json()) };