2d65896bbd
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
37 lines
747 B
TypeScript
37 lines
747 B
TypeScript
export interface NormalizeSDKResponseOptions {
|
|
preferResponseOnMissingData?: boolean
|
|
}
|
|
|
|
export function normalizeSDKResponse<TData>(
|
|
response: unknown,
|
|
fallback: TData,
|
|
options?: NormalizeSDKResponseOptions,
|
|
): TData {
|
|
if (response == null) {
|
|
return fallback
|
|
}
|
|
|
|
if (Array.isArray(response)) {
|
|
return response as TData
|
|
}
|
|
|
|
if (typeof response === "object" && "data" in response) {
|
|
const data = (response as { data?: unknown }).data
|
|
if (data != null) {
|
|
return data as TData
|
|
}
|
|
|
|
if (options?.preferResponseOnMissingData === true) {
|
|
return response as TData
|
|
}
|
|
|
|
return fallback
|
|
}
|
|
|
|
if (options?.preferResponseOnMissingData === true) {
|
|
return response as TData
|
|
}
|
|
|
|
return fallback
|
|
}
|