1
0
mirror of synced 2026-03-25 04:28:34 +08:00

feat(editor, data-source): 支持http数据源请求、响应裁剪配置

This commit is contained in:
roymondchen
2023-10-31 15:29:04 +08:00
parent cb2ff28129
commit f48afa98f2
5 changed files with 146 additions and 44 deletions

View File

@@ -126,21 +126,29 @@ export default class HttpDataSource extends DataSource {
public async request(options: Partial<HttpOptions> = {}) {
this.isLoading = true;
let reqOptions = {
...this.httpOptions,
...options,
};
try {
for (const method of this.#beforeRequest) {
await method({ options, params: {}, dataSource: this, app: this.app });
await method({ options: reqOptions, params: {}, dataSource: this, app: this.app });
}
if (typeof this.schema.beforeRequest === 'function') {
reqOptions = this.schema.beforeRequest(reqOptions, { app: this.app, dataSource: this });
}
// 注意在编辑器中mockData不会为空至少是默认值不会发起请求
const res = this.mockData
? this.mockData
: await this.#fetch?.({
...this.httpOptions,
...options,
});
let res = this.mockData ? this.mockData : await this.#fetch?.(reqOptions);
for (const method of this.#afterRequest) {
await method({ res, options, params: {}, dataSource: this, app: this.app });
await method({ res, options: reqOptions, params: {}, dataSource: this, app: this.app });
}
if (typeof this.schema.afterResponse === 'function') {
res = this.schema.afterResponse(res, { app: this.app, dataSource: this });
}
if (this.schema.responseOptions?.dataPath) {