Skip to content

Commit

Permalink
Use _fields with REST API call and pick to filter response
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Feb 11, 2022
1 parent 5bcc91c commit 5635e3f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-directory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@wordpress/icons": "file:../icons",
"@wordpress/notices": "file:../notices",
"@wordpress/plugins": "file:../plugins",
"@wordpress/url": "file:../url",
"lodash": "^4.17.21"
},
"publishConfig": {
Expand Down
48 changes: 26 additions & 22 deletions packages/block-directory/src/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { pick } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -8,6 +13,7 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { store as noticesStore } from '@wordpress/notices';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -86,8 +92,26 @@ export const installBlockType = ( block ) => async ( {
} );

// Ensures that the block metadata is propagated to the editor when registered on the server.
const metadataFields = [
'api_version',
'title',
'category',
'parent',
'icon',
'description',
'keywords',
'attributes',
'provides_context',
'uses_context',
'supports',
'styles',
'example',
'variations',
];
await apiFetch( {
path: `/wp/v2/block-types/${ name }`,
path: addQueryArgs( `/wp/v2/block-types/${ name }`, {
_fields: metadataFields,
} ),
} )
// Ignore when the block is not registered on the server.
.catch( () => {} )
Expand All @@ -96,27 +120,7 @@ export const installBlockType = ( block ) => async ( {
return;
}
unstable__bootstrapServerSideBlockDefinitions( {
[ name ]: [
'api_version',
'title',
'category',
'parent',
'icon',
'description',
'keywords',
'attributes',
'provides_context',
'uses_context',
'supports',
'styles',
'example',
'variations',
].reduce( ( accumulator, key ) => {
if ( key in response ) {
accumulator[ key ] = response[ key ];
}
return accumulator;
}, {} ),
[ name ]: pick( response, metadataFields ),
} );
} );

Expand Down

0 comments on commit 5635e3f

Please sign in to comment.