32 lines
917 B
JavaScript
32 lines
917 B
JavaScript
/**
|
|
* 将 API 返回的 PascalCase 字段名转为前端使用的 camelCase(递归处理嵌套对象/数组)
|
|
*/
|
|
function toCamelCaseKey(key) {
|
|
if (!key || typeof key !== 'string') return key;
|
|
if (key[0] === key[0].toLowerCase()) return key;
|
|
return key.charAt(0).toLowerCase() + key.slice(1);
|
|
}
|
|
|
|
export function normalizeApiKeys(value) {
|
|
if (value === null || value === undefined) return value;
|
|
if (Array.isArray(value)) {
|
|
return value.map(item => normalizeApiKeys(item));
|
|
}
|
|
if (typeof value !== 'object') return value;
|
|
|
|
const result = {};
|
|
for (const [key, val] of Object.entries(value)) {
|
|
result[toCamelCaseKey(key)] = normalizeApiKeys(val);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** 兼容 API 返回 records / Records */
|
|
export function getApiRecords(data) {
|
|
if (!data) return [];
|
|
const records = data.records ?? data.Records;
|
|
return Array.isArray(records) ? records : [];
|
|
}
|
|
|
|
export default normalizeApiKeys;
|