From bddfc6c4a5cad403882e8b32bbe960184d43976a Mon Sep 17 00:00:00 2001 From: Chris Van Patten Date: Mon, 8 Feb 2021 17:19:33 -0500 Subject: [PATCH 1/2] Fix: make preloaded OPTIONS requests use `parse` setting --- .../api-fetch/src/middlewares/preloading.js | 6 +- .../src/middlewares/test/preloading.js | 66 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/api-fetch/src/middlewares/preloading.js b/packages/api-fetch/src/middlewares/preloading.js index b2e787dcd6abf9..fe946ab1b0e24a 100644 --- a/packages/api-fetch/src/middlewares/preloading.js +++ b/packages/api-fetch/src/middlewares/preloading.js @@ -68,7 +68,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 ] + ); } } diff --git a/packages/api-fetch/src/middlewares/test/preloading.js b/packages/api-fetch/src/middlewares/test/preloading.js index 7fa3d75dfb4320..49e74e2852f495 100644 --- a/packages/api-fetch/src/middlewares/test/preloading.js +++ b/packages/api-fetch/src/middlewares/test/preloading.js @@ -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', () => { From 6f848d85ee10154332ee57bf28c50000646162b7 Mon Sep 17 00:00:00 2001 From: Chris Van Patten Date: Wed, 14 Apr 2021 11:54:45 -0400 Subject: [PATCH 2/2] add: Changelog entry and fix typo in test --- packages/api-fetch/CHANGELOG.md | 4 ++++ packages/api-fetch/src/middlewares/test/preloading.js | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/api-fetch/CHANGELOG.md b/packages/api-fetch/CHANGELOG.md index 0e59192086e1de..532e820aa5c637 100644 --- a/packages/api-fetch/CHANGELOG.md +++ b/packages/api-fetch/CHANGELOG.md @@ -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. + ## 3.8.1 (2019-04-22) - Added deprecation to `useApiFetch` hook. diff --git a/packages/api-fetch/src/middlewares/test/preloading.js b/packages/api-fetch/src/middlewares/test/preloading.js index 49e74e2852f495..3f470e9a8a6e69 100644 --- a/packages/api-fetch/src/middlewares/test/preloading.js +++ b/packages/api-fetch/src/middlewares/test/preloading.js @@ -205,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 = { @@ -218,7 +218,7 @@ describe( 'Preloading Middleware', () => { return true; }; - const ret = prelooadingMiddleware( requestOptions, callback ); + const ret = preloadingMiddleware( requestOptions, callback ); expect( ret ).toBe( true ); } ); } );