fix: preserve wiki_data keys during request transform
This commit is contained in:
parent
5c870b148e
commit
3ec5e9f48a
1 changed files with 21 additions and 8 deletions
|
|
@ -21,20 +21,33 @@ export function snakeToCamel<T>(obj: T): T {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms camelCase keys to snake_case
|
* Keys that contain data maps where the keys themselves are data values
|
||||||
|
* (like wiki page names), not property names that should be transformed
|
||||||
*/
|
*/
|
||||||
export function camelToSnake<T>(obj: T): T {
|
const DATA_MAP_KEYS = new Set(['wiki_data', 'wikiData'])
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms camelCase keys to snake_case
|
||||||
|
* @param obj - The object to transform
|
||||||
|
* @param skipValueTransform - If true, don't transform nested values (used for data maps)
|
||||||
|
*/
|
||||||
|
export function camelToSnake<T>(obj: T, skipValueTransform = false): T {
|
||||||
if (obj === null || obj === undefined) return obj
|
if (obj === null || obj === undefined) return obj
|
||||||
|
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
return obj.map(camelToSnake) as T
|
return obj.map(item => camelToSnake(item, skipValueTransform)) as T
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof obj === 'object') {
|
if (typeof obj === 'object') {
|
||||||
const result: any = {}
|
const result: any = {}
|
||||||
for (const [key, value] of Object.entries(obj)) {
|
for (const [key, value] of Object.entries(obj)) {
|
||||||
const snakeKey = key.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`)
|
const snakeKey = skipValueTransform ? key : key.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`)
|
||||||
result[snakeKey] = camelToSnake(value)
|
// If this key is a data map, don't transform its nested keys
|
||||||
|
if (DATA_MAP_KEYS.has(key) || DATA_MAP_KEYS.has(snakeKey)) {
|
||||||
|
result[snakeKey] = value // Keep value as-is
|
||||||
|
} else {
|
||||||
|
result[snakeKey] = camelToSnake(value, skipValueTransform)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue