2026-02-16 18:20:19 +09:00
|
|
|
export interface NormalizeSDKResponseOptions {
|
|
|
|
|
preferResponseOnMissingData?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizeSDKResponse<TData>(
|
|
|
|
|
response: unknown,
|
|
|
|
|
fallback: TData,
|
|
|
|
|
options?: NormalizeSDKResponseOptions,
|
|
|
|
|
): TData {
|
2026-04-11 23:35:57 +09:00
|
|
|
if (response == null) {
|
2026-02-16 18:20:19 +09:00
|
|
|
return fallback
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(response)) {
|
|
|
|
|
return response as TData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof response === "object" && "data" in response) {
|
|
|
|
|
const data = (response as { data?: unknown }).data
|
2026-04-11 23:35:57 +09:00
|
|
|
if (data != null) {
|
2026-02-16 18:20:19 +09:00
|
|
|
return data as TData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options?.preferResponseOnMissingData === true) {
|
|
|
|
|
return response as TData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options?.preferResponseOnMissingData === true) {
|
|
|
|
|
return response as TData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
|
}
|