import {z} from 'zod' export type WorkflowType = 'basic' | 'advanced' | 'platinum' | 'custom'; export type WorkflowNode = {id?: (string | null) | undefined, name: string, type: string, subtype: string, settings?: ({[key: string]: unknown} | null) | undefined}; export type WorkflowState = 'active' | 'inactive'; export type UpdateWorkflow = {name?: (string | null) | undefined, source_id?: (string | string | null) | undefined, destination_id?: (string | string | null) | undefined, workflow_type?: (WorkflowType | null) | undefined, workflow_nodes?: (Array | null) | undefined, template_id?: (string | null) | undefined, schedule?: ('every 15 minutes' | 'every hour' | 'every 2 hours' | 'every 4 hours' | 'every 6 hours' | 'every 8 hours' | 'every 10 hours' | 'every 12 hours' | 'daily' | 'weekly' | 'monthly' | null) | undefined, reprocess_all?: (boolean | null) | undefined, skip_preflight?: (boolean | null) | undefined, state?: (WorkflowState | null) | undefined}; export type UpdateApiApiV1WorkflowsWorkflowIdArgs = {workflow_id: string | string, 'unstructured-api-key'?: (string | null) | undefined, body: UpdateWorkflow}; export type UpdateApiApiV1WorkflowsWorkflowIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const workflowType = z.enum(["basic", "advanced", "platinum", "custom"]); export const workflowNode = z.object({id: z.string().nullable().optional(), name: z.string(), type: z.string(), subtype: z.string(), settings: z.record(z.string(), z.unknown()).nullable().optional()}); export const cronTabEntry = z.object({cron_expression: z.string()}); export const workflowSchedule = z.object({crontab_entries: z.array(cronTabEntry)}); export const workflowState = z.enum(["active", "inactive"]); export const workflowEditLockResponse = z.object({locked_by: z.string().nullable(), locked_at: z.string(), expires_at: z.string(), held_by_you: z.boolean()}); export const workflowInformation = z.object({id: z.string(), name: z.string(), key: z.string().nullable().optional(), sources: z.array(z.string()), destinations: z.array(z.string()), workflow_type: workflowType.nullable().optional(), workflow_nodes: z.array(workflowNode), schedule: workflowSchedule.nullable().optional(), status: workflowState, created_at: z.string(), updated_at: z.string().nullable().optional(), reprocess_all: z.boolean().optional(), skip_preflight: z.boolean().optional(), edit_lock: workflowEditLockResponse.nullable().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 Workflow * * PUT /api/v1/workflows/{workflow_id} */ export const updateApiApiV1WorkflowsWorkflowId = async (args: UpdateApiApiV1WorkflowsWorkflowIdArgs, options: UpdateApiApiV1WorkflowsWorkflowIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['unstructured-api-key'] != null) headers.set("unstructured-api-key", String(args['unstructured-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/workflows/${encodeURIComponent(String(args.workflow_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(workflowInformation, await res.json()) };