File tree Expand file tree Collapse file tree 1 file changed +15
-11
lines changed
Expand file tree Collapse file tree 1 file changed +15
-11
lines changed Original file line number Diff line number Diff line change 66 CONTENT_TYPE ,
77 FUNCTION ,
88 OBJECT ,
9+ STRING ,
910} from './constants' ;
1011import {
1112 DefaultResponse ,
@@ -68,18 +69,21 @@ export async function parseResponseData<
6869 typeof response . blob === FUNCTION
6970 ) {
7071 data = await response . blob ( ) ; // Parse as blob
71- } else if ( mimeType . startsWith ( 'text/' ) ) {
72- data = await response . text ( ) ; // Parse as text
7372 } else {
74- try {
75- const responseClone = response . clone ( ) ;
76-
77- // Handle edge case of no content type being provided... We assume JSON here.
78- data = await responseClone . json ( ) ;
79- // eslint-disable-next-line @typescript-eslint/no-unused-vars
80- } catch ( _e ) {
81- // Handle streams
82- data = await response . text ( ) ;
73+ data = await response . text ( ) ;
74+
75+ if ( typeof data === STRING ) {
76+ const trimmed = data . trim ( ) ;
77+ if (
78+ ( trimmed . startsWith ( '{' ) && trimmed . endsWith ( '}' ) ) ||
79+ ( trimmed . startsWith ( '[' ) && trimmed . endsWith ( ']' ) )
80+ ) {
81+ try {
82+ data = JSON . parse ( trimmed ) ;
83+ } catch {
84+ // leave as text if parsing fails
85+ }
86+ }
8387 }
8488 }
8589 // eslint-disable-next-line @typescript-eslint/no-unused-vars
You can’t perform that action at this time.
0 commit comments