diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 444d66674d9839..e5687c013042b9 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -39,6 +39,7 @@ export const rootEntitiesConfig = [ 'url', ].join( ',' ), }, + plural: '__unstableBases', syncConfig: { fetch: async () => { return apiFetch( { path: '/' } ); @@ -63,6 +64,7 @@ export const rootEntitiesConfig = [ name: 'site', kind: 'root', baseURL: '/wp/v2/settings', + plural: 'sites', getTitle: ( record ) => { return record?.title ?? __( 'Site Title' ); }, @@ -92,6 +94,7 @@ export const rootEntitiesConfig = [ key: 'slug', baseURL: '/wp/v2/types', baseURLParams: { context: 'edit' }, + plural: 'postTypes', syncConfig: { fetch: async ( id ) => { return apiFetch( { @@ -220,6 +223,7 @@ export const rootEntitiesConfig = [ kind: 'root', baseURL: '/wp/v2/themes', baseURLParams: { context: 'edit' }, + plural: 'themes', key: 'stylesheet', }, { @@ -228,6 +232,7 @@ export const rootEntitiesConfig = [ kind: 'root', baseURL: '/wp/v2/plugins', baseURLParams: { context: 'edit' }, + plural: 'plugins', key: 'plugin', }, { @@ -408,32 +413,19 @@ async function loadTaxonomyEntities() { * const nameSingular = getMethodName( 'root', 'theme', 'get' ); * // nameSingular is getRootTheme * - * const namePlural = getMethodName( 'root', 'theme', 'set' ); + * const namePlural = getMethodName( 'root', 'themes', 'set' ); * // namePlural is setRootThemes * ``` * - * @param {string} kind Entity kind. - * @param {string} name Entity name. - * @param {string} prefix Function prefix. - * @param {boolean} usePlural Whether to use the plural form or not. + * @param {string} kind Entity kind. + * @param {string} name Entity name. + * @param {string} prefix Function prefix. * * @return {string} Method name */ -export const getMethodName = ( - kind, - name, - prefix = 'get', - usePlural = false -) => { - const entityConfig = rootEntitiesConfig.find( - ( config ) => config.kind === kind && config.name === name - ); +export const getMethodName = ( kind, name, prefix = 'get' ) => { const kindPrefix = kind === 'root' ? '' : pascalCase( kind ); - const nameSuffix = pascalCase( name ) + ( usePlural ? 's' : '' ); - const suffix = - usePlural && 'plural' in entityConfig && entityConfig?.plural - ? pascalCase( entityConfig.plural ) - : nameSuffix; + const suffix = pascalCase( name ); return `${ prefix }${ kindPrefix }${ suffix }`; }; diff --git a/packages/core-data/src/index.js b/packages/core-data/src/index.js index 2a9290ac3daa30..484622c6e40302 100644 --- a/packages/core-data/src/index.js +++ b/packages/core-data/src/index.js @@ -22,23 +22,29 @@ import { unlock } from './private-apis'; // The "kind" and the "name" of the entity are combined to generate these shortcuts. const entitySelectors = rootEntitiesConfig.reduce( ( result, entity ) => { - const { kind, name } = entity; + const { kind, name, plural } = entity; result[ getMethodName( kind, name ) ] = ( state, key, query ) => selectors.getEntityRecord( state, kind, name, key, query ); - result[ getMethodName( kind, name, 'get', true ) ] = ( state, query ) => - selectors.getEntityRecords( state, kind, name, query ); + + if ( plural ) { + result[ getMethodName( kind, plural, 'get' ) ] = ( state, query ) => + selectors.getEntityRecords( state, kind, name, query ); + } return result; }, {} ); const entityResolvers = rootEntitiesConfig.reduce( ( result, entity ) => { - const { kind, name } = entity; + const { kind, name, plural } = entity; result[ getMethodName( kind, name ) ] = ( key, query ) => resolvers.getEntityRecord( kind, name, key, query ); - const pluralMethodName = getMethodName( kind, name, 'get', true ); - result[ pluralMethodName ] = ( ...args ) => - resolvers.getEntityRecords( kind, name, ...args ); - result[ pluralMethodName ].shouldInvalidate = ( action ) => - resolvers.getEntityRecords.shouldInvalidate( action, kind, name ); + + if ( plural ) { + const pluralMethodName = getMethodName( kind, plural, 'get' ); + result[ pluralMethodName ] = ( ...args ) => + resolvers.getEntityRecords( kind, name, ...args ); + result[ pluralMethodName ].shouldInvalidate = ( action ) => + resolvers.getEntityRecords.shouldInvalidate( action, kind, name ); + } return result; }, {} ); diff --git a/packages/core-data/src/test/entities.js b/packages/core-data/src/test/entities.js index c9a432c92c1442..e142f9bf70c6c7 100644 --- a/packages/core-data/src/test/entities.js +++ b/packages/core-data/src/test/entities.js @@ -27,14 +27,8 @@ describe( 'getMethodName', () => { expect( methodName ).toEqual( 'setPostType' ); } ); - it( 'should use the plural form', () => { - const methodName = getMethodName( 'root', 'postType', 'get', true ); - - expect( methodName ).toEqual( 'getPostTypes' ); - } ); - it( 'should use the given plural form', () => { - const methodName = getMethodName( 'root', 'taxonomy', 'get', true ); + const methodName = getMethodName( 'root', 'taxonomies', 'get' ); expect( methodName ).toEqual( 'getTaxonomies' ); } );