Skip to content

Commit

Permalink
Merge pull request #12 from seatable/optimize-code
Browse files Browse the repository at this point in the history
optimize code
  • Loading branch information
shuntian authored Nov 3, 2023
2 parents a606ae7 + ad95b65 commit 7d94ff8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
35 changes: 27 additions & 8 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,30 @@ const getAccessToken = (config) => {
return axios.get(url, { headers: headers });
};

const isSingleOrMultiple = (type) => {
return type === ColumnTypes.SINGLE_SELECT || type === ColumnTypes.MULTIPLE_SELECT;
}

const formatColumnOptionsToMap = (column) => {
const { options = [] } = column.data || {};
let options_map = {};
options.forEach(option => {
options_map[option.id] = option.name;
});
return options_map;
};

const formatQueryResult = (result) => {
const { metadata: columns, results: rows } = result;
const columnMap = columns.reduce((keyMap, column) => {
if (column.type === ColumnTypes.SINGLE_SELECT || column.type === ColumnTypes.MULTIPLE_SELECT) {
const { options = [] } = column.data || {};
let options_map = {};
options.forEach(option => {
options_map[option.id] = option.name;
});
column.options_map = options_map;
if (isSingleOrMultiple(column.type)) {
column.options_map = formatColumnOptionsToMap(column);
}
if (column.type === ColumnTypes.LINK || column.type === ColumnTypes.LINK_FORMULA) {
const { array_type, array_data } = column.data;
if (isSingleOrMultiple(array_type)) {
column.options_map = formatColumnOptionsToMap({ data: array_data });
}
}
keyMap[column.key] = column;
return keyMap;
Expand Down Expand Up @@ -47,8 +61,13 @@ const formatQueryResult = (result) => {
case ColumnTypes.LINK_FORMULA: {
if (!Array.isArray(cellValue)) {
cellValue = [];
} else {
break;
}
const { array_type } = data;
if (!isSingleOrMultiple(array_type)) {
cellValue = cellValue.map(item => item.display_value);
} else {
cellValue = cellValue.map(item => options_map[item.display_value]);
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const { Base } = require('../lib');

const config = {
server: 'http://127.0.0.1',
APIToken: '41c667e75ff105563fbea5cf59cb3549d37e51c0'
APIToken: 'acdf4e21d4a0f36686848d81523a2df4ab67bf2d'
};


async function testAPI() {
const base = new Base(config);
await base.auth();
const data = await base.query('select * from Table1');
const data = await base.query('select * from Table2');
fs.writeFileSync('./abc.json', JSON.stringify(data, null, 2))
}

Expand Down

0 comments on commit 7d94ff8

Please sign in to comment.