From 6c19dad23dc87c8cab9d89b4ebd2c82fb63f66ae Mon Sep 17 00:00:00 2001 From: Junyi Date: Fri, 7 Jul 2023 16:59:46 +0700 Subject: [PATCH] chore(plugin-workflow): adjust types (#2206) * chore(plugin-workflow): adjust types * fix(plugin-workflow): fix types --- packages/plugins/workflow/package.json | 7 ++++--- packages/plugins/workflow/src/client/index.tsx | 1 + packages/plugins/workflow/src/server/Plugin.ts | 5 ++--- packages/plugins/workflow/src/server/Processor.ts | 4 +--- .../plugins/workflow/src/server/__tests__/index.ts | 2 +- .../src/server/__tests__/instructions/update.test.ts | 4 ++-- packages/plugins/workflow/src/server/actions/nodes.ts | 2 +- .../plugins/workflow/src/server/functions/index.ts | 3 +-- packages/plugins/workflow/src/server/index.ts | 1 + .../workflow/src/server/instructions/aggregate.ts | 5 +++-- .../workflow/src/server/instructions/calculation.ts | 2 +- .../workflow/src/server/instructions/condition.ts | 10 +++++++--- .../plugins/workflow/src/server/instructions/create.ts | 2 +- .../plugins/workflow/src/server/instructions/delay.ts | 3 +-- .../workflow/src/server/instructions/destroy.ts | 2 +- .../plugins/workflow/src/server/instructions/index.ts | 4 ++-- .../plugins/workflow/src/server/instructions/loop.ts | 3 +-- .../src/server/instructions/manual/forms/index.ts | 2 +- .../workflow/src/server/instructions/parallel.ts | 3 +-- .../plugins/workflow/src/server/instructions/query.ts | 2 +- .../workflow/src/server/instructions/request.ts | 3 ++- .../plugins/workflow/src/server/instructions/update.ts | 2 +- .../plugins/workflow/src/server/triggers/collection.ts | 2 +- packages/plugins/workflow/src/server/triggers/index.ts | 2 +- .../plugins/workflow/src/server/triggers/schedule.ts | 2 +- .../workflow/src/server/{models => types}/Execution.ts | 0 .../workflow/src/server/{models => types}/FlowNode.ts | 0 .../workflow/src/server/{models => types}/Job.ts | 0 .../workflow/src/server/{models => types}/Workflow.ts | 0 packages/plugins/workflow/src/server/types/index.ts | 4 ++++ 30 files changed, 44 insertions(+), 38 deletions(-) rename packages/plugins/workflow/src/server/{models => types}/Execution.ts (100%) rename packages/plugins/workflow/src/server/{models => types}/FlowNode.ts (100%) rename packages/plugins/workflow/src/server/{models => types}/Job.ts (100%) rename packages/plugins/workflow/src/server/{models => types}/Workflow.ts (100%) create mode 100644 packages/plugins/workflow/src/server/types/index.ts diff --git a/packages/plugins/workflow/package.json b/packages/plugins/workflow/package.json index 369b2b633d..5b9d9e24ae 100644 --- a/packages/plugins/workflow/package.json +++ b/packages/plugins/workflow/package.json @@ -1,8 +1,8 @@ { "name": "@nocobase/plugin-workflow", - "displayName": "workflow", + "displayName": "Workflow", "displayName.zh-CN": "工作流", - "description": " a powerful workflow plugin designed to support business process management and automation. .", + "description": "A powerful workflow plugin designed to support business process management and automation.", "description.zh-CN": "工作流插件,为业务流程管理和自动化提供支持。", "version": "0.10.1-alpha.1", "license": "AGPL-3.0", @@ -25,7 +25,8 @@ "cron-parser": "4.4.0", "lru-cache": "8.0.5", "moment": "^2.29.2", - "react-js-cron": "^3.1.0" + "react-js-cron": "^3.1.0", + "sequelize": "^6.26.0" }, "devDependencies": { "react": "18.x", diff --git a/packages/plugins/workflow/src/client/index.tsx b/packages/plugins/workflow/src/client/index.tsx index c0097920df..2655e1057c 100644 --- a/packages/plugins/workflow/src/client/index.tsx +++ b/packages/plugins/workflow/src/client/index.tsx @@ -2,6 +2,7 @@ export * from './Branch'; export * from './FlowContext'; export * from './nodes'; export { triggers } from './triggers'; +export { useWorkflowVariableOptions } from './variable'; import { Plugin, useCollectionDataSource } from '@nocobase/client'; import React from 'react'; diff --git a/packages/plugins/workflow/src/server/Plugin.ts b/packages/plugins/workflow/src/server/Plugin.ts index d87d7b9e00..d33d28354c 100644 --- a/packages/plugins/workflow/src/server/Plugin.ts +++ b/packages/plugins/workflow/src/server/Plugin.ts @@ -11,14 +11,13 @@ import initFields from './fields'; import initActions from './actions'; import { EXECUTION_STATUS } from './constants'; import initInstructions, { Instruction } from './instructions'; -import ExecutionModel from './models/Execution'; -import JobModel from './models/Job'; -import WorkflowModel from './models/Workflow'; import Processor from './Processor'; import initTriggers, { Trigger } from './triggers'; import initFunctions, { CustomFunction } from './functions'; import { createLogger, Logger, LoggerOptions, getLoggerLevel, getLoggerFilePath } from '@nocobase/logger'; +import type { WorkflowModel, ExecutionModel, JobModel } from './types'; + type Pending = [ExecutionModel, JobModel?]; type ID = number | string; diff --git a/packages/plugins/workflow/src/server/Processor.ts b/packages/plugins/workflow/src/server/Processor.ts index cd484c1978..3492f5c017 100644 --- a/packages/plugins/workflow/src/server/Processor.ts +++ b/packages/plugins/workflow/src/server/Processor.ts @@ -5,9 +5,7 @@ import { parse } from '@nocobase/utils'; import Plugin from '.'; import { EXECUTION_STATUS, JOB_STATUS } from './constants'; import { Runner } from './instructions'; -import ExecutionModel from './models/Execution'; -import FlowNodeModel from './models/FlowNode'; -import JobModel from './models/Job'; +import type { ExecutionModel, FlowNodeModel, JobModel } from './types'; export interface ProcessorOptions extends Transactionable { plugin: Plugin; diff --git a/packages/plugins/workflow/src/server/__tests__/index.ts b/packages/plugins/workflow/src/server/__tests__/index.ts index fcc0d3765d..e653333011 100644 --- a/packages/plugins/workflow/src/server/__tests__/index.ts +++ b/packages/plugins/workflow/src/server/__tests__/index.ts @@ -4,7 +4,7 @@ import { lodash } from '@nocobase/utils'; import path from 'path'; import Plugin from '..'; import { JOB_STATUS } from '../constants'; -import FlowNodeModel from '../models/FlowNode'; +import type { FlowNodeModel } from '../types'; export function sleep(ms: number) { return new Promise((resolve) => { diff --git a/packages/plugins/workflow/src/server/__tests__/instructions/update.test.ts b/packages/plugins/workflow/src/server/__tests__/instructions/update.test.ts index 69a104fedf..e3f783f13a 100644 --- a/packages/plugins/workflow/src/server/__tests__/instructions/update.test.ts +++ b/packages/plugins/workflow/src/server/__tests__/instructions/update.test.ts @@ -1,14 +1,14 @@ import { Application } from '@nocobase/server'; import Database from '@nocobase/database'; import { getApp, sleep } from '..'; -import DefinedWorkflowModel from '../../models/Workflow'; +import type { WorkflowModel as WorkflowModelType } from '../../types'; describe('workflow > instructions > update', () => { let app: Application; let db: Database; let PostRepo; let WorkflowModel; - let workflow: DefinedWorkflowModel; + let workflow: WorkflowModelType; beforeEach(async () => { app = await getApp(); diff --git a/packages/plugins/workflow/src/server/actions/nodes.ts b/packages/plugins/workflow/src/server/actions/nodes.ts index 0bf5fcad10..d9a590a151 100644 --- a/packages/plugins/workflow/src/server/actions/nodes.ts +++ b/packages/plugins/workflow/src/server/actions/nodes.ts @@ -1,6 +1,6 @@ import { Context, utils } from '@nocobase/actions'; import { MultipleRelationRepository, Op } from '@nocobase/database'; -import WorkflowModel from '../models/Workflow'; +import type { WorkflowModel } from '../types'; export async function create(context: Context, next) { const { db } = context; diff --git a/packages/plugins/workflow/src/server/functions/index.ts b/packages/plugins/workflow/src/server/functions/index.ts index 1e0d9c07c5..ec0de60f30 100644 --- a/packages/plugins/workflow/src/server/functions/index.ts +++ b/packages/plugins/workflow/src/server/functions/index.ts @@ -1,6 +1,5 @@ import Plugin from '..'; -import ExecutionModel from '../models/Execution'; -import FlowNodeModel from '../models/FlowNode'; +import type { ExecutionModel, FlowNodeModel } from '../types'; export type CustomFunction = (this: { execution: ExecutionModel; node?: FlowNodeModel }) => any; diff --git a/packages/plugins/workflow/src/server/index.ts b/packages/plugins/workflow/src/server/index.ts index 4d2b97e163..d759225921 100644 --- a/packages/plugins/workflow/src/server/index.ts +++ b/packages/plugins/workflow/src/server/index.ts @@ -3,3 +3,4 @@ export * from './constants'; export { Trigger } from './triggers'; export { default as Processor } from './Processor'; export { default } from './Plugin'; +export * from './types'; diff --git a/packages/plugins/workflow/src/server/instructions/aggregate.ts b/packages/plugins/workflow/src/server/instructions/aggregate.ts index 112875431c..7917891e58 100644 --- a/packages/plugins/workflow/src/server/instructions/aggregate.ts +++ b/packages/plugins/workflow/src/server/instructions/aggregate.ts @@ -1,7 +1,8 @@ import { BelongsToManyRepository, DataTypes, HasManyRepository } from '@nocobase/database'; -import { JOB_STATUS } from '../constants'; -import FlowNodeModel from '../models/FlowNode'; + import Processor from '../Processor'; +import { JOB_STATUS } from '../constants'; +import type { FlowNodeModel } from '../types'; const aggregators = { count: 'count', diff --git a/packages/plugins/workflow/src/server/instructions/calculation.ts b/packages/plugins/workflow/src/server/instructions/calculation.ts index 4464e6b840..8a5e1e169c 100644 --- a/packages/plugins/workflow/src/server/instructions/calculation.ts +++ b/packages/plugins/workflow/src/server/instructions/calculation.ts @@ -3,7 +3,7 @@ import { parse } from '@nocobase/utils'; import { Instruction } from '.'; import { Processor } from '..'; import { JOB_STATUS } from '../constants'; -import FlowNodeModel from '../models/FlowNode'; +import type { FlowNodeModel } from '../types'; interface CalculationConfig { dynamic?: boolean | string; diff --git a/packages/plugins/workflow/src/server/instructions/condition.ts b/packages/plugins/workflow/src/server/instructions/condition.ts index cb00b01848..9df48fb12f 100644 --- a/packages/plugins/workflow/src/server/instructions/condition.ts +++ b/packages/plugins/workflow/src/server/instructions/condition.ts @@ -3,9 +3,11 @@ import { Registry } from '@nocobase/utils'; import { Instruction } from '.'; import { Processor } from '..'; import { JOB_STATUS } from '../constants'; -import FlowNodeModel from '../models/FlowNode'; +import type { FlowNodeModel } from '../types'; -export const calculators = new Registry(); +type Comparer = (a: any, b: any) => boolean; + +export const calculators = new Registry(); // built-in functions function equal(a, b) { @@ -92,7 +94,9 @@ type CalculationGroup = { type Calculation = CalculationItem | CalculationGroup; function calculate(calculation: CalculationItem = {}) { - let fn: Function; + type NewType = Comparer; + + let fn: NewType; if (!(calculation.calculator && (fn = calculators.get(calculation.calculator)))) { throw new Error(`no calculator function registered for "${calculation.calculator}"`); } diff --git a/packages/plugins/workflow/src/server/instructions/create.ts b/packages/plugins/workflow/src/server/instructions/create.ts index 39d5ea7ff6..758f2874fe 100644 --- a/packages/plugins/workflow/src/server/instructions/create.ts +++ b/packages/plugins/workflow/src/server/instructions/create.ts @@ -1,5 +1,5 @@ import { JOB_STATUS } from '../constants'; -import FlowNodeModel from '../models/FlowNode'; +import type { FlowNodeModel } from '../types'; export default { async run(node: FlowNodeModel, input, processor) { diff --git a/packages/plugins/workflow/src/server/instructions/delay.ts b/packages/plugins/workflow/src/server/instructions/delay.ts index c2b6013a72..57c667f2bd 100644 --- a/packages/plugins/workflow/src/server/instructions/delay.ts +++ b/packages/plugins/workflow/src/server/instructions/delay.ts @@ -1,9 +1,8 @@ import Plugin from '..'; import { EXECUTION_STATUS, JOB_STATUS } from '../constants'; -import ExecutionModel from '../models/Execution'; -import JobModel from '../models/Job'; import Processor from '../Processor'; import { Instruction } from '.'; +import type { ExecutionModel, JobModel } from '../types'; type ValueOf = T[keyof T]; diff --git a/packages/plugins/workflow/src/server/instructions/destroy.ts b/packages/plugins/workflow/src/server/instructions/destroy.ts index ccb2510c68..4aacf3ce7b 100644 --- a/packages/plugins/workflow/src/server/instructions/destroy.ts +++ b/packages/plugins/workflow/src/server/instructions/destroy.ts @@ -1,5 +1,5 @@ import { JOB_STATUS } from '../constants'; -import FlowNodeModel from '../models/FlowNode'; +import type { FlowNodeModel } from '../types'; export default { async run(node: FlowNodeModel, input, processor) { diff --git a/packages/plugins/workflow/src/server/instructions/index.ts b/packages/plugins/workflow/src/server/instructions/index.ts index bccda86858..ec9c24e836 100644 --- a/packages/plugins/workflow/src/server/instructions/index.ts +++ b/packages/plugins/workflow/src/server/instructions/index.ts @@ -2,11 +2,11 @@ import path from 'path'; import { requireModule } from '@nocobase/utils'; -import FlowNodeModel from '../models/FlowNode'; - import Plugin from '..'; import Processor from '../Processor'; +import type { FlowNodeModel } from '../types'; + export type Job = { status: number; result?: unknown; diff --git a/packages/plugins/workflow/src/server/instructions/loop.ts b/packages/plugins/workflow/src/server/instructions/loop.ts index 596ed631d2..a354c69d00 100644 --- a/packages/plugins/workflow/src/server/instructions/loop.ts +++ b/packages/plugins/workflow/src/server/instructions/loop.ts @@ -1,7 +1,6 @@ -import FlowNodeModel from '../models/FlowNode'; -import JobModel from '../models/Job'; import Processor from '../Processor'; import { JOB_STATUS } from '../constants'; +import type { FlowNodeModel, JobModel } from '../types'; function getTargetLength(target) { let length = 0; diff --git a/packages/plugins/workflow/src/server/instructions/manual/forms/index.ts b/packages/plugins/workflow/src/server/instructions/manual/forms/index.ts index 676c8c864e..b1fe09f627 100644 --- a/packages/plugins/workflow/src/server/instructions/manual/forms/index.ts +++ b/packages/plugins/workflow/src/server/instructions/manual/forms/index.ts @@ -6,7 +6,7 @@ import update from './update'; export type FormHandler = (this: ManualInstruction, instance, formConfig, processor: Processor) => Promise; -export default function({ formTypes }) { +export default function ({ formTypes }) { formTypes.register('create', create); formTypes.register('update', update); } diff --git a/packages/plugins/workflow/src/server/instructions/parallel.ts b/packages/plugins/workflow/src/server/instructions/parallel.ts index d0cda6a8bf..acd1bd91dc 100644 --- a/packages/plugins/workflow/src/server/instructions/parallel.ts +++ b/packages/plugins/workflow/src/server/instructions/parallel.ts @@ -1,7 +1,6 @@ -import FlowNodeModel from '../models/FlowNode'; -import JobModel from '../models/Job'; import Processor from '../Processor'; import { JOB_STATUS } from '../constants'; +import type { FlowNodeModel, JobModel } from '../types'; export const PARALLEL_MODE = { ALL: 'all', diff --git a/packages/plugins/workflow/src/server/instructions/query.ts b/packages/plugins/workflow/src/server/instructions/query.ts index 98bfb8cfcc..affeddce2e 100644 --- a/packages/plugins/workflow/src/server/instructions/query.ts +++ b/packages/plugins/workflow/src/server/instructions/query.ts @@ -1,6 +1,6 @@ -import FlowNodeModel from '../models/FlowNode'; import Processor from '../Processor'; import { JOB_STATUS } from '../constants'; +import type { FlowNodeModel } from '../types'; export default { async run(node: FlowNodeModel, input, processor: Processor) { diff --git a/packages/plugins/workflow/src/server/instructions/request.ts b/packages/plugins/workflow/src/server/instructions/request.ts index 41b950f084..a3cab2e63a 100644 --- a/packages/plugins/workflow/src/server/instructions/request.ts +++ b/packages/plugins/workflow/src/server/instructions/request.ts @@ -3,7 +3,7 @@ import axios, { AxiosRequestConfig } from 'axios'; import { Instruction } from './index'; import { JOB_STATUS } from '../constants'; import Processor from '../Processor'; -import FlowNodeModel from '../models/FlowNode'; +import type { FlowNodeModel } from '../types'; export interface Header { name: string; @@ -53,6 +53,7 @@ export default class implements Instruction { const config = processor.getParsedValue(node.config, node) as RequestConfig; + // eslint-disable-next-line promise/catch-or-return request(config) .then((response) => { job.set({ diff --git a/packages/plugins/workflow/src/server/instructions/update.ts b/packages/plugins/workflow/src/server/instructions/update.ts index 94848172e0..2af2ff6734 100644 --- a/packages/plugins/workflow/src/server/instructions/update.ts +++ b/packages/plugins/workflow/src/server/instructions/update.ts @@ -1,6 +1,6 @@ -import FlowNodeModel from '../models/FlowNode'; import Processor from '../Processor'; import { JOB_STATUS } from '../constants'; +import type { FlowNodeModel } from '../types'; export default { async run(node: FlowNodeModel, input, processor: Processor) { diff --git a/packages/plugins/workflow/src/server/triggers/collection.ts b/packages/plugins/workflow/src/server/triggers/collection.ts index 81e772e59a..71b84a9e2b 100644 --- a/packages/plugins/workflow/src/server/triggers/collection.ts +++ b/packages/plugins/workflow/src/server/triggers/collection.ts @@ -1,6 +1,6 @@ import { Collection, Model } from '@nocobase/database'; import { Trigger } from '..'; -import WorkflowModel from '../models/Workflow'; +import type { WorkflowModel } from '../types'; export interface CollectionChangeTriggerConfig { collection: string; diff --git a/packages/plugins/workflow/src/server/triggers/index.ts b/packages/plugins/workflow/src/server/triggers/index.ts index 1dc25c7b73..e89c9b47ac 100644 --- a/packages/plugins/workflow/src/server/triggers/index.ts +++ b/packages/plugins/workflow/src/server/triggers/index.ts @@ -2,7 +2,7 @@ import path from 'path'; import { requireModule } from '@nocobase/utils'; import Plugin from '..'; -import WorkflowModel from '../models/Workflow'; +import type { WorkflowModel } from '../types'; export abstract class Trigger { constructor(public readonly plugin: Plugin) {} diff --git a/packages/plugins/workflow/src/server/triggers/schedule.ts b/packages/plugins/workflow/src/server/triggers/schedule.ts index a974034004..48ddaeb673 100644 --- a/packages/plugins/workflow/src/server/triggers/schedule.ts +++ b/packages/plugins/workflow/src/server/triggers/schedule.ts @@ -1,7 +1,7 @@ import { fn, literal, Op, where } from '@nocobase/database'; import parser from 'cron-parser'; import Plugin, { Trigger } from '..'; -import WorkflowModel from '../models/Workflow'; +import type { WorkflowModel } from '../types'; export type ScheduleOnField = | string diff --git a/packages/plugins/workflow/src/server/models/Execution.ts b/packages/plugins/workflow/src/server/types/Execution.ts similarity index 100% rename from packages/plugins/workflow/src/server/models/Execution.ts rename to packages/plugins/workflow/src/server/types/Execution.ts diff --git a/packages/plugins/workflow/src/server/models/FlowNode.ts b/packages/plugins/workflow/src/server/types/FlowNode.ts similarity index 100% rename from packages/plugins/workflow/src/server/models/FlowNode.ts rename to packages/plugins/workflow/src/server/types/FlowNode.ts diff --git a/packages/plugins/workflow/src/server/models/Job.ts b/packages/plugins/workflow/src/server/types/Job.ts similarity index 100% rename from packages/plugins/workflow/src/server/models/Job.ts rename to packages/plugins/workflow/src/server/types/Job.ts diff --git a/packages/plugins/workflow/src/server/models/Workflow.ts b/packages/plugins/workflow/src/server/types/Workflow.ts similarity index 100% rename from packages/plugins/workflow/src/server/models/Workflow.ts rename to packages/plugins/workflow/src/server/types/Workflow.ts diff --git a/packages/plugins/workflow/src/server/types/index.ts b/packages/plugins/workflow/src/server/types/index.ts new file mode 100644 index 0000000000..247248e3e0 --- /dev/null +++ b/packages/plugins/workflow/src/server/types/index.ts @@ -0,0 +1,4 @@ +export type { default as WorkflowModel } from './Workflow'; +export type { default as FlowNodeModel } from './FlowNode'; +export type { default as ExecutionModel } from './Execution'; +export type { default as JobModel } from './Job';