refactor(core,data-source,schema): 将AppCore定义移到schema中

This commit is contained in:
roymondchen 2023-09-13 16:47:34 +08:00
parent ea1cae7968
commit 502e59cfd7
7 changed files with 69 additions and 52 deletions

View File

@ -20,17 +20,19 @@ import { EventEmitter } from 'events';
import { has, isEmpty } from 'lodash-es';
import { createDataSourceManager, DataSourceManager, RequestFunction } from '@tmagic/data-source';
import { createDataSourceManager, DataSourceManager } from '@tmagic/data-source';
import {
ActionType,
CodeBlockDSL,
CodeItemConfig,
CompItemConfig,
DataSourceItemConfig,
DeprecatedEventConfig,
EventConfig,
Id,
MApp,
type AppCore,
type CodeBlockDSL,
type CodeItemConfig,
type CompItemConfig,
type DataSourceItemConfig,
type DeprecatedEventConfig,
type EventConfig,
type Id,
type MApp,
type RequestFunction,
} from '@tmagic/schema';
import Env from './Env';
@ -56,7 +58,7 @@ interface EventCache {
args: any[];
}
class App extends EventEmitter {
class App extends EventEmitter implements AppCore {
public env: Env = new Env();
public dsl?: MApp;
public codeDsl?: CodeBlockDSL;

View File

@ -20,11 +20,11 @@ import EventEmitter from 'events';
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 { DataSource, HttpDataSource } from './data-sources';
import type { AppCore, DataSourceManagerData, DataSourceManagerOptions, HttpDataSourceSchema } from './types';
import type { DataSourceManagerData, DataSourceManagerOptions, HttpDataSourceSchema } from './types';
class DataSourceManager extends EventEmitter {
private static dataSourceClassMap = new Map<string, typeof DataSource>();

View File

@ -17,10 +17,10 @@
*/
import { cloneDeep } from 'lodash-es';
import type { AppCore } from '@tmagic/schema';
import { getDepNodeIds, getNodes, replaceChildNode } from '@tmagic/utils';
import DataSourceManager from './DataSourceManager';
import type { AppCore } from './types';
/**
*

View File

@ -17,9 +17,9 @@
*/
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';
/**

View File

@ -15,9 +15,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { HttpOptions, RequestFunction } from '@tmagic/schema';
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';
@ -69,7 +70,10 @@ export default class HttpDataSource extends DataSource {
public type = 'http';
public isLoading = false;
public error?: Error;
public error?: {
msg?: string;
code?: string | number;
};
public schema: HttpDataSourceSchema;
public httpOptions: HttpOptions;
@ -114,24 +118,34 @@ export default class HttpDataSource extends DataSource {
}
public async request(options: HttpOptions) {
await Promise.all(
this.beforeRequest.map((method) => method({ options, params: {}, dataSource: this, app: this.app })),
);
try {
await Promise.all(
this.beforeRequest.map((method) => method({ options, params: {}, dataSource: this, app: this.app })),
);
const res = await this.fetch?.({
...this.httpOptions,
...options,
});
const res = await this.fetch?.({
...this.httpOptions,
...options,
});
await Promise.all(
this.afterRequest.map((method) => method({ res, options, params: {}, dataSource: this, app: this.app })),
);
await Promise.all(
this.afterRequest.map((method) => method({ res, options, params: {}, dataSource: this, app: this.app })),
);
if (this.schema.responseOptions?.dataPath) {
const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res);
this.setData(data);
} else {
this.setData(res);
if (this.schema.responseOptions?.dataPath) {
const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res);
this.setData(data);
} else {
this.setData(res);
}
this.error = undefined;
} catch (error: any) {
this.error = {
msg: error.message,
};
this.emit('error', error);
}
}

View File

@ -1,29 +1,10 @@
import type { DataSourceSchema, MApp } from '@tmagic/schema';
export interface AppCore {
dsl?: MApp;
platform?: string;
jsEngine?: string;
request?: RequestFunction;
}
import type { AppCore, DataSourceSchema, HttpOptions, RequestFunction } from '@tmagic/schema';
export interface DataSourceOptions {
schema: DataSourceSchema;
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 {
type: 'http';
options: HttpOptions;

View File

@ -15,6 +15,26 @@
* See the License for the specific language governing permissions and
* 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 {
CONTAINER = 'container',