automated_api/src/modules/flow/nodes/start/index.ts
lixin 0afa56d652 build(automated): 更新项目依赖并调整配置
- 更新项目名称为 automated
- 更新多个依赖包版本,包括 @cool-midway、@midwayjs、@langchain 等
- 更新作者信息为 lixin&liqiannan
- 更新 TypeScript 版本至 5.5.4
- 添加 newman、prettier 等新依赖
- 优化 package.json 结构和格式
2025-01-09 17:58:04 +08:00

93 lines
2.2 KiB
TypeScript

import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { FlowNode } from '../../runner/node';
import { FlowContext } from '../../runner/context';
import { CoolCommException } from '@cool-midway/core';
import * as _ from 'lodash';
/**
* 开始节点
*/
@Provide()
@Scope(ScopeEnum.Prototype)
export class NodeStart extends FlowNode {
/**
* 执行
* @param context
*/
async run(context: FlowContext) {
const { inputParams } = this.config;
// 获得请求参数(带值的)
const requestParams = context.getRequestParams();
const reuslt = {};
// 校验并设置输出参数
for (const param of inputParams) {
let value = requestParams[param.name];
if (param.required && this.isEmptyValue(value)) {
throw new CoolCommException(`参数 ${param.name} 为必填`);
}
const checkType = this.checkType(value, param.type);
if (value !== undefined && !checkType) {
throw new CoolCommException(`参数 ${param.name} 类型错误`);
} else {
value = this.transformValue(value, param.type);
}
reuslt[param.field] = value;
context.set(`${this.getPrefix()}.${param.name}`, value, 'output');
}
return {
success: true,
result: reuslt,
};
}
/**
* 判断是否为空值
* @param value
* @returns
*/
isEmptyValue(value) {
// 检查是否是 null 或 undefined
if (_.isNil(value)) {
return true;
}
// 特别对待非容器类型(如数字和布尔值)
if (_.isNumber(value) || _.isBoolean(value)) {
return false;
}
// 对字符串、数组、对象使用 _.isEmpty
return _.isEmpty(value);
}
/**
* 转换值
* @param value
* @param type
*/
transformValue(value: any, type: string) {
if (type == 'number') {
return Number(value);
}
return value;
}
/**
* 字段类型
* @param value
* @returns
*/
checkType(value: any, type: string) {
if (type == 'image') {
return typeof value === 'string' && value.startsWith('http');
}
if (type == 'text') {
return typeof value === 'string';
}
if (type == 'number') {
return !isNaN(value);
}
return true;
}
}