From aec5367a05c6b2663a6e7161539efc03bb935140 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 24 Apr 2023 18:36:36 +0200 Subject: [PATCH 1/3] Use apiFetch instead of window.fetch --- packages/core-data/src/resolvers.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index b33bb42e65337..60198cd266134 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -409,15 +409,13 @@ export const getAutosave = export const __experimentalGetTemplateForLink = ( link ) => async ( { dispatch, resolveSelect } ) => { - // Ideally this should be using an apiFetch call - // We could potentially do so by adding a "filter" to the `wp_template` end point. - // Also it seems the returned object is not a regular REST API post type. let template; try { - template = await window - .fetch( addQueryArgs( link, { '_wp-find-template': true } ) ) - .then( ( res ) => res.json() ) - .then( ( { data } ) => data ); + template = await apiFetch( { + url: addQueryArgs( link, { + '_wp-find-template': true, + } ), + } ).then( ( { data } ) => data ); } catch ( e ) { // For non-FSE themes, it is possible that this request returns an error. } From d090e437c3c3542133f7f0d57e73eb7698f290a5 Mon Sep 17 00:00:00 2001 From: scruffian Date: Thu, 27 Apr 2023 14:50:04 +0200 Subject: [PATCH 2/3] add comment --- packages/core-data/src/resolvers.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 60198cd266134..c82bf5677788c 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -411,6 +411,8 @@ export const __experimentalGetTemplateForLink = async ( { dispatch, resolveSelect } ) => { let template; try { + // This is NOT calling a REST endpoint but rather ends up with a response from + // an Ajax function which has a different shape from a WP_REST_Response. template = await apiFetch( { url: addQueryArgs( link, { '_wp-find-template': true, From 7479aa92bf57871ea0d611599aa0bda22e4aeb4e Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 27 Apr 2023 16:18:51 +0100 Subject: [PATCH 3/3] Fix mocking in test --- packages/edit-site/src/store/test/actions.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/packages/edit-site/src/store/test/actions.js b/packages/edit-site/src/store/test/actions.js index 48b7d1ed71d70..2df1cc72b6611 100644 --- a/packages/edit-site/src/store/test/actions.js +++ b/packages/edit-site/src/store/test/actions.js @@ -168,21 +168,13 @@ describe( 'actions', () => { const ID = 'emptytheme//single'; const SLUG = 'single'; - window.fetch = async ( path ) => { - if ( path === '/?_wp-find-template=true' ) { - return { - json: async () => ( { data: { id: ID, slug: SLUG } } ), - }; - } - - throw { - code: 'unknown_path', - message: `Unknown path: ${ path }`, - }; - }; - apiFetch.setFetchHandler( async ( options ) => { - const { method = 'GET', path } = options; + const { method = 'GET', path, url } = options; + + // Called with url arg in `__experimentalGetTemplateForLink` + if ( url ) { + return { data: { id: ID, slug: SLUG } }; + } if ( method === 'GET' ) { if ( path.startsWith( '/wp/v2/types' ) ) {