From 52d0d882b6ee4c20c4dfbc7243519775e5275dac Mon Sep 17 00:00:00 2001 From: "guokai.jgk" Date: Fri, 13 Nov 2020 16:15:13 +0800 Subject: [PATCH] fix: datasource engine adpater --- packages/datasource-engine/src/utils.ts | 92 +++++-------------------- 1 file changed, 19 insertions(+), 73 deletions(-) diff --git a/packages/datasource-engine/src/utils.ts b/packages/datasource-engine/src/utils.ts index fb0c72cff..f0109068d 100644 --- a/packages/datasource-engine/src/utils.ts +++ b/packages/datasource-engine/src/utils.ts @@ -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 = {}; 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;