feat: 🎸 添加数据源引擎

This commit is contained in:
牧毅 2020-08-19 09:50:16 +08:00
parent bd8714810a
commit 624e2f8d1e
28 changed files with 542 additions and 0 deletions

5
packages/datasource-engine/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/node_modules/
*.log
.DS_Store
/es/
/lib/

View File

@ -0,0 +1 @@
export type * from '../../es/handlers/fetch';

View File

@ -0,0 +1 @@
module.exports = require('../../lib/handlers/fetch').default;

View File

@ -0,0 +1 @@
export type * from '../../es/handlers/mtop';

View File

@ -0,0 +1 @@
module.exports = require('../../lib/handlers/mtop').default;

View File

@ -0,0 +1 @@
export type * from '../../es/handlers/url-params';

View File

@ -0,0 +1 @@
module.exports = require('../../lib/handlers/url-params').default;

View File

@ -0,0 +1,34 @@
{
"name": "@ali/lowcode-datasource-engine",
"version": "0.1.11",
"description": "DataSource Engine for lowcode",
"main": "lib/index.js",
"module": "es/index.js",
"typings": "es/index.d.ts",
"files": [
"handlers",
"src",
"lib",
"es"
],
"scripts": {
"start": "tsc --watch",
"clean": "rm -rf es lib",
"build": "rm -rf es lib && tsc --module esnext --target es6 && mv lib es && tsc",
"prepublishOnly": "npm run build"
},
"author": "",
"license": "ISC",
"publishConfig": {
"registry": "https://registry.npm.alibaba-inc.com"
},
"devDependencies": {
"typescript": "^3.9.7"
},
"dependencies": {
"@ali/universal-mtop": "^5.1.9",
"query-string": "^6.13.1",
"tslib": "^2.0.1",
"universal-request": "^2.2.0"
}
}

View File

@ -0,0 +1,134 @@
import {
DataSourceConfig,
DataSourceEngineOptions,
IDataSourceEngine,
IDataSourceEngineFactory,
IRuntimeContext,
} from '../types';
import { RuntimeDataSource } from './RuntimeDataSource';
export class DataSourceEngine implements IDataSourceEngine {
private _dataSourceMap: Record<string, RuntimeDataSource> = {};
constructor(
private _dataSourceConfig: DataSourceConfig,
private _runtimeContext: IRuntimeContext,
private _options?: DataSourceEngineOptions,
) {
_dataSourceConfig.list?.forEach((ds) => {
// 确保数据源都有处理器
const requestHandler =
ds.requestHandler || _options?.requestHandlersMap?.[ds.type];
if (!requestHandler) {
throw new Error(`No request handler for "${ds.type}" data source`);
}
this._dataSourceMap[ds.id] = new RuntimeDataSource(
ds.id,
ds.type,
getValue(ds.options) || {},
requestHandler.bind(_runtimeContext),
ds.dataHandler ? ds.dataHandler.bind(_runtimeContext) : undefined,
(data) => {
_runtimeContext.setState({ [ds.id]: data });
},
);
});
}
public get dataSourceMap() {
return this._dataSourceMap;
}
public async reloadDataSource() {
try {
const allDataSourceConfigList = this._dataSourceConfig.list || [];
// urlParams 类型的优先加载
for (const ds of allDataSourceConfigList) {
if (ds.type === 'urlParams' && (getValue(ds.isInit) ?? true)) {
await this._dataSourceMap[ds.id].load();
}
}
await sleep(0); // TODO: 如何优雅地解决 setState 的异步问题?
// 然后是所有其他的
const remainDataSourceConfigList = allDataSourceConfigList.filter(
(x) => x.type !== 'urlParams',
);
// 先发起异步的
const asyncLoadings: Array<Promise<unknown>> = [];
for (const ds of remainDataSourceConfigList) {
if (getValue(ds.isInit) ?? true) {
const options = getValue(ds.options);
if (options && !options.isSync) {
this._dataSourceMap[ds.id].setOptions(options);
asyncLoadings.push(
this._dataSourceMap[ds.id].load(options?.params).catch(() => {}),
);
}
}
}
try {
// 再按先后顺序发起同步请求
for (const ds of remainDataSourceConfigList) {
if (getValue(ds.isInit) ?? true) {
const options = getValue(ds.options);
if (options && options.isSync) {
this._dataSourceMap[ds.id].setOptions(options);
await this._dataSourceMap[ds.id].load(options?.params);
await sleep(0); // TODO: 如何优雅地解决 setState 的异步问题?
}
}
}
} catch (e) {}
await Promise.all(asyncLoadings);
} finally {
const allDataHandler = this._dataSourceConfig.dataHandler;
if (allDataHandler) {
await allDataHandler(this._getDataMapOfAll());
}
}
}
private _getDataMapOfAll(): Record<string, unknown> {
const dataMap: Record<string, unknown> = {};
Object.entries(this._dataSourceMap).forEach(([dsId, ds]) => {
dataMap[dsId] = ds.data;
});
return dataMap;
}
}
export const create: IDataSourceEngineFactory['create'] = (
dataSourceConfig,
runtimeContext,
options,
) => {
return new DataSourceEngine(dataSourceConfig, runtimeContext, options);
};
function getValue<T>(valueOrValueGetter: T | (() => T)): T;
function getValue<T extends boolean>(
valueOrValueGetter: T | (() => T),
): T | undefined {
if (typeof valueOrValueGetter === 'function') {
try {
return valueOrValueGetter();
} catch (e) {
return undefined;
}
} else {
return valueOrValueGetter;
}
}
function sleep(ms: number = 0) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View File

@ -0,0 +1,98 @@
import {
DataSourceOptions,
IRuntimeDataSource,
RequestHandler,
RuntimeDataSourceStatus,
} from '../types';
export class RuntimeDataSource<
TParams extends Record<string, unknown> = Record<string, unknown>,
TRequestResult = unknown,
TResultData = unknown
> implements IRuntimeDataSource<TParams, TResultData> {
private _status: RuntimeDataSourceStatus = RuntimeDataSourceStatus.Initial;
private _data?: TResultData;
private _error?: Error;
private _latestOptions: DataSourceOptions<TParams>;
constructor(
private _id: string,
private _type: string,
private _initialOptions: DataSourceOptions<TParams>,
private _requestHandler: RequestHandler<
DataSourceOptions<TParams>,
TRequestResult
>,
private _dataHandler:
| ((
data: TRequestResult | undefined,
error: unknown | undefined,
) => TResultData | Promise<TResultData>)
| undefined,
private _onLoaded: (data: TResultData) => void,
) {
this._latestOptions = _initialOptions;
}
public get status() {
return this._status;
}
public get data() {
return this._data;
}
public get error() {
return this._error;
}
public async load(params?: TParams): Promise<TResultData> {
try {
this._latestOptions = {
...this._latestOptions,
params: {
...this._latestOptions.params,
...params,
} as TParams,
};
this._status = RuntimeDataSourceStatus.Loading;
const data = await this._request(this._latestOptions);
this._status = RuntimeDataSourceStatus.Loaded;
this._onLoaded(data);
this._data = data;
return data;
} catch (err) {
this._error = err;
this._status = RuntimeDataSourceStatus.Error;
throw err;
}
}
public setOptions(options: DataSourceOptions<TParams>) {
this._latestOptions = options;
}
private async _request(options: DataSourceOptions<TParams>) {
try {
const reqResult = await this._requestHandler(options);
const data = this._dataHandler
? await this._dataHandler(reqResult, undefined)
: ((reqResult as unknown) as TResultData);
return data;
} catch (err) {
if (this._dataHandler) {
const data = await this._dataHandler(undefined, err);
return data;
}
throw err;
}
}
}

View File

@ -0,0 +1 @@
export { create } from './DataSourceEngine';

View File

@ -0,0 +1,24 @@
import request from 'universal-request';
import type { AsObject, RequestOptions } from 'universal-request/lib/types';
import { DataSourceOptions, RequestHandler } from '../types';
const fetchHandler: RequestHandler = async ({
url,
uri,
data,
params,
...otherOptions
}: DataSourceOptions) => {
const reqOptions = {
url: ((url || uri) as unknown) as string,
data: ((data || params) as unknown) as AsObject,
...otherOptions,
};
const res = await request(reqOptions as RequestOptions);
return res.data;
};
export default fetchHandler;

View File

@ -0,0 +1,15 @@
import mtop from '@ali/universal-mtop';
import { RequestHandler } from '../types';
const mtopHandler: RequestHandler = async ({ data, params, ...options }) => {
const reqOptions = {
...options,
data: data || params,
};
const res = await mtop(reqOptions);
return res.data;
};
export default mtopHandler;

View File

@ -0,0 +1,14 @@
import qs from 'query-string';
import { RequestHandler } from '../types';
export default function urlParamsHandler({
search,
}: {
search: string | Record<string, unknown>;
}): RequestHandler {
const urlParams = typeof search === 'string' ? qs.parse(search) : search;
return async () => {
return urlParams;
};
}

View File

@ -0,0 +1,2 @@
export * from './core';
export * from './types';

View File

@ -0,0 +1,6 @@
import { DataSourceConfigItem } from './DataSourceConfigItem';
export type DataSourceConfig = {
list?: DataSourceConfigItem[];
dataHandler?: (dataMap: Record<string, unknown>) => void | Promise<void>;
};

View File

@ -0,0 +1,17 @@
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;
};

View File

@ -0,0 +1,7 @@
import { RequestHandler } from './RequestHandler';
export type DataSourceEngineOptions = {
requestHandlersMap?: {
[dataSourceType: string]: RequestHandler;
};
};

View File

@ -0,0 +1,10 @@
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;
};

View File

@ -0,0 +1,9 @@
import { IRuntimeDataSource } from './IRuntimeDataSource';
export interface IDataSourceEngine {
/** 数据源, key 是数据源的 ID */
readonly dataSourceMap: Record<string, IRuntimeDataSource>;
/** 重新加载所有的数据源 */
reloadDataSource(): Promise<void>;
}

View File

@ -0,0 +1,12 @@
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;
}

View File

@ -0,0 +1,24 @@
import { IRuntimeDataSource } from './IRuntimeDataSource';
/** 运行时上下文 */
export interface IRuntimeContext<
TState extends object = 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> };
}

View File

@ -0,0 +1,22 @@
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>;
}

View File

@ -0,0 +1,6 @@
import { DataSourceOptions } from './DataSourceOptions';
export type RequestHandler<
TOptions extends DataSourceOptions = DataSourceOptions,
TResult = unknown
> = (options: TOptions) => Promise<TResult>;

View File

@ -0,0 +1,14 @@
/** 数据源的状态 */
export enum RuntimeDataSourceStatus {
/** 初始状态,尚未加载 */
Initial = 'init',
/** 正在加载 */
Loading = 'loading',
/** 已加载(无错误) */
Loaded = 'loaded',
/** 加载出错了 */
Error = 'error',
}

View File

@ -0,0 +1 @@
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';

View File

@ -0,0 +1 @@
declare module '@ali/universal-mtop';

View File

@ -0,0 +1,80 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": [
"DOM",
"ES2015",
"ES2016",
"ES2017",
"ES2018",
"ES2019"
] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./lib" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
"removeComments": false /* Do not emit comments to output. */,
// "noEmit": true, /* Do not emit outputs. */
"importHelpers": true /* Import emit helpers from 'tslib'. */,
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"rootDirs": [
"src"
] /* List of root folders whose combined content represents the structure of the project at runtime. */,
"typeRoots": [
"./node_modules/@types"
] /* List of folders to include type definitions from. */,
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}