Skip to content

Commit

Permalink
fix(data-source): http数据源中mock数据应该是数据源定义好的数据,不再是请求的数据源
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen authored and jia000 committed Dec 9, 2024
1 parent 8b7dca8 commit 2564631
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/data-source/src/data-sources/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -141,21 +139,25 @@ export default class HttpDataSource extends DataSource<HttpDataSourceSchema> {
}

// 注意:在编辑器中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;
Expand Down

0 comments on commit 2564631

Please sign in to comment.