X Tutup
Skip to content

Commit ec780a2

Browse files
twinkle77JackLian
authored andcommitted
fix: support data source import
1 parent 9e7fd0d commit ec780a2

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

demo/src/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@ preference.set('DataSourcePane', {
99
dataSourceTypes: [
1010
{
1111
type: 'fetch',
12+
schema: {
13+
type: "object",
14+
properties: {
15+
id: {
16+
type: "string"
17+
},
18+
type: {
19+
type: "string"
20+
},
21+
isInit: {
22+
type: "boolean",
23+
},
24+
options: {
25+
type: "object",
26+
properties: {
27+
method: {
28+
type: "string",
29+
},
30+
isCors: {
31+
type: "boolean",
32+
},
33+
timeout: {
34+
type: "integer",
35+
},
36+
uri: {
37+
type: "string",
38+
},
39+
},
40+
required: ["method", "isCors", "timeout", "uri"]
41+
}
42+
},
43+
required: ["id", "type", "isInit", "options"]
44+
}
1245
},
1346
{
1447
type: 'jsonp',

packages/plugin-datasource-pane/src/components/DataSourceImport/DataSourceImport.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@ export class DataSourceImport extends PureComponent<
3232
static defaultProps = {
3333
defaultValue: [
3434
{
35-
type: 'http',
36-
id: 'test',
37-
},
35+
type: 'fetch',
36+
isInit: false,
37+
options: {
38+
method: 'GET',
39+
isCors: true,
40+
timeout: 5000,
41+
uri: '/info',
42+
params: {},
43+
headers: {}
44+
},
45+
id: 'info'
46+
}
3847
],
3948
};
4049

@@ -47,8 +56,10 @@ export class DataSourceImport extends PureComponent<
4756
return new Promise((resolve, reject) => {
4857
const { isCodeValid, code } = this.state;
4958

50-
if (isCodeValid) reject(new Error('格式有误'));
51-
resolve({ schema: code });
59+
if (!isCodeValid) reject(new Error('导入格式有误'));
60+
61+
// 只 resolve 通过 schema 校验的数据
62+
resolve(this.deriveValue(JSON.parse(code)));
5263
});
5364
};
5465

@@ -79,14 +90,25 @@ export class DataSourceImport extends PureComponent<
7990

8091
return (result as DataSourceConfig[]).filter((dataSource) => {
8192
if (!dataSource.type) return false;
93+
8294
const dataSourceType = dataSourceTypes.find(
8395
(type) => type.type === dataSource.type,
8496
);
97+
8598
if (!dataSourceType) return false;
86-
return ajv.validate(dataSourceType.schema, dataSource);
99+
100+
// 校验失败的数据源,给予用户提示
101+
const validate = ajv.compile(dataSourceType.schema)
102+
const valid = validate(dataSource)
103+
if (!valid) console.warn(validate.errors)
104+
return valid
87105
});
88106
};
89107

108+
/**
109+
* 看代码是未使用到
110+
* @deprecated
111+
*/
90112
handleComplete = () => {
91113
if (this.monacoRef) {
92114
if (
@@ -116,8 +138,8 @@ export class DataSourceImport extends PureComponent<
116138
}
117139
};
118140

119-
handleEditorDidMount = (isFullscreen: boolean, editor: MonacoEditor, monaco: MonacoEditor) => {
120-
this.monacoRef = monaco?.editor;
141+
handleEditorDidMount = (editor: MonacoEditor, monaco: MonacoEditor) => {
142+
this.monacoRef = editor?.editor;
121143
};
122144

123145
render() {

packages/plugin-datasource-pane/src/pane/DataSourcePane.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ export class DataSourcePane extends PureComponent<
8585
componentDidMount() {
8686
this.serviceS = this.context?.stateService?.subscribe?.((state: any) => {
8787
this.setState({ current: state });
88-
if (state.changed && state.value === 'idle') {
88+
// 监听导入成功事件
89+
if (state.changed && (state.value === 'idle' || state.event?.type === "FINISH_IMPORT")) {
8990
// TODO add hook
9091
this.props.onSchemaChange?.({
9192
list: state.context.dataSourceList,
@@ -221,7 +222,7 @@ export class DataSourcePane extends PureComponent<
221222
return;
222223
}
223224
const repeatedDataSourceList = data.filter(
224-
(item) => !!this.state.current.dataSourceList.find(
225+
(item) => !!this.state.current.context.dataSourceList.find(
225226
(dataSource: DataSourceConfig) => dataSource.id === item.id,
226227
),
227228
);
@@ -237,6 +238,8 @@ export class DataSourcePane extends PureComponent<
237238
return;
238239
}
239240
importDataSourceList();
241+
}).catch(err => {
242+
console.warn(err?.message)
240243
});
241244
}
242245
};

0 commit comments

Comments
 (0)
X Tutup