diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index f0ef7d6b..ecda43fe 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -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; diff --git a/packages/data-source/src/DataSourceManager.ts b/packages/data-source/src/DataSourceManager.ts index 73c86edf..637d0999 100644 --- a/packages/data-source/src/DataSourceManager.ts +++ b/packages/data-source/src/DataSourceManager.ts @@ -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(); diff --git a/packages/data-source/src/createDataSourceManager.ts b/packages/data-source/src/createDataSourceManager.ts index 1bd97256..a280e8b2 100644 --- a/packages/data-source/src/createDataSourceManager.ts +++ b/packages/data-source/src/createDataSourceManager.ts @@ -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'; /** * 创建数据源管理器 diff --git a/packages/data-source/src/data-sources/Base.ts b/packages/data-source/src/data-sources/Base.ts index 0e3317ad..ec6c41d1 100644 --- a/packages/data-source/src/data-sources/Base.ts +++ b/packages/data-source/src/data-sources/Base.ts @@ -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'; /** diff --git a/packages/data-source/src/data-sources/Http.ts b/packages/data-source/src/data-sources/Http.ts index 5bfefec8..e4d74865 100644 --- a/packages/data-source/src/data-sources/Http.ts +++ b/packages/data-source/src/data-sources/Http.ts @@ -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); } } diff --git a/packages/data-source/src/types.ts b/packages/data-source/src/types.ts index 45e66a5f..7b363fc7 100644 --- a/packages/data-source/src/types.ts +++ b/packages/data-source/src/types.ts @@ -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; - -export interface HttpOptions { - url: string; - params?: Record; - data?: Record; - headers?: Record; - method?: Method; -} - export interface HttpDataSourceSchema extends DataSourceSchema { type: 'http'; options: HttpOptions; diff --git a/packages/schema/src/index.ts b/packages/schema/src/index.ts index 928069b0..0f77599b 100644 --- a/packages/schema/src/index.ts +++ b/packages/schema/src/index.ts @@ -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; + data?: Record; + headers?: Record; + method?: Method; + [key: string]: any; +} + +export type RequestFunction = (options: HttpOptions) => Promise; + +export interface AppCore { + dsl?: MApp; + platform?: string; + jsEngine?: string; + request?: RequestFunction; + [key: string]: any; +} export enum NodeType { CONTAINER = 'container',