This commit is contained in:
Mauve
2026-09-16 10:43:59 +08:00
parent a7f2e0ca01
commit 9f2becfa67
36 changed files with 3997 additions and 264 deletions
+31
View File
@@ -0,0 +1,31 @@
/**
* 将 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;