Skip to content

Commit 9c4fe3b

Browse files
Added Store String Mappings
This adds StoreRegistry entries for: - abilities - boot - core-data - editor - notices - preferences - upload-media
1 parent bc92f8f commit 9c4fe3b

10 files changed

Lines changed: 214 additions & 134 deletions

File tree

‎packages/abilities/src/store/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ export const store = createReduxStore( STORE_NAME, {
2121
} );
2222

2323
register( store );
24+
25+
/**
26+
* Bind the store descriptor to the store name so that string-based
27+
* access (e.g. `select( 'core/abilities' )`) is typed.
28+
*/
29+
declare module '@wordpress/data' {
30+
interface StoreRegistry {
31+
[ STORE_NAME ]: typeof store;
32+
}
33+
}

‎packages/boot/src/store/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ export const store = createReduxStore( STORE_NAME, {
2121
register( store );
2222

2323
export { STORE_NAME };
24+
25+
/**
26+
* Bind the store descriptor to the store name so that string-based
27+
* access (e.g. `select( 'wordpress/boot' )`) is typed.
28+
*/
29+
declare module '@wordpress/data' {
30+
interface StoreRegistry {
31+
[ STORE_NAME ]: typeof store;
32+
}
33+
}

‎packages/core-data/src/index.js‎

Lines changed: 4 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,7 @@
1-
/**
2-
* WordPress dependencies
3-
*/
4-
import { createReduxStore, register } from '@wordpress/data';
5-
6-
/**
7-
* Internal dependencies
8-
*/
9-
import reducer from './reducer';
10-
import * as selectors from './selectors';
11-
import * as privateSelectors from './private-selectors';
12-
import * as actions from './actions';
13-
import * as privateActions from './private-actions';
14-
import * as resolvers from './resolvers';
15-
import createLocksActions from './locks/actions';
16-
import {
17-
rootEntitiesConfig,
18-
additionalEntityConfigLoaders,
19-
getMethodName,
20-
} from './entities';
21-
import { STORE_NAME } from './name';
22-
import { unlock } from './lock-unlock';
23-
import { dynamicActions, dynamicSelectors } from './dynamic-entities';
24-
import logEntityDeprecation from './utils/log-entity-deprecation';
25-
26-
// The entity selectors/resolvers and actions are shortcuts to their generic equivalents
27-
// (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecords)
28-
// Instead of getEntityRecord, the consumer could use more user-friendly named selector: getPostType, getTaxonomy...
29-
// The "kind" and the "name" of the entity are combined to generate these shortcuts.
30-
const entitiesConfig = [
31-
...rootEntitiesConfig,
32-
...additionalEntityConfigLoaders.filter( ( config ) => !! config.name ),
33-
];
34-
35-
const entitySelectors = entitiesConfig.reduce( ( result, entity ) => {
36-
const { kind, name, plural } = entity;
37-
38-
const getEntityRecordMethodName = getMethodName( kind, name );
39-
result[ getEntityRecordMethodName ] = ( state, key, query ) => {
40-
logEntityDeprecation( kind, name, getEntityRecordMethodName, {
41-
isShorthandSelector: true,
42-
alternativeFunctionName: 'getEntityRecord',
43-
} );
44-
return selectors.getEntityRecord( state, kind, name, key, query );
45-
};
46-
47-
if ( plural ) {
48-
const getEntityRecordsMethodName = getMethodName( kind, plural, 'get' );
49-
result[ getEntityRecordsMethodName ] = ( state, query ) => {
50-
logEntityDeprecation( kind, name, getEntityRecordsMethodName, {
51-
isShorthandSelector: true,
52-
alternativeFunctionName: 'getEntityRecords',
53-
} );
54-
return selectors.getEntityRecords( state, kind, name, query );
55-
};
56-
}
57-
return result;
58-
}, {} );
59-
60-
const entityResolvers = entitiesConfig.reduce( ( result, entity ) => {
61-
const { kind, name, plural } = entity;
62-
const getEntityRecordMethodName = getMethodName( kind, name );
63-
result[ getEntityRecordMethodName ] = ( key, query ) => {
64-
logEntityDeprecation( kind, name, getEntityRecordMethodName, {
65-
isShorthandSelector: true,
66-
alternativeFunctionName: 'getEntityRecord',
67-
} );
68-
return resolvers.getEntityRecord( kind, name, key, query );
69-
};
70-
71-
if ( plural ) {
72-
const getEntityRecordsMethodName = getMethodName( kind, plural, 'get' );
73-
result[ getEntityRecordsMethodName ] = ( ...args ) => {
74-
logEntityDeprecation( kind, plural, getEntityRecordsMethodName, {
75-
isShorthandSelector: true,
76-
alternativeFunctionName: 'getEntityRecords',
77-
} );
78-
return resolvers.getEntityRecords( kind, name, ...args );
79-
};
80-
result[ getEntityRecordsMethodName ].shouldInvalidate = ( action ) =>
81-
resolvers.getEntityRecords.shouldInvalidate( action, kind, name );
82-
}
83-
return result;
84-
}, {} );
85-
86-
const entityActions = entitiesConfig.reduce( ( result, entity ) => {
87-
const { kind, name } = entity;
88-
89-
const saveEntityRecordMethodName = getMethodName( kind, name, 'save' );
90-
result[ saveEntityRecordMethodName ] = ( record, options ) => {
91-
logEntityDeprecation( kind, name, saveEntityRecordMethodName, {
92-
isShorthandSelector: true,
93-
alternativeFunctionName: 'saveEntityRecord',
94-
} );
95-
return actions.saveEntityRecord( kind, name, record, options );
96-
};
97-
98-
const deleteEntityRecordMethodName = getMethodName( kind, name, 'delete' );
99-
result[ deleteEntityRecordMethodName ] = ( key, query, options ) => {
100-
logEntityDeprecation( kind, name, deleteEntityRecordMethodName, {
101-
isShorthandSelector: true,
102-
alternativeFunctionName: 'deleteEntityRecord',
103-
} );
104-
return actions.deleteEntityRecord( kind, name, key, query, options );
105-
};
106-
107-
return result;
108-
}, {} );
109-
110-
const storeConfig = () => ( {
111-
reducer,
112-
actions: {
113-
...dynamicActions,
114-
...actions,
115-
...entityActions,
116-
...createLocksActions(),
117-
},
118-
selectors: {
119-
...dynamicSelectors,
120-
...selectors,
121-
...entitySelectors,
122-
},
123-
resolvers: { ...resolvers, ...entityResolvers },
124-
} );
125-
126-
/**
127-
* Store definition for the code data namespace.
128-
*
129-
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
130-
*/
131-
export const store = createReduxStore( STORE_NAME, storeConfig() );
132-
unlock( store ).registerPrivateSelectors( privateSelectors );
133-
unlock( store ).registerPrivateActions( privateActions );
134-
register( store ); // Register store after unlocking private selectors to allow resolvers to use them.
1+
// Ensure the StoreRegistry module augmentation is included in the declaration
2+
// output so that consumers get typed access via store name (e.g. select('core')).
3+
/// <reference path="./store-registry.ts" preserve="true" />
4+
export { store } from './store';
1355

1366
/**
1377
* Enums cannot be exported private without losing the ability to narrow types
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { store } from './store';
2+
3+
/**
4+
* Bind the store descriptor to the store name so that string-based
5+
* access (e.g. `select( 'core' )`) is typed.
6+
*/
7+
declare module '@wordpress/data' {
8+
interface StoreRegistry {
9+
core: typeof store;
10+
}
11+
}

‎packages/core-data/src/store.js‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { createReduxStore, register } from '@wordpress/data';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import reducer from './reducer';
10+
import * as selectors from './selectors';
11+
import * as privateSelectors from './private-selectors';
12+
import * as actions from './actions';
13+
import * as privateActions from './private-actions';
14+
import * as resolvers from './resolvers';
15+
import createLocksActions from './locks/actions';
16+
import {
17+
rootEntitiesConfig,
18+
additionalEntityConfigLoaders,
19+
getMethodName,
20+
} from './entities';
21+
import { STORE_NAME } from './name';
22+
import { unlock } from './lock-unlock';
23+
import { dynamicActions, dynamicSelectors } from './dynamic-entities';
24+
import logEntityDeprecation from './utils/log-entity-deprecation';
25+
26+
// The entity selectors/resolvers and actions are shortcuts to their generic equivalents
27+
// (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecords)
28+
// Instead of getEntityRecord, the consumer could use more user-friendly named selector: getPostType, getTaxonomy...
29+
// The "kind" and the "name" of the entity are combined to generate these shortcuts.
30+
const entitiesConfig = [
31+
...rootEntitiesConfig,
32+
...additionalEntityConfigLoaders.filter( ( config ) => !! config.name ),
33+
];
34+
35+
const entitySelectors = entitiesConfig.reduce( ( result, entity ) => {
36+
const { kind, name, plural } = entity;
37+
38+
const getEntityRecordMethodName = getMethodName( kind, name );
39+
result[ getEntityRecordMethodName ] = ( state, key, query ) => {
40+
logEntityDeprecation( kind, name, getEntityRecordMethodName, {
41+
isShorthandSelector: true,
42+
alternativeFunctionName: 'getEntityRecord',
43+
} );
44+
return selectors.getEntityRecord( state, kind, name, key, query );
45+
};
46+
47+
if ( plural ) {
48+
const getEntityRecordsMethodName = getMethodName( kind, plural, 'get' );
49+
result[ getEntityRecordsMethodName ] = ( state, query ) => {
50+
logEntityDeprecation( kind, name, getEntityRecordsMethodName, {
51+
isShorthandSelector: true,
52+
alternativeFunctionName: 'getEntityRecords',
53+
} );
54+
return selectors.getEntityRecords( state, kind, name, query );
55+
};
56+
}
57+
return result;
58+
}, {} );
59+
60+
const entityResolvers = entitiesConfig.reduce( ( result, entity ) => {
61+
const { kind, name, plural } = entity;
62+
const getEntityRecordMethodName = getMethodName( kind, name );
63+
result[ getEntityRecordMethodName ] = ( key, query ) => {
64+
logEntityDeprecation( kind, name, getEntityRecordMethodName, {
65+
isShorthandSelector: true,
66+
alternativeFunctionName: 'getEntityRecord',
67+
} );
68+
return resolvers.getEntityRecord( kind, name, key, query );
69+
};
70+
71+
if ( plural ) {
72+
const getEntityRecordsMethodName = getMethodName( kind, plural, 'get' );
73+
result[ getEntityRecordsMethodName ] = ( ...args ) => {
74+
logEntityDeprecation( kind, plural, getEntityRecordsMethodName, {
75+
isShorthandSelector: true,
76+
alternativeFunctionName: 'getEntityRecords',
77+
} );
78+
return resolvers.getEntityRecords( kind, name, ...args );
79+
};
80+
result[ getEntityRecordsMethodName ].shouldInvalidate = ( action ) =>
81+
resolvers.getEntityRecords.shouldInvalidate( action, kind, name );
82+
}
83+
return result;
84+
}, {} );
85+
86+
const entityActions = entitiesConfig.reduce( ( result, entity ) => {
87+
const { kind, name } = entity;
88+
89+
const saveEntityRecordMethodName = getMethodName( kind, name, 'save' );
90+
result[ saveEntityRecordMethodName ] = ( record, options ) => {
91+
logEntityDeprecation( kind, name, saveEntityRecordMethodName, {
92+
isShorthandSelector: true,
93+
alternativeFunctionName: 'saveEntityRecord',
94+
} );
95+
return actions.saveEntityRecord( kind, name, record, options );
96+
};
97+
98+
const deleteEntityRecordMethodName = getMethodName( kind, name, 'delete' );
99+
result[ deleteEntityRecordMethodName ] = ( key, query, options ) => {
100+
logEntityDeprecation( kind, name, deleteEntityRecordMethodName, {
101+
isShorthandSelector: true,
102+
alternativeFunctionName: 'deleteEntityRecord',
103+
} );
104+
return actions.deleteEntityRecord( kind, name, key, query, options );
105+
};
106+
107+
return result;
108+
}, {} );
109+
110+
const storeConfig = () => ( {
111+
reducer,
112+
actions: {
113+
...dynamicActions,
114+
...actions,
115+
...entityActions,
116+
...createLocksActions(),
117+
},
118+
selectors: {
119+
...dynamicSelectors,
120+
...selectors,
121+
...entitySelectors,
122+
},
123+
resolvers: { ...resolvers, ...entityResolvers },
124+
} );
125+
126+
/**
127+
* Store definition for the code data namespace.
128+
*
129+
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
130+
*/
131+
export const store = createReduxStore( STORE_NAME, storeConfig() );
132+
unlock( store ).registerPrivateSelectors( privateSelectors );
133+
unlock( store ).registerPrivateActions( privateActions );
134+
register( store ); // Register store after unlocking private selectors to allow resolvers to use them.

‎packages/editor/src/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Ensure the StoreRegistry module augmentation is included in the declaration
2+
// output so that consumers get typed access via store name (e.g. select('core/editor')).
3+
/// <reference path="./store-registry.ts" preserve="true" />
4+
15
/**
26
* Internal dependencies
37
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { store } from './store';
2+
3+
/**
4+
* Bind the store descriptor to the store name so that string-based
5+
* access (e.g. `select( 'core/editor' )`) is typed.
6+
*/
7+
declare module '@wordpress/data' {
8+
interface StoreRegistry {
9+
'core/editor': typeof store;
10+
}
11+
}

‎packages/notices/src/store/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ export const store = createReduxStore( 'core/notices', {
2222
} );
2323

2424
register( store );
25+
26+
/**
27+
* Bind the store descriptor to the store name so that string-based
28+
* access (e.g. `select( 'core/notices' )`) is typed.
29+
*/
30+
declare module '@wordpress/data' {
31+
interface StoreRegistry {
32+
'core/notices': typeof store;
33+
}
34+
}

‎packages/preferences/src/store/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ export const store = createReduxStore<
2828
} );
2929

3030
register( store );
31+
32+
/**
33+
* Bind the store descriptor to the store name so that string-based
34+
* access (e.g. `select( 'core/preferences' )`) is typed.
35+
*/
36+
declare module '@wordpress/data' {
37+
interface StoreRegistry {
38+
[ STORE_NAME ]: typeof store;
39+
}
40+
}

‎packages/upload-media/src/store/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ if ( ! select( store ) ) {
4545
unlock( store ).registerPrivateActions( privateActions );
4646
// @ts-ignore
4747
unlock( store ).registerPrivateSelectors( privateSelectors );
48+
49+
/**
50+
* Bind the store descriptor to the store name so that string-based
51+
* access (e.g. `select( 'core/upload-media' )`) is typed.
52+
*/
53+
declare module '@wordpress/data' {
54+
interface StoreRegistry {
55+
[ STORE_NAME ]: typeof store;
56+
}
57+
}

0 commit comments

Comments
 (0)