From bf99ad2ca09c0f510100509c4cb6783aba5389db Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Fri, 3 Jun 2022 17:06:30 +0100 Subject: [PATCH 1/5] Fix retrieving autosaves when using a custom rest_namespace When specifying `rest_namespace` in a custom post type, a hardcoded `wp/v2` in `getAutosaves` results in a 404 --- packages/core-data/src/resolvers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 830d9ca97bb0f7..7bee51a16b2e1b 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -327,9 +327,9 @@ export const getAutosaves = ( postType, postId ) => async ( { dispatch, resolveSelect, } ) => { - const { rest_base: restBase } = await resolveSelect.getPostType( postType ); + const { rest_base: restBase, rest_namespace: restNamespace } = await resolveSelect.getPostType( postType ); const autosaves = await apiFetch( { - path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`, + path: `/${ restNamespace }/${ restBase }/${ postId }/autosaves?context=edit`, } ); if ( autosaves && autosaves.length ) { From 976725e4a02fc5d4662b5655556202e0626defde Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Sun, 5 Jun 2022 13:01:09 +0300 Subject: [PATCH 2/5] linter fixes for rest namespace resolution in autosave resolver --- packages/core-data/src/resolvers.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 7bee51a16b2e1b..279d73a4be5721 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -327,7 +327,10 @@ export const getAutosaves = ( postType, postId ) => async ( { dispatch, resolveSelect, } ) => { - const { rest_base: restBase, rest_namespace: restNamespace } = await resolveSelect.getPostType( postType ); + const { + rest_base: restBase, + rest_namespace: restNamespace, + } = await resolveSelect.getPostType( postType ); const autosaves = await apiFetch( { path: `/${ restNamespace }/${ restBase }/${ postId }/autosaves?context=edit`, } ); From 6b77f5b52865f0ba0a835428bcb7d609b092a6f4 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Sun, 5 Jun 2022 13:10:19 +0300 Subject: [PATCH 3/5] try to fix autosave resolver and namespaces not present --- packages/core-data/src/resolvers.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 279d73a4be5721..f97d469dbbf19d 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -327,12 +327,11 @@ export const getAutosaves = ( postType, postId ) => async ( { dispatch, resolveSelect, } ) => { - const { - rest_base: restBase, - rest_namespace: restNamespace, - } = await resolveSelect.getPostType( postType ); + const type = await resolveSelect.getPostType( postType ); + const base = type.rest_base; + const namespace = type?.rest_namespace ?? 'wp/v2'; const autosaves = await apiFetch( { - path: `/${ restNamespace }/${ restBase }/${ postId }/autosaves?context=edit`, + path: `/${ namespace }/${ base }/${ postId }/autosaves?context=edit`, } ); if ( autosaves && autosaves.length ) { From 790a58bb58caa2c2d323e32e9c26897b1733b687 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Tue, 7 Jun 2022 00:16:20 +0300 Subject: [PATCH 4/5] update resolvers for autosaves endpoint --- packages/core-data/src/resolvers.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index f97d469dbbf19d..3afe339891983e 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -327,11 +327,12 @@ export const getAutosaves = ( postType, postId ) => async ( { dispatch, resolveSelect, } ) => { - const type = await resolveSelect.getPostType( postType ); - const base = type.rest_base; - const namespace = type?.rest_namespace ?? 'wp/v2'; + const { + rest_base: restBase, + rest_namespace: restNamespace = 'wp/v2', + } = await resolveSelect.getPostType( postType ); const autosaves = await apiFetch( { - path: `/${ namespace }/${ base }/${ postId }/autosaves?context=edit`, + path: `/${ restNamespace }/${ restBase }/${ postId }/autosaves?context=edit`, } ); if ( autosaves && autosaves.length ) { From 5d4eb60f116db72a616c476475c0fdc565b37efb Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 27 Jun 2022 14:15:00 +0400 Subject: [PATCH 5/5] Fix prettier erro Co-authored-by: Jonny Harris --- packages/core-data/src/resolvers.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 2e9311caf14b15..5b4eaac015e5bf 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -348,9 +348,8 @@ export const canUserEditEntityRecord = export const getAutosaves = ( postType, postId ) => async ( { dispatch, resolveSelect } ) => { - const { rest_base: restBase, rest_namespace: restNamespace = 'wp/v2', } = await resolveSelect.getPostType( - postType - ); + const { rest_base: restBase, rest_namespace: restNamespace = 'wp/v2' } = + await resolveSelect.getPostType( postType ); const autosaves = await apiFetch( { path: `/${ restNamespace }/${ restBase }/${ postId }/autosaves?context=edit`, } );