fix: datasource engine adpater

This commit is contained in:
guokai.jgk 2020-11-13 16:15:13 +08:00
parent 7856d7a8cc
commit 52d0d882b6

View File

@ -12,29 +12,19 @@ import {
RuntimeOptionsConfig, RuntimeOptionsConfig,
} from '@ali/lowcode-types'; } from '@ali/lowcode-types';
export const transformExpression = ( export const transformExpression = (code: string, context: IDataSourceRuntimeContext) => {
code: string,
context: IDataSourceRuntimeContext,
) => {
try { try {
return new Function(`return (${code})`).call(context); return new Function(`return (${code})`).call(context);
} catch (error) { } catch (error) {
console.error( console.error(`transformExpression error, code is ${code}, context is ${context}, error is ${error}`);
`transformExpression error, code is ${code}, context is ${context}, error is ${error}`,
);
} }
}; };
export const transformFunction = ( export const transformFunction = (code: string, context: IDataSourceRuntimeContext) => {
code: string,
context: IDataSourceRuntimeContext,
) => {
try { try {
return new Function(`return (${code})`).call(context).bind(context); return new Function(`return (${code})`).call(context).bind(context);
} catch (error) { } catch (error) {
console.error( console.error(`transformFunction error, code is ${code}, context is ${context}, error is ${error}`);
`transformFunction error, code is ${code}, context is ${context}, error is ${error}`,
);
} }
}; };
@ -42,19 +32,14 @@ export const transformBoolStr = (str: string) => {
return str !== 'false'; return str !== 'false';
}; };
export const getRuntimeJsValue = ( export const getRuntimeJsValue = (value: JSExpression | JSFunction, context: IDataSourceRuntimeContext) => {
value: JSExpression | JSFunction,
context: IDataSourceRuntimeContext,
) => {
if (!['JSExpression', 'JSFunction'].includes(value.type)) { if (!['JSExpression', 'JSFunction'].includes(value.type)) {
console.error(`translate error, value is ${JSON.stringify(value)}`); console.error(`translate error, value is ${JSON.stringify(value)}`);
return ''; return '';
} }
// TODO: 类型修复 // TODO: 类型修复
const code = value.compiled || value.value; const code = value.compiled || value.value;
return value.type === 'JSFunction' return value.type === 'JSFunction' ? transformFunction(code, context) : transformExpression(code, context);
? transformFunction(code, context)
: transformExpression(code, context);
}; };
export const getRuntimeBaseValue = (type: string, value: any) => { export const getRuntimeBaseValue = (type: string, value: any) => {
@ -62,9 +47,7 @@ export const getRuntimeBaseValue = (type: string, value: any) => {
case 'string': case 'string':
return `${value}`; return `${value}`;
case 'boolean': case 'boolean':
return typeof value === 'string' return typeof value === 'string' ? transformBoolStr(value as string) : !!value;
? transformBoolStr(value as string)
: !!value;
case 'number': case 'number':
return Number(value); return Number(value);
default: default:
@ -72,22 +55,15 @@ export const getRuntimeBaseValue = (type: string, value: any) => {
} }
}; };
export const getRuntimeValueFromConfig = ( export const getRuntimeValueFromConfig = (type: string, value: CompositeValue, context: IDataSourceRuntimeContext) => {
type: string,
value: CompositeValue,
context: IDataSourceRuntimeContext,
) => {
if (!value) return undefined; if (!value) return undefined;
if (isJSExpression(value) || isJSFunction(value)) { if (isJSExpression(value) || isJSFunction(value)) {
return getRuntimeBaseValue(type, getRuntimeJsValue(value, context)); return getRuntimeBaseValue(type, getRuntimeJsValue(value, context));
} }
return getRuntimeBaseValue(type, value); return value;
}; };
export const buildJsonObj = ( export const buildJsonObj = (params: JSONObject | JSExpression, context: IDataSourceRuntimeContext) => {
params: JSONObject | JSExpression,
context: IDataSourceRuntimeContext,
) => {
const result: Record<string, any> = {}; const result: Record<string, any> = {};
if (isJSExpression(params)) { if (isJSExpression(params)) {
return transformExpression(params.value, context); return transformExpression(params.value, context);
@ -97,17 +73,14 @@ export const buildJsonObj = (
if (isJSExpression(currentParam)) { if (isJSExpression(currentParam)) {
result[key] = transformExpression(currentParam.value, context); result[key] = transformExpression(currentParam.value, context);
} else { } else {
result[key] = getRuntimeBaseValue(currentParam.type, currentParam.value); result[key] = currentParam;
} }
}); });
return result; return result;
}; };
export const buildShouldFetch = ( export const buildShouldFetch = (ds: InterpretDataSourceConfig, context: IDataSourceRuntimeContext) => {
ds: InterpretDataSourceConfig,
context: IDataSourceRuntimeContext,
) => {
if (!ds.options || !ds.shouldFetch) { if (!ds.options || !ds.shouldFetch) {
return true; // 默认为 true return true; // 默认为 true
} }
@ -118,10 +91,7 @@ export const buildShouldFetch = (
return getRuntimeBaseValue('boolean', ds.shouldFetch); return getRuntimeBaseValue('boolean', ds.shouldFetch);
}; };
export const buildOptions = ( export const buildOptions = (ds: InterpretDataSourceConfig, context: IDataSourceRuntimeContext) => {
ds: InterpretDataSourceConfig,
context: IDataSourceRuntimeContext,
) => {
const { options } = ds; const { options } = ds;
if (!options) return undefined; if (!options) return undefined;
// eslint-disable-next-line space-before-function-paren // eslint-disable-next-line space-before-function-paren
@ -139,53 +109,29 @@ export const buildOptions = (
Object.keys(options).forEach((key: string) => { Object.keys(options).forEach((key: string) => {
switch (key) { switch (key) {
case 'uri': case 'uri':
fetchOptions.uri = getRuntimeValueFromConfig( fetchOptions.uri = getRuntimeValueFromConfig('string', options.uri, context);
'string',
options.uri,
context,
);
break; break;
case 'params': case 'params':
fetchOptions.params = buildJsonObj(options.params!, context); fetchOptions.params = buildJsonObj(options.params!, context);
break; break;
case 'method': case 'method':
fetchOptions.method = getRuntimeValueFromConfig( fetchOptions.method = getRuntimeValueFromConfig('string', options.method, context);
'string',
options.method,
context,
);
break; break;
case 'isCors': case 'isCors':
fetchOptions.isCors = getRuntimeValueFromConfig( fetchOptions.isCors = getRuntimeValueFromConfig('boolean', options.isCors, context);
'boolean',
options.isCors,
context,
);
break; break;
case 'timeout': case 'timeout':
fetchOptions.timeout = getRuntimeValueFromConfig( fetchOptions.timeout = getRuntimeValueFromConfig('number', options.timeout, context);
'number',
options.timeout,
context,
);
break; break;
case 'headers': case 'headers':
fetchOptions.headers = buildJsonObj(options.headers!, context); fetchOptions.headers = buildJsonObj(options.headers!, context);
break; break;
case 'v': case 'v':
fetchOptions.v = getRuntimeValueFromConfig( fetchOptions.v = getRuntimeValueFromConfig('string', options.v, context);
'string',
options.v,
context,
);
break; break;
default: default:
// 其余的除了做表达式或者 function 的转换,直接透传 // 其余的除了做表达式或者 function 的转换,直接透传
fetchOptions[key] = getRuntimeValueFromConfig( fetchOptions[key] = getRuntimeValueFromConfig('unknown', options[key], context);
'unknown',
options[key],
context,
);
} }
}); });
return fetchOptions; return fetchOptions;