import { ParseMethod , TextResponse , JsonResponse } from '../types' ; export default async function parseResponse < T extends ParseMethod = 'json' > ( apiPromise , parseMethod ? ) { type ReturnType = T extends 'raw' | null ? Response : T extends 'json' | undefined ? JsonResponse : T extends 'text' ? TextResponse : never ; const response = await apiPromise ; if ( ! response . ok ) { return Promise . reject ( response ) ; } if ( parseMethod === null || parseMethod === 'raw' ) { return response as ReturnType ; } if ( parseMethod === 'text' ) { const text = await response . text ( ) ; const result = { response , text , } ; return result as ReturnType ; } if ( parseMethod === undefined || parseMethod === 'json' ) { const json = await response . json ( ) ; const result = { json , response , } ; return result as ReturnType ; } throw new Error ( `Expected parseResponse=json|text|raw|null, got '${ parseMethod }'.` ) ; }