@wordpress/core-data 4.13.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +148 -65
  3. package/build/entities.js +38 -51
  4. package/build/entities.js.map +1 -1
  5. package/build/hooks/index.js +14 -0
  6. package/build/hooks/index.js.map +1 -1
  7. package/build/hooks/use-entity-record.js +10 -6
  8. package/build/hooks/use-entity-record.js.map +1 -1
  9. package/build/hooks/use-entity-records.js +2 -0
  10. package/build/hooks/use-entity-records.js.map +1 -1
  11. package/build/hooks/use-query-select.js +5 -2
  12. package/build/hooks/use-query-select.js.map +1 -1
  13. package/build/hooks/use-resource-permissions.js +74 -11
  14. package/build/hooks/use-resource-permissions.js.map +1 -1
  15. package/build/index.js +1 -30
  16. package/build/index.js.map +1 -1
  17. package/build/resolvers.js +59 -39
  18. package/build/resolvers.js.map +1 -1
  19. package/build/selectors.js +9 -46
  20. package/build/selectors.js.map +1 -1
  21. package/build-module/entities.js +38 -52
  22. package/build-module/entities.js.map +1 -1
  23. package/build-module/hooks/index.js +1 -0
  24. package/build-module/hooks/index.js.map +1 -1
  25. package/build-module/hooks/use-entity-record.js +10 -6
  26. package/build-module/hooks/use-entity-record.js.map +1 -1
  27. package/build-module/hooks/use-entity-records.js +2 -0
  28. package/build-module/hooks/use-entity-records.js.map +1 -1
  29. package/build-module/hooks/use-query-select.js +4 -1
  30. package/build-module/hooks/use-query-select.js.map +1 -1
  31. package/build-module/hooks/use-resource-permissions.js +70 -10
  32. package/build-module/hooks/use-resource-permissions.js.map +1 -1
  33. package/build-module/index.js +0 -5
  34. package/build-module/index.js.map +1 -1
  35. package/build-module/resolvers.js +59 -39
  36. package/build-module/resolvers.js.map +1 -1
  37. package/build-module/selectors.js +7 -44
  38. package/build-module/selectors.js.map +1 -1
  39. package/build-types/actions.d.ts +188 -0
  40. package/build-types/actions.d.ts.map +1 -0
  41. package/build-types/batch/create-batch.d.ts +71 -0
  42. package/build-types/batch/create-batch.d.ts.map +1 -0
  43. package/build-types/batch/default-processor.d.ts +11 -0
  44. package/build-types/batch/default-processor.d.ts.map +1 -0
  45. package/build-types/batch/index.d.ts +3 -0
  46. package/build-types/batch/index.d.ts.map +1 -0
  47. package/build-types/entities.d.ts +128 -0
  48. package/build-types/entities.d.ts.map +1 -0
  49. package/build-types/entity-provider.d.ts +68 -0
  50. package/build-types/entity-provider.d.ts.map +1 -0
  51. package/build-types/entity-types/attachment.d.ts +121 -0
  52. package/build-types/entity-types/attachment.d.ts.map +1 -0
  53. package/build-types/entity-types/base-entity-records.d.ts +37 -0
  54. package/build-types/entity-types/base-entity-records.d.ts.map +1 -0
  55. package/build-types/entity-types/comment.d.ts +82 -0
  56. package/build-types/entity-types/comment.d.ts.map +1 -0
  57. package/build-types/entity-types/helpers.d.ts +123 -0
  58. package/build-types/entity-types/helpers.d.ts.map +1 -0
  59. package/build-types/entity-types/index.d.ts +64 -0
  60. package/build-types/entity-types/index.d.ts.map +1 -0
  61. package/build-types/entity-types/menu-location.d.ts +25 -0
  62. package/build-types/entity-types/menu-location.d.ts.map +1 -0
  63. package/build-types/entity-types/nav-menu-item.d.ts +88 -0
  64. package/build-types/entity-types/nav-menu-item.d.ts.map +1 -0
  65. package/build-types/entity-types/nav-menu.d.ts +45 -0
  66. package/build-types/entity-types/nav-menu.d.ts.map +1 -0
  67. package/build-types/entity-types/page.d.ts +120 -0
  68. package/build-types/entity-types/page.d.ts.map +1 -0
  69. package/build-types/entity-types/plugin.d.ts +62 -0
  70. package/build-types/entity-types/plugin.d.ts.map +1 -0
  71. package/build-types/entity-types/post.d.ts +128 -0
  72. package/build-types/entity-types/post.d.ts.map +1 -0
  73. package/build-types/entity-types/settings.d.ts +89 -0
  74. package/build-types/entity-types/settings.d.ts.map +1 -0
  75. package/build-types/entity-types/sidebar.d.ts +55 -0
  76. package/build-types/entity-types/sidebar.d.ts.map +1 -0
  77. package/build-types/entity-types/taxonomy.d.ts +83 -0
  78. package/build-types/entity-types/taxonomy.d.ts.map +1 -0
  79. package/build-types/entity-types/theme.d.ts +206 -0
  80. package/build-types/entity-types/theme.d.ts.map +1 -0
  81. package/build-types/entity-types/type.d.ts +71 -0
  82. package/build-types/entity-types/type.d.ts.map +1 -0
  83. package/build-types/entity-types/user.d.ts +93 -0
  84. package/build-types/entity-types/user.d.ts.map +1 -0
  85. package/build-types/entity-types/widget-type.d.ts +33 -0
  86. package/build-types/entity-types/widget-type.d.ts.map +1 -0
  87. package/build-types/entity-types/widget.d.ts +59 -0
  88. package/build-types/entity-types/widget.d.ts.map +1 -0
  89. package/build-types/entity-types/wp-template-part.d.ts +80 -0
  90. package/build-types/entity-types/wp-template-part.d.ts.map +1 -0
  91. package/build-types/entity-types/wp-template.d.ts +80 -0
  92. package/build-types/entity-types/wp-template.d.ts.map +1 -0
  93. package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts +139 -0
  94. package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts.map +1 -0
  95. package/build-types/fetch/__experimental-fetch-url-data.d.ts +35 -0
  96. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -0
  97. package/build-types/fetch/index.d.ts +3 -0
  98. package/build-types/fetch/index.d.ts.map +1 -0
  99. package/build-types/hooks/constants.d.ts +7 -0
  100. package/build-types/hooks/constants.d.ts.map +1 -0
  101. package/build-types/hooks/index.d.ts +4 -0
  102. package/build-types/hooks/index.d.ts.map +1 -0
  103. package/build-types/hooks/memoize.d.ts +3 -0
  104. package/build-types/hooks/memoize.d.ts.map +1 -0
  105. package/build-types/hooks/use-entity-record.d.ts +124 -0
  106. package/build-types/hooks/use-entity-record.d.ts.map +1 -0
  107. package/build-types/hooks/use-entity-records.d.ts +49 -0
  108. package/build-types/hooks/use-entity-records.d.ts.map +1 -0
  109. package/build-types/hooks/use-query-select.d.ts +46 -0
  110. package/build-types/hooks/use-query-select.d.ts.map +1 -0
  111. package/build-types/hooks/use-resource-permissions.d.ts +99 -0
  112. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -0
  113. package/build-types/index.d.ts +131 -0
  114. package/build-types/index.d.ts.map +1 -0
  115. package/build-types/locks/actions.d.ts +7 -0
  116. package/build-types/locks/actions.d.ts.map +1 -0
  117. package/build-types/locks/engine.d.ts +5 -0
  118. package/build-types/locks/engine.d.ts.map +1 -0
  119. package/build-types/locks/reducer.d.ts +10 -0
  120. package/build-types/locks/reducer.d.ts.map +1 -0
  121. package/build-types/locks/selectors.d.ts +5 -0
  122. package/build-types/locks/selectors.d.ts.map +1 -0
  123. package/build-types/locks/utils.d.ts +8 -0
  124. package/build-types/locks/utils.d.ts.map +1 -0
  125. package/build-types/name.d.ts +8 -0
  126. package/build-types/name.d.ts.map +1 -0
  127. package/build-types/queried-data/actions.d.ts +32 -0
  128. package/build-types/queried-data/actions.d.ts.map +1 -0
  129. package/build-types/queried-data/get-query-parts.d.ts +60 -0
  130. package/build-types/queried-data/get-query-parts.d.ts.map +1 -0
  131. package/build-types/queried-data/index.d.ts +4 -0
  132. package/build-types/queried-data/index.d.ts.map +1 -0
  133. package/build-types/queried-data/reducer.d.ts +54 -0
  134. package/build-types/queried-data/reducer.d.ts.map +1 -0
  135. package/build-types/queried-data/selectors.d.ts +16 -0
  136. package/build-types/queried-data/selectors.d.ts.map +1 -0
  137. package/build-types/reducer.d.ts +156 -0
  138. package/build-types/reducer.d.ts.map +1 -0
  139. package/build-types/resolvers.d.ts +75 -0
  140. package/build-types/resolvers.d.ts.map +1 -0
  141. package/build-types/selectors.d.ts +524 -0
  142. package/build-types/selectors.d.ts.map +1 -0
  143. package/build-types/types.d.ts +4 -0
  144. package/build-types/types.d.ts.map +1 -0
  145. package/build-types/utils/conservative-map-item.d.ts +12 -0
  146. package/build-types/utils/conservative-map-item.d.ts.map +1 -0
  147. package/build-types/utils/forward-resolver.d.ts +10 -0
  148. package/build-types/utils/forward-resolver.d.ts.map +1 -0
  149. package/build-types/utils/get-normalized-comma-separable.d.ts +12 -0
  150. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -0
  151. package/build-types/utils/if-matching-action.d.ts +14 -0
  152. package/build-types/utils/if-matching-action.d.ts.map +1 -0
  153. package/build-types/utils/index.d.ts +9 -0
  154. package/build-types/utils/index.d.ts.map +1 -0
  155. package/build-types/utils/is-raw-attribute.d.ts +10 -0
  156. package/build-types/utils/is-raw-attribute.d.ts.map +1 -0
  157. package/build-types/utils/on-sub-key.d.ts +4 -0
  158. package/build-types/utils/on-sub-key.d.ts.map +1 -0
  159. package/build-types/utils/replace-action.d.ts +13 -0
  160. package/build-types/utils/replace-action.d.ts.map +1 -0
  161. package/build-types/utils/with-weak-map-cache.d.ts +12 -0
  162. package/build-types/utils/with-weak-map-cache.d.ts.map +1 -0
  163. package/package.json +13 -11
  164. package/src/entities.js +325 -0
  165. package/src/entity-types/attachment.ts +3 -4
  166. package/src/entity-types/comment.ts +3 -4
  167. package/src/entity-types/index.ts +31 -88
  168. package/src/entity-types/menu-location.ts +3 -4
  169. package/src/entity-types/nav-menu-item.ts +3 -4
  170. package/src/entity-types/nav-menu.ts +3 -3
  171. package/src/entity-types/page.ts +3 -3
  172. package/src/entity-types/plugin.ts +3 -3
  173. package/src/entity-types/post.ts +3 -3
  174. package/src/entity-types/settings.ts +3 -3
  175. package/src/entity-types/sidebar.ts +3 -4
  176. package/src/entity-types/taxonomy.ts +3 -4
  177. package/src/entity-types/theme.ts +7 -3
  178. package/src/entity-types/type.ts +3 -3
  179. package/src/entity-types/user.ts +3 -3
  180. package/src/entity-types/widget-type.ts +3 -4
  181. package/src/entity-types/widget.ts +3 -3
  182. package/src/entity-types/wp-template-part.ts +3 -4
  183. package/src/entity-types/wp-template.ts +3 -4
  184. package/src/hooks/index.ts +4 -0
  185. package/src/hooks/test/use-entity-record.js +41 -1
  186. package/src/hooks/test/use-resource-permissions.js +32 -36
  187. package/src/hooks/use-entity-record.ts +18 -6
  188. package/src/hooks/use-entity-records.ts +2 -0
  189. package/src/hooks/use-query-select.ts +4 -1
  190. package/src/hooks/use-resource-permissions.ts +84 -20
  191. package/src/index.js +0 -5
  192. package/src/resolvers.js +80 -45
  193. package/src/selectors.ts +202 -341
  194. package/src/test/resolvers.js +118 -4
  195. package/tsconfig.json +21 -0
  196. package/tsconfig.tsbuildinfo +1 -0
  197. package/build/entity-types/entities.js +0 -6
  198. package/build/entity-types/entities.js.map +0 -1
  199. package/build-module/entity-types/entities.js +0 -2
  200. package/build-module/entity-types/entities.js.map +0 -1
  201. package/src/entities.ts +0 -548
  202. package/src/entity-types/entities.ts +0 -130
@@ -9,7 +9,6 @@ import type {
9
9
  } from './helpers';
10
10
 
11
11
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
12
- import type { DefaultContextOf } from './index';
13
12
 
14
13
  declare module './base-entity-records' {
15
14
  export namespace BaseEntityRecords {
@@ -94,5 +93,6 @@ declare module './base-entity-records' {
94
93
  }
95
94
  }
96
95
 
97
- export type Settings< C extends Context = DefaultContextOf< 'root', 'site' > > =
98
- OmitNevers< _BaseEntityRecords.Settings< C > >;
96
+ export type Settings< C extends Context = 'view' > = OmitNevers<
97
+ _BaseEntityRecords.Settings< C >
98
+ >;
@@ -4,7 +4,6 @@
4
4
  import type { Context, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import type { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -55,6 +54,6 @@ declare module './base-entity-records' {
55
54
 
56
55
  type SidebarStatus = 'active' | 'inactive';
57
56
 
58
- export type Sidebar<
59
- C extends Context = DefaultContextOf< 'root', 'sidebar' >
60
- > = OmitNevers< _BaseEntityRecords.Sidebar< C > >;
57
+ export type Sidebar< C extends Context = 'edit' > = OmitNevers<
58
+ _BaseEntityRecords.Sidebar< C >
59
+ >;
@@ -4,7 +4,6 @@
4
4
  import type { Context, ContextualField, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import type { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -88,6 +87,6 @@ declare module './base-entity-records' {
88
87
  }
89
88
  }
90
89
 
91
- export type Taxonomy<
92
- C extends Context = DefaultContextOf< 'postType', 'taxonomy' >
93
- > = OmitNevers< _BaseEntityRecords.Taxonomy< C > >;
90
+ export type Taxonomy< C extends Context = 'edit' > = OmitNevers<
91
+ _BaseEntityRecords.Taxonomy< C >
92
+ >;
@@ -4,7 +4,6 @@
4
4
  import type { Context, PostFormat, RenderedText, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import type { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -78,6 +77,10 @@ declare module './base-entity-records' {
78
77
  * Whether theme opts in to wide alignment CSS class.
79
78
  */
80
79
  'align-wide': boolean;
80
+ /**
81
+ * Whether appearanceTools are enabled in Global Styles.
82
+ */
83
+ 'appearance-tools': boolean;
81
84
  /**
82
85
  * Whether posts and comments RSS feed links are added to head.
83
86
  */
@@ -218,5 +221,6 @@ declare module './base-entity-records' {
218
221
  }
219
222
  }
220
223
 
221
- export type Theme< C extends Context = DefaultContextOf< 'root', 'theme' > > =
222
- OmitNevers< _BaseEntityRecords.Theme< C > >;
224
+ export type Theme< C extends Context = 'edit' > = OmitNevers<
225
+ _BaseEntityRecords.Theme< C >
226
+ >;
@@ -4,7 +4,6 @@
4
4
  import type { Context, ContextualField, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import type { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -76,5 +75,6 @@ declare module './base-entity-records' {
76
75
  }
77
76
  }
78
77
 
79
- export type Type< C extends Context = DefaultContextOf< 'root', 'postType' > > =
80
- OmitNevers< _BaseEntityRecords.Type< C > >;
78
+ export type Type< C extends Context = 'edit' > = OmitNevers<
79
+ _BaseEntityRecords.Type< C >
80
+ >;
@@ -9,7 +9,6 @@ import type {
9
9
  } from './helpers';
10
10
 
11
11
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
12
- import type { DefaultContextOf } from './index';
13
12
 
14
13
  declare module './base-entity-records' {
15
14
  export namespace BaseEntityRecords {
@@ -110,5 +109,6 @@ declare module './base-entity-records' {
110
109
  }
111
110
  }
112
111
 
113
- export type User< C extends Context = DefaultContextOf< 'root', 'user' > > =
114
- OmitNevers< _BaseEntityRecords.User< C > >;
112
+ export type User< C extends Context = 'edit' > = OmitNevers<
113
+ _BaseEntityRecords.User< C >
114
+ >;
@@ -4,7 +4,6 @@
4
4
  import type { Context, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import type { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -33,6 +32,6 @@ declare module './base-entity-records' {
33
32
  }
34
33
  }
35
34
 
36
- export type WidgetType<
37
- C extends Context = DefaultContextOf< 'root', 'widgetType' >
38
- > = OmitNevers< _BaseEntityRecords.WidgetType< C > >;
35
+ export type WidgetType< C extends Context = 'edit' > = OmitNevers<
36
+ _BaseEntityRecords.WidgetType< C >
37
+ >;
@@ -4,7 +4,6 @@
4
4
  import type { Context, ContextualField, OmitNevers } from './helpers';
5
5
 
6
6
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7
- import type { DefaultContextOf } from './index';
8
7
 
9
8
  declare module './base-entity-records' {
10
9
  export namespace BaseEntityRecords {
@@ -60,5 +59,6 @@ declare module './base-entity-records' {
60
59
  }
61
60
  }
62
61
 
63
- export type Widget< C extends Context = DefaultContextOf< 'root', 'widget' > > =
64
- OmitNevers< _BaseEntityRecords.Widget< C > >;
62
+ export type Widget< C extends Context = 'edit' > = OmitNevers<
63
+ _BaseEntityRecords.Widget< C >
64
+ >;
@@ -10,7 +10,6 @@ import type {
10
10
  } from './helpers';
11
11
 
12
12
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
13
- import type { DefaultContextOf } from './index';
14
13
 
15
14
  declare module './base-entity-records' {
16
15
  export namespace BaseEntityRecords {
@@ -90,6 +89,6 @@ declare module './base-entity-records' {
90
89
  }
91
90
  }
92
91
 
93
- export type WpTemplatePart<
94
- C extends Context = DefaultContextOf< 'postType', 'wp_template_part' >
95
- > = OmitNevers< _BaseEntityRecords.WpTemplatePart< C > >;
92
+ export type WpTemplatePart< C extends Context = 'edit' > = OmitNevers<
93
+ _BaseEntityRecords.WpTemplatePart< C >
94
+ >;
@@ -10,7 +10,6 @@ import type {
10
10
  } from './helpers';
11
11
 
12
12
  import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
13
- import type { DefaultContextOf } from './index';
14
13
 
15
14
  declare module './base-entity-records' {
16
15
  export namespace BaseEntityRecords {
@@ -90,6 +89,6 @@ declare module './base-entity-records' {
90
89
  }
91
90
  }
92
91
 
93
- export type WpTemplate<
94
- C extends Context = DefaultContextOf< 'postType', 'wp_template' >
95
- > = OmitNevers< _BaseEntityRecords.WpTemplate< C > >;
92
+ export type WpTemplate< C extends Context = 'edit' > = OmitNevers<
93
+ _BaseEntityRecords.WpTemplate< C >
94
+ >;
@@ -6,3 +6,7 @@ export {
6
6
  default as useEntityRecords,
7
7
  __experimentalUseEntityRecords,
8
8
  } from './use-entity-records';
9
+ export {
10
+ default as useResourcePermissions,
11
+ __experimentalUseResourcePermissions,
12
+ } from './use-resource-permissions';
@@ -71,7 +71,38 @@ describe( 'useEntityRecord', () => {
71
71
 
72
72
  expect( data ).toEqual( {
73
73
  edit: expect.any( Function ),
74
- editedRecord: {},
74
+ editedRecord: { hello: 'world', id: 1 },
75
+ hasEdits: false,
76
+ record: { hello: 'world', id: 1 },
77
+ save: expect.any( Function ),
78
+ hasResolved: true,
79
+ isResolving: false,
80
+ status: 'SUCCESS',
81
+ } );
82
+ } );
83
+
84
+ it( 'applies edits to the entity record', async () => {
85
+ // Provide response
86
+ triggerFetch.mockImplementation( () => TEST_RECORD );
87
+
88
+ let widget;
89
+ const TestComponent = () => {
90
+ widget = useEntityRecord( 'root', 'widget', 1 );
91
+ return <div />;
92
+ };
93
+ render(
94
+ <RegistryProvider value={ registry }>
95
+ <TestComponent />
96
+ </RegistryProvider>
97
+ );
98
+
99
+ await act( async () => {
100
+ jest.advanceTimersByTime( 1 );
101
+ } );
102
+
103
+ expect( widget ).toEqual( {
104
+ edit: expect.any( Function ),
105
+ editedRecord: { hello: 'world', id: 1 },
75
106
  hasEdits: false,
76
107
  record: { hello: 'world', id: 1 },
77
108
  save: expect.any( Function ),
@@ -79,5 +110,14 @@ describe( 'useEntityRecord', () => {
79
110
  isResolving: false,
80
111
  status: 'SUCCESS',
81
112
  } );
113
+
114
+ await act( async () => {
115
+ widget.edit( { hello: 'foo' } );
116
+ jest.advanceTimersByTime( 1 );
117
+ } );
118
+
119
+ expect( widget.hasEdits ).toEqual( true );
120
+ expect( widget.record ).toEqual( { hello: 'world', id: 1 } );
121
+ expect( widget.editedRecord ).toEqual( { hello: 'foo', id: 1 } );
82
122
  } );
83
123
  } );
@@ -50,28 +50,26 @@ describe( 'useResourcePermissions', () => {
50
50
  <TestComponent />
51
51
  </RegistryProvider>
52
52
  );
53
- expect( data ).toEqual( [
54
- false,
55
- {
56
- status: 'IDLE',
57
- isResolving: false,
58
- canCreate: false,
59
- },
60
- ] );
53
+ expect( data ).toEqual( {
54
+ status: 'IDLE',
55
+ isResolving: false,
56
+ hasResolved: false,
57
+ canCreate: false,
58
+ canRead: false,
59
+ } );
61
60
 
62
61
  // Required to make sure no updates happen outside of act()
63
62
  await act( async () => {
64
63
  jest.advanceTimersByTime( 1 );
65
64
  } );
66
65
 
67
- expect( data ).toEqual( [
68
- true,
69
- {
70
- status: 'SUCCESS',
71
- isResolving: false,
72
- canCreate: true,
73
- },
74
- ] );
66
+ expect( data ).toEqual( {
67
+ status: 'SUCCESS',
68
+ isResolving: false,
69
+ hasResolved: true,
70
+ canCreate: true,
71
+ canRead: false,
72
+ } );
75
73
  } );
76
74
 
77
75
  it( 'retrieves the relevant permissions for a resource with a key', async () => {
@@ -85,31 +83,29 @@ describe( 'useResourcePermissions', () => {
85
83
  <TestComponent />
86
84
  </RegistryProvider>
87
85
  );
88
- expect( data ).toEqual( [
89
- false,
90
- {
91
- status: 'IDLE',
92
- isResolving: false,
93
- canCreate: false,
94
- canUpdate: false,
95
- canDelete: false,
96
- },
97
- ] );
86
+ expect( data ).toEqual( {
87
+ status: 'IDLE',
88
+ isResolving: false,
89
+ hasResolved: false,
90
+ canCreate: false,
91
+ canRead: false,
92
+ canUpdate: false,
93
+ canDelete: false,
94
+ } );
98
95
 
99
96
  // Required to make sure no updates happen outside of act()
100
97
  await act( async () => {
101
98
  jest.advanceTimersByTime( 1 );
102
99
  } );
103
100
 
104
- expect( data ).toEqual( [
105
- true,
106
- {
107
- status: 'SUCCESS',
108
- isResolving: false,
109
- canCreate: true,
110
- canUpdate: false,
111
- canDelete: false,
112
- },
113
- ] );
101
+ expect( data ).toEqual( {
102
+ status: 'SUCCESS',
103
+ isResolving: false,
104
+ hasResolved: true,
105
+ canCreate: true,
106
+ canRead: false,
107
+ canUpdate: false,
108
+ canDelete: false,
109
+ } );
114
110
  } );
115
111
  } );
@@ -56,6 +56,8 @@ export interface Options {
56
56
  /**
57
57
  * Resolves the specified entity record.
58
58
  *
59
+ * @since 6.1.0 Introduced in WordPress core.
60
+ *
59
61
  * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
60
62
  * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
61
63
  * @param recordId ID of the requested entity record.
@@ -84,8 +86,8 @@ export interface Options {
84
86
  *
85
87
  * @example
86
88
  * ```js
87
- * import { useState } from '@wordpress/data';
88
89
  * import { useDispatch } from '@wordpress/data';
90
+ * import { useCallback } from '@wordpress/element';
89
91
  * import { __ } from '@wordpress/i18n';
90
92
  * import { TextControl } from '@wordpress/components';
91
93
  * import { store as noticeStore } from '@wordpress/notices';
@@ -93,17 +95,19 @@ export interface Options {
93
95
  *
94
96
  * function PageRenameForm( { id } ) {
95
97
  * const page = useEntityRecord( 'postType', 'page', id );
96
- * const [ title, setTitle ] = useState( () => page.record.title.rendered );
97
98
  * const { createSuccessNotice, createErrorNotice } =
98
99
  * useDispatch( noticeStore );
99
100
  *
101
+ * const setTitle = useCallback( ( title ) => {
102
+ * page.edit( { title } );
103
+ * }, [ page.edit ] );
104
+ *
100
105
  * if ( page.isResolving ) {
101
106
  * return 'Loading...';
102
107
  * }
103
108
  *
104
109
  * async function onRename( event ) {
105
110
  * event.preventDefault();
106
- * page.edit( { title } );
107
111
  * try {
108
112
  * await page.save();
109
113
  * createSuccessNotice( __( 'Page renamed.' ), {
@@ -118,7 +122,7 @@ export interface Options {
118
122
  * <form onSubmit={ onRename }>
119
123
  * <TextControl
120
124
  * label={ __( 'Name' ) }
121
- * value={ title }
125
+ * value={ page.editedRecord.title }
122
126
  * onChange={ setTitle }
123
127
  * />
124
128
  * <button type="submit">{ __( 'Save' ) }</button>
@@ -161,8 +165,16 @@ export default function useEntityRecord< RecordType >(
161
165
 
162
166
  const { editedRecord, hasEdits } = useSelect(
163
167
  ( select ) => ( {
164
- editedRecord: select( coreStore ).getEditedEntityRecord(),
165
- hasEdits: select( coreStore ).hasEditsForEntityRecord(),
168
+ editedRecord: select( coreStore ).getEditedEntityRecord(
169
+ kind,
170
+ name,
171
+ recordId
172
+ ),
173
+ hasEdits: select( coreStore ).hasEditsForEntityRecord(
174
+ kind,
175
+ name,
176
+ recordId
177
+ ),
166
178
  } ),
167
179
  [ kind, name, recordId ]
168
180
  );
@@ -24,6 +24,8 @@ const EMPTY_ARRAY = [];
24
24
  /**
25
25
  * Resolves the specified entity records.
26
26
  *
27
+ * @since 6.1.0 Introduced in WordPress core.
28
+ *
27
29
  * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
28
30
  * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
29
31
  * @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.
@@ -35,6 +35,9 @@ interface QuerySelectResponse< Data > {
35
35
  * Like useSelect, but the selectors return objects containing
36
36
  * both the original data AND the resolution info.
37
37
  *
38
+ * @since 6.1.0 Introduced in WordPress core.
39
+ * @private
40
+ *
38
41
  * @param {Function} mapQuerySelect see useSelect
39
42
  * @param {Array} deps see useSelect
40
43
  *
@@ -71,7 +74,7 @@ interface QuerySelectResponse< Data > {
71
74
  *
72
75
  * @return {QuerySelectResponse} Queried data.
73
76
  */
74
- export default function __experimentalUseQuerySelect( mapQuerySelect, deps ) {
77
+ export default function useQuerySelect( mapQuerySelect, deps ) {
75
78
  return useSelect( ( select, registry ) => {
76
79
  const resolve = ( store ) => enrichSelectors( select( store ) );
77
80
  return mapQuerySelect( resolve, registry );
@@ -1,3 +1,8 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import deprecated from '@wordpress/deprecated';
5
+
1
6
  /**
2
7
  * Internal dependencies
3
8
  */
@@ -39,6 +44,8 @@ type ResourcePermissionsResolution< IdType > = [
39
44
  /**
40
45
  * Resolves resource permissions.
41
46
  *
47
+ * @since 6.1.0 Introduced in WordPress core.
48
+ *
42
49
  * @param resource The resource in question, e.g. media.
43
50
  * @param id ID of a specific resource entry, if needed, e.g. 10.
44
51
  *
@@ -65,6 +72,36 @@ type ResourcePermissionsResolution< IdType > = [
65
72
  * // <PagesList />
66
73
  * ```
67
74
  *
75
+ * @example
76
+ * ```js
77
+ * import { useResourcePermissions } from '@wordpress/core-data';
78
+ *
79
+ * function Page({ pageId }) {
80
+ * const {
81
+ * canCreate,
82
+ * canUpdate,
83
+ * canDelete,
84
+ * isResolving
85
+ * } = useResourcePermissions( 'pages', pageId );
86
+ *
87
+ * if ( isResolving ) {
88
+ * return 'Loading ...';
89
+ * }
90
+ *
91
+ * return (
92
+ * <div>
93
+ * {canCreate ? (<button>+ Create a new page</button>) : false}
94
+ * {canUpdate ? (<button>Edit page</button>) : false}
95
+ * {canDelete ? (<button>Delete page</button>) : false}
96
+ * // ...
97
+ * </div>
98
+ * );
99
+ * }
100
+ *
101
+ * // Rendered in the application:
102
+ * // <Page pageId={ 15 } />
103
+ * ```
104
+ *
68
105
  * In the above example, when `PagesList` is rendered into an
69
106
  * application, the appropriate permissions and the resolution details will be retrieved from
70
107
  * the store state using `canUser()`, or resolved if missing.
@@ -72,7 +109,7 @@ type ResourcePermissionsResolution< IdType > = [
72
109
  * @return Entity records data.
73
110
  * @template IdType
74
111
  */
75
- export default function __experimentalUseResourcePermissions< IdType = void >(
112
+ export default function useResourcePermissions< IdType = void >(
76
113
  resource: string,
77
114
  id?: IdType
78
115
  ): ResourcePermissionsResolution< IdType > {
@@ -81,22 +118,39 @@ export default function __experimentalUseResourcePermissions< IdType = void >(
81
118
  const { canUser } = resolve( coreStore );
82
119
  const create = canUser( 'create', resource );
83
120
  if ( ! id ) {
84
- return [
85
- create.hasResolved,
86
- {
87
- status: create.status,
88
- isResolving: create.isResolving,
89
- canCreate: create.hasResolved && create.data,
90
- },
91
- ];
121
+ const read = canUser( 'read', resource );
122
+
123
+ const isResolving = create.isResolving || read.isResolving;
124
+ const hasResolved = create.hasResolved && read.hasResolved;
125
+ let status = Status.Idle;
126
+ if ( isResolving ) {
127
+ status = Status.Resolving;
128
+ } else if ( hasResolved ) {
129
+ status = Status.Success;
130
+ }
131
+
132
+ return {
133
+ status,
134
+ isResolving,
135
+ hasResolved,
136
+ canCreate: create.hasResolved && create.data,
137
+ canRead: read.hasResolved && read.data,
138
+ };
92
139
  }
93
140
 
141
+ const read = canUser( 'read', resource, id );
94
142
  const update = canUser( 'update', resource, id );
95
143
  const _delete = canUser( 'delete', resource, id );
96
144
  const isResolving =
97
- create.isResolving || update.isResolving || _delete.isResolving;
145
+ read.isResolving ||
146
+ create.isResolving ||
147
+ update.isResolving ||
148
+ _delete.isResolving;
98
149
  const hasResolved =
99
- create.hasResolved && update.hasResolved && _delete.hasResolved;
150
+ read.hasResolved &&
151
+ create.hasResolved &&
152
+ update.hasResolved &&
153
+ _delete.hasResolved;
100
154
 
101
155
  let status = Status.Idle;
102
156
  if ( isResolving ) {
@@ -104,17 +158,27 @@ export default function __experimentalUseResourcePermissions< IdType = void >(
104
158
  } else if ( hasResolved ) {
105
159
  status = Status.Success;
106
160
  }
107
- return [
161
+ return {
162
+ status,
163
+ isResolving,
108
164
  hasResolved,
109
- {
110
- status,
111
- isResolving,
112
- canCreate: hasResolved && create.data,
113
- canUpdate: hasResolved && update.data,
114
- canDelete: hasResolved && _delete.data,
115
- },
116
- ];
165
+ canRead: hasResolved && read.data,
166
+ canCreate: hasResolved && create.data,
167
+ canUpdate: hasResolved && update.data,
168
+ canDelete: hasResolved && _delete.data,
169
+ };
117
170
  },
118
171
  [ resource, id ]
119
172
  );
120
173
  }
174
+
175
+ export function __experimentalUseResourcePermissions(
176
+ resource: string,
177
+ id?: unknown
178
+ ) {
179
+ deprecated( `wp.data.__experimentalUseResourcePermissions`, {
180
+ alternative: 'wp.data.useResourcePermissions',
181
+ since: '6.1',
182
+ } );
183
+ return useResourcePermissions( resource, id );
184
+ }
package/src/index.js CHANGED
@@ -60,17 +60,12 @@ const storeConfig = () => ( {
60
60
  * Store definition for the code data namespace.
61
61
  *
62
62
  * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
63
- *
64
- * @type {Object}
65
63
  */
66
64
  export const store = createReduxStore( STORE_NAME, storeConfig() );
67
65
 
68
66
  register( store );
69
67
 
70
68
  export { default as EntityProvider } from './entity-provider';
71
- export { default as useEntityRecord } from './hooks/use-entity-record';
72
- export { default as useEntityRecords } from './hooks/use-entity-records';
73
- export { default as __experimentalUseResourcePermissions } from './hooks/use-resource-permissions';
74
69
  export * from './entity-provider';
75
70
  export * from './entity-types';
76
71
  export * from './fetch';