Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Fetch: Make preloaded OPTIONS requests use parse setting #28862

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/api-fetch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Breaking changes

- `OPTIONS` requests which are handled by the preloading middleware are no longer resolved as unparsed responses unless you explicitly set `parse: false`, for consistency with other request methods. If you expect an unparsed response, add `{ parse: false }` to your request options to preserve the previous behavior.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it impact any existing REST API calls in Gutenberg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not impact any existing calls in the Gutenberg codebase — I think the reason this wasn't noticed previously is that all the core requests already explicitly set { parse: false }… it just wasn't actually doing anything!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm it now that you mentioned it :)


## 3.23.1 (2021-04-15)

### Bug Fixes
Expand Down
6 changes: 5 additions & 1 deletion packages/api-fetch/src/middlewares/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ function createPreloadingMiddleware( preloadedData ) {
cache[ method ] &&
cache[ method ][ path ]
) {
return Promise.resolve( cache[ method ][ path ] );
return Promise.resolve(
parse
? cache[ method ][ path ].body
: cache[ method ][ path ]
);
}
}

Expand Down
70 changes: 68 additions & 2 deletions packages/api-fetch/src/middlewares/test/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,72 @@ describe( 'Preloading Middleware', () => {
expect( nextSpy ).toHaveBeenCalled();
} );
} );

describe( 'and the OPTIONS request has a parse flag', () => {
it( 'should return the full response if parse: false', () => {
const data = {
body: {
status: 'this is the preloaded response',
},
headers: {
Allow: 'GET, POST',
},
};

const preloadedData = {
OPTIONS: {
'wp/v2/posts': data,
},
};

const preloadingMiddleware = createPreloadingMiddleware(
preloadedData
);

const requestOptions = {
method: 'OPTIONS',
path: 'wp/v2/posts',
parse: false,
};

const response = preloadingMiddleware( requestOptions );
return response.then( ( value ) => {
expect( value ).toEqual( data );
} );
} );

it( 'should return only the response body if parse: true', () => {
const body = {
status: 'this is the preloaded response',
};

const preloadedData = {
OPTIONS: {
'wp/v2/posts': {
body,
headers: {
Allow: 'GET, POST',
},
},
},
};

const preloadingMiddleware = createPreloadingMiddleware(
preloadedData
);

const requestOptions = {
method: 'OPTIONS',
path: 'wp/v2/posts',
parse: true,
};

const response = preloadingMiddleware( requestOptions );
return response.then( ( value ) => {
expect( value ).toEqual( body );
} );
} );
} );
} );

describe( 'when the requested data is not from a preloaded endpoint', () => {
Expand Down Expand Up @@ -139,7 +205,7 @@ describe( 'Preloading Middleware', () => {
[ 'method empty', { [ method ]: {} } ],
] )( '%s', ( label, preloadedData ) => {
it( 'should move to the next middleware if no preloaded data', () => {
const prelooadingMiddleware = createPreloadingMiddleware(
const preloadingMiddleware = createPreloadingMiddleware(
preloadedData
);
const requestOptions = {
Expand All @@ -152,7 +218,7 @@ describe( 'Preloading Middleware', () => {
return true;
};

const ret = prelooadingMiddleware( requestOptions, callback );
const ret = preloadingMiddleware( requestOptions, callback );
expect( ret ).toBe( true );
} );
} );
Expand Down