From b9bf8002dce78212f542c854f7bb0075ce2596dc Mon Sep 17 00:00:00 2001 From: "guokai.jgk" Date: Fri, 23 Oct 2020 11:30:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20will=20fetch=20=E6=8C=89=E7=85=A7?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/core/RuntimeDataSourceItem.ts | 14 ++++++++++---- packages/datasource-engine/src/helpers/index.ts | 12 +++++++----- packages/types/src/data-source-runtime.ts | 8 ++++++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/datasource-engine/src/core/RuntimeDataSourceItem.ts b/packages/datasource-engine/src/core/RuntimeDataSourceItem.ts index d4184707a..0abfe5a18 100644 --- a/packages/datasource-engine/src/core/RuntimeDataSourceItem.ts +++ b/packages/datasource-engine/src/core/RuntimeDataSourceItem.ts @@ -106,12 +106,18 @@ class RuntimeDataSourceItem< return; } + let fetchOptions = this._options; + // willFetch - this._dataSourceConfig.willFetch!(); + try { + fetchOptions = await this._dataSourceConfig.willFetch!(this._options); + } catch (error) { + console.error(error); + } // 约定如果 params 有内容,直接做替换,如果没有就用默认的 options 的 - if (params && this._options) { - this._options.params = params; + if (params && fetchOptions) { + fetchOptions.params = params; } const dataHandler = this._dataSourceConfig.dataHandler!; @@ -124,7 +130,7 @@ class RuntimeDataSourceItem< // _context 会给传,但是用不用由 handler 说了算 const result = await (this._request as RequestHandler<{ data: TResultData; - }>)(this._options, this._context).then(dataHandler, errorHandler); + }>)(fetchOptions, this._context).then(dataHandler, errorHandler); // setState this._context.setState({ diff --git a/packages/datasource-engine/src/helpers/index.ts b/packages/datasource-engine/src/helpers/index.ts index 71565b434..1b2726566 100644 --- a/packages/datasource-engine/src/helpers/index.ts +++ b/packages/datasource-engine/src/helpers/index.ts @@ -1,7 +1,8 @@ -import { DataHandler } from '@ali/lowcode-types'; - -// eslint-disable-next-line @typescript-eslint/no-empty-function -function noop() {} +import { + DataHandler, + RuntimeOptionsConfig, + WillFetch, +} from '@ali/lowcode-types'; // 默认的 dataSourceItem 的 dataHandler export const defaultDataHandler: DataHandler = async (response: { @@ -9,7 +10,8 @@ export const defaultDataHandler: DataHandler = async (response: { }) => response.data; // 默认的 dataSourceItem 的 willFetch -export const defaultWillFetch = noop; +export const defaultWillFetch: WillFetch = (options: RuntimeOptionsConfig) => + options; // 默认的 dataSourceItem 的 shouldFetch export const defaultShouldFetch = () => true; diff --git a/packages/types/src/data-source-runtime.ts b/packages/types/src/data-source-runtime.ts index eb27dab61..9a89e39aa 100644 --- a/packages/types/src/data-source-runtime.ts +++ b/packages/types/src/data-source-runtime.ts @@ -14,7 +14,7 @@ export interface RuntimeDataSourceConfig { isInit?: boolean; isSync?: boolean; type?: string; - willFetch?: () => void; + willFetch?: WillFetch; shouldFetch?: () => boolean; requestHandler?: () => void; // TODO: 待定 dataHandler?: DataHandler; @@ -23,6 +23,10 @@ export interface RuntimeDataSourceConfig { [otherKey: string]: unknown; } +export type WillFetch = ( + options: RuntimeOptionsConfig, +) => Promise | RuntimeOptionsConfig; + export type DataHandler = (response: { data: T; [index: string]: unknown; @@ -45,7 +49,7 @@ export interface RuntimeOptionsConfig { // 可以采用 react 的 state,但是需要注意必须提供同步的 setState 功能 export interface IDataSourceRuntimeContext< TState extends Record = Record - > { +> { /** 当前数据源的内容 */ state: TState; /** 设置状态(浅合并) */