Skip to content

Commit dcc6d3f

Browse files
committed
refactor: improve api datasource types
1 parent dbd8e4f commit dcc6d3f

2 files changed

Lines changed: 63 additions & 37 deletions

File tree

src/interfaces.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,19 @@ export type VisualDataSourceType =
106106
| 'static'
107107
| 'api'
108108
| 'csv'
109-
| 'database'
110-
| 'tingyun'
111-
| 'prometheus'
112-
| 'apacheDruid';
109+
| VisualDatabaseType
110+
| VisualDataSourceOtherType;
111+
112+
export type VisualDatabaseType =
113+
| 'mysql'
114+
| 'oracle'
115+
| 'db2'
116+
| 'sqlserver'
117+
| 'postgresql'
118+
| 'dm'
119+
| 'clickhouse';
120+
121+
export type VisualDataSourceOtherType = 'prometheus' | 'apacheDruid' | 'tingyun';
113122

114123
export interface VisualDataFilter {
115124
/** 全局过滤器的 id */
@@ -126,32 +135,49 @@ export interface VisualDataFilter {
126135

127136
export interface VisualDataSource {
128137
type?: VisualDataSourceType;
129-
useFilter?: boolean; // 是否使用过滤器
138+
/** 是否使用过滤器 */
139+
useFilter?: boolean;
140+
/** 过滤器数组 */
130141
filters?: VisualDataFilter[];
131-
api?: string; // API 地址
132-
apiHeaders?: string; // 请求头
133-
apiBody?: any; // POST 请求体
134-
apiMethod?: 'get' | 'post'; // 请求方法
135-
apiParams?: any; // GET 请求参数
136-
local?: boolean;
137-
cookie?: boolean;
138-
csv?: number;
139-
database?: number;
142+
/** Mock 数据模板 */
143+
mockTemplate?: string;
144+
/** 时间戳 */
145+
timePeriod?: number;
146+
/** 静态数据源 */
147+
staticData?: string;
148+
/** API 数据源配置 */
149+
api?: {
150+
url: string;
151+
method: 'get' | 'post' | 'put' | 'delete';
152+
headers?: any;
153+
body?: any;
154+
params?: any;
155+
proxy?: boolean;
156+
cookie?: boolean;
157+
};
158+
/** 数据源 ID */
159+
csv?: string | number;
160+
/** 数据库的数据源 ID */
161+
database?: string | number;
162+
/** 数据库查询 sql */
140163
sql?: string;
141-
staticData?: string; // 静态数据源
142-
tingyun?: number;
143-
tyFilter?: string; // 编辑器所需字符串
144-
prometheus?: number;
164+
/** 数据源 ID */
165+
prometheus?: string | number;
166+
/** 数据源配置 */
145167
prometheusConfig?: {
146168
method: string;
147169
path: string;
148170
query: string;
149171
step: number;
150172
};
151-
apacheDruid?: number;
173+
/** 数据源 ID */
174+
apacheDruid?: string | number;
175+
/** 数据源配置 */
152176
apacheDruidConfig?: {
153177
query: string;
154178
};
155-
mockTemplate?: string;
156-
timePeriod?: number;
179+
/** 数据源 ID */
180+
tingyun?: string | number;
181+
/** 数据源配置 */
182+
tingyunConfig?: string;
157183
}

src/utils.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@ import { VisualDataSource } from './interfaces';
99
export function mergeDataSource(dataSource?: VisualDataSource, config?: VisualDataSource) {
1010
if (dataSource) {
1111
try {
12-
if (isString(dataSource.apiHeaders)) {
13-
dataSource.apiHeaders = JSON.parse(dataSource.apiHeaders);
12+
if (isString(dataSource.api?.headers)) {
13+
dataSource.api.headers = JSON.parse(dataSource.api.headers);
1414
}
15-
if (isString(dataSource.apiBody)) {
16-
dataSource.apiBody = JSON.parse(dataSource.apiBody);
15+
if (isString(dataSource.api?.body)) {
16+
dataSource.api.body = JSON.parse(dataSource.api.body);
1717
}
18-
if (isString(dataSource.apiParams)) {
19-
dataSource.apiParams = JSON.parse(dataSource.apiParams);
18+
if (isString(dataSource.api?.params)) {
19+
dataSource.api.params = JSON.parse(dataSource.api.params);
2020
}
21-
if (isString(dataSource.tyFilter)) {
22-
dataSource.tyFilter = JSON.parse(dataSource.tyFilter || '{}');
21+
if (isString(dataSource.tingyunConfig)) {
22+
dataSource.tingyunConfig = JSON.parse(dataSource.tingyunConfig || '{}');
2323
}
2424
} catch (error) {
2525
console.error(error);
2626
}
2727

2828
merge(dataSource, config);
2929

30-
if (isObject(dataSource.apiHeaders)) {
31-
dataSource.apiHeaders = JSON.stringify(dataSource.apiHeaders);
30+
if (isObject(dataSource.api?.headers)) {
31+
dataSource.api.headers = JSON.stringify(dataSource.api.headers);
3232
}
33-
if (isObject(dataSource.apiBody)) {
34-
dataSource.apiBody = JSON.stringify(dataSource.apiBody);
33+
if (isObject(dataSource.api?.body)) {
34+
dataSource.api.body = JSON.stringify(dataSource.api.body);
3535
}
36-
if (isObject(dataSource.apiParams)) {
37-
dataSource.apiParams = JSON.stringify(dataSource.apiParams);
36+
if (isObject(dataSource.api?.params)) {
37+
dataSource.api.params = JSON.stringify(dataSource.api.params);
3838
}
39-
if (isObject(dataSource.tyFilter)) {
40-
dataSource.tyFilter = JSON.stringify(dataSource.tyFilter);
39+
if (isObject(dataSource.tingyunConfig)) {
40+
dataSource.tingyunConfig = JSON.stringify(dataSource.tingyunConfig);
4141
}
4242
}
4343
}

0 commit comments

Comments
 (0)