mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 01:21:58 +00:00
Merge branch 'fix-datasource-engine' into 'release/1.0.0'
fix: datasource engine adpater See merge request !1045742
This commit is contained in:
commit
f4e9b3a339
@ -12,29 +12,19 @@ import {
|
||||
RuntimeOptionsConfig,
|
||||
} from '@ali/lowcode-types';
|
||||
|
||||
export const transformExpression = (
|
||||
code: string,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) => {
|
||||
export const transformExpression = (code: string, context: IDataSourceRuntimeContext) => {
|
||||
try {
|
||||
return new Function(`return (${code})`).call(context);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`transformExpression error, code is ${code}, context is ${context}, error is ${error}`,
|
||||
);
|
||||
console.error(`transformExpression error, code is ${code}, context is ${context}, error is ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const transformFunction = (
|
||||
code: string,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) => {
|
||||
export const transformFunction = (code: string, context: IDataSourceRuntimeContext) => {
|
||||
try {
|
||||
return new Function(`return (${code})`).call(context).bind(context);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`transformFunction error, code is ${code}, context is ${context}, error is ${error}`,
|
||||
);
|
||||
console.error(`transformFunction error, code is ${code}, context is ${context}, error is ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
@ -42,19 +32,14 @@ export const transformBoolStr = (str: string) => {
|
||||
return str !== 'false';
|
||||
};
|
||||
|
||||
export const getRuntimeJsValue = (
|
||||
value: JSExpression | JSFunction,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) => {
|
||||
export const getRuntimeJsValue = (value: JSExpression | JSFunction, context: IDataSourceRuntimeContext) => {
|
||||
if (!['JSExpression', 'JSFunction'].includes(value.type)) {
|
||||
console.error(`translate error, value is ${JSON.stringify(value)}`);
|
||||
return '';
|
||||
}
|
||||
// TODO: 类型修复
|
||||
const code = value.compiled || value.value;
|
||||
return value.type === 'JSFunction'
|
||||
? transformFunction(code, context)
|
||||
: transformExpression(code, context);
|
||||
return value.type === 'JSFunction' ? transformFunction(code, context) : transformExpression(code, context);
|
||||
};
|
||||
|
||||
export const getRuntimeBaseValue = (type: string, value: any) => {
|
||||
@ -62,9 +47,7 @@ export const getRuntimeBaseValue = (type: string, value: any) => {
|
||||
case 'string':
|
||||
return `${value}`;
|
||||
case 'boolean':
|
||||
return typeof value === 'string'
|
||||
? transformBoolStr(value as string)
|
||||
: !!value;
|
||||
return typeof value === 'string' ? transformBoolStr(value as string) : !!value;
|
||||
case 'number':
|
||||
return Number(value);
|
||||
default:
|
||||
@ -72,22 +55,15 @@ export const getRuntimeBaseValue = (type: string, value: any) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getRuntimeValueFromConfig = (
|
||||
type: string,
|
||||
value: CompositeValue,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) => {
|
||||
export const getRuntimeValueFromConfig = (type: string, value: CompositeValue, context: IDataSourceRuntimeContext) => {
|
||||
if (!value) return undefined;
|
||||
if (isJSExpression(value) || isJSFunction(value)) {
|
||||
return getRuntimeBaseValue(type, getRuntimeJsValue(value, context));
|
||||
}
|
||||
return getRuntimeBaseValue(type, value);
|
||||
return value;
|
||||
};
|
||||
|
||||
export const buildJsonObj = (
|
||||
params: JSONObject | JSExpression,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) => {
|
||||
export const buildJsonObj = (params: JSONObject | JSExpression, context: IDataSourceRuntimeContext) => {
|
||||
const result: Record<string, any> = {};
|
||||
if (isJSExpression(params)) {
|
||||
return transformExpression(params.value, context);
|
||||
@ -97,17 +73,14 @@ export const buildJsonObj = (
|
||||
if (isJSExpression(currentParam)) {
|
||||
result[key] = transformExpression(currentParam.value, context);
|
||||
} else {
|
||||
result[key] = getRuntimeBaseValue(currentParam.type, currentParam.value);
|
||||
result[key] = currentParam;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
export const buildShouldFetch = (
|
||||
ds: InterpretDataSourceConfig,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) => {
|
||||
export const buildShouldFetch = (ds: InterpretDataSourceConfig, context: IDataSourceRuntimeContext) => {
|
||||
if (!ds.options || !ds.shouldFetch) {
|
||||
return true; // 默认为 true
|
||||
}
|
||||
@ -118,10 +91,7 @@ export const buildShouldFetch = (
|
||||
return getRuntimeBaseValue('boolean', ds.shouldFetch);
|
||||
};
|
||||
|
||||
export const buildOptions = (
|
||||
ds: InterpretDataSourceConfig,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) => {
|
||||
export const buildOptions = (ds: InterpretDataSourceConfig, context: IDataSourceRuntimeContext) => {
|
||||
const { options } = ds;
|
||||
if (!options) return undefined;
|
||||
// eslint-disable-next-line space-before-function-paren
|
||||
@ -139,53 +109,29 @@ export const buildOptions = (
|
||||
Object.keys(options).forEach((key: string) => {
|
||||
switch (key) {
|
||||
case 'uri':
|
||||
fetchOptions.uri = getRuntimeValueFromConfig(
|
||||
'string',
|
||||
options.uri,
|
||||
context,
|
||||
);
|
||||
fetchOptions.uri = getRuntimeValueFromConfig('string', options.uri, context);
|
||||
break;
|
||||
case 'params':
|
||||
fetchOptions.params = buildJsonObj(options.params!, context);
|
||||
break;
|
||||
case 'method':
|
||||
fetchOptions.method = getRuntimeValueFromConfig(
|
||||
'string',
|
||||
options.method,
|
||||
context,
|
||||
);
|
||||
fetchOptions.method = getRuntimeValueFromConfig('string', options.method, context);
|
||||
break;
|
||||
case 'isCors':
|
||||
fetchOptions.isCors = getRuntimeValueFromConfig(
|
||||
'boolean',
|
||||
options.isCors,
|
||||
context,
|
||||
);
|
||||
fetchOptions.isCors = getRuntimeValueFromConfig('boolean', options.isCors, context);
|
||||
break;
|
||||
case 'timeout':
|
||||
fetchOptions.timeout = getRuntimeValueFromConfig(
|
||||
'number',
|
||||
options.timeout,
|
||||
context,
|
||||
);
|
||||
fetchOptions.timeout = getRuntimeValueFromConfig('number', options.timeout, context);
|
||||
break;
|
||||
case 'headers':
|
||||
fetchOptions.headers = buildJsonObj(options.headers!, context);
|
||||
break;
|
||||
case 'v':
|
||||
fetchOptions.v = getRuntimeValueFromConfig(
|
||||
'string',
|
||||
options.v,
|
||||
context,
|
||||
);
|
||||
fetchOptions.v = getRuntimeValueFromConfig('string', options.v, context);
|
||||
break;
|
||||
default:
|
||||
// 其余的除了做表达式或者 function 的转换,直接透传
|
||||
fetchOptions[key] = getRuntimeValueFromConfig(
|
||||
'unknown',
|
||||
options[key],
|
||||
context,
|
||||
);
|
||||
fetchOptions[key] = getRuntimeValueFromConfig('unknown', options[key], context);
|
||||
}
|
||||
});
|
||||
return fetchOptions;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user