forked from alibaba/lowcode-datasource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
162 lines (147 loc) · 5.11 KB
/
utils.ts
File metadata and controls
162 lines (147 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint-disable no-new-func */
import {
IDataSourceRuntimeContext,
InterpretDataSourceConfig,
isJSExpression,
isJSFunction,
IPublicTypeJSExpression,
IPublicTypeJSFunction,
RuntimeOptionsConfig,
} from '@alilc/lowcode-types';
import { CompositeValue, JSONObject } from '@alilc/lowcode-datasource-types';
function isObject(obj: unknown) {
return Object.prototype.toString.call(obj).indexOf('Object') !== -1;
}
export const transformExpression = (code: string, context: IDataSourceRuntimeContext) => {
// 补充异常情况兼容性
if (code === undefined) {
return function () {};
}
if (code === '') {
return function () { return ''; };
}
try {
return new Function(`return (${code})`).call(context);
} catch (error) {
console.error(`transformExpression error, code is ${code}, context is ${context}, error is ${error}`);
}
};
export const transformFunction = (code: string, context: IDataSourceRuntimeContext) => {
if (code === undefined) {
return function () {};
}
if (code === '') {
return function () { return ''; };
}
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}`);
}
};
export const transformBoolStr = (str: string) => {
return str !== 'false';
};
export const getRuntimeJsValue = (value: IPublicTypeJSExpression | IPublicTypeJSFunction, 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);
};
export const getRuntimeBaseValue = (type: string, value: any) => {
switch (type) {
case 'string':
return `${value}`;
case 'boolean':
return typeof value === 'string' ? transformBoolStr(value as string) : !!value;
case 'number':
return Number(value);
default:
return value;
}
};
export const getRuntimeValueFromConfig = (type: string, value: CompositeValue, context: IDataSourceRuntimeContext) => {
if (value === undefined) {
return undefined;
}
if (isJSExpression(value) || isJSFunction(value)) {
return getRuntimeBaseValue(type, getRuntimeJsValue(value, context));
}
return value;
};
export const buildJsonObj = (params: JSONObject | IPublicTypeJSExpression, context: IDataSourceRuntimeContext) => {
if (isJSExpression(params)) {
return transformExpression(params.value, context);
} else if (isObject(params)) { // 处理params内部为JSExpression的问题
const newParams: Record<string, unknown> = {};
for (const [name, param] of Object.entries(params)) {
if (isJSExpression(param)) {
newParams[name] = transformExpression(param?.value, context);
} else if (isObject(param)) {
newParams[name] = buildJsonObj(param as JSONObject, context);
} else {
newParams[name] = param;
}
}
return newParams;
}
return params;
};
export const buildShouldFetch = (ds: InterpretDataSourceConfig, context: IDataSourceRuntimeContext) => {
if (!ds.options || !ds.shouldFetch) {
return true; // 默认为 true
}
if (isJSExpression(ds.shouldFetch) || isJSFunction(ds.shouldFetch)) {
return getRuntimeJsValue(ds.shouldFetch, context);
}
return getRuntimeBaseValue('boolean', ds.shouldFetch);
};
export const buildOptions = (ds: InterpretDataSourceConfig, context: IDataSourceRuntimeContext) => {
const { options } = ds;
if (!options) return undefined;
// eslint-disable-next-line space-before-function-paren
return function () {
// 默认值
const fetchOptions: RuntimeOptionsConfig = {
uri: '',
params: {},
method: 'GET',
isCors: true,
timeout: 5000,
headers: undefined,
v: '1.0',
};
Object.keys(options).forEach((key: string) => {
switch (key) {
case 'uri':
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);
break;
case 'isCors':
fetchOptions.isCors = getRuntimeValueFromConfig('boolean', options.isCors, context);
break;
case 'timeout':
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);
break;
default:
// 其余的除了做表达式或者 function 的转换,直接透传
fetchOptions[key] = getRuntimeValueFromConfig('unknown', options[key], context);
}
});
return fetchOptions;
};
};