From 9ff36eae5477903709fe207321c2d557fa8c86ea Mon Sep 17 00:00:00 2001 From: roymondchen Date: Fri, 20 Sep 2024 12:42:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(data-source):=20http=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90=E4=B8=ADmock=E6=95=B0=E6=8D=AE=E5=BA=94=E8=AF=A5?= =?UTF-8?q?=E6=98=AF=E6=95=B0=E6=8D=AE=E6=BA=90=E5=AE=9A=E4=B9=89=E5=A5=BD?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=EF=BC=8C=E4=B8=8D=E5=86=8D=E6=98=AF?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E7=9A=84=E6=95=B0=E6=8D=AE=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data-source/src/data-sources/Http.ts | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/data-source/src/data-sources/Http.ts b/packages/data-source/src/data-sources/Http.ts index 4dbb9b67..3fd9039e 100644 --- a/packages/data-source/src/data-sources/Http.ts +++ b/packages/data-source/src/data-sources/Http.ts @@ -15,8 +15,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { cloneDeep } from 'lodash-es'; - import type { HttpOptions, RequestFunction } from '@tmagic/core'; import { getValueByKeyPath } from '@tmagic/core'; @@ -141,21 +139,25 @@ export default class HttpDataSource extends DataSource { } // 注意:在编辑器中mockData不会为空,至少是默认值,不会发起请求 - let res = this.mockData ? cloneDeep(this.mockData) : await this.#fetch?.(reqOptions); - - for (const method of this.#afterRequest) { - await method({ res, options: reqOptions, params: {}, dataSource: this, app: this.app }); - } - - if (typeof this.schema.afterResponse === 'function') { - res = await this.schema.afterResponse(res, { app: this.app, dataSource: this, options: reqOptions }); - } - - if (this.schema.responseOptions?.dataPath) { - const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res); - this.setData(data); + if (this.mockData) { + this.setData(this.mockData); } else { - this.setData(res); + let res = await this.#fetch?.(reqOptions); + + for (const method of this.#afterRequest) { + await method({ res, options: reqOptions, params: {}, dataSource: this, app: this.app }); + } + + if (typeof this.schema.afterResponse === 'function') { + res = await this.schema.afterResponse(res, { app: this.app, dataSource: this, options: reqOptions }); + } + + if (this.schema.responseOptions?.dataPath) { + const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res); + this.setData(data); + } else { + this.setData(res); + } } this.error = undefined;