mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-04-06 03:57:56 +08:00
feat(data-source): 数据源中新增app属性,http数据源新增钩子
This commit is contained in:
parent
0865cf4952
commit
c5a1c2db76
@ -64,6 +64,7 @@ class DataSourceManager extends EventEmitter {
|
|||||||
let ds: DataSource;
|
let ds: DataSource;
|
||||||
if (config.type === 'http') {
|
if (config.type === 'http') {
|
||||||
ds = new HttpDataSource({
|
ds = new HttpDataSource({
|
||||||
|
app: this.app,
|
||||||
schema: config as HttpDataSourceSchema,
|
schema: config as HttpDataSourceSchema,
|
||||||
request: this.app.request,
|
request: this.app.request,
|
||||||
});
|
});
|
||||||
@ -72,6 +73,7 @@ class DataSourceManager extends EventEmitter {
|
|||||||
const DataSourceClass = DataSourceManager.dataSourceClassMap.get(config.type) || DataSource;
|
const DataSourceClass = DataSourceManager.dataSourceClassMap.get(config.type) || DataSource;
|
||||||
|
|
||||||
ds = new DataSourceClass({
|
ds = new DataSourceClass({
|
||||||
|
app: this.app,
|
||||||
schema: config,
|
schema: config,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
|
|
||||||
|
import type Core from '@tmagic/core';
|
||||||
import type { CodeBlockContent, DataSchema } from '@tmagic/schema';
|
import type { CodeBlockContent, DataSchema } from '@tmagic/schema';
|
||||||
|
|
||||||
import type { DataSourceOptions } from '@data-source/types';
|
import type { DataSourceOptions } from '@data-source/types';
|
||||||
@ -34,12 +35,15 @@ export default class DataSource extends EventEmitter {
|
|||||||
|
|
||||||
public data: Record<string, any> = {};
|
public data: Record<string, any> = {};
|
||||||
|
|
||||||
|
public app: Core;
|
||||||
|
|
||||||
private fields: DataSchema[] = [];
|
private fields: DataSchema[] = [];
|
||||||
private methods: CodeBlockContent[] = [];
|
private methods: CodeBlockContent[] = [];
|
||||||
|
|
||||||
constructor(options: DataSourceOptions) {
|
constructor(options: DataSourceOptions) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
this.app = options.app;
|
||||||
this.id = options.schema.id;
|
this.id = options.schema.id;
|
||||||
this.setFields(options.schema.fields);
|
this.setFields(options.schema.fields);
|
||||||
this.setMethods(options.schema.methods || []);
|
this.setMethods(options.schema.methods || []);
|
||||||
|
@ -74,12 +74,15 @@ export default class HttpDataSource extends DataSource {
|
|||||||
public httpOptions: HttpOptions;
|
public httpOptions: HttpOptions;
|
||||||
|
|
||||||
private fetch?: RequestFunction;
|
private fetch?: RequestFunction;
|
||||||
|
private beforeRequest: ((...args: any[]) => any)[] = [];
|
||||||
|
private afterRequest: ((...args: any[]) => any)[] = [];
|
||||||
|
|
||||||
constructor(options: HttpDataSourceOptions) {
|
constructor(options: HttpDataSourceOptions) {
|
||||||
const { options: httpOptions, ...dataSourceOptions } = options.schema;
|
const { options: httpOptions, ...dataSourceOptions } = options.schema;
|
||||||
|
|
||||||
super({
|
super({
|
||||||
schema: dataSourceOptions,
|
schema: dataSourceOptions,
|
||||||
|
app: options.app,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.schema = options.schema;
|
this.schema = options.schema;
|
||||||
@ -90,6 +93,16 @@ export default class HttpDataSource extends DataSource {
|
|||||||
} else if (typeof globalThis.fetch === 'function') {
|
} else if (typeof globalThis.fetch === 'function') {
|
||||||
this.fetch = webRequest;
|
this.fetch = webRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.getMethods().forEach((method) => {
|
||||||
|
if (typeof method.content !== 'function') return;
|
||||||
|
if (method.timing === 'beforeRequest') {
|
||||||
|
this.beforeRequest.push(method.content);
|
||||||
|
}
|
||||||
|
if (method.timing === 'afterRequest') {
|
||||||
|
this.afterRequest.push(method.content);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async init() {
|
public async init() {
|
||||||
@ -101,11 +114,19 @@ export default class HttpDataSource extends DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async request(options: HttpOptions) {
|
public async request(options: HttpOptions) {
|
||||||
|
await Promise.all(
|
||||||
|
this.beforeRequest.map((method) => method({ options, params: {}, dataSource: this, app: this.app })),
|
||||||
|
);
|
||||||
|
|
||||||
const res = await this.fetch?.({
|
const res = await this.fetch?.({
|
||||||
...this.httpOptions,
|
...this.httpOptions,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
this.afterRequest.map((method) => method({ res, options, params: {}, dataSource: this, app: this.app })),
|
||||||
|
);
|
||||||
|
|
||||||
if (this.schema.responseOptions?.dataPath) {
|
if (this.schema.responseOptions?.dataPath) {
|
||||||
const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res);
|
const data = getValueByKeyPath(this.schema.responseOptions.dataPath, res);
|
||||||
this.setData(data);
|
this.setData(data);
|
||||||
|
@ -3,6 +3,7 @@ import type { DataSourceSchema } from '@tmagic/schema';
|
|||||||
|
|
||||||
export interface DataSourceOptions {
|
export interface DataSourceOptions {
|
||||||
schema: DataSourceSchema;
|
schema: DataSourceSchema;
|
||||||
|
app: Core;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'post' | 'POST' | 'put' | 'PUT';
|
export type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'post' | 'POST' | 'put' | 'PUT';
|
||||||
@ -28,6 +29,7 @@ export interface HttpDataSourceSchema extends DataSourceSchema {
|
|||||||
|
|
||||||
export interface HttpDataSourceOptions {
|
export interface HttpDataSourceOptions {
|
||||||
schema: HttpDataSourceSchema;
|
schema: HttpDataSourceSchema;
|
||||||
|
app: Core;
|
||||||
request?: RequestFunction;
|
request?: RequestFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user