Update fetcher to avoid parsing non-JSON responses

This commit is contained in:
tniezg 2021-08-30 16:26:57 +02:00
parent 320fc0645d
commit 92bb7bcf51

View File

@ -47,7 +47,18 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (
try {
const response: Response = await fetch(request)
const data = await response.json()
const responseContentType = response.headers.get('content-type')
let data
if (
!responseContentType ||
(!responseContentType.includes('application/json') &&
!responseContentType.includes('application/vnd.api+json'))
) {
data = await response.text()
} else {
data = await response.json()
}
if (!response.ok) {
// Use the "traditional" approach and reject non 2xx responses.