mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-02-28 21:20:28 +00:00
refactor: 迁移datasource-engine to ailowcode-datasource
This commit is contained in:
parent
0a09a61bbb
commit
06e34b1a43
@ -1,3 +0,0 @@
|
||||
/node_modules
|
||||
/es
|
||||
/lib
|
||||
@ -1,8 +0,0 @@
|
||||
module.exports = {
|
||||
extends: '../../.eslintrc',
|
||||
rules: {
|
||||
'@typescript-eslint/no-parameter-properties': 0,
|
||||
'no-param-reassign': 0,
|
||||
'max-len': 0,
|
||||
},
|
||||
};
|
||||
6
packages/datasource-engine/.gitignore
vendored
6
packages/datasource-engine/.gitignore
vendored
@ -1,6 +0,0 @@
|
||||
/node_modules/
|
||||
*.log
|
||||
.DS_Store
|
||||
/es/
|
||||
/lib/
|
||||
/dist
|
||||
@ -1,5 +0,0 @@
|
||||
module.exports = {
|
||||
singleQuote: true,
|
||||
trailingComma: 'all',
|
||||
printWidth: 120,
|
||||
};
|
||||
@ -1,144 +0,0 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
<a name="1.0.22"></a>
|
||||
## [1.0.22](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@1.0.21...@ali/lowcode-datasource-engine@1.0.22) (2020-11-16)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* datasource engine adpater ([52d0d88](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/52d0d88))
|
||||
|
||||
|
||||
|
||||
|
||||
<a name="1.0.21"></a>
|
||||
## [1.0.21](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@1.0.20...@ali/lowcode-datasource-engine@1.0.21) (2020-11-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* 合并数据源引擎修复代码 ([53f3554](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/53f3554))
|
||||
|
||||
|
||||
|
||||
|
||||
<a name="1.0.20"></a>
|
||||
## [1.0.20](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@1.0.18...@ali/lowcode-datasource-engine@1.0.20) (2020-11-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add package json typings ([6378595](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/6378595))
|
||||
* datasource package.json files 新增lib文件 ([a8a1749](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/a8a1749))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* update datasource engine ([bf7b7d1](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/bf7b7d1))
|
||||
|
||||
|
||||
|
||||
|
||||
<a name="1.0.18"></a>
|
||||
## [1.0.18](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@1.0.17...@ali/lowcode-datasource-engine@1.0.18) (2020-11-05)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* datasource版本错误问题 ([a247878](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/a247878))
|
||||
|
||||
|
||||
|
||||
|
||||
<a name="1.0.17"></a>
|
||||
## [1.0.17](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@1.0.16...@ali/lowcode-datasource-engine@1.0.17) (2020-11-05)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="1.0.16"></a>
|
||||
## [1.0.16](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@1.0.15...@ali/lowcode-datasource-engine@1.0.16) (2020-11-05)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="1.0.15"></a>
|
||||
## [1.0.15](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@1.0.14...@ali/lowcode-datasource-engine@1.0.15) (2020-11-04)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="1.0.14"></a>
|
||||
## [1.0.14](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@0.1.17...@ali/lowcode-datasource-engine@1.0.14) (2020-11-04)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="1.0.13"></a>
|
||||
## [1.0.13](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@0.1.17...@ali/lowcode-datasource-engine@1.0.13) (2020-11-04)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="0.1.17"></a>
|
||||
## [0.1.17](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@0.1.16...@ali/lowcode-datasource-engine@0.1.17) (2020-11-02)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="0.1.16"></a>
|
||||
## [0.1.16](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@0.1.15...@ali/lowcode-datasource-engine@0.1.16) (2020-10-20)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="0.1.15"></a>
|
||||
## [0.1.15](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@0.1.14...@ali/lowcode-datasource-engine@0.1.15) (2020-10-19)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="0.1.14"></a>
|
||||
## [0.1.14](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@0.1.13...@ali/lowcode-datasource-engine@0.1.14) (2020-09-29)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="0.1.13"></a>
|
||||
## [0.1.13](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/compare/@ali/lowcode-datasource-engine@0.1.12...@ali/lowcode-datasource-engine@0.1.13) (2020-09-28)
|
||||
|
||||
|
||||
|
||||
|
||||
**Note:** Version bump only for package @ali/lowcode-datasource-engine
|
||||
|
||||
<a name="0.1.12"></a>
|
||||
## 0.1.12 (2020-09-28)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* 🎸 按 826 对齐结论调整出码和数据源引擎 ([b9a562e](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/b9a562e))
|
||||
* 🎸 添加数据源引擎 ([624e2f8](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/624e2f8))
|
||||
* 🎸 与国凯的数据源保持一致,将 urlParams 所需的 search 参数直接传入 ([19fabc1](https://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/commit/19fabc1))
|
||||
@ -1,8 +0,0 @@
|
||||
export default {
|
||||
babel: {
|
||||
compileEnhancements: false,
|
||||
},
|
||||
files: ['./test/core/*.ts', './test/scenes/**/*.test.ts'],
|
||||
require: ['ts-node/register/transpile-only'],
|
||||
extensions: ['ts'],
|
||||
};
|
||||
1
packages/datasource-engine/interpret.d.ts
vendored
1
packages/datasource-engine/interpret.d.ts
vendored
@ -1 +0,0 @@
|
||||
export { create } from './dist/interpret';
|
||||
@ -1 +0,0 @@
|
||||
module.exports = require('./dist/interpret/index');
|
||||
@ -1,39 +0,0 @@
|
||||
{
|
||||
"name": "@ali/lowcode-datasource-engine",
|
||||
"version": "1.0.23-alpha.2",
|
||||
"description": "DataSource Engine for lowcode",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"files": [
|
||||
"dist",
|
||||
"src",
|
||||
"interpret*",
|
||||
"runtime*"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "rm -rf dist",
|
||||
"build": "npm run clean && tsc ",
|
||||
"test": "ava",
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ali/lowcode-datasource-types": "^1.0.21",
|
||||
"@ali/lowcode-types": "^1.0.20",
|
||||
"promise.allsettled": "^1.0.2",
|
||||
"typescript": "^3.9.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ava/babel": "^1.0.1",
|
||||
"@types/promise.allsettled": "^1.0.3",
|
||||
"@types/sinon": "^9.0.5",
|
||||
"ava": "3.11.1",
|
||||
"get-port": "^5.1.1",
|
||||
"json5": "^2.1.3",
|
||||
"sinon": "^9.0.3",
|
||||
"ts-node": "^8.10.2",
|
||||
"tslib": "^2.0.1"
|
||||
},
|
||||
"publishConfig": {
|
||||
"registry": "https://registry.npm.alibaba-inc.com"
|
||||
}
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
## 关于 @ali/lc-datasource-engine
|
||||
|
||||
低代码引擎数据源核心代码
|
||||
|
||||
## doc
|
||||
|
||||
[原理介绍](https://yuque.antfin-inc.com/docs/share/6ba9dab7-0712-4302-a5bb-b17d4a5f8505?# 《DataSource Engine》)
|
||||
|
||||
|
||||
[fetch流程图](https://yuque.antfin-inc.com/docs/share/e9baef9a-3586-40fc-8708-eaeee0d7937e?# 《fetch 流程》)
|
||||
|
||||
|
||||
## 使用
|
||||
|
||||
```ts
|
||||
// 面向运行时渲染,直接给 schema
|
||||
import { create } from '@ali/lowcode-datasource-engine/interpret';
|
||||
|
||||
// 面向出码,需要给处理过后的内容
|
||||
import { create } from '@ali/lowcode-datasource-engine/runtime';
|
||||
|
||||
import { createFetchHandler } from '@ali/lowcode-datasource-fetch-handler';
|
||||
|
||||
import { createMtopHandler } from '@ali/lowcode-datasource-mtop-handler';
|
||||
|
||||
// dataSource 可以是 schema 协议内容 或者是运行时的转化后的配置内容 (出码专用)
|
||||
|
||||
|
||||
// context 上下文(setState 为必选)
|
||||
const dataSourceEngine = create(dataSource, context, {
|
||||
requestHandlersMap: { // 可选参数,以下内容为当前默认的内容
|
||||
urlParams: handlersMap.urlParams('?bar=1&test=2'),
|
||||
fetch: createFetchHandler,
|
||||
mtop: createMtopHandler
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
console.log(dsf.dataSourceMap) // 符合集团协议的 datasourceMap https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#QUSn5
|
||||
|
||||
dsf.dataSourceMap['id'].load() // 加载
|
||||
|
||||
dsf.dataSourceMap['id'].status // 获取状态
|
||||
|
||||
dsf.dataSourceMap['id'].data // 获取数据
|
||||
|
||||
dsf.dataSourceMap['id'].error // 获取错误信息
|
||||
|
||||
dsf.reloadDataSource(); // 刷新所有数据源
|
||||
|
||||
```
|
||||
1
packages/datasource-engine/runtime.d.ts
vendored
1
packages/datasource-engine/runtime.d.ts
vendored
@ -1 +0,0 @@
|
||||
export { create } from './dist/runtime';
|
||||
@ -1 +0,0 @@
|
||||
module.exports = require('./dist/runtime/index');
|
||||
@ -1,143 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/indent */
|
||||
import {
|
||||
IDataSourceRuntimeContext,
|
||||
IRuntimeDataSource,
|
||||
RequestHandler,
|
||||
RuntimeDataSourceConfig,
|
||||
RuntimeDataSourceStatus,
|
||||
RuntimeOptionsConfig,
|
||||
UrlParamsHandler,
|
||||
} from '@ali/lowcode-types';
|
||||
|
||||
class RuntimeDataSourceItem<TParams extends Record<string, unknown> = Record<string, unknown>, TResultData = unknown>
|
||||
implements IRuntimeDataSource<TParams, TResultData> {
|
||||
private _data?: TResultData;
|
||||
|
||||
private _error?: Error;
|
||||
|
||||
private _status = RuntimeDataSourceStatus.Initial;
|
||||
|
||||
private _dataSourceConfig: RuntimeDataSourceConfig;
|
||||
|
||||
private _request: RequestHandler<{ data: TResultData }> | UrlParamsHandler<TResultData>;
|
||||
|
||||
private _context: IDataSourceRuntimeContext;
|
||||
|
||||
private _options?: RuntimeOptionsConfig;
|
||||
|
||||
constructor(
|
||||
dataSourceConfig: RuntimeDataSourceConfig,
|
||||
request: RequestHandler<{ data: TResultData }> | UrlParamsHandler<TResultData>,
|
||||
context: IDataSourceRuntimeContext,
|
||||
) {
|
||||
this._dataSourceConfig = dataSourceConfig;
|
||||
this._request = request;
|
||||
this._context = context;
|
||||
}
|
||||
|
||||
get data() {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
get error() {
|
||||
return this._error;
|
||||
}
|
||||
|
||||
get status() {
|
||||
return this._status;
|
||||
}
|
||||
|
||||
async load(params?: TParams) {
|
||||
if (!this._dataSourceConfig) return;
|
||||
// 考虑没有绑定对应的 handler 的情况
|
||||
if (!this._request) {
|
||||
this._error = new Error(`no ${this._dataSourceConfig.type} handler provide`);
|
||||
this._status = RuntimeDataSourceStatus.Error;
|
||||
throw this._error;
|
||||
}
|
||||
|
||||
// TODO: urlParams 有没有更好的处理方式
|
||||
if (this._dataSourceConfig.type === 'urlParams') {
|
||||
const response = await (this._request as UrlParamsHandler<TResultData>)(this._context);
|
||||
this._context.setState({
|
||||
[this._dataSourceConfig.id]: response,
|
||||
});
|
||||
|
||||
this._data = response;
|
||||
this._status = RuntimeDataSourceStatus.Loaded;
|
||||
return response;
|
||||
}
|
||||
|
||||
if (!this._dataSourceConfig.options) {
|
||||
throw new Error(`${this._dataSourceConfig.id} has no options`);
|
||||
}
|
||||
|
||||
if (typeof this._dataSourceConfig.options === 'function') {
|
||||
this._options = this._dataSourceConfig.options();
|
||||
}
|
||||
|
||||
// 考虑转换之后是 null 的场景
|
||||
if (!this._options) {
|
||||
throw new Error(`${this._dataSourceConfig.id} options transform error`);
|
||||
}
|
||||
|
||||
// 临时变量存,每次可能结果不一致,不做缓存
|
||||
let shouldFetch = true;
|
||||
|
||||
if (this._dataSourceConfig.shouldFetch) {
|
||||
if (typeof this._dataSourceConfig.shouldFetch === 'function') {
|
||||
shouldFetch = this._dataSourceConfig.shouldFetch();
|
||||
} else if (typeof this._dataSourceConfig.shouldFetch === 'boolean') {
|
||||
shouldFetch = this._dataSourceConfig.shouldFetch;
|
||||
}
|
||||
}
|
||||
|
||||
if (!shouldFetch) {
|
||||
this._status = RuntimeDataSourceStatus.Error;
|
||||
this._error = new Error(`the ${this._dataSourceConfig.id} request should not fetch, please check the condition`);
|
||||
throw this._error;
|
||||
}
|
||||
|
||||
let fetchOptions = this._options;
|
||||
|
||||
// willFetch
|
||||
try {
|
||||
fetchOptions = await this._dataSourceConfig.willFetch!(this._options);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// 约定如果 params 有内容,直接做替换,如果没有就用默认的 options 的
|
||||
if (params && fetchOptions) {
|
||||
fetchOptions.params = params;
|
||||
}
|
||||
|
||||
const dataHandler = this._dataSourceConfig.dataHandler!;
|
||||
const { errorHandler } = this._dataSourceConfig;
|
||||
|
||||
// 调用实际的请求,获取到对应的数据和状态后赋值给当前的 dataSource
|
||||
try {
|
||||
this._status = RuntimeDataSourceStatus.Loading;
|
||||
|
||||
// _context 会给传,但是用不用由 handler 说了算
|
||||
const result = await (this._request as RequestHandler<{
|
||||
data: TResultData;
|
||||
}>)(fetchOptions, this._context).then(dataHandler, errorHandler);
|
||||
|
||||
// setState
|
||||
this._context.setState({
|
||||
[this._dataSourceConfig.id]: result,
|
||||
});
|
||||
// 结果赋值
|
||||
this._data = result;
|
||||
this._status = RuntimeDataSourceStatus.Loaded;
|
||||
return this._data;
|
||||
} catch (error) {
|
||||
this._error = error;
|
||||
this._status = RuntimeDataSourceStatus.Error;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { RuntimeDataSourceItem };
|
||||
@ -1,47 +0,0 @@
|
||||
import { getRuntimeValueFromConfig, getRuntimeJsValue, buildOptions, buildShouldFetch } from './../utils';
|
||||
// 将不同渠道给的 schema 转为 runtime 需要的类型
|
||||
|
||||
import { defaultDataHandler, defaultWillFetch } from '../helpers';
|
||||
import {
|
||||
DataSourceMap,
|
||||
IDataSourceRuntimeContext,
|
||||
InterpretDataSource,
|
||||
InterpretDataSourceConfig,
|
||||
RuntimeDataSourceConfig,
|
||||
} from '@ali/lowcode-types';
|
||||
|
||||
const adapt2Runtime = (dataSource: InterpretDataSource, context: IDataSourceRuntimeContext) => {
|
||||
const { list: interpretConfigList, dataHandler: interpretDataHandler } = dataSource;
|
||||
const dataHandler: (dataMap?: DataSourceMap) => void = interpretDataHandler
|
||||
? getRuntimeJsValue(interpretDataHandler, context)
|
||||
: undefined;
|
||||
|
||||
// 为空判断
|
||||
if (!interpretConfigList || !interpretConfigList.length) {
|
||||
return {
|
||||
list: [],
|
||||
dataHandler,
|
||||
};
|
||||
}
|
||||
const list: RuntimeDataSourceConfig[] = interpretConfigList.map((el: InterpretDataSourceConfig) => {
|
||||
return {
|
||||
id: el.id,
|
||||
isInit: getRuntimeValueFromConfig('boolean', el.isInit, context), // 默认 true
|
||||
isSync: getRuntimeValueFromConfig('boolean', el.isSync, context), // 默认 false
|
||||
type: el.type || 'fetch',
|
||||
willFetch: el.willFetch ? getRuntimeJsValue(el.willFetch, context) : defaultWillFetch,
|
||||
shouldFetch: buildShouldFetch(el, context),
|
||||
dataHandler: el.dataHandler ? getRuntimeJsValue(el.dataHandler, context) : defaultDataHandler,
|
||||
errorHandler: el.errorHandler ? getRuntimeJsValue(el.errorHandler, context) : undefined,
|
||||
requestHandler: el.requestHandler ? getRuntimeJsValue(el.requestHandler, context) : undefined,
|
||||
options: buildOptions(el, context),
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
list,
|
||||
dataHandler,
|
||||
};
|
||||
};
|
||||
|
||||
export { adapt2Runtime };
|
||||
@ -1,2 +0,0 @@
|
||||
export { RuntimeDataSourceItem } from './RuntimeDataSourceItem';
|
||||
export { adapt2Runtime } from './adapter';
|
||||
@ -1,70 +0,0 @@
|
||||
import { DataSourceMap, RuntimeDataSource, RuntimeDataSourceConfig } from '@ali/lowcode-types';
|
||||
import allSettled from 'promise.allsettled';
|
||||
|
||||
// 兼容低版本的浏览器
|
||||
const promiseSettled = typeof Promise.allSettled === 'function' ? Promise.allSettled.bind(Promise) : allSettled;
|
||||
|
||||
export const reloadDataSourceFactory = (
|
||||
dataSource: RuntimeDataSource,
|
||||
dataSourceMap: DataSourceMap,
|
||||
dataHandler?: (dataSourceMap: DataSourceMap) => void,
|
||||
) => async () => {
|
||||
const allAsyncLoadings: Array<Promise<any>> = [];
|
||||
|
||||
// TODO: 那么,如果有新的类型过来,这个地方怎么处理???
|
||||
// 单独处理 urlParams 类型的
|
||||
dataSource.list
|
||||
.filter(
|
||||
(el: RuntimeDataSourceConfig) =>
|
||||
// eslint-disable-next-line implicit-arrow-linebreak
|
||||
el.type === 'urlParams' && (typeof el.isInit === 'boolean' ? el.isInit : true),
|
||||
)
|
||||
.forEach((el: RuntimeDataSourceConfig) => {
|
||||
dataSourceMap[el.id].load();
|
||||
});
|
||||
|
||||
const remainRuntimeDataSourceList = dataSource.list.filter((el: RuntimeDataSourceConfig) => el.type !== 'urlParams');
|
||||
|
||||
// 处理并行
|
||||
for (const ds of remainRuntimeDataSourceList) {
|
||||
if (!ds.options) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
// 需要考虑出码直接不传值的情况
|
||||
ds.isInit &&
|
||||
!ds.isSync
|
||||
) {
|
||||
allAsyncLoadings.push(dataSourceMap[ds.id].load());
|
||||
}
|
||||
}
|
||||
|
||||
// 处理串行
|
||||
for (const ds of remainRuntimeDataSourceList) {
|
||||
if (!ds.options) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
// 需要考虑出码直接不传值的情况
|
||||
ds.isInit &&
|
||||
ds.isSync
|
||||
) {
|
||||
try {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await dataSourceMap[ds.id].load();
|
||||
} catch (e) {
|
||||
// TODO: 这个错误直接吃掉?
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await promiseSettled(allAsyncLoadings);
|
||||
|
||||
// 所有的初始化请求都结束之后,调用钩子函数
|
||||
|
||||
if (dataHandler) {
|
||||
dataHandler(dataSourceMap);
|
||||
}
|
||||
};
|
||||
@ -1,33 +0,0 @@
|
||||
import {
|
||||
DataHandler,
|
||||
RequestHandler,
|
||||
RequestHandlersMap,
|
||||
RuntimeDataSourceConfig,
|
||||
RuntimeOptionsConfig,
|
||||
UrlParamsHandler,
|
||||
WillFetch,
|
||||
} from '@ali/lowcode-types';
|
||||
|
||||
// 默认的 dataSourceItem 的 dataHandler
|
||||
export const defaultDataHandler: DataHandler = async <T = unknown>(response: { data: T }) => response.data;
|
||||
|
||||
// 默认的 dataSourceItem 的 willFetch
|
||||
export const defaultWillFetch: WillFetch = (options: RuntimeOptionsConfig) => options;
|
||||
|
||||
// 默认的 dataSourceItem 的 shouldFetch
|
||||
export const defaultShouldFetch = () => true;
|
||||
|
||||
type GetRequestHandler<T = unknown> = (
|
||||
ds: RuntimeDataSourceConfig,
|
||||
requestHandlersMap: RequestHandlersMap<{ data: T }>,
|
||||
) => RequestHandler<{ data: T }> | UrlParamsHandler<T>;
|
||||
|
||||
// 从当前 dataSourceItem 中获取 requestHandler
|
||||
export const getRequestHandler: GetRequestHandler = (ds, requestHandlersMap) => {
|
||||
if (ds.type === 'custom') {
|
||||
// 自定义类型处理
|
||||
return (ds.requestHandler as unknown) as RequestHandler<{ data: unknown }>; // 理论上这里应该是能强转的,就算为空,应该在 request 请求的时候触发失败
|
||||
}
|
||||
// type 协议默认值 fetch
|
||||
return requestHandlersMap[ds.type || 'fetch'];
|
||||
};
|
||||
@ -1,4 +0,0 @@
|
||||
import createInterpret from './interpret/DataSourceEngineFactory';
|
||||
import createRuntime from './runtime/RuntimeDataSourceEngineFactory';
|
||||
|
||||
export { createInterpret, createRuntime };
|
||||
@ -1,43 +0,0 @@
|
||||
import { adapt2Runtime } from '../core/adapter';
|
||||
import { RuntimeDataSourceItem } from '../core/RuntimeDataSourceItem';
|
||||
import { reloadDataSourceFactory } from '../core/reloadDataSourceFactory';
|
||||
import {
|
||||
IDataSourceRuntimeContext,
|
||||
InterpretDataSource,
|
||||
IRuntimeDataSource,
|
||||
RequestHandlersMap,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceConfig,
|
||||
} from '@ali/lowcode-types';
|
||||
import { getRequestHandler } from '../helpers';
|
||||
|
||||
/**
|
||||
* @param dataSource
|
||||
* @param context
|
||||
* @param extraConfig: { requestHandlersMap }
|
||||
*/
|
||||
|
||||
export default (
|
||||
dataSource: InterpretDataSource,
|
||||
context: IDataSourceRuntimeContext,
|
||||
extraConfig: {
|
||||
requestHandlersMap: RequestHandlersMap<{ data: unknown }>;
|
||||
} = { requestHandlersMap: {} },
|
||||
) => {
|
||||
const { requestHandlersMap } = extraConfig;
|
||||
|
||||
const runtimeDataSource: RuntimeDataSource = adapt2Runtime(dataSource, context);
|
||||
|
||||
const dataSourceMap = runtimeDataSource.list.reduce(
|
||||
(prev: Record<string, IRuntimeDataSource>, current: RuntimeDataSourceConfig) => {
|
||||
prev[current.id] = new RuntimeDataSourceItem(current, getRequestHandler(current, requestHandlersMap), context);
|
||||
return prev;
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
return {
|
||||
dataSourceMap,
|
||||
reloadDataSource: reloadDataSourceFactory(runtimeDataSource, dataSourceMap, runtimeDataSource.dataHandler),
|
||||
};
|
||||
};
|
||||
@ -1,3 +0,0 @@
|
||||
import createInterpret from './DataSourceEngineFactory';
|
||||
|
||||
export const create = createInterpret;
|
||||
@ -1,55 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/indent */
|
||||
import {
|
||||
IRuntimeDataSource,
|
||||
IDataSourceRuntimeContext,
|
||||
RequestHandlersMap,
|
||||
RuntimeDataSourceConfig,
|
||||
RuntimeDataSource,
|
||||
} from '@ali/lowcode-types';
|
||||
|
||||
import { RuntimeDataSourceItem } from '../core';
|
||||
import { reloadDataSourceFactory } from '../core/reloadDataSourceFactory';
|
||||
import { defaultDataHandler, defaultShouldFetch, defaultWillFetch, getRequestHandler } from '../helpers';
|
||||
|
||||
/**
|
||||
* @param dataSource
|
||||
* @param context
|
||||
* @param extraConfig: { requestHandlersMap }
|
||||
*/
|
||||
|
||||
export default (
|
||||
dataSource: RuntimeDataSource,
|
||||
context: IDataSourceRuntimeContext,
|
||||
extraConfig: {
|
||||
requestHandlersMap: RequestHandlersMap<{ data: unknown }>;
|
||||
} = { requestHandlersMap: {} },
|
||||
) => {
|
||||
const { requestHandlersMap } = extraConfig;
|
||||
|
||||
// TODO: 对于出码类型,需要做一层数据兼容,给一些必要的值设置默认值,先兜底几个必要的
|
||||
dataSource.list.forEach(ds => {
|
||||
ds.isInit = ds.isInit || true;
|
||||
ds.isSync = ds.isSync || false;
|
||||
// eslint-disable-next-line no-nested-ternary
|
||||
ds.shouldFetch = !ds.shouldFetch
|
||||
? defaultShouldFetch
|
||||
: typeof ds.shouldFetch === 'function'
|
||||
? ds.shouldFetch.bind(context)
|
||||
: ds.shouldFetch;
|
||||
ds.willFetch = ds.willFetch ? ds.willFetch.bind(context) : defaultWillFetch;
|
||||
ds.dataHandler = ds.dataHandler ? ds.dataHandler.bind(context) : defaultDataHandler;
|
||||
});
|
||||
|
||||
const dataSourceMap = dataSource.list.reduce(
|
||||
(prev: Record<string, IRuntimeDataSource>, current: RuntimeDataSourceConfig) => {
|
||||
prev[current.id] = new RuntimeDataSourceItem(current, getRequestHandler(current, requestHandlersMap), context);
|
||||
return prev;
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
return {
|
||||
dataSourceMap,
|
||||
reloadDataSource: reloadDataSourceFactory(dataSource, dataSourceMap, dataSource.dataHandler),
|
||||
};
|
||||
};
|
||||
@ -1,3 +0,0 @@
|
||||
import createRuntime from './RuntimeDataSourceEngineFactory';
|
||||
|
||||
export const create = createRuntime;
|
||||
@ -1,6 +0,0 @@
|
||||
import { DataSourceConfigItem } from './DataSourceConfigItem';
|
||||
|
||||
export type DataSourceConfig = {
|
||||
list?: DataSourceConfigItem[];
|
||||
dataHandler?: (dataMap: Record<string, unknown>) => void | Promise<void>;
|
||||
};
|
||||
@ -1,17 +0,0 @@
|
||||
import { DataSourceOptions } from './DataSourceOptions';
|
||||
import { RequestHandler } from './RequestHandler';
|
||||
|
||||
export type DataSourceConfigItem = {
|
||||
id: string;
|
||||
type: string;
|
||||
isInit?: boolean | (() => boolean | undefined);
|
||||
|
||||
options?: DataSourceOptions | (() => DataSourceOptions | undefined);
|
||||
|
||||
requestHandler?: RequestHandler;
|
||||
|
||||
dataHandler?: (
|
||||
data: unknown | undefined,
|
||||
error: unknown | undefined,
|
||||
) => unknown;
|
||||
};
|
||||
@ -1,7 +0,0 @@
|
||||
import { RequestHandler } from './RequestHandler';
|
||||
|
||||
export type DataSourceEngineOptions = {
|
||||
requestHandlersMap?: {
|
||||
[dataSourceType: string]: RequestHandler;
|
||||
};
|
||||
};
|
||||
@ -1,10 +0,0 @@
|
||||
export type DataSourceOptions<TParams = Record<string, unknown>> = {
|
||||
uri?: string;
|
||||
params?: TParams;
|
||||
method?: string;
|
||||
isCors?: boolean;
|
||||
timeout?: number;
|
||||
headers?: Record<string, string>;
|
||||
isSync?: boolean;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
@ -1,3 +0,0 @@
|
||||
export type DataSourceResponse<TData = unknown> = {
|
||||
data: TData;
|
||||
};
|
||||
@ -1,9 +0,0 @@
|
||||
import { IRuntimeDataSource } from './IRuntimeDataSource';
|
||||
|
||||
export interface IDataSourceEngine {
|
||||
/** 数据源, key 是数据源的 ID */
|
||||
readonly dataSourceMap: Record<string, IRuntimeDataSource>;
|
||||
|
||||
/** 重新加载所有的数据源 */
|
||||
reloadDataSource(): Promise<void>;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
import { DataSourceConfig } from './DataSourceConfig';
|
||||
import { DataSourceEngineOptions } from './DataSourceEngineOptions';
|
||||
import { IDataSourceEngine } from './IDataSourceEngine';
|
||||
import { IRuntimeContext } from './IRuntimeContext';
|
||||
|
||||
export interface IDataSourceEngineFactory {
|
||||
create(
|
||||
dataSourceConfig: DataSourceConfig,
|
||||
runtimeContext: IRuntimeContext,
|
||||
options?: DataSourceEngineOptions,
|
||||
): IDataSourceEngine;
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
import { IRuntimeDataSource } from './IRuntimeDataSource';
|
||||
|
||||
/** 运行时上下文 */
|
||||
export interface IRuntimeContext<
|
||||
TState = Record<string, unknown>
|
||||
> {
|
||||
/** 当前容器的状态 */
|
||||
readonly state: TState;
|
||||
|
||||
/** 设置状态(浅合并) */
|
||||
setState(state: Partial<TState>): void;
|
||||
|
||||
/** 数据源, key 是数据源的 ID */
|
||||
dataSourceMap: Record<string, IRuntimeDataSource>;
|
||||
|
||||
/** 重新加载所有的数据源 */
|
||||
reloadDataSource(): Promise<void>;
|
||||
|
||||
/** 页面容器 */
|
||||
readonly page: IRuntimeContext & { props: Record<string, unknown> };
|
||||
|
||||
/** 低代码业务组件容器 */
|
||||
readonly component: IRuntimeContext & { props: Record<string, unknown> };
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
import { RuntimeDataSourceStatus } from './RuntimeDataSourceStatus';
|
||||
|
||||
/**
|
||||
* 运行时的数据源(对外暴露的接口)
|
||||
* @see https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#Jwgj5
|
||||
*/
|
||||
export interface IRuntimeDataSource<TParams = unknown, TResultData = unknown> {
|
||||
/** 当前状态(initial/loading/loaded/error) */
|
||||
readonly status: RuntimeDataSourceStatus;
|
||||
|
||||
/** 加载成功时的数据 */
|
||||
readonly data?: TResultData;
|
||||
|
||||
/** 加载出错的时候的错误信息 */
|
||||
readonly error?: Error;
|
||||
|
||||
/**
|
||||
* 加载数据 (无论是否曾经加载过)
|
||||
* 注意:若提供 params,则会和默认配置的参数做浅合并;否则会使用默认配置的参数。
|
||||
*/
|
||||
load(params?: TParams): Promise<TResultData>;
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
import { DataSourceOptions } from './DataSourceOptions';
|
||||
import { DataSourceResponse } from './DataSourceResponse';
|
||||
|
||||
export type RequestHandler<
|
||||
TOptions extends DataSourceOptions = DataSourceOptions,
|
||||
TResult extends DataSourceResponse = DataSourceResponse
|
||||
> = (options: TOptions) => Promise<TResult>;
|
||||
@ -1,14 +0,0 @@
|
||||
/** 数据源的状态 */
|
||||
export enum RuntimeDataSourceStatus {
|
||||
/** 初始状态,尚未加载 */
|
||||
Initial = 'init',
|
||||
|
||||
/** 正在加载 */
|
||||
Loading = 'loading',
|
||||
|
||||
/** 已加载(无错误) */
|
||||
Loaded = 'loaded',
|
||||
|
||||
/** 加载出错了 */
|
||||
Error = 'error',
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
export * from './DataSourceConfig';
|
||||
export * from './DataSourceConfigItem';
|
||||
export * from './DataSourceEngineOptions';
|
||||
export * from './DataSourceOptions';
|
||||
export * from './IDataSourceEngine';
|
||||
export * from './IDataSourceEngineFactory';
|
||||
export * from './IRuntimeContext';
|
||||
export * from './IRuntimeDataSource';
|
||||
export * from './RequestHandler';
|
||||
export * from './RuntimeDataSourceStatus';
|
||||
1
packages/datasource-engine/src/typing.d.ts
vendored
1
packages/datasource-engine/src/typing.d.ts
vendored
@ -1 +0,0 @@
|
||||
declare module '@ali/universal-mtop';
|
||||
3
packages/datasource-engine/src/typings.d.ts
vendored
3
packages/datasource-engine/src/typings.d.ts
vendored
@ -1,3 +0,0 @@
|
||||
declare module '@ali/mirror-io-client-mopen';
|
||||
declare module '@ali/mirror-io-client-mtop';
|
||||
declare module '@ali/mirror-io-client-universal-mtop';
|
||||
@ -1,139 +0,0 @@
|
||||
/* eslint-disable no-new-func */
|
||||
|
||||
import {
|
||||
CompositeValue,
|
||||
IDataSourceRuntimeContext,
|
||||
InterpretDataSourceConfig,
|
||||
isJSExpression,
|
||||
isJSFunction,
|
||||
JSExpression,
|
||||
JSFunction,
|
||||
JSONObject,
|
||||
RuntimeOptionsConfig,
|
||||
} from '@ali/lowcode-types';
|
||||
|
||||
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}`);
|
||||
}
|
||||
};
|
||||
|
||||
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}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const transformBoolStr = (str: string) => {
|
||||
return str !== 'false';
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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) return undefined;
|
||||
if (isJSExpression(value) || isJSFunction(value)) {
|
||||
return getRuntimeBaseValue(type, getRuntimeJsValue(value, context));
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
export const buildJsonObj = (params: JSONObject | JSExpression, context: IDataSourceRuntimeContext) => {
|
||||
const result: Record<string, any> = {};
|
||||
if (isJSExpression(params)) {
|
||||
return transformExpression(params.value, context);
|
||||
}
|
||||
Object.keys(params).forEach((key: string) => {
|
||||
const currentParam: any = params[key];
|
||||
if (isJSExpression(currentParam)) {
|
||||
result[key] = transformExpression(currentParam.value, context);
|
||||
} else {
|
||||
result[key] = currentParam;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
};
|
||||
@ -1,25 +0,0 @@
|
||||
export function bindRuntimeContext<T, U>(x: T, ctx: U): T {
|
||||
if (typeof x === 'function') {
|
||||
return x.bind(ctx);
|
||||
}
|
||||
|
||||
if (typeof x !== 'object') {
|
||||
return x;
|
||||
}
|
||||
|
||||
if (x === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Array.isArray(x)) {
|
||||
return (x.map((item) => bindRuntimeContext(item, ctx)) as unknown) as T;
|
||||
}
|
||||
|
||||
const res = {} as Record<string, unknown>;
|
||||
|
||||
Object.entries(x).forEach(([k, v]) => {
|
||||
res[k] = bindRuntimeContext(v, ctx);
|
||||
});
|
||||
|
||||
return (res as unknown) as T;
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
export async function delay(ms: number = 0) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
export * from './mock-context';
|
||||
export * from './delay';
|
||||
export * from './bind-runtime-context';
|
||||
@ -1,53 +0,0 @@
|
||||
import {
|
||||
IDataSourceRuntimeContext,
|
||||
IDataSourceEngine
|
||||
} from '@ali/lowcode-types';
|
||||
|
||||
export class MockContext<TState extends Record<string, unknown> = Record<string, unknown>>
|
||||
implements IDataSourceRuntimeContext<TState> {
|
||||
private _dataSourceEngine: IDataSourceEngine;
|
||||
|
||||
public constructor(
|
||||
private _state: TState,
|
||||
private _createDataSourceEngine: (
|
||||
context: IDataSourceRuntimeContext<TState>
|
||||
) => IDataSourceEngine,
|
||||
private _customMethods: Record<string, () => any> = {}
|
||||
) {
|
||||
this._dataSourceEngine = _createDataSourceEngine(this);
|
||||
|
||||
// 自定义方法
|
||||
Object.assign(this, _customMethods);
|
||||
}
|
||||
|
||||
public get state() {
|
||||
return this._state;
|
||||
}
|
||||
|
||||
public setState(state: Partial<TState>) {
|
||||
this._state = {
|
||||
...this._state,
|
||||
...state,
|
||||
};
|
||||
}
|
||||
|
||||
public get dataSourceMap() {
|
||||
return this._dataSourceEngine.dataSourceMap;
|
||||
}
|
||||
|
||||
public async reloadDataSource(): Promise<void> {
|
||||
this._dataSourceEngine.reloadDataSource();
|
||||
}
|
||||
|
||||
public get page(): any {
|
||||
throw new Error('this.page should not be accessed by datasource-engine');
|
||||
}
|
||||
|
||||
public get component(): any {
|
||||
throw new Error(
|
||||
'this.component should not be accessed by datasource-engine'
|
||||
);
|
||||
}
|
||||
|
||||
[customMethod: string]: any;
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
# 关于此场景
|
||||
|
||||
数据源的 type 可以是 `custom` 类型的, 此时需要提供 `requestHandler` 给数据源
|
||||
@ -1,67 +0,0 @@
|
||||
import { RuntimeDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const dataSource: RuntimeDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'custom',
|
||||
isSync: true,
|
||||
requestHandler: options => {
|
||||
return new Promise(res => {
|
||||
setTimeout(() => {
|
||||
// test return data
|
||||
res({
|
||||
data: {
|
||||
id: 9527,
|
||||
name: 'Alice',
|
||||
uri: options.uri,
|
||||
},
|
||||
});
|
||||
}, 1000);
|
||||
});
|
||||
},
|
||||
options() {
|
||||
return {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'orders',
|
||||
isInit: true,
|
||||
type: 'custom',
|
||||
isSync: true,
|
||||
requestHandler: () => {
|
||||
return new Promise((res, rej) => {
|
||||
setTimeout(() => {
|
||||
// test return data
|
||||
rej(new Error('test error'));
|
||||
}, 1000);
|
||||
});
|
||||
},
|
||||
options() {
|
||||
return {
|
||||
uri: 'https://mocks.alibaba-inc.com/orders.json',
|
||||
params: {
|
||||
userId: this.state.user.id,
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
// 这个 api 是假的,调不通的,当前场景是故意需要报错的
|
||||
id: 'members',
|
||||
isInit: true,
|
||||
type: 'custom',
|
||||
isSync: true,
|
||||
options() {
|
||||
return {
|
||||
uri: 'https://mocks.alibaba-inc.com/members.json',
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,67 +0,0 @@
|
||||
import { InterpretDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的 schema:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const DATA_SOURCE_SCHEMA: InterpretDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'custom',
|
||||
isSync: true,
|
||||
requestHandler: {
|
||||
type: 'JSFunction',
|
||||
value: `function(options){
|
||||
return new Promise(res => {
|
||||
setTimeout(() => {
|
||||
// test return data
|
||||
res({
|
||||
data: {
|
||||
id: 9527,
|
||||
name: 'Alice',
|
||||
uri: options.uri,
|
||||
}
|
||||
});
|
||||
}, 1000);
|
||||
});
|
||||
}`,
|
||||
},
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'orders',
|
||||
isInit: true,
|
||||
type: 'custom',
|
||||
isSync: true,
|
||||
requestHandler: {
|
||||
type: 'JSFunction',
|
||||
value: `function(options){
|
||||
return new Promise((res, rej) => {
|
||||
setTimeout(() => {
|
||||
// test return data
|
||||
return rej(new Error('test error'));
|
||||
}, 1000);
|
||||
});
|
||||
}`,
|
||||
},
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/orders.json',
|
||||
params: {
|
||||
type: 'JSExpression',
|
||||
value: '{ userId: this.state.user.id }',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'members',
|
||||
isInit: true,
|
||||
type: 'custom',
|
||||
isSync: true,
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/members.json',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,90 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { bindRuntimeContext, MockContext } from '../../_helpers';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const normalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options?: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
|
||||
const USER_DATA = {
|
||||
id: 9527,
|
||||
name: 'Alice',
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json'
|
||||
};
|
||||
const ERROR_MSG = 'test error';
|
||||
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(bindRuntimeContext(dataSource, ctx), ctx));
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Initial);
|
||||
t.is(context.dataSourceMap.members.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await clock.tickAsync(1050);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await clock.tickAsync(1050);
|
||||
|
||||
// members 因为没有 requestHandler 直接就挂了
|
||||
t.is(context.dataSourceMap.members.status, RuntimeDataSourceStatus.Error)
|
||||
|
||||
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.deepEqual(context.dataSourceMap.members.data, undefined);
|
||||
t.not(context.dataSourceMap.members.error, undefined);
|
||||
t.regex(context.dataSourceMap.orders.error!.message, new RegExp(ERROR_MSG));
|
||||
t.regex(context.dataSourceMap.members.error!.message, new RegExp('no custom handler provide'));
|
||||
|
||||
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.calledOnce);
|
||||
t.deepEqual(context.state.user, USER_DATA);
|
||||
t.is(context.state.orders, undefined);
|
||||
t.is(context.state.members, undefined);
|
||||
};
|
||||
|
||||
normalScene.title = (providedTitle) => providedTitle || 'normal scene';
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,3 +0,0 @@
|
||||
# 关于此场景
|
||||
|
||||
这个是一个及其简单的场景 -- 就是直接调用 fetch,没有啥 dataHandler 之类的
|
||||
@ -1,15 +0,0 @@
|
||||
import { RuntimeDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const dataSource: RuntimeDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
type: 'fetch',
|
||||
options: () => ({
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
}),
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,15 +0,0 @@
|
||||
import { InterpretDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的 schema:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const DATA_SOURCE_SCHEMA: InterpretDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
type: 'fetch',
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,72 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { delay, MockContext } from '../../_helpers';
|
||||
// import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const abnormalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
const ERROR_MSG = 'test error';
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
throw new Error(ERROR_MSG);
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(dataSource, ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}));
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该失败了,error 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Error);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, undefined);
|
||||
t.not(context.dataSourceMap.user.error, undefined);
|
||||
t.regex(context.dataSourceMap.user.error!.message, new RegExp(ERROR_MSG));
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.notCalled);
|
||||
t.deepEqual(context.state.user, undefined);
|
||||
|
||||
// fetchHandler 不应该被调
|
||||
t.assert(fetchHandler.calledOnce);
|
||||
};
|
||||
|
||||
abnormalScene.title = (providedTitle) => providedTitle || 'abnormal scene';
|
||||
@ -1,81 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const normalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
|
||||
const USER_DATA = {
|
||||
name: 'Alice',
|
||||
age: 18,
|
||||
};
|
||||
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
return { data: USER_DATA };
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(dataSource, ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}));
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该成功了,loaded 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loaded);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, USER_DATA);
|
||||
t.deepEqual(context.dataSourceMap.user.error, undefined);
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.calledOnce);
|
||||
t.deepEqual(context.state.user, USER_DATA);
|
||||
|
||||
// fetchHandler 应该被调用了一次
|
||||
t.assert(fetchHandler.calledOnce);
|
||||
|
||||
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
|
||||
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
|
||||
// 检查调用参数
|
||||
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
|
||||
};
|
||||
|
||||
normalScene.title = (providedTitle) => providedTitle || 'normal scene';
|
||||
@ -1,22 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,3 +0,0 @@
|
||||
# 关于此场景
|
||||
|
||||
有些场景下,多个数据源之间有依赖关系,这时候可以将其都设置为 `isSync: true`, 这样这些数据源就会按配置面板的顺序进行串行调用。
|
||||
@ -1,33 +0,0 @@
|
||||
import { RuntimeDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const dataSource: RuntimeDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
isSync: true,
|
||||
options() {
|
||||
return {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'orders',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
isSync: true,
|
||||
options() {
|
||||
return {
|
||||
uri: 'https://mocks.alibaba-inc.com/orders.json',
|
||||
params: {
|
||||
userId: this.state.user.id,
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,30 +0,0 @@
|
||||
import { InterpretDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的 schema:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const DATA_SOURCE_SCHEMA: InterpretDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
isSync: true,
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'orders',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
isSync: true,
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/orders.json',
|
||||
params: {
|
||||
type: 'JSExpression',
|
||||
value: '{ userId: this.state.user.id }',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,96 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { bindRuntimeContext, delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const abnormalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
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);
|
||||
};
|
||||
|
||||
abnormalScene.title = (providedTitle) => providedTitle || 'abnormal scene';
|
||||
@ -1,92 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { bindRuntimeContext, delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const normalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
|
||||
const USER_DATA = {
|
||||
id: 9527,
|
||||
name: 'Alice',
|
||||
};
|
||||
|
||||
const ORDERS_DATA = [{ id: 123 }, { id: 456 }];
|
||||
|
||||
const fetchHandler = sinon.fake(async ({ uri }) => {
|
||||
await delay(100);
|
||||
return { data: /user/.test(uri) ? USER_DATA : ORDERS_DATA };
|
||||
});
|
||||
|
||||
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]);
|
||||
|
||||
// 最后应该成功了,loaded 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loaded);
|
||||
t.is(context.dataSourceMap.orders.status, RuntimeDataSourceStatus.Loaded);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, USER_DATA);
|
||||
t.deepEqual(context.dataSourceMap.user.error, undefined);
|
||||
t.deepEqual(context.dataSourceMap.orders.data, ORDERS_DATA);
|
||||
t.deepEqual(context.dataSourceMap.orders.error, undefined);
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.calledTwice);
|
||||
t.deepEqual(context.state.user, USER_DATA);
|
||||
t.deepEqual(context.state.orders, ORDERS_DATA);
|
||||
|
||||
// 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);
|
||||
};
|
||||
|
||||
normalScene.title = (providedTitle) => providedTitle || 'normal scene';
|
||||
@ -1,22 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,3 +0,0 @@
|
||||
# 关于此场景
|
||||
|
||||
这个是一个及其简单的场景 -- 就是直接调用 fetch,没有啥 dataHandler 之类的
|
||||
@ -1,13 +0,0 @@
|
||||
import { RuntimeDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const dataSource: RuntimeDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'urlParams',
|
||||
isInit: true,
|
||||
type: 'urlParams',
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,13 +0,0 @@
|
||||
import { InterpretDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的 schema:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const DATA_SOURCE_SCHEMA: InterpretDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'urlParams',
|
||||
isInit: true,
|
||||
type: 'urlParams',
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,72 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { MockContext } from '../../_helpers';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const normalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
|
||||
const URL_PARAMS = {
|
||||
name: 'Alice',
|
||||
age: '18',
|
||||
};
|
||||
|
||||
const urlParamsHandler = sinon.fake(async () => {
|
||||
return URL_PARAMS; // TODO: 别的都是返回的套了一层 data 的,但是 urlParams 的为啥不一样?
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(dataSource, ctx, {
|
||||
requestHandlersMap: {
|
||||
urlParams: urlParamsHandler,
|
||||
},
|
||||
}));
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.urlParams.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该成功了,loaded 状态
|
||||
t.is(context.dataSourceMap.urlParams.status, RuntimeDataSourceStatus.Loaded);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.urlParams.data, URL_PARAMS);
|
||||
t.deepEqual(context.dataSourceMap.urlParams.error, undefined);
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.calledOnce);
|
||||
t.deepEqual(context.state.urlParams, URL_PARAMS);
|
||||
|
||||
// fetchHandler 应该被调用了一次
|
||||
t.assert(urlParamsHandler.calledOnce);
|
||||
|
||||
// 检查调用参数 url 没有 options
|
||||
t.deepEqual(urlParamsHandler.firstCall.args, [context]);
|
||||
};
|
||||
|
||||
normalScene.title = (providedTitle) => providedTitle || 'normal scene';
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,26 +0,0 @@
|
||||
# 关于此场景
|
||||
|
||||
这个是一个很常见的场景 -- 查出来的数据里面套的还有一层数据,可能有异常状态得需要处理下。
|
||||
|
||||
比如,期望的正常的数据应该是:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"data": {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
而异常场景下,服务端会返回:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": false,
|
||||
"message": "这是错误原因",
|
||||
"code": "错误码"
|
||||
}
|
||||
```
|
||||
|
||||
-- 这时候期望有异常监控埋点。
|
||||
@ -1,30 +0,0 @@
|
||||
import { RuntimeDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const dataSource: RuntimeDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
options: () => ({
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
}),
|
||||
dataHandler: function dataHandler(response: any) {
|
||||
const { data } = response;
|
||||
if (!data) {
|
||||
throw new Error('empty data!');
|
||||
}
|
||||
|
||||
if (data.success) {
|
||||
return data.data;
|
||||
}
|
||||
|
||||
this.recordError({ type: 'FETCH_ERROR', detail: data });
|
||||
|
||||
throw new Error(data.message);
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,35 +0,0 @@
|
||||
import { InterpretDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的 schema:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const DATA_SOURCE_SCHEMA: InterpretDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
},
|
||||
dataHandler: {
|
||||
type: 'JSFunction',
|
||||
value: `
|
||||
function dataHandler(response){
|
||||
const { data } = response;
|
||||
if (!data) {
|
||||
throw new Error('empty data!');
|
||||
}
|
||||
|
||||
if (data.success){
|
||||
return data.data;
|
||||
}
|
||||
|
||||
this.recordError({ type: 'FETCH_ERROR', detail: data });
|
||||
|
||||
throw new Error(data.message);
|
||||
}
|
||||
`,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,95 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { bindRuntimeContext, delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const abnormalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
const ERROR_MSG = 'test error';
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
return {
|
||||
data: {
|
||||
success: false,
|
||||
message: ERROR_MSG,
|
||||
code: 'E_FOO',
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>(
|
||||
{},
|
||||
(ctx) => create(bindRuntimeContext(dataSource, ctx), ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}),
|
||||
{
|
||||
recordError() { },
|
||||
},
|
||||
);
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
const recordError = sinon.spy(context, 'recordError');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该失败了,error 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Error);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, undefined);
|
||||
t.not(context.dataSourceMap.user.error, undefined);
|
||||
|
||||
t.regex(context.dataSourceMap.user.error!.message, new RegExp(ERROR_MSG));
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.notCalled);
|
||||
t.deepEqual(context.state.user, undefined);
|
||||
|
||||
// fetchHandler 应该被调用了一次
|
||||
t.assert(fetchHandler.calledOnce);
|
||||
|
||||
// 检查调用参数
|
||||
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
|
||||
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
|
||||
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
|
||||
|
||||
// 埋点应该也会被调用
|
||||
t.assert(recordError.calledOnce);
|
||||
t.snapshot(recordError.firstCall.args);
|
||||
};
|
||||
|
||||
abnormalScene.title = (providedTitle) => providedTitle || 'abnormal scene';
|
||||
@ -1,96 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { bindRuntimeContext, delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const normalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
|
||||
const USER_DATA = {
|
||||
name: 'Alice',
|
||||
age: 18,
|
||||
};
|
||||
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
return {
|
||||
data: {
|
||||
success: true,
|
||||
data: USER_DATA,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>(
|
||||
{},
|
||||
(ctx) => create(bindRuntimeContext(dataSource, ctx), ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}),
|
||||
{
|
||||
recordError() { },
|
||||
},
|
||||
);
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
const recordError = sinon.spy(context, 'recordError');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该成功了,loaded 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loaded);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, USER_DATA);
|
||||
t.deepEqual(context.dataSourceMap.user.error, undefined);
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.calledOnce);
|
||||
t.deepEqual(context.state.user, USER_DATA);
|
||||
|
||||
// fetchHandler 应该被调用了一次
|
||||
t.assert(fetchHandler.calledOnce);
|
||||
|
||||
// 检查调用参数
|
||||
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
|
||||
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
|
||||
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
|
||||
|
||||
// 埋点不应该被调用
|
||||
t.assert(recordError.notCalled);
|
||||
};
|
||||
|
||||
normalScene.title = (providedTitle) => providedTitle || 'normal scene';
|
||||
@ -1,21 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
# Snapshot report for `test/scenes/custom-response-status/abnormal-interpret.test.ts`
|
||||
|
||||
The actual snapshot is saved in `abnormal-interpret.test.ts.snap`.
|
||||
|
||||
Generated by [AVA](https://avajs.dev).
|
||||
|
||||
## abnormal scene
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
[
|
||||
{
|
||||
detail: {
|
||||
code: 'E_FOO',
|
||||
message: 'test error',
|
||||
success: false,
|
||||
},
|
||||
type: 'FETCH_ERROR',
|
||||
},
|
||||
]
|
||||
Binary file not shown.
@ -1,20 +0,0 @@
|
||||
# Snapshot report for `test/scenes/p0-1-custom-response-status/abnormal-runtime.test.ts`
|
||||
|
||||
The actual snapshot is saved in `abnormal-runtime.test.ts.snap`.
|
||||
|
||||
Generated by [AVA](https://avajs.dev).
|
||||
|
||||
## abnormal scene
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
[
|
||||
{
|
||||
detail: {
|
||||
code: 'E_FOO',
|
||||
message: 'test error',
|
||||
success: false,
|
||||
},
|
||||
type: 'FETCH_ERROR',
|
||||
},
|
||||
]
|
||||
Binary file not shown.
@ -1,3 +0,0 @@
|
||||
# 关于此场景
|
||||
|
||||
有些数据源的错误可以忽略(吃掉)-- 通过 dataHandler 捕获 error,只要其不重新抛出 error 而且不返回 rejected 状态的 Promise.
|
||||
@ -1,21 +0,0 @@
|
||||
import { RuntimeDataSource } from '@ali/lowcode-types';
|
||||
|
||||
export const DEFAULT_USER_DATA = { id: 0, name: 'guest' }; // 返回一个兜底的数据
|
||||
|
||||
// 这里仅仅是数据源部分的:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const dataSource: RuntimeDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
options: () => ({
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
}),
|
||||
dataHandler: function dataHandler(response: any) {
|
||||
return response.data;
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,24 +0,0 @@
|
||||
import { InterpretDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的 schema:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const DATA_SOURCE_SCHEMA: InterpretDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
},
|
||||
dataHandler: {
|
||||
type: 'JSFunction',
|
||||
value: `
|
||||
function dataHandler(response) {
|
||||
return response.data;
|
||||
}
|
||||
`,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,78 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const abnormalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
const ERROR_MSG = 'test error';
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
throw new Error(ERROR_MSG);
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(dataSource, ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}));
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 注意 error 是会被吃掉了,还是 loaded 状态
|
||||
// FIXME: 根据协议内容,dataHandler 返回的结果是需要抛出错误的,那么 fetchHandler 的错误难道不需要处理?
|
||||
// TODO: 提案:request 如果挂了,不应该需要走 dataHandler 了,没有意义
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Error);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, undefined);
|
||||
t.not(context.dataSourceMap.user.error, undefined);
|
||||
t.regex(context.dataSourceMap.user.error!.message, new RegExp(ERROR_MSG));
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.notCalled);
|
||||
|
||||
// fetchHandler 应该没调
|
||||
t.assert.skip(fetchHandler.notCalled);
|
||||
|
||||
// 检查调用参数
|
||||
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';
|
||||
@ -1,81 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const normalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
|
||||
const USER_DATA = {
|
||||
name: 'Alice',
|
||||
age: 18,
|
||||
};
|
||||
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
return { data: USER_DATA };
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>({}, (ctx) => create(dataSource, ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}));
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该成功了,loaded 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loaded);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, USER_DATA);
|
||||
t.deepEqual(context.dataSourceMap.user.error, undefined);
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.calledOnce);
|
||||
t.deepEqual(context.state.user, USER_DATA);
|
||||
|
||||
// fetchHandler 应该被调用了一次
|
||||
t.assert(fetchHandler.calledOnce);
|
||||
|
||||
// 检查调用参数
|
||||
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
|
||||
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
|
||||
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
|
||||
};
|
||||
|
||||
normalScene.title = (providedTitle) => providedTitle || 'normal scene';
|
||||
@ -1,22 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { normalScene } from './_macro-normal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(normalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
@ -1,3 +0,0 @@
|
||||
# 关于此场景
|
||||
|
||||
某些场景下 dataHandler 不能同步返回,比如可能需要读取某个特殊的异步的数据源并合并响应数据。
|
||||
@ -1,30 +0,0 @@
|
||||
import { RuntimeDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const dataSource: RuntimeDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
options: () => ({
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
}),
|
||||
dataHandler: async function dataHandler(response: any) {
|
||||
const { data } = response;
|
||||
if (!data) {
|
||||
throw new Error('empty data!');
|
||||
}
|
||||
|
||||
if (data.success) {
|
||||
return data.data;
|
||||
}
|
||||
|
||||
this.recordError({ type: 'FETCH_ERROR', detail: data });
|
||||
|
||||
throw new Error(data.message);
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,35 +0,0 @@
|
||||
import { InterpretDataSource } from '@ali/lowcode-types';
|
||||
|
||||
// 这里仅仅是数据源部分的 schema:
|
||||
// @see: https://yuque.antfin-inc.com/mo/spec/spec-low-code-building-schema#XMeF5
|
||||
export const DATA_SOURCE_SCHEMA: InterpretDataSource = {
|
||||
list: [
|
||||
{
|
||||
id: 'user',
|
||||
isInit: true,
|
||||
type: 'fetch',
|
||||
options: {
|
||||
uri: 'https://mocks.alibaba-inc.com/user.json',
|
||||
},
|
||||
dataHandler: {
|
||||
type: 'JSFunction',
|
||||
value: `
|
||||
async function dataHandler(response){
|
||||
const { data } = response;
|
||||
if (!data) {
|
||||
throw new Error('empty data!');
|
||||
}
|
||||
|
||||
if (data.success){
|
||||
return data.data;
|
||||
}
|
||||
|
||||
this.recordError({ type: 'FETCH_ERROR', detail: data });
|
||||
|
||||
throw new Error(data.message);
|
||||
}
|
||||
`,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
@ -1,95 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { bindRuntimeContext, delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const abnormalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
const ERROR_MSG = 'test error';
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
return {
|
||||
data: {
|
||||
success: false,
|
||||
message: ERROR_MSG,
|
||||
code: 'E_FOO',
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>(
|
||||
{},
|
||||
(ctx) => create(bindRuntimeContext(dataSource, ctx), ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}),
|
||||
{
|
||||
recordError() { },
|
||||
},
|
||||
);
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
const recordError = sinon.spy(context, 'recordError');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该失败了,error 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Error);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, undefined);
|
||||
t.not(context.dataSourceMap.user.error, undefined);
|
||||
|
||||
t.regex(context.dataSourceMap.user.error!.message, new RegExp(ERROR_MSG));
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.notCalled);
|
||||
t.deepEqual(context.state.user, undefined);
|
||||
|
||||
// fetchHandler 应该被调用了一次
|
||||
t.assert(fetchHandler.calledOnce);
|
||||
|
||||
// 检查调用参数
|
||||
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
|
||||
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
|
||||
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
|
||||
|
||||
// 埋点应该也会被调用
|
||||
t.assert(recordError.calledOnce);
|
||||
t.snapshot(recordError.firstCall.args);
|
||||
};
|
||||
|
||||
abnormalScene.title = (providedTitle) => providedTitle || 'abnormal scene';
|
||||
@ -1,96 +0,0 @@
|
||||
import {
|
||||
InterpretDataSource,
|
||||
IDataSourceEngine,
|
||||
IDataSourceRuntimeContext,
|
||||
RuntimeDataSource,
|
||||
RuntimeDataSourceStatus,
|
||||
} from '@ali/lowcode-types';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { bindRuntimeContext, delay, MockContext } from '../../_helpers';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
|
||||
import type { ExecutionContext, Macro } from 'ava';
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
export const normalScene: Macro<[
|
||||
{
|
||||
create: (
|
||||
dataSource: any,
|
||||
ctx: IDataSourceRuntimeContext,
|
||||
options: any
|
||||
) => IDataSourceEngine;
|
||||
dataSource: RuntimeDataSource | InterpretDataSource;
|
||||
}
|
||||
]> = async (
|
||||
t: ExecutionContext<{ clock: SinonFakeTimers }>,
|
||||
{ create, dataSource },
|
||||
) => {
|
||||
const { clock } = t.context;
|
||||
|
||||
const USER_DATA = {
|
||||
name: 'Alice',
|
||||
age: 18,
|
||||
};
|
||||
|
||||
const fetchHandler = sinon.fake(async () => {
|
||||
await delay(100);
|
||||
return {
|
||||
data: {
|
||||
success: true,
|
||||
data: USER_DATA,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const context = new MockContext<Record<string, unknown>>(
|
||||
{},
|
||||
(ctx) => create(bindRuntimeContext(dataSource, ctx), ctx, {
|
||||
requestHandlersMap: {
|
||||
fetch: fetchHandler,
|
||||
},
|
||||
}),
|
||||
{
|
||||
recordError() { },
|
||||
},
|
||||
);
|
||||
|
||||
const setState = sinon.spy(context, 'setState');
|
||||
const recordError = sinon.spy(context, 'recordError');
|
||||
|
||||
// 一开始应该是初始状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Initial);
|
||||
|
||||
const loading = context.reloadDataSource();
|
||||
|
||||
await clock.tickAsync(50);
|
||||
|
||||
// 中间应该有 loading 态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loading);
|
||||
|
||||
await Promise.all([clock.runAllAsync(), loading]);
|
||||
|
||||
// 最后应该成功了,loaded 状态
|
||||
t.is(context.dataSourceMap.user.status, RuntimeDataSourceStatus.Loaded);
|
||||
|
||||
// 检查数据源的数据
|
||||
t.deepEqual(context.dataSourceMap.user.data, USER_DATA);
|
||||
t.deepEqual(context.dataSourceMap.user.error, undefined);
|
||||
|
||||
// 检查状态数据
|
||||
t.assert(setState.calledOnce);
|
||||
t.deepEqual(context.state.user, USER_DATA);
|
||||
|
||||
// fetchHandler 应该被调用了一次
|
||||
t.assert(fetchHandler.calledOnce);
|
||||
|
||||
// 检查调用参数
|
||||
const firstListItemOptions = DATA_SOURCE_SCHEMA.list[0].options;
|
||||
const fetchHandlerCallArgs = fetchHandler.firstCall.args[0];
|
||||
t.is(firstListItemOptions.uri, fetchHandlerCallArgs.uri);
|
||||
|
||||
// 埋点不应该被调用
|
||||
t.assert(recordError.notCalled);
|
||||
};
|
||||
|
||||
normalScene.title = (providedTitle) => providedTitle || 'normal scene';
|
||||
@ -1,21 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon from 'sinon';
|
||||
|
||||
import { create } from '../../../src/interpret';
|
||||
import { DATA_SOURCE_SCHEMA } from './_datasource-schema';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
import type { SinonFakeTimers } from 'sinon';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource: DATA_SOURCE_SCHEMA,
|
||||
});
|
||||
@ -1,20 +0,0 @@
|
||||
import test, { ExecutionContext } from 'ava';
|
||||
import sinon, { SinonFakeTimers } from 'sinon';
|
||||
|
||||
import { create } from '../../../src/runtime';
|
||||
|
||||
import { dataSource } from './_datasource-runtime';
|
||||
import { abnormalScene } from './_macro-abnormal';
|
||||
|
||||
test.before((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock = sinon.useFakeTimers();
|
||||
});
|
||||
|
||||
test.after((t: ExecutionContext<{ clock: SinonFakeTimers }>) => {
|
||||
t.context.clock.restore();
|
||||
});
|
||||
|
||||
test(abnormalScene, {
|
||||
create,
|
||||
dataSource,
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user