fix: eslint

This commit is contained in:
guokai.jgk 2020-10-26 15:32:55 +08:00
parent 9d97e569b1
commit c346137092
4 changed files with 138 additions and 86 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ali/lowcode-datasource-engine",
"version": "1.0.2-alpha.1",
"version": "1.0.2-alpha.2",
"main": "dist/index.js",
"files": [
"dist",

View File

@ -15,6 +15,7 @@ export const reloadDataSourceFactory = (
dataSource.list
.filter(
(el: RuntimeDataSourceConfig) =>
// eslint-disable-next-line implicit-arrow-linebreak
el.type === 'urlParams' &&
(typeof el.isInit === 'boolean' ? el.isInit : true),
)
@ -51,8 +52,13 @@ export const reloadDataSourceFactory = (
ds.isInit &&
ds.isSync
) {
// eslint-disable-next-line no-await-in-loop
await dataSourceMap[ds.id].load();
try {
// eslint-disable-next-line no-await-in-loop
await dataSourceMap[ds.id].load();
} catch (e) {
// TODO: 这个错误直接吃掉?
console.error(e);
}
}
}

View File

@ -9,6 +9,7 @@ import {
JSExpression,
JSFunction,
JSONObject,
RuntimeOptionsConfig,
} from '@ali/lowcode-types';
export const transformExpression = (
@ -123,25 +124,70 @@ export const buildOptions = (
) => {
const { options } = ds;
if (!options) return undefined;
// eslint-disable-next-line space-before-function-paren
return function() {
return {
uri: getRuntimeValueFromConfig('string', options.uri, context),
params: options.params ? buildJsonObj(options.params, context) : {},
method: options.method
? getRuntimeValueFromConfig('string', options.method, context)
: 'GET',
isCors: options.isCors
? getRuntimeValueFromConfig('boolean', options.isCors, context)
: true,
timeout: options.timeout
? getRuntimeValueFromConfig('number', options.timeout, context)
: 5000,
headers: options.headers
? buildJsonObj(options.headers, context)
: undefined,
v: options.v
? getRuntimeValueFromConfig('string', options.v, context)
: '1.0',
// 默认值
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;
};
};

View File

@ -25,72 +25,72 @@ export const abnormalScene: Macro<[
]> = async (
t: ExecutionContext<{ clock: SinonFakeTimers }>,
{ create, dataSource },
) => {
const { clock } = t.context;
) => {
const { clock } = t.context;
const USER_DATA = {
id: 9527,
name: 'Alice',
};
const ERROR_MSG = 'test error';
const fetchHandler = sinon.fake(async ({ uri }) => {
await delay(100);
if (/user/.test(uri)) {
return { data: USER_DATA };
} else {
throw new Error(ERROR_MSG);
}
});
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(bindRuntimeContext(dataSource, ctx), ctx, {
requestHandlersMap: {
fetch: fetchHandler,
},
}));
const setState = sinon.spy(context, 'setState');
// 一开始应该是初始状态
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Initial);
const loading = context.reloadDataSource();
await clock.tickAsync(50);
// 中间应该有 loading 态
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
await clock.tickAsync(50);
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Loading);
await Promise.all([clock.runAllAsync(), loading]);
// 最后 user 应该成功了loaded
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loaded);
// 最后 orders 应该失败了error 状态
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Error);
// 检查数据源的数据
t.deepEqual(context.dataSourceMap.user.data, USER_DATA);
t.is(context.dataSourceMap.user.error, undefined);
t.deepEqual(context.dataSourceMap.orders.data, undefined);
t.not(context.dataSourceMap.orders.error, undefined);
t.regex(context.dataSourceMap.orders.error!.message, new RegExp(ERROR_MSG));
// 检查状态数据
t.assert(setState.calledOnce);
t.deepEqual(context.state.user, USER_DATA);
t.is(context.state.orders, undefined);
// fetchHandler 应该被调用了2次
t.assert(fetchHandler.calledTwice);
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
// 检查调用参数
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
const USER_DATA = {
id: 9527,
name: 'Alice',
};
const ERROR_MSG = 'test error';
const fetchHandler = sinon.fake(async ({ uri }) => {
await delay(100);
if (/user/.test(uri)) {
return { data: USER_DATA };
} else {
throw new Error(ERROR_MSG);
}
});
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(bindRuntimeContext(dataSource, ctx), ctx, {
requestHandlersMap: {
fetch: fetchHandler,
},
}));
const setState = sinon.spy(context, 'setState');
// 一开始应该是初始状态
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Initial);
const loading = context.reloadDataSource();
await clock.tickAsync(50);
// 中间应该有 loading 态
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
await clock.tickAsync(50);
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Loading);
await Promise.all([clock.runAllAsync(), loading]);
// 最后 user 应该成功了loaded
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loaded);
// 最后 orders 应该失败了error 状态
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Error);
// 检查数据源的数据
t.deepEqual(context.dataSourceMap.user.data, USER_DATA);
t.is(context.dataSourceMap.user.error, undefined);
t.deepEqual(context.dataSourceMap.orders.data, undefined);
t.not(context.dataSourceMap.orders.error, undefined);
t.regex(context.dataSourceMap.orders.error!.message, new RegExp(ERROR_MSG));
// 检查状态数据
t.assert(setState.calledOnce);
t.deepEqual(context.state.user, USER_DATA);
t.is(context.state.orders, undefined);
// fetchHandler 应该被调用了2次
t.assert(fetchHandler.calledTwice);
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
// 检查调用参数
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
};
abnormalScene.title = (providedTitle) => providedTitle || 'abnormal scene';