feat: 🎸 Rax 出码适配数据源引擎的默认 requestHandlers

This commit is contained in:
牧毅 2020-08-18 13:57:44 +08:00
parent e4f6654aa7
commit 5f529aeb40
2 changed files with 35 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import changeCase from 'change-case';
import { CLASS_DEFINE_CHUNK_NAME, COMMON_CHUNK_NAME } from '../../../const/generator';
import {
@ -32,6 +33,27 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
...pre,
};
const dataSourceConfig = isContainerSchema(pre.ir) ? pre.ir.dataSource : null;
const dataSourceItems: DataSourceConfig[] = (dataSourceConfig && dataSourceConfig.list) || [];
const dataSourceEngineOptions = { runtimeConfig: true };
if (dataSourceItems.length > 0) {
Object.assign(dataSourceEngineOptions, {
requestHandlers: dataSourceItems.reduce(
(handlers, ds) =>
ds.type in handlers
? handlers
: {
...handlers,
[ds.type]: {
type: 'JSExpression',
value: `require('@ali/lowcode-datasource-engine/handlers/${changeCase.kebabCase(ds.type)}')`,
},
},
{} as Record<string, CompositeValue>,
),
});
}
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
@ -47,8 +69,12 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
content: `
_dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });`,
_dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(
this._dataSourceConfig,
this._context,
${generateUnknownType(dataSourceEngineOptions)}
);`,
linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start],
});
@ -62,9 +88,6 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
linkAfter: [RAX_CHUNK_NAME.ClassDidMountBegin],
});
const dataSourceConfig = isContainerSchema(pre.ir) ? pre.ir.dataSource : null;
const dataSourceItems: DataSourceConfig[] = (dataSourceConfig && dataSourceConfig.list) || [];
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,

View File

@ -39,7 +39,13 @@ class Home$$Page extends Component {
_context = this._createContext();
_dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, {
runtimeConfig: true,
requestHandlers: {
urlParams: require('@ali/lowcode-datasource-engine/handlers/url-params'),
fetch: require('@ali/lowcode-datasource-engine/handlers/fetch'),
},
});
_utils = this._defineUtils();