Skip to content

Commit 7d94ff8

Browse files
authored
Merge pull request #12 from seatable/optimize-code
optimize code
2 parents a606ae7 + ad95b65 commit 7d94ff8

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/utils/index.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,30 @@ const getAccessToken = (config) => {
99
return axios.get(url, { headers: headers });
1010
};
1111

12+
const isSingleOrMultiple = (type) => {
13+
return type === ColumnTypes.SINGLE_SELECT || type === ColumnTypes.MULTIPLE_SELECT;
14+
}
15+
16+
const formatColumnOptionsToMap = (column) => {
17+
const { options = [] } = column.data || {};
18+
let options_map = {};
19+
options.forEach(option => {
20+
options_map[option.id] = option.name;
21+
});
22+
return options_map;
23+
};
24+
1225
const formatQueryResult = (result) => {
1326
const { metadata: columns, results: rows } = result;
1427
const columnMap = columns.reduce((keyMap, column) => {
15-
if (column.type === ColumnTypes.SINGLE_SELECT || column.type === ColumnTypes.MULTIPLE_SELECT) {
16-
const { options = [] } = column.data || {};
17-
let options_map = {};
18-
options.forEach(option => {
19-
options_map[option.id] = option.name;
20-
});
21-
column.options_map = options_map;
28+
if (isSingleOrMultiple(column.type)) {
29+
column.options_map = formatColumnOptionsToMap(column);
30+
}
31+
if (column.type === ColumnTypes.LINK || column.type === ColumnTypes.LINK_FORMULA) {
32+
const { array_type, array_data } = column.data;
33+
if (isSingleOrMultiple(array_type)) {
34+
column.options_map = formatColumnOptionsToMap({ data: array_data });
35+
}
2236
}
2337
keyMap[column.key] = column;
2438
return keyMap;
@@ -47,8 +61,13 @@ const formatQueryResult = (result) => {
4761
case ColumnTypes.LINK_FORMULA: {
4862
if (!Array.isArray(cellValue)) {
4963
cellValue = [];
50-
} else {
64+
break;
65+
}
66+
const { array_type } = data;
67+
if (!isSingleOrMultiple(array_type)) {
5168
cellValue = cellValue.map(item => item.display_value);
69+
} else {
70+
cellValue = cellValue.map(item => options_map[item.display_value]);
5271
}
5372
break;
5473
}

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ const { Base } = require('../lib');
33

44
const config = {
55
server: 'http://127.0.0.1',
6-
APIToken: '41c667e75ff105563fbea5cf59cb3549d37e51c0'
6+
APIToken: 'acdf4e21d4a0f36686848d81523a2df4ab67bf2d'
77
};
88

99

1010
async function testAPI() {
1111
const base = new Base(config);
1212
await base.auth();
13-
const data = await base.query('select * from Table1');
13+
const data = await base.query('select * from Table2');
1414
fs.writeFileSync('./abc.json', JSON.stringify(data, null, 2))
1515
}
1616

0 commit comments

Comments
 (0)