Skip to content

Commit

Permalink
[Mobile] - Gallery block - Fix getMediaItems (WordPress#63426)
Browse files Browse the repository at this point in the history
* Mobile - Fix getMediaItems

* Rearrange conditions for parseResponse

* Fix response conditions

Co-authored-by: geriux <[email protected]>
Co-authored-by: dcalhoun <[email protected]>
  • Loading branch information
3 people authored Jul 12, 2024
1 parent 6e1869c commit 0b26364
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/react-native-editor/src/api-fetch-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const setTimeoutPromise = ( delay ) =>
new Promise( ( resolve ) => setTimeout( resolve, delay ) );

const fetchHandler = (
{ path, method = 'GET', data },
{ path, method = 'GET', data, parse },
retries = 20,
retryCount = 1
) => {
Expand All @@ -57,7 +57,23 @@ const fetchHandler = (
}

const parseResponse = ( response ) => {
if ( typeof response === 'string' ) {
const isStringResponse = typeof response === 'string';

// If the 'parse' parameter is false, return the response as a new Response object
// with the JSON string. This is necessary because one of the middlewares in the API
// fetch library expects the response to be a JavaScript Response object with JSON
// functionality. By doing this, we ensure that the response is handled correctly
// by fetch-all-middleware.
if ( parse === false ) {
const body = isStringResponse
? response
: JSON.stringify( response );
return new Response( body, {
headers: { 'Content-Type': 'application/json' },
} );
}

if ( isStringResponse ) {
response = JSON.parse( response );
}
return response;
Expand Down

0 comments on commit 0b26364

Please sign in to comment.