import {z} from 'zod' export type WorkflowState = 'active' | 'inactive'; export type SortDirection = 'asc' | 'desc'; export type GetApiApiV1WorkflowsArgs = {dag_node_configuration_id?: (string | null) | undefined, source_id?: (string | string | null) | undefined, destination_id?: (string | string | null) | undefined, status?: (WorkflowState | null) | undefined, page?: (number | null) | undefined, page_size?: (number | null) | undefined, created_since?: (string | null) | undefined, created_before?: (string | null) | undefined, name?: (string | null) | undefined, sort_by?: string | undefined, sort_direction?: SortDirection | undefined, show_only_soft_deleted?: (boolean | null) | undefined, 'unstructured-api-key'?: (string | null) | undefined}; export type GetApiApiV1WorkflowsOptions = {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 getApiApiV1WorkflowsResponse = z.array(workflowInformation); 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 }; /** * List Workflows * * GET /api/v1/workflows/ */ export const getApiApiV1Workflows = async (args: GetApiApiV1WorkflowsArgs, options: GetApiApiV1WorkflowsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.dag_node_configuration_id != null) query.append("dag_node_configuration_id", String(args.dag_node_configuration_id)) if (args.source_id != null) query.append("source_id", String(args.source_id)) if (args.destination_id != null) query.append("destination_id", String(args.destination_id)) if (args.status != null) query.append("status", String(args.status)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.created_since != null) query.append("created_since", String(args.created_since)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.name != null) query.append("name", String(args.name)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.show_only_soft_deleted != null) query.append("show_only_soft_deleted", String(args.show_only_soft_deleted)) if (args['unstructured-api-key'] != null) headers.set("unstructured-api-key", String(args['unstructured-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/workflows/${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(getApiApiV1WorkflowsResponse, await res.json()) };