1
0
mirror of synced 2026-04-04 07:08:34 +08:00

refactor(core,data-source,schema): 将AppCore定义移到schema中

This commit is contained in:
roymondchen
2023-09-13 16:47:34 +08:00
parent ea1cae7968
commit 502e59cfd7
7 changed files with 69 additions and 52 deletions

View File

@@ -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);
}
}