mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-05-05 09:46:56 +08:00
refactor(core,data-source,schema): 将AppCore定义移到schema中
This commit is contained in:
parent
ea1cae7968
commit
502e59cfd7
@ -20,17 +20,19 @@ import { EventEmitter } from 'events';
|
|||||||
|
|
||||||
import { has, isEmpty } from 'lodash-es';
|
import { has, isEmpty } from 'lodash-es';
|
||||||
|
|
||||||
import { createDataSourceManager, DataSourceManager, RequestFunction } from '@tmagic/data-source';
|
import { createDataSourceManager, DataSourceManager } from '@tmagic/data-source';
|
||||||
import {
|
import {
|
||||||
ActionType,
|
ActionType,
|
||||||
CodeBlockDSL,
|
type AppCore,
|
||||||
CodeItemConfig,
|
type CodeBlockDSL,
|
||||||
CompItemConfig,
|
type CodeItemConfig,
|
||||||
DataSourceItemConfig,
|
type CompItemConfig,
|
||||||
DeprecatedEventConfig,
|
type DataSourceItemConfig,
|
||||||
EventConfig,
|
type DeprecatedEventConfig,
|
||||||
Id,
|
type EventConfig,
|
||||||
MApp,
|
type Id,
|
||||||
|
type MApp,
|
||||||
|
type RequestFunction,
|
||||||
} from '@tmagic/schema';
|
} from '@tmagic/schema';
|
||||||
|
|
||||||
import Env from './Env';
|
import Env from './Env';
|
||||||
@ -56,7 +58,7 @@ interface EventCache {
|
|||||||
args: any[];
|
args: any[];
|
||||||
}
|
}
|
||||||
|
|
||||||
class App extends EventEmitter {
|
class App extends EventEmitter implements AppCore {
|
||||||
public env: Env = new Env();
|
public env: Env = new Env();
|
||||||
public dsl?: MApp;
|
public dsl?: MApp;
|
||||||
public codeDsl?: CodeBlockDSL;
|
public codeDsl?: CodeBlockDSL;
|
||||||
|
@ -20,11 +20,11 @@ import EventEmitter from 'events';
|
|||||||
|
|
||||||
import { cloneDeep, template } from 'lodash-es';
|
import { cloneDeep, template } from 'lodash-es';
|
||||||
|
|
||||||
import type { DataSourceSchema, Id, MNode } from '@tmagic/schema';
|
import type { AppCore, DataSourceSchema, Id, MNode } from '@tmagic/schema';
|
||||||
import { compiledCond, compiledNode } from '@tmagic/utils';
|
import { compiledCond, compiledNode } from '@tmagic/utils';
|
||||||
|
|
||||||
import { DataSource, HttpDataSource } from './data-sources';
|
import { DataSource, HttpDataSource } from './data-sources';
|
||||||
import type { AppCore, DataSourceManagerData, DataSourceManagerOptions, HttpDataSourceSchema } from './types';
|
import type { DataSourceManagerData, DataSourceManagerOptions, HttpDataSourceSchema } from './types';
|
||||||
|
|
||||||
class DataSourceManager extends EventEmitter {
|
class DataSourceManager extends EventEmitter {
|
||||||
private static dataSourceClassMap = new Map<string, typeof DataSource>();
|
private static dataSourceClassMap = new Map<string, typeof DataSource>();
|
||||||
|
@ -17,10 +17,10 @@
|
|||||||
*/
|
*/
|
||||||
import { cloneDeep } from 'lodash-es';
|
import { cloneDeep } from 'lodash-es';
|
||||||
|
|
||||||
|
import type { AppCore } from '@tmagic/schema';
|
||||||
import { getDepNodeIds, getNodes, replaceChildNode } from '@tmagic/utils';
|
import { getDepNodeIds, getNodes, replaceChildNode } from '@tmagic/utils';
|
||||||
|
|
||||||
import DataSourceManager from './DataSourceManager';
|
import DataSourceManager from './DataSourceManager';
|
||||||
import type { AppCore } from './types';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建数据源管理器
|
* 创建数据源管理器
|
||||||
|
@ -17,9 +17,9 @@
|
|||||||
*/
|
*/
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
|
|
||||||
import type { CodeBlockContent, DataSchema } from '@tmagic/schema';
|
import type { AppCore, CodeBlockContent, DataSchema } from '@tmagic/schema';
|
||||||
|
|
||||||
import type { AppCore, DataSourceOptions } from '@data-source/types';
|
import type { DataSourceOptions } from '@data-source/types';
|
||||||
import { getDefaultValueFromFields } from '@data-source/util';
|
import { getDefaultValueFromFields } from '@data-source/util';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,9 +15,10 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
import { HttpOptions, RequestFunction } from '@tmagic/schema';
|
||||||
import { getValueByKeyPath } from '@tmagic/utils';
|
import { getValueByKeyPath } from '@tmagic/utils';
|
||||||
|
|
||||||
import { HttpDataSourceOptions, HttpDataSourceSchema, HttpOptions, RequestFunction } from '@data-source/types';
|
import { HttpDataSourceOptions, HttpDataSourceSchema } from '@data-source/types';
|
||||||
|
|
||||||
import DataSource from './Base';
|
import DataSource from './Base';
|
||||||
|
|
||||||
@ -69,7 +70,10 @@ export default class HttpDataSource extends DataSource {
|
|||||||
public type = 'http';
|
public type = 'http';
|
||||||
|
|
||||||
public isLoading = false;
|
public isLoading = false;
|
||||||
public error?: Error;
|
public error?: {
|
||||||
|
msg?: string;
|
||||||
|
code?: string | number;
|
||||||
|
};
|
||||||
public schema: HttpDataSourceSchema;
|
public schema: HttpDataSourceSchema;
|
||||||
public httpOptions: HttpOptions;
|
public httpOptions: HttpOptions;
|
||||||
|
|
||||||
@ -114,24 +118,34 @@ export default class HttpDataSource extends DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async request(options: HttpOptions) {
|
public async request(options: HttpOptions) {
|
||||||
await Promise.all(
|
try {
|
||||||
this.beforeRequest.map((method) => method({ options, params: {}, dataSource: this, app: this.app })),
|
await Promise.all(
|
||||||
);
|
this.beforeRequest.map((method) => method({ options, params: {}, dataSource: this, app: this.app })),
|
||||||
|
);
|
||||||
|
|
||||||
const res = await this.fetch?.({
|
const res = await this.fetch?.({
|
||||||
...this.httpOptions,
|
...this.httpOptions,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
this.afterRequest.map((method) => method({ res, options, params: {}, dataSource: this, app: this.app })),
|
this.afterRequest.map((method) => method({ res, options, params: {}, dataSource: this, app: this.app })),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (this.schema.responseOptions?.dataPath) {
|
if (this.schema.responseOptions?.dataPath) {
|
||||||
const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res);
|
const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res);
|
||||||
this.setData(data);
|
this.setData(data);
|
||||||
} else {
|
} else {
|
||||||
this.setData(res);
|
this.setData(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.error = undefined;
|
||||||
|
} catch (error: any) {
|
||||||
|
this.error = {
|
||||||
|
msg: error.message,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.emit('error', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,29 +1,10 @@
|
|||||||
import type { DataSourceSchema, MApp } from '@tmagic/schema';
|
import type { AppCore, DataSourceSchema, HttpOptions, RequestFunction } from '@tmagic/schema';
|
||||||
|
|
||||||
export interface AppCore {
|
|
||||||
dsl?: MApp;
|
|
||||||
platform?: string;
|
|
||||||
jsEngine?: string;
|
|
||||||
request?: RequestFunction;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DataSourceOptions {
|
export interface DataSourceOptions {
|
||||||
schema: DataSourceSchema;
|
schema: DataSourceSchema;
|
||||||
app: AppCore;
|
app: AppCore;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'post' | 'POST' | 'put' | 'PUT';
|
|
||||||
|
|
||||||
export type RequestFunction = (options: HttpOptions) => Promise<any>;
|
|
||||||
|
|
||||||
export interface HttpOptions {
|
|
||||||
url: string;
|
|
||||||
params?: Record<string, string>;
|
|
||||||
data?: Record<string, any>;
|
|
||||||
headers?: Record<string, string>;
|
|
||||||
method?: Method;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface HttpDataSourceSchema extends DataSourceSchema {
|
export interface HttpDataSourceSchema extends DataSourceSchema {
|
||||||
type: 'http';
|
type: 'http';
|
||||||
options: HttpOptions;
|
options: HttpOptions;
|
||||||
|
@ -15,6 +15,26 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
export type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'post' | 'POST' | 'put' | 'PUT';
|
||||||
|
|
||||||
|
export interface HttpOptions {
|
||||||
|
url: string;
|
||||||
|
params?: Record<string, string>;
|
||||||
|
data?: Record<string, any>;
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
method?: Method;
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RequestFunction = (options: HttpOptions) => Promise<any>;
|
||||||
|
|
||||||
|
export interface AppCore {
|
||||||
|
dsl?: MApp;
|
||||||
|
platform?: string;
|
||||||
|
jsEngine?: string;
|
||||||
|
request?: RequestFunction;
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
export enum NodeType {
|
export enum NodeType {
|
||||||
CONTAINER = 'container',
|
CONTAINER = 'container',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user