* Recursively removes undefined values from an object to ensure clean JSON serialization. * This prevents issues with APIs that don't accept explicit undefined values.
(obj: T)
| 18 | * This prevents issues with APIs that don't accept explicit undefined values. |
| 19 | */ |
| 20 | function removeUndefinedValues<T>(obj: T): T { |
| 21 | if (obj === null || obj === undefined) { |
| 22 | return obj |
| 23 | } |
| 24 | if (Array.isArray(obj)) { |
| 25 | return obj.map(removeUndefinedValues) as T |
| 26 | } |
| 27 | if (typeof obj === 'object') { |
| 28 | const result: Record<string, unknown> = {} |
| 29 | for (const [key, value] of Object.entries(obj)) { |
| 30 | if (value !== undefined) { |
| 31 | result[key] = removeUndefinedValues(value) |
| 32 | } |
| 33 | } |
| 34 | return result as T |
| 35 | } |
| 36 | return obj |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Reset the cached CodebuffClient instance. |