@wordpress/core-data 4.2.1 → 4.4.1

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 (147) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +68 -11
  3. package/build/actions.js +61 -41
  4. package/build/actions.js.map +1 -1
  5. package/build/batch/default-processor.js +1 -1
  6. package/build/batch/default-processor.js.map +1 -1
  7. package/build/entities.js +36 -23
  8. package/build/entities.js.map +1 -1
  9. package/build/entity-provider.js +46 -44
  10. package/build/entity-provider.js.map +1 -1
  11. package/build/entity-types/{base-entity-types.js → base-entity-records.js} +9 -9
  12. package/build/entity-types/base-entity-records.js.map +1 -0
  13. package/build/fetch/__experimental-fetch-link-suggestions.js +48 -11
  14. package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
  15. package/build/fetch/__experimental-fetch-url-data.js +1 -1
  16. package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
  17. package/build/hooks/use-entity-record.js +15 -5
  18. package/build/hooks/use-entity-record.js.map +1 -1
  19. package/build/hooks/use-entity-records.js +14 -2
  20. package/build/hooks/use-entity-records.js.map +1 -1
  21. package/build/hooks/use-query-select.js.map +1 -1
  22. package/build/index.js +9 -21
  23. package/build/index.js.map +1 -1
  24. package/build/queried-data/actions.js +4 -4
  25. package/build/queried-data/actions.js.map +1 -1
  26. package/build/queried-data/get-query-parts.js +7 -3
  27. package/build/queried-data/get-query-parts.js.map +1 -1
  28. package/build/queried-data/reducer.js +8 -6
  29. package/build/queried-data/reducer.js.map +1 -1
  30. package/build/reducer.js +77 -25
  31. package/build/reducer.js.map +1 -1
  32. package/build/resolvers.js +60 -24
  33. package/build/resolvers.js.map +1 -1
  34. package/build/selectors.js +122 -55
  35. package/build/selectors.js.map +1 -1
  36. package/build/types.js +6 -0
  37. package/build/types.js.map +1 -0
  38. package/build/utils/conservative-map-item.js +1 -1
  39. package/build/utils/conservative-map-item.js.map +1 -1
  40. package/build/utils/if-matching-action.js +4 -2
  41. package/build/utils/if-matching-action.js.map +1 -1
  42. package/build/utils/is-raw-attribute.js +1 -1
  43. package/build/utils/is-raw-attribute.js.map +1 -1
  44. package/build/utils/on-sub-key.js +3 -1
  45. package/build/utils/on-sub-key.js.map +1 -1
  46. package/build/utils/replace-action.js +4 -2
  47. package/build/utils/replace-action.js.map +1 -1
  48. package/build-module/actions.js +62 -42
  49. package/build-module/actions.js.map +1 -1
  50. package/build-module/batch/default-processor.js +1 -1
  51. package/build-module/batch/default-processor.js.map +1 -1
  52. package/build-module/entities.js +32 -19
  53. package/build-module/entities.js.map +1 -1
  54. package/build-module/entity-provider.js +47 -45
  55. package/build-module/entity-provider.js.map +1 -1
  56. package/build-module/entity-types/{base-entity-types.js → base-entity-records.js} +7 -7
  57. package/build-module/entity-types/base-entity-records.js.map +1 -0
  58. package/build-module/fetch/__experimental-fetch-link-suggestions.js +48 -11
  59. package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
  60. package/build-module/fetch/__experimental-fetch-url-data.js +1 -1
  61. package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
  62. package/build-module/hooks/use-entity-record.js +15 -5
  63. package/build-module/hooks/use-entity-record.js.map +1 -1
  64. package/build-module/hooks/use-entity-records.js +14 -2
  65. package/build-module/hooks/use-entity-records.js.map +1 -1
  66. package/build-module/hooks/use-query-select.js.map +1 -1
  67. package/build-module/index.js +10 -22
  68. package/build-module/index.js.map +1 -1
  69. package/build-module/queried-data/actions.js +4 -4
  70. package/build-module/queried-data/actions.js.map +1 -1
  71. package/build-module/queried-data/get-query-parts.js +7 -3
  72. package/build-module/queried-data/get-query-parts.js.map +1 -1
  73. package/build-module/queried-data/reducer.js +8 -6
  74. package/build-module/queried-data/reducer.js.map +1 -1
  75. package/build-module/reducer.js +75 -26
  76. package/build-module/reducer.js.map +1 -1
  77. package/build-module/resolvers.js +55 -25
  78. package/build-module/resolvers.js.map +1 -1
  79. package/build-module/selectors.js +114 -55
  80. package/build-module/selectors.js.map +1 -1
  81. package/build-module/types.js +2 -0
  82. package/build-module/types.js.map +1 -0
  83. package/build-module/utils/conservative-map-item.js +1 -1
  84. package/build-module/utils/conservative-map-item.js.map +1 -1
  85. package/build-module/utils/if-matching-action.js +4 -2
  86. package/build-module/utils/if-matching-action.js.map +1 -1
  87. package/build-module/utils/is-raw-attribute.js +1 -1
  88. package/build-module/utils/is-raw-attribute.js.map +1 -1
  89. package/build-module/utils/on-sub-key.js +3 -1
  90. package/build-module/utils/on-sub-key.js.map +1 -1
  91. package/build-module/utils/replace-action.js +4 -2
  92. package/build-module/utils/replace-action.js.map +1 -1
  93. package/package.json +11 -11
  94. package/src/actions.js +62 -42
  95. package/src/batch/default-processor.js +1 -0
  96. package/src/{entities.js → entities.ts} +50 -21
  97. package/src/entity-provider.js +50 -44
  98. package/src/entity-types/attachment.ts +5 -5
  99. package/src/entity-types/{base-entity-types.ts → base-entity-records.ts} +5 -5
  100. package/src/entity-types/comment.ts +5 -5
  101. package/src/entity-types/helpers.ts +1 -1
  102. package/src/entity-types/index.ts +4 -4
  103. package/src/entity-types/menu-location.ts +5 -5
  104. package/src/entity-types/nav-menu-item.ts +10 -5
  105. package/src/entity-types/nav-menu.ts +5 -5
  106. package/src/entity-types/navigation-area.ts +5 -5
  107. package/src/entity-types/page.ts +5 -5
  108. package/src/entity-types/plugin.ts +10 -5
  109. package/src/entity-types/post.ts +5 -5
  110. package/src/entity-types/settings.ts +10 -5
  111. package/src/entity-types/sidebar.ts +6 -7
  112. package/src/entity-types/taxonomy.ts +5 -5
  113. package/src/entity-types/theme.ts +5 -5
  114. package/src/entity-types/type.ts +5 -5
  115. package/src/entity-types/user.ts +10 -5
  116. package/src/entity-types/widget-type.ts +5 -5
  117. package/src/entity-types/widget.ts +5 -5
  118. package/src/entity-types/wp-template-part.ts +5 -5
  119. package/src/entity-types/wp-template.ts +5 -5
  120. package/src/fetch/__experimental-fetch-link-suggestions.js +56 -20
  121. package/src/fetch/__experimental-fetch-url-data.js +1 -0
  122. package/src/fetch/test/__experimental-fetch-link-suggestions.js +39 -1
  123. package/src/hooks/use-entity-record.ts +19 -8
  124. package/src/hooks/use-entity-records.ts +23 -6
  125. package/src/hooks/use-query-select.ts +12 -7
  126. package/src/index.js +10 -15
  127. package/src/locks/test/selectors.js +4 -4
  128. package/src/queried-data/actions.js +4 -4
  129. package/src/queried-data/get-query-parts.js +5 -5
  130. package/src/queried-data/reducer.js +6 -6
  131. package/src/reducer.js +67 -24
  132. package/src/resolvers.js +52 -31
  133. package/src/{selectors.js → selectors.ts} +118 -57
  134. package/src/test/actions.js +138 -33
  135. package/src/test/entities.js +11 -11
  136. package/src/test/reducer.js +4 -4
  137. package/src/test/resolvers.js +5 -5
  138. package/src/test/selectors.js +22 -22
  139. package/src/types.ts +3 -0
  140. package/src/utils/conservative-map-item.js +1 -1
  141. package/src/utils/if-matching-action.js +4 -2
  142. package/src/utils/is-raw-attribute.js +1 -1
  143. package/src/utils/on-sub-key.js +3 -1
  144. package/src/utils/replace-action.js +4 -2
  145. package/src/utils/test/is-raw-attribute.js +4 -4
  146. package/build/entity-types/base-entity-types.js.map +0 -1
  147. package/build-module/entity-types/base-entity-types.js.map +0 -1
@@ -11,48 +11,50 @@ import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
11
11
  */
12
12
 
13
13
  import { STORE_NAME } from './name';
14
+ /** @typedef {import('@wordpress/blocks').WPBlock} WPBlock */
15
+
14
16
  const EMPTY_ARRAY = [];
15
17
  /**
16
18
  * Internal dependencies
17
19
  */
18
20
 
19
- import { defaultEntities, kinds } from './entities';
20
- const entities = { ...defaultEntities.reduce((acc, entity) => {
21
- if (!acc[entity.kind]) {
22
- acc[entity.kind] = {};
21
+ import { rootEntitiesConfig, additionalEntityConfigLoaders } from './entities';
22
+ const entityContexts = { ...rootEntitiesConfig.reduce((acc, loader) => {
23
+ if (!acc[loader.kind]) {
24
+ acc[loader.kind] = {};
23
25
  }
24
26
 
25
- acc[entity.kind][entity.name] = {
26
- context: createContext()
27
+ acc[loader.kind][loader.name] = {
28
+ context: createContext(undefined)
27
29
  };
28
30
  return acc;
29
31
  }, {}),
30
- ...kinds.reduce((acc, kind) => {
31
- acc[kind.name] = {};
32
+ ...additionalEntityConfigLoaders.reduce((acc, loader) => {
33
+ acc[loader.kind] = {};
32
34
  return acc;
33
35
  }, {})
34
36
  };
35
37
 
36
- const getEntity = (kind, type) => {
37
- if (!entities[kind]) {
38
+ const getEntityContext = (kind, name) => {
39
+ if (!entityContexts[kind]) {
38
40
  throw new Error(`Missing entity config for kind: ${kind}.`);
39
41
  }
40
42
 
41
- if (!entities[kind][type]) {
42
- entities[kind][type] = {
43
- context: createContext()
43
+ if (!entityContexts[kind][name]) {
44
+ entityContexts[kind][name] = {
45
+ context: createContext(undefined)
44
46
  };
45
47
  }
46
48
 
47
- return entities[kind][type];
49
+ return entityContexts[kind][name].context;
48
50
  };
49
51
  /**
50
52
  * Context provider component for providing
51
- * an entity for a specific entity type.
53
+ * an entity for a specific entity.
52
54
  *
53
55
  * @param {Object} props The component's props.
54
56
  * @param {string} props.kind The entity kind.
55
- * @param {string} props.type The entity type.
57
+ * @param {string} props.type The entity name.
56
58
  * @param {number} props.id The entity ID.
57
59
  * @param {*} props.children The children to wrap.
58
60
  *
@@ -64,11 +66,11 @@ const getEntity = (kind, type) => {
64
66
  export default function EntityProvider(_ref) {
65
67
  let {
66
68
  kind,
67
- type,
69
+ type: name,
68
70
  id,
69
71
  children
70
72
  } = _ref;
71
- const Provider = getEntity(kind, type).context.Provider;
73
+ const Provider = getEntityContext(kind, name).Provider;
72
74
  return createElement(Provider, {
73
75
  value: id
74
76
  }, children);
@@ -78,11 +80,11 @@ export default function EntityProvider(_ref) {
78
80
  * provided entity of the specified type.
79
81
  *
80
82
  * @param {string} kind The entity kind.
81
- * @param {string} type The entity type.
83
+ * @param {string} name The entity name.
82
84
  */
83
85
 
84
- export function useEntityId(kind, type) {
85
- return useContext(getEntity(kind, type).context);
86
+ export function useEntityId(kind, name) {
87
+ return useContext(getEntityContext(kind, name));
86
88
  }
87
89
  /**
88
90
  * Hook that returns the value and a setter for the
@@ -90,7 +92,7 @@ export function useEntityId(kind, type) {
90
92
  * entity of the specified type.
91
93
  *
92
94
  * @param {string} kind The entity kind.
93
- * @param {string} type The entity type.
95
+ * @param {string} name The entity name.
94
96
  * @param {string} prop The property name.
95
97
  * @param {string} [_id] An entity ID to use instead of the context-provided one.
96
98
  *
@@ -102,8 +104,8 @@ export function useEntityId(kind, type) {
102
104
  * `protected` props.
103
105
  */
104
106
 
105
- export function useEntityProp(kind, type, prop, _id) {
106
- const providerId = useEntityId(kind, type);
107
+ export function useEntityProp(kind, name, prop, _id) {
108
+ const providerId = useEntityId(kind, name);
107
109
  const id = _id !== null && _id !== void 0 ? _id : providerId;
108
110
  const {
109
111
  value,
@@ -113,22 +115,22 @@ export function useEntityProp(kind, type, prop, _id) {
113
115
  getEntityRecord,
114
116
  getEditedEntityRecord
115
117
  } = select(STORE_NAME);
116
- const entity = getEntityRecord(kind, type, id); // Trigger resolver.
118
+ const record = getEntityRecord(kind, name, id); // Trigger resolver.
117
119
 
118
- const editedEntity = getEditedEntityRecord(kind, type, id);
119
- return entity && editedEntity ? {
120
- value: editedEntity[prop],
121
- fullValue: entity[prop]
120
+ const editedRecord = getEditedEntityRecord(kind, name, id);
121
+ return record && editedRecord ? {
122
+ value: editedRecord[prop],
123
+ fullValue: record[prop]
122
124
  } : {};
123
- }, [kind, type, id, prop]);
125
+ }, [kind, name, id, prop]);
124
126
  const {
125
127
  editEntityRecord
126
128
  } = useDispatch(STORE_NAME);
127
129
  const setValue = useCallback(newValue => {
128
- editEntityRecord(kind, type, id, {
130
+ editEntityRecord(kind, name, id, {
129
131
  [prop]: newValue
130
132
  });
131
- }, [kind, type, id, prop]);
133
+ }, [kind, name, id, prop]);
132
134
  return [value, setValue, fullValue];
133
135
  }
134
136
  /**
@@ -143,18 +145,18 @@ export function useEntityProp(kind, type, prop, _id) {
143
145
  * or similar components or hooks.
144
146
  *
145
147
  * @param {string} kind The entity kind.
146
- * @param {string} type The entity type.
148
+ * @param {string} name The entity name.
147
149
  * @param {Object} options
148
150
  * @param {string} [options.id] An entity ID to use instead of the context-provided one.
149
151
  *
150
152
  * @return {[WPBlock[], Function, Function]} The block array and setters.
151
153
  */
152
154
 
153
- export function useEntityBlockEditor(kind, type) {
155
+ export function useEntityBlockEditor(kind, name) {
154
156
  let {
155
157
  id: _id
156
158
  } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
157
- const providerId = useEntityId(kind, type);
159
+ const providerId = useEntityId(kind, name);
158
160
  const id = _id !== null && _id !== void 0 ? _id : providerId;
159
161
  const {
160
162
  content,
@@ -163,12 +165,12 @@ export function useEntityBlockEditor(kind, type) {
163
165
  const {
164
166
  getEditedEntityRecord
165
167
  } = select(STORE_NAME);
166
- const editedEntity = getEditedEntityRecord(kind, type, id);
168
+ const editedRecord = getEditedEntityRecord(kind, name, id);
167
169
  return {
168
- blocks: editedEntity.blocks,
169
- content: editedEntity.content
170
+ blocks: editedRecord.blocks,
171
+ content: editedRecord.content
170
172
  };
171
- }, [kind, type, id]);
173
+ }, [kind, name, id]);
172
174
  const {
173
175
  __unstableCreateUndoLevel,
174
176
  editEntityRecord
@@ -179,7 +181,7 @@ export function useEntityBlockEditor(kind, type) {
179
181
  // set content to a function already or the blocks are already in state.
180
182
  if (content && typeof content !== 'function' && !blocks) {
181
183
  const parsedContent = parse(content);
182
- editEntityRecord(kind, type, id, {
184
+ editEntityRecord(kind, name, id, {
183
185
  blocks: parsedContent
184
186
  }, {
185
187
  undoIgnore: true
@@ -197,7 +199,7 @@ export function useEntityBlockEditor(kind, type) {
197
199
  const noChange = blocks === edits.blocks;
198
200
 
199
201
  if (noChange) {
200
- return __unstableCreateUndoLevel(kind, type, id);
202
+ return __unstableCreateUndoLevel(kind, name, id);
201
203
  } // We create a new function here on every persistent edit
202
204
  // to make sure the edit makes the post dirty and creates
203
205
  // a new undo level.
@@ -210,8 +212,8 @@ export function useEntityBlockEditor(kind, type) {
210
212
  return __unstableSerializeAndClean(blocksForSerialization);
211
213
  };
212
214
 
213
- editEntityRecord(kind, type, id, edits);
214
- }, [kind, type, id, blocks]);
215
+ editEntityRecord(kind, name, id, edits);
216
+ }, [kind, name, id, blocks]);
215
217
  const onInput = useCallback((newBlocks, options) => {
216
218
  const {
217
219
  selection
@@ -220,8 +222,8 @@ export function useEntityBlockEditor(kind, type) {
220
222
  blocks: newBlocks,
221
223
  selection
222
224
  };
223
- editEntityRecord(kind, type, id, edits);
224
- }, [kind, type, id]);
225
+ editEntityRecord(kind, name, id, edits);
226
+ }, [kind, name, id]);
225
227
  return [blocks !== null && blocks !== void 0 ? blocks : EMPTY_ARRAY, onInput, onChange];
226
228
  }
227
229
  //# sourceMappingURL=entity-provider.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/entity-provider.js"],"names":["createContext","useContext","useCallback","useEffect","useSelect","useDispatch","parse","__unstableSerializeAndClean","STORE_NAME","EMPTY_ARRAY","defaultEntities","kinds","entities","reduce","acc","entity","kind","name","context","getEntity","type","Error","EntityProvider","id","children","Provider","useEntityId","useEntityProp","prop","_id","providerId","value","fullValue","select","getEntityRecord","getEditedEntityRecord","editedEntity","editEntityRecord","setValue","newValue","useEntityBlockEditor","content","blocks","__unstableCreateUndoLevel","parsedContent","undoIgnore","onChange","newBlocks","options","selection","edits","noChange","blocksForSerialization","onInput"],"mappings":";;AAAA;AACA;AACA;AACA,SACCA,aADD,EAECC,UAFD,EAGCC,WAHD,EAICC,SAJD,QAKO,oBALP;AAMA,SAASC,SAAT,EAAoBC,WAApB,QAAuC,iBAAvC;AACA,SAASC,KAAT,EAAgBC,2BAAhB,QAAmD,mBAAnD;AAEA;AACA;AACA;;AACA,SAASC,UAAT,QAA2B,QAA3B;AAEA,MAAMC,WAAW,GAAG,EAApB;AAEA;AACA;AACA;;AACA,SAASC,eAAT,EAA0BC,KAA1B,QAAuC,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,CAlB2B,CAA5B;AAqBA,QAAMW,OAAO,GAAGnD,WAAW,CAC1B,CAAE6C,SAAF,EAAaC,OAAb,KAA0B;AACzB,UAAM;AAAEC,MAAAA;AAAF,QAAgBD,OAAtB;AACA,UAAME,KAAK,GAAG;AAAER,MAAAA,MAAM,EAAEK,SAAV;AAAqBE,MAAAA;AAArB,KAAd;AACAZ,IAAAA,gBAAgB,CAAErB,IAAF,EAAQI,IAAR,EAAcG,EAAd,EAAkB2B,KAAlB,CAAhB;AACA,GALyB,EAM1B,CAAElC,IAAF,EAAQI,IAAR,EAAcG,EAAd,CAN0B,CAA3B;AASA,SAAO,CAAEmB,MAAF,aAAEA,MAAF,cAAEA,MAAF,GAAYjC,WAAZ,EAAyB4C,OAAzB,EAAkCP,QAAlC,CAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tcreateContext,\n\tuseContext,\n\tuseCallback,\n\tuseEffect,\n} from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { parse, __unstableSerializeAndClean } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from './name';\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Internal dependencies\n */\nimport { defaultEntities, kinds } from './entities';\n\nconst entities = {\n\t...defaultEntities.reduce( ( acc, entity ) => {\n\t\tif ( ! acc[ entity.kind ] ) {\n\t\t\tacc[ entity.kind ] = {};\n\t\t}\n\t\tacc[ entity.kind ][ entity.name ] = { context: createContext() };\n\t\treturn acc;\n\t}, {} ),\n\t...kinds.reduce( ( acc, kind ) => {\n\t\tacc[ kind.name ] = {};\n\t\treturn acc;\n\t}, {} ),\n};\nconst getEntity = ( kind, type ) => {\n\tif ( ! entities[ kind ] ) {\n\t\tthrow new Error( `Missing entity config for kind: ${ kind }.` );\n\t}\n\n\tif ( ! entities[ kind ][ type ] ) {\n\t\tentities[ kind ][ type ] = { context: createContext() };\n\t}\n\n\treturn entities[ kind ][ type ];\n};\n\n/**\n * Context provider component for providing\n * an entity for a specific entity type.\n *\n * @param {Object} props The component's props.\n * @param {string} props.kind The entity kind.\n * @param {string} props.type The entity type.\n * @param {number} props.id The entity ID.\n * @param {*} props.children The children to wrap.\n *\n * @return {Object} The provided children, wrapped with\n * the entity's context provider.\n */\nexport default function EntityProvider( { kind, type, id, children } ) {\n\tconst Provider = getEntity( kind, type ).context.Provider;\n\treturn <Provider value={ id }>{ children }</Provider>;\n}\n\n/**\n * Hook that returns the ID for the nearest\n * provided entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} type The entity type.\n */\nexport function useEntityId( kind, type ) {\n\treturn useContext( getEntity( kind, type ).context );\n}\n\n/**\n * Hook that returns the value and a setter for the\n * specified property of the nearest provided\n * entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} type The entity type.\n * @param {string} prop The property name.\n * @param {string} [_id] An entity ID to use instead of the context-provided one.\n *\n * @return {[*, Function, *]} An array where the first item is the\n * property value, the second is the\n * setter and the third is the full value\n * \t\t\t\t\t\t\t object from REST API containing more\n * \t\t\t\t\t\t\t information like `raw`, `rendered` and\n * \t\t\t\t\t\t\t `protected` props.\n */\nexport function useEntityProp( kind, type, prop, _id ) {\n\tconst providerId = useEntityId( kind, type );\n\tconst id = _id ?? providerId;\n\n\tconst { value, fullValue } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord } = select(\n\t\t\t\tSTORE_NAME\n\t\t\t);\n\t\t\tconst entity = getEntityRecord( kind, type, id ); // Trigger resolver.\n\t\t\tconst editedEntity = getEditedEntityRecord( kind, type, id );\n\t\t\treturn entity && editedEntity\n\t\t\t\t? {\n\t\t\t\t\t\tvalue: editedEntity[ prop ],\n\t\t\t\t\t\tfullValue: entity[ prop ],\n\t\t\t\t }\n\t\t\t\t: {};\n\t\t},\n\t\t[ kind, type, id, prop ]\n\t);\n\tconst { editEntityRecord } = useDispatch( STORE_NAME );\n\tconst setValue = useCallback(\n\t\t( newValue ) => {\n\t\t\teditEntityRecord( kind, type, id, {\n\t\t\t\t[ prop ]: newValue,\n\t\t\t} );\n\t\t},\n\t\t[ kind, type, id, prop ]\n\t);\n\n\treturn [ value, setValue, fullValue ];\n}\n\n/**\n * Hook that returns block content getters and setters for\n * the nearest provided entity of the specified type.\n *\n * The return value has the shape `[ blocks, onInput, onChange ]`.\n * `onInput` is for block changes that don't create undo levels\n * or dirty the post, non-persistent changes, and `onChange` is for\n * peristent changes. They map directly to the props of a\n * `BlockEditorProvider` and are intended to be used with it,\n * or similar components or hooks.\n *\n * @param {string} kind The entity kind.\n * @param {string} type The entity type.\n * @param {Object} options\n * @param {string} [options.id] An entity ID to use instead of the context-provided one.\n *\n * @return {[WPBlock[], Function, Function]} The block array and setters.\n */\nexport function useEntityBlockEditor( kind, type, { id: _id } = {} ) {\n\tconst providerId = useEntityId( kind, type );\n\tconst id = _id ?? providerId;\n\tconst { content, blocks } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEditedEntityRecord } = select( STORE_NAME );\n\t\t\tconst editedEntity = getEditedEntityRecord( kind, type, id );\n\t\t\treturn {\n\t\t\t\tblocks: editedEntity.blocks,\n\t\t\t\tcontent: editedEntity.content,\n\t\t\t};\n\t\t},\n\t\t[ kind, type, id ]\n\t);\n\tconst { __unstableCreateUndoLevel, editEntityRecord } = useDispatch(\n\t\tSTORE_NAME\n\t);\n\n\tuseEffect( () => {\n\t\t// Load the blocks from the content if not already in state\n\t\t// Guard against other instances that might have\n\t\t// set content to a function already or the blocks are already in state.\n\t\tif ( content && typeof content !== 'function' && ! blocks ) {\n\t\t\tconst parsedContent = parse( content );\n\t\t\teditEntityRecord(\n\t\t\t\tkind,\n\t\t\t\ttype,\n\t\t\t\tid,\n\t\t\t\t{\n\t\t\t\t\tblocks: parsedContent,\n\t\t\t\t},\n\t\t\t\t{ undoIgnore: true }\n\t\t\t);\n\t\t}\n\t}, [ content ] );\n\n\tconst onChange = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection } = options;\n\t\t\tconst edits = { blocks: newBlocks, selection };\n\n\t\t\tconst noChange = blocks === edits.blocks;\n\t\t\tif ( noChange ) {\n\t\t\t\treturn __unstableCreateUndoLevel( kind, type, id );\n\t\t\t}\n\n\t\t\t// We create a new function here on every persistent edit\n\t\t\t// to make sure the edit makes the post dirty and creates\n\t\t\t// a new undo level.\n\t\t\tedits.content = ( { blocks: blocksForSerialization = [] } ) =>\n\t\t\t\t__unstableSerializeAndClean( blocksForSerialization );\n\n\t\t\teditEntityRecord( kind, type, id, edits );\n\t\t},\n\t\t[ kind, type, id, blocks ]\n\t);\n\n\tconst onInput = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection } = options;\n\t\t\tconst edits = { blocks: newBlocks, selection };\n\t\t\teditEntityRecord( kind, type, id, edits );\n\t\t},\n\t\t[ kind, type, id ]\n\t);\n\n\treturn [ blocks ?? EMPTY_ARRAY, onInput, onChange ];\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/core-data/src/entity-provider.js"],"names":["createContext","useContext","useCallback","useEffect","useSelect","useDispatch","parse","__unstableSerializeAndClean","STORE_NAME","EMPTY_ARRAY","rootEntitiesConfig","additionalEntityConfigLoaders","entityContexts","reduce","acc","loader","kind","name","context","undefined","getEntityContext","Error","EntityProvider","type","id","children","Provider","useEntityId","useEntityProp","prop","_id","providerId","value","fullValue","select","getEntityRecord","getEditedEntityRecord","record","editedRecord","editEntityRecord","setValue","newValue","useEntityBlockEditor","content","blocks","__unstableCreateUndoLevel","parsedContent","undoIgnore","onChange","newBlocks","options","selection","edits","noChange","blocksForSerialization","onInput"],"mappings":";;AAAA;AACA;AACA;AACA,SACCA,aADD,EAECC,UAFD,EAGCC,WAHD,EAICC,SAJD,QAKO,oBALP;AAMA,SAASC,SAAT,EAAoBC,WAApB,QAAuC,iBAAvC;AACA,SAASC,KAAT,EAAgBC,2BAAhB,QAAmD,mBAAnD;AAEA;AACA;AACA;;AACA,SAASC,UAAT,QAA2B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gB,CAAEvB,IAAF,EAAQC,IAAR,EAAcO,EAAd,EAAkB4B,KAAlB,CAAhB;AACA,GAjB0B,EAkB3B,CAAEpC,IAAF,EAAQC,IAAR,EAAcO,EAAd,EAAkBoB,MAAlB,CAlB2B,CAA5B;AAqBA,QAAMW,OAAO,GAAGrD,WAAW,CAC1B,CAAE+C,SAAF,EAAaC,OAAb,KAA0B;AACzB,UAAM;AAAEC,MAAAA;AAAF,QAAgBD,OAAtB;AACA,UAAME,KAAK,GAAG;AAAER,MAAAA,MAAM,EAAEK,SAAV;AAAqBE,MAAAA;AAArB,KAAd;AACAZ,IAAAA,gBAAgB,CAAEvB,IAAF,EAAQC,IAAR,EAAcO,EAAd,EAAkB4B,KAAlB,CAAhB;AACA,GALyB,EAM1B,CAAEpC,IAAF,EAAQC,IAAR,EAAcO,EAAd,CAN0B,CAA3B;AASA,SAAO,CAAEoB,MAAF,aAAEA,MAAF,cAAEA,MAAF,GAAYnC,WAAZ,EAAyB8C,OAAzB,EAAkCP,QAAlC,CAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tcreateContext,\n\tuseContext,\n\tuseCallback,\n\tuseEffect,\n} from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { parse, __unstableSerializeAndClean } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport { STORE_NAME } from './name';\n\n/** @typedef {import('@wordpress/blocks').WPBlock} WPBlock */\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Internal dependencies\n */\nimport { rootEntitiesConfig, additionalEntityConfigLoaders } from './entities';\n\nconst entityContexts = {\n\t...rootEntitiesConfig.reduce( ( acc, loader ) => {\n\t\tif ( ! acc[ loader.kind ] ) {\n\t\t\tacc[ loader.kind ] = {};\n\t\t}\n\t\tacc[ loader.kind ][ loader.name ] = {\n\t\t\tcontext: createContext( undefined ),\n\t\t};\n\t\treturn acc;\n\t}, {} ),\n\t...additionalEntityConfigLoaders.reduce( ( acc, loader ) => {\n\t\tacc[ loader.kind ] = {};\n\t\treturn acc;\n\t}, {} ),\n};\nconst getEntityContext = ( kind, name ) => {\n\tif ( ! entityContexts[ kind ] ) {\n\t\tthrow new Error( `Missing entity config for kind: ${ kind }.` );\n\t}\n\n\tif ( ! entityContexts[ kind ][ name ] ) {\n\t\tentityContexts[ kind ][ name ] = {\n\t\t\tcontext: createContext( undefined ),\n\t\t};\n\t}\n\n\treturn entityContexts[ kind ][ name ].context;\n};\n\n/**\n * Context provider component for providing\n * an entity for a specific entity.\n *\n * @param {Object} props The component's props.\n * @param {string} props.kind The entity kind.\n * @param {string} props.type The entity name.\n * @param {number} props.id The entity ID.\n * @param {*} props.children The children to wrap.\n *\n * @return {Object} The provided children, wrapped with\n * the entity's context provider.\n */\nexport default function EntityProvider( { kind, type: name, id, children } ) {\n\tconst Provider = getEntityContext( kind, name ).Provider;\n\treturn <Provider value={ id }>{ children }</Provider>;\n}\n\n/**\n * Hook that returns the ID for the nearest\n * provided entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n */\nexport function useEntityId( kind, name ) {\n\treturn useContext( getEntityContext( kind, name ) );\n}\n\n/**\n * Hook that returns the value and a setter for the\n * specified property of the nearest provided\n * entity of the specified type.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {string} prop The property name.\n * @param {string} [_id] An entity ID to use instead of the context-provided one.\n *\n * @return {[*, Function, *]} An array where the first item is the\n * property value, the second is the\n * setter and the third is the full value\n * \t\t\t\t\t\t\t object from REST API containing more\n * \t\t\t\t\t\t\t information like `raw`, `rendered` and\n * \t\t\t\t\t\t\t `protected` props.\n */\nexport function useEntityProp( kind, name, prop, _id ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\n\tconst { value, fullValue } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord } = select(\n\t\t\t\tSTORE_NAME\n\t\t\t);\n\t\t\tconst record = getEntityRecord( kind, name, id ); // Trigger resolver.\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn record && editedRecord\n\t\t\t\t? {\n\t\t\t\t\t\tvalue: editedRecord[ prop ],\n\t\t\t\t\t\tfullValue: record[ prop ],\n\t\t\t\t }\n\t\t\t\t: {};\n\t\t},\n\t\t[ kind, name, id, prop ]\n\t);\n\tconst { editEntityRecord } = useDispatch( STORE_NAME );\n\tconst setValue = useCallback(\n\t\t( newValue ) => {\n\t\t\teditEntityRecord( kind, name, id, {\n\t\t\t\t[ prop ]: newValue,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, id, prop ]\n\t);\n\n\treturn [ value, setValue, fullValue ];\n}\n\n/**\n * Hook that returns block content getters and setters for\n * the nearest provided entity of the specified type.\n *\n * The return value has the shape `[ blocks, onInput, onChange ]`.\n * `onInput` is for block changes that don't create undo levels\n * or dirty the post, non-persistent changes, and `onChange` is for\n * peristent changes. They map directly to the props of a\n * `BlockEditorProvider` and are intended to be used with it,\n * or similar components or hooks.\n *\n * @param {string} kind The entity kind.\n * @param {string} name The entity name.\n * @param {Object} options\n * @param {string} [options.id] An entity ID to use instead of the context-provided one.\n *\n * @return {[WPBlock[], Function, Function]} The block array and setters.\n */\nexport function useEntityBlockEditor( kind, name, { id: _id } = {} ) {\n\tconst providerId = useEntityId( kind, name );\n\tconst id = _id ?? providerId;\n\tconst { content, blocks } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEditedEntityRecord } = select( STORE_NAME );\n\t\t\tconst editedRecord = getEditedEntityRecord( kind, name, id );\n\t\t\treturn {\n\t\t\t\tblocks: editedRecord.blocks,\n\t\t\t\tcontent: editedRecord.content,\n\t\t\t};\n\t\t},\n\t\t[ kind, name, id ]\n\t);\n\tconst { __unstableCreateUndoLevel, editEntityRecord } = useDispatch(\n\t\tSTORE_NAME\n\t);\n\n\tuseEffect( () => {\n\t\t// Load the blocks from the content if not already in state\n\t\t// Guard against other instances that might have\n\t\t// set content to a function already or the blocks are already in state.\n\t\tif ( content && typeof content !== 'function' && ! blocks ) {\n\t\t\tconst parsedContent = parse( content );\n\t\t\teditEntityRecord(\n\t\t\t\tkind,\n\t\t\t\tname,\n\t\t\t\tid,\n\t\t\t\t{\n\t\t\t\t\tblocks: parsedContent,\n\t\t\t\t},\n\t\t\t\t{ undoIgnore: true }\n\t\t\t);\n\t\t}\n\t}, [ content ] );\n\n\tconst onChange = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection } = options;\n\t\t\tconst edits = { blocks: newBlocks, selection };\n\n\t\t\tconst noChange = blocks === edits.blocks;\n\t\t\tif ( noChange ) {\n\t\t\t\treturn __unstableCreateUndoLevel( kind, name, id );\n\t\t\t}\n\n\t\t\t// We create a new function here on every persistent edit\n\t\t\t// to make sure the edit makes the post dirty and creates\n\t\t\t// a new undo level.\n\t\t\tedits.content = ( { blocks: blocksForSerialization = [] } ) =>\n\t\t\t\t__unstableSerializeAndClean( blocksForSerialization );\n\n\t\t\teditEntityRecord( kind, name, id, edits );\n\t\t},\n\t\t[ kind, name, id, blocks ]\n\t);\n\n\tconst onInput = useCallback(\n\t\t( newBlocks, options ) => {\n\t\t\tconst { selection } = options;\n\t\t\tconst edits = { blocks: newBlocks, selection };\n\t\t\teditEntityRecord( kind, name, id, edits );\n\t\t},\n\t\t[ kind, name, id ]\n\t);\n\n\treturn [ blocks ?? EMPTY_ARRAY, onInput, onChange ];\n}\n"]}
@@ -1,10 +1,10 @@
1
1
  /**
2
- * This module exists solely to make the BaseEntityTypes namespace extensible
2
+ * This module exists solely to make the BaseEntityRecords namespace extensible
3
3
  * with declaration merging:
4
4
  *
5
5
  * ```ts
6
- * declare module './base-entity-types' {
7
- * export namespace BaseEntityTypes {
6
+ * declare module './base-entity-records' {
7
+ * export namespace BaseEntityRecords {
8
8
  * export interface Comment< C extends Context > {
9
9
  * id: number;
10
10
  * // ...
@@ -19,7 +19,7 @@
19
19
  * ```ts
20
20
  * import type { Context } from '@wordpress/core-data';
21
21
  * declare module '@wordpress/core-data' {
22
- * export namespace BaseEntityTypes {
22
+ * export namespace BaseEntityRecords {
23
23
  * export interface Comment< C extends Context > {
24
24
  * numberOfViews: number;
25
25
  * }
@@ -33,7 +33,7 @@
33
33
  * // c.id is still present
34
34
  * ```
35
35
  */
36
- export let BaseEntityTypes;
36
+ export let BaseEntityRecords;
37
37
 
38
- (function (_BaseEntityTypes) {})(BaseEntityTypes || (BaseEntityTypes = {}));
39
- //# sourceMappingURL=base-entity-types.js.map
38
+ (function (_BaseEntityRecords) {})(BaseEntityRecords || (BaseEntityRecords = {}));
39
+ //# sourceMappingURL=base-entity-records.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/core-data/src/entity-types/base-entity-records.ts"],"names":["BaseEntityRecords"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;mCAAiBA,iB,KAAAA,iB","sourcesContent":["/**\n * This module exists solely to make the BaseEntityRecords namespace extensible\n * with declaration merging:\n *\n * ```ts\n * declare module './base-entity-records' {\n * export namespace BaseEntityRecords {\n * \t\t export interface Comment< C extends Context > {\n * \t\t id: number;\n * \t\t // ...\n * \t }\n * \t }\n * }\n * ```\n *\n * The huge upside is that consumers of @wordpress/core-data may extend the\n * exported data types using interface merging as follows:\n *\n * ```ts\n * import type { Context } from '@wordpress/core-data';\n * declare module '@wordpress/core-data' {\n * export namespace BaseEntityRecords {\n * export interface Comment< C extends Context > {\n * numberOfViews: number;\n * }\n * }\n * }\n *\n * import type { Comment } from '@wordpress/core-data';\n * const c : Comment< 'view' > = ...;\n *\n * // c.numberOfViews is a number\n * // c.id is still present\n * ```\n */\nexport namespace BaseEntityRecords {}\n"]}
@@ -8,7 +8,7 @@ import { __ } from '@wordpress/i18n';
8
8
  /**
9
9
  * Filters the search by type
10
10
  *
11
- * @typedef { 'post' | 'term' | 'post-format' } WPLinkSearchType
11
+ * @typedef { 'attachment' | 'post' | 'term' | 'post-format' } WPLinkSearchType
12
12
  */
13
13
 
14
14
  /**
@@ -37,6 +37,17 @@ import { __ } from '@wordpress/i18n';
37
37
  * @property {WPKind} [kind] Link kind of post-type or taxonomy
38
38
  */
39
39
 
40
+ /**
41
+ * @typedef WPLinkSearchResultAugments
42
+ *
43
+ * @property {{kind: WPKind}} [meta] Contains kind information.
44
+ * @property {WPKind} [subtype] Optional subtype if it exists.
45
+ */
46
+
47
+ /**
48
+ * @typedef {WPLinkSearchResult & WPLinkSearchResultAugments} WPLinkSearchResultAugmented
49
+ */
50
+
40
51
  /**
41
52
  * @typedef WPEditorSettings
42
53
  *
@@ -80,6 +91,8 @@ const fetchLinkSuggestions = async function (search) {
80
91
  const {
81
92
  disablePostFormats = false
82
93
  } = settings;
94
+ /** @type {Promise<WPLinkSearchResult>[]} */
95
+
83
96
  const queries = [];
84
97
 
85
98
  if (!type || type === 'post') {
@@ -122,7 +135,8 @@ const fetchLinkSuggestions = async function (search) {
122
135
  }
123
136
  };
124
137
  });
125
- }).catch(() => []));
138
+ }).catch(() => []) // Fail by returning no results.
139
+ );
126
140
  }
127
141
 
128
142
  if (!disablePostFormats && (!type || type === 'post-format')) {
@@ -143,28 +157,51 @@ const fetchLinkSuggestions = async function (search) {
143
157
  }
144
158
  };
145
159
  });
146
- }).catch(() => []));
160
+ }).catch(() => []) // Fail by returning no results.
161
+ );
162
+ }
163
+
164
+ if (!type || type === 'attachment') {
165
+ queries.push(apiFetch({
166
+ path: addQueryArgs('/wp/v2/media', {
167
+ search,
168
+ page,
169
+ per_page: perPage
170
+ })
171
+ }).then(results => {
172
+ return results.map(result => {
173
+ return { ...result,
174
+ meta: {
175
+ kind: 'media'
176
+ }
177
+ };
178
+ });
179
+ }).catch(() => []) // Fail by returning no results.
180
+ );
147
181
  }
148
182
 
149
183
  return Promise.all(queries).then(results => {
150
- return results.reduce((accumulator, current) => accumulator.concat(current), // Flatten list.
184
+ return results.reduce((
185
+ /** @type {WPLinkSearchResult[]} */
186
+ accumulator, current) => accumulator.concat(current), // Flatten list.
151
187
  []).filter(
152
188
  /**
153
189
  * @param {{ id: number }} result
154
190
  */
155
191
  result => {
156
192
  return !!result.id;
157
- }).slice(0, perPage).map(
158
- /**
159
- * @param {{ id: number, url:string, title?:string, subtype?: string, type?: string }} result
160
- */
161
- result => {
193
+ }).slice(0, perPage).map((
194
+ /** @type {WPLinkSearchResultAugmented} */
195
+ result) => {
162
196
  var _result$meta;
163
197
 
198
+ const isMedia = result.type === 'attachment';
164
199
  return {
165
200
  id: result.id,
166
- url: result.url,
167
- title: decodeEntities(result.title || '') || __('(no title)'),
201
+ // @ts-ignore fix when we make this a TS file
202
+ url: isMedia ? result.source_url : result.url,
203
+ title: decodeEntities(isMedia ? // @ts-ignore fix when we make this a TS file
204
+ result.title.rendered : result.title || '') || __('(no title)'),
168
205
  type: result.subtype || result.type,
169
206
  kind: result === null || result === void 0 ? void 0 : (_result$meta = result.meta) === null || _result$meta === void 0 ? void 0 : _result$meta.kind
170
207
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/fetch/__experimental-fetch-link-suggestions.js"],"names":["apiFetch","addQueryArgs","decodeEntities","__","fetchLinkSuggestions","search","searchOptions","settings","isInitialSuggestions","type","undefined","subtype","page","perPage","disablePostFormats","queries","push","path","per_page","then","results","map","result","meta","kind","catch","Promise","all","reduce","accumulator","current","concat","filter","id","slice","url","title"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAP,MAAqB,sBAArB;AACA,SAASC,YAAT,QAA6B,gBAA7B;AACA,SAASC,cAAT,QAA+B,0BAA/B;AACA,SAASC,EAAT,QAAmB,iBAAnB;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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,EAjBf,CADD;AAoBA;;AAED,SAAOC,OAAO,CAACC,GAAR,CAAaZ,OAAb,EAAuBI,IAAvB,CAA+BC,OAAF,IAAe;AAClD,WAAOA,OAAO,CACZQ,MADK,CAEL,CAAEC,WAAF,EAAeC,OAAf,KAA4BD,WAAW,CAACE,MAAZ,CAAoBD,OAApB,CAFvB,EAEsD;AAC3D,MAHK,EAKLE,MALK;AAML;AACJ;AACA;AACMV,IAAAA,MAAF,IAAc;AACb,aAAO,CAAC,CAAEA,MAAM,CAACW,EAAjB;AACA,KAXI,EAaLC,KAbK,CAaE,CAbF,EAaKrB,OAbL,EAcLQ,GAdK;AAeL;AACJ;AACA;AACMC,IAAAA,MAAF,IAAc;AAAA;;AACb,aAAO;AACNW,QAAAA,EAAE,EAAEX,MAAM,CAACW,EADL;AAENE,QAAAA,GAAG,EAAEb,MAAM,CAACa,GAFN;AAGNC,QAAAA,KAAK,EACJlC,cAAc,CAAEoB,MAAM,CAACc,KAAP,IAAgB,EAAlB,CAAd,IACAjC,EAAE,CAAE,YAAF,CALG;AAMNM,QAAAA,IAAI,EAAEa,MAAM,CAACX,OAAP,IAAkBW,MAAM,CAACb,IANzB;AAONe,QAAAA,IAAI,EAAEF,MAAF,aAAEA,MAAF,uCAAEA,MAAM,CAAEC,IAAV,iDAAE,aAAcC;AAPd,OAAP;AASA,KA5BI,CAAP;AA8BA,GA/BM,CAAP;AAgCA,CAtHD;;AAwHA,eAAepB,oBAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { addQueryArgs } from '@wordpress/url';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Filters the search by type\n *\n * @typedef { 'post' | 'term' | 'post-format' } WPLinkSearchType\n */\n\n/**\n * A link with an id may be of kind post-type or taxonomy\n *\n * @typedef { 'post-type' | 'taxonomy' } WPKind\n */\n\n/**\n * @typedef WPLinkSearchOptions\n *\n * @property {boolean} [isInitialSuggestions] Displays initial search suggestions, when true.\n * @property {WPLinkSearchType} [type] Filters by search type.\n * @property {string} [subtype] Slug of the post-type or taxonomy.\n * @property {number} [page] Which page of results to return.\n * @property {number} [perPage] Search results per page.\n */\n\n/**\n * @typedef WPLinkSearchResult\n *\n * @property {number} id Post or term id.\n * @property {string} url Link url.\n * @property {string} title Title of the link.\n * @property {string} type The taxonomy or post type slug or type URL.\n * @property {WPKind} [kind] Link kind of post-type or taxonomy\n */\n\n/**\n * @typedef WPEditorSettings\n *\n * @property {boolean} [ disablePostFormats ] Disables post formats, when true.\n */\n\n/**\n * Fetches link suggestions from the API.\n *\n * @async\n * @param {string} search\n * @param {WPLinkSearchOptions} [searchOptions]\n * @param {WPEditorSettings} [settings]\n *\n * @example\n * ```js\n * import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data';\n *\n * //...\n *\n * export function initialize( id, settings ) {\n *\n * settings.__experimentalFetchLinkSuggestions = (\n * search,\n * searchOptions\n * ) => fetchLinkSuggestions( search, searchOptions, settings );\n * ```\n * @return {Promise< WPLinkSearchResult[] >} List of search suggestions\n */\nconst fetchLinkSuggestions = async (\n\tsearch,\n\tsearchOptions = {},\n\tsettings = {}\n) => {\n\tconst {\n\t\tisInitialSuggestions = false,\n\t\ttype = undefined,\n\t\tsubtype = undefined,\n\t\tpage = undefined,\n\t\tperPage = isInitialSuggestions ? 3 : 20,\n\t} = searchOptions;\n\n\tconst { disablePostFormats = false } = settings;\n\n\tconst queries = [];\n\n\tif ( ! type || type === 'post' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'post-type', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! type || type === 'term' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'term',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'taxonomy', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] )\n\t\t);\n\t}\n\n\tif ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post-format',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'taxonomy', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] )\n\t\t);\n\t}\n\n\treturn Promise.all( queries ).then( ( results ) => {\n\t\treturn results\n\t\t\t.reduce(\n\t\t\t\t( accumulator, current ) => accumulator.concat( current ), // Flatten list.\n\t\t\t\t[]\n\t\t\t)\n\t\t\t.filter(\n\t\t\t\t/**\n\t\t\t\t * @param {{ id: number }} result\n\t\t\t\t */\n\t\t\t\t( result ) => {\n\t\t\t\t\treturn !! result.id;\n\t\t\t\t}\n\t\t\t)\n\t\t\t.slice( 0, perPage )\n\t\t\t.map(\n\t\t\t\t/**\n\t\t\t\t * @param {{ id: number, url:string, title?:string, subtype?: string, type?: string }} result\n\t\t\t\t */\n\t\t\t\t( result ) => {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tid: result.id,\n\t\t\t\t\t\turl: result.url,\n\t\t\t\t\t\ttitle:\n\t\t\t\t\t\t\tdecodeEntities( result.title || '' ) ||\n\t\t\t\t\t\t\t__( '(no title)' ),\n\t\t\t\t\t\ttype: result.subtype || result.type,\n\t\t\t\t\t\tkind: result?.meta?.kind,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t);\n\t} );\n};\n\nexport default fetchLinkSuggestions;\n"]}
1
+ {"version":3,"sources":["@wordpress/core-data/src/fetch/__experimental-fetch-link-suggestions.js"],"names":["apiFetch","addQueryArgs","decodeEntities","__","fetchLinkSuggestions","search","searchOptions","settings","isInitialSuggestions","type","undefined","subtype","page","perPage","disablePostFormats","queries","push","path","per_page","then","results","map","result","meta","kind","catch","Promise","all","reduce","accumulator","current","concat","filter","id","slice","isMedia","url","source_url","title","rendered"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAP,MAAqB,sBAArB;AACA,SAASC,YAAT,QAA6B,gBAA7B;AACA,SAASC,cAAT,QAA+B,0BAA/B;AACA,SAASC,EAAT,QAAmB,iBAAnB;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;A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C,QAFT,GAGJjB,MAAM,CAACgB,KAAP,IAAgB,EAJN,CAAd,IAKKnC,EAAE,CAAE,YAAF,CAVF;AAWNM,QAAAA,IAAI,EAAEa,MAAM,CAACX,OAAP,IAAkBW,MAAM,CAACb,IAXzB;AAYNe,QAAAA,IAAI,EAAEF,MAAF,aAAEA,MAAF,uCAAEA,MAAM,CAAEC,IAAV,iDAAE,aAAcC;AAZd,OAAP;AAcA,KAhCK,CAAP;AAiCA,GAlCM,CAAP;AAmCA,CA/ID;;AAiJA,eAAepB,oBAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { addQueryArgs } from '@wordpress/url';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Filters the search by type\n *\n * @typedef { 'attachment' | 'post' | 'term' | 'post-format' } WPLinkSearchType\n */\n\n/**\n * A link with an id may be of kind post-type or taxonomy\n *\n * @typedef { 'post-type' | 'taxonomy' } WPKind\n */\n\n/**\n * @typedef WPLinkSearchOptions\n *\n * @property {boolean} [isInitialSuggestions] Displays initial search suggestions, when true.\n * @property {WPLinkSearchType} [type] Filters by search type.\n * @property {string} [subtype] Slug of the post-type or taxonomy.\n * @property {number} [page] Which page of results to return.\n * @property {number} [perPage] Search results per page.\n */\n\n/**\n * @typedef WPLinkSearchResult\n *\n * @property {number} id Post or term id.\n * @property {string} url Link url.\n * @property {string} title Title of the link.\n * @property {string} type The taxonomy or post type slug or type URL.\n * @property {WPKind} [kind] Link kind of post-type or taxonomy\n */\n\n/**\n * @typedef WPLinkSearchResultAugments\n *\n * @property {{kind: WPKind}} [meta] Contains kind information.\n * @property {WPKind} [subtype] Optional subtype if it exists.\n */\n\n/**\n * @typedef {WPLinkSearchResult & WPLinkSearchResultAugments} WPLinkSearchResultAugmented\n */\n\n/**\n * @typedef WPEditorSettings\n *\n * @property {boolean} [ disablePostFormats ] Disables post formats, when true.\n */\n\n/**\n * Fetches link suggestions from the API.\n *\n * @async\n * @param {string} search\n * @param {WPLinkSearchOptions} [searchOptions]\n * @param {WPEditorSettings} [settings]\n *\n * @example\n * ```js\n * import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data';\n *\n * //...\n *\n * export function initialize( id, settings ) {\n *\n * settings.__experimentalFetchLinkSuggestions = (\n * search,\n * searchOptions\n * ) => fetchLinkSuggestions( search, searchOptions, settings );\n * ```\n * @return {Promise< WPLinkSearchResult[] >} List of search suggestions\n */\nconst fetchLinkSuggestions = async (\n\tsearch,\n\tsearchOptions = {},\n\tsettings = {}\n) => {\n\tconst {\n\t\tisInitialSuggestions = false,\n\t\ttype = undefined,\n\t\tsubtype = undefined,\n\t\tpage = undefined,\n\t\tperPage = isInitialSuggestions ? 3 : 20,\n\t} = searchOptions;\n\n\tconst { disablePostFormats = false } = settings;\n\n\t/** @type {Promise<WPLinkSearchResult>[]} */\n\tconst queries = [];\n\n\tif ( ! type || type === 'post' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'post-type', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! type || type === 'term' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'term',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'taxonomy', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post-format',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'taxonomy', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! type || type === 'attachment' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/media', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'media' },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\treturn Promise.all( queries ).then( ( results ) => {\n\t\treturn results\n\t\t\t.reduce(\n\t\t\t\t( /** @type {WPLinkSearchResult[]} */ accumulator, current ) =>\n\t\t\t\t\taccumulator.concat( current ), // Flatten list.\n\t\t\t\t[]\n\t\t\t)\n\t\t\t.filter(\n\t\t\t\t/**\n\t\t\t\t * @param {{ id: number }} result\n\t\t\t\t */\n\t\t\t\t( result ) => {\n\t\t\t\t\treturn !! result.id;\n\t\t\t\t}\n\t\t\t)\n\t\t\t.slice( 0, perPage )\n\t\t\t.map( ( /** @type {WPLinkSearchResultAugmented} */ result ) => {\n\t\t\t\tconst isMedia = result.type === 'attachment';\n\n\t\t\t\treturn {\n\t\t\t\t\tid: result.id,\n\t\t\t\t\t// @ts-ignore fix when we make this a TS file\n\t\t\t\t\turl: isMedia ? result.source_url : result.url,\n\t\t\t\t\ttitle:\n\t\t\t\t\t\tdecodeEntities(\n\t\t\t\t\t\t\tisMedia\n\t\t\t\t\t\t\t\t? // @ts-ignore fix when we make this a TS file\n\t\t\t\t\t\t\t\t result.title.rendered\n\t\t\t\t\t\t\t\t: result.title || ''\n\t\t\t\t\t\t) || __( '(no title)' ),\n\t\t\t\t\ttype: result.subtype || result.type,\n\t\t\t\t\tkind: result?.meta?.kind,\n\t\t\t\t};\n\t\t\t} );\n\t} );\n};\n\nexport default fetchLinkSuggestions;\n"]}
@@ -53,7 +53,7 @@ const fetchUrlData = async function (url) {
53
53
 
54
54
  const protocol = getProtocol(url);
55
55
 
56
- if (!isValidProtocol(protocol) || !protocol.startsWith('http') || !/^https?:\/\/[^\/\s]/i.test(url)) {
56
+ if (!protocol || !isValidProtocol(protocol) || !protocol.startsWith('http') || !/^https?:\/\/[^\/\s]/i.test(url)) {
57
57
  return Promise.reject(`${url} does not have a valid protocol. URLs must be "http" based`);
58
58
  }
59
59
 
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/fetch/__experimental-fetch-url-data.js"],"names":["apiFetch","addQueryArgs","prependHTTP","isURL","getProtocol","isValidProtocol","CACHE","Map","fetchUrlData","url","options","endpoint","args","Promise","reject","protocol","startsWith","test","has","get","path","then","res","set"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAP,MAAqB,sBAArB;AACA,SACCC,YADD,EAECC,WAFD,EAGCC,KAHD,EAICC,WAJD,EAKCC,eALD,QAMO,gBANP;AAQA;AACA;AACA;AACA;AACA;;AACA,MAAMC,KAAK,GAAG,IAAIC,GAAJ,EAAd;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,YAAY,GAAG,gBAAQC,GAAR,EAA+B;AAAA,MAAlBC,OAAkB,uEAAR,EAAQ;AACnD,QAAMC,QAAQ,GAAG,iCAAjB;AAEA,QAAMC,IAAI,GAAG;AACZH,IAAAA,GAAG,EAAEP,WAAW,CAAEO,GAAF;AADJ,GAAb;;AAIA,MAAK,CAAEN,KAAK,CAAEM,GAAF,CAAZ,EAAsB;AACrB,WAAOI,OAAO,CAACC,MAAR,CAAiB,GAAGL,GAAK,sBAAzB,CAAP;AACA,GATkD,CAWnD;AACA;;;AACA,QAAMM,QAAQ,GAAGX,WAAW,CAAEK,GAAF,CAA5B;;AAEA,MACC,CAAEJ,eAAe,CAAEU,QAAF,CAAjB,IACA,CAAEA,QAAQ,CAACC,UAAT,CAAqB,MAArB,CADF,IAEA,CAAE,uBAAuBC,IAAvB,CAA6BR,GAA7B,CAHH,EAIE;AACD,WAAOI,OAAO,CAACC,MAAR,CACL,GAAGL,GAAK,4DADH,CAAP;AAGA;;AAED,MAAKH,KAAK,CAACY,GAAN,CAAWT,GAAX,CAAL,EAAwB;AACvB,WAAOH,KAAK,CAACa,GAAN,CAAWV,GAAX,CAAP;AACA;;AAED,SAAOT,QAAQ,CAAE;AAChBoB,IAAAA,IAAI,EAAEnB,YAAY,CAAEU,QAAF,EAAYC,IAAZ,CADF;AAEhB,OAAGF;AAFa,GAAF,CAAR,CAGHW,IAHG,CAGKC,GAAF,IAAW;AACpBhB,IAAAA,KAAK,CAACiB,GAAN,CAAWd,GAAX,EAAgBa,GAAhB;AACA,WAAOA,GAAP;AACA,GANM,CAAP;AAOA,CApCD;;AAsCA,eAAed,YAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport {\n\taddQueryArgs,\n\tprependHTTP,\n\tisURL,\n\tgetProtocol,\n\tisValidProtocol,\n} from '@wordpress/url';\n\n/**\n * A simple in-memory cache for requests.\n * This avoids repeat HTTP requests which may be beneficial\n * for those wishing to preserve low-bandwidth.\n */\nconst CACHE = new Map();\n\n/**\n * @typedef WPRemoteUrlData\n *\n * @property {string} title contents of the remote URL's `<title>` tag.\n */\n\n/**\n * Fetches data about a remote URL.\n * eg: <title> tag, favicon...etc.\n *\n * @async\n * @param {string} url the URL to request details from.\n * @param {Object?} options any options to pass to the underlying fetch.\n * @example\n * ```js\n * import { __experimentalFetchUrlData as fetchUrlData } from '@wordpress/core-data';\n *\n * //...\n *\n * export function initialize( id, settings ) {\n *\n * settings.__experimentalFetchUrlData = (\n * url\n * ) => fetchUrlData( url );\n * ```\n * @return {Promise< WPRemoteUrlData[] >} Remote URL data.\n */\nconst fetchUrlData = async ( url, options = {} ) => {\n\tconst endpoint = '/wp-block-editor/v1/url-details';\n\n\tconst args = {\n\t\turl: prependHTTP( url ),\n\t};\n\n\tif ( ! isURL( url ) ) {\n\t\treturn Promise.reject( `${ url } is not a valid URL.` );\n\t}\n\n\t// Test for \"http\" based URL as it is possible for valid\n\t// yet unusable URLs such as `tel:123456` to be passed.\n\tconst protocol = getProtocol( url );\n\n\tif (\n\t\t! isValidProtocol( protocol ) ||\n\t\t! protocol.startsWith( 'http' ) ||\n\t\t! /^https?:\\/\\/[^\\/\\s]/i.test( url )\n\t) {\n\t\treturn Promise.reject(\n\t\t\t`${ url } does not have a valid protocol. URLs must be \"http\" based`\n\t\t);\n\t}\n\n\tif ( CACHE.has( url ) ) {\n\t\treturn CACHE.get( url );\n\t}\n\n\treturn apiFetch( {\n\t\tpath: addQueryArgs( endpoint, args ),\n\t\t...options,\n\t} ).then( ( res ) => {\n\t\tCACHE.set( url, res );\n\t\treturn res;\n\t} );\n};\n\nexport default fetchUrlData;\n"]}
1
+ {"version":3,"sources":["@wordpress/core-data/src/fetch/__experimental-fetch-url-data.js"],"names":["apiFetch","addQueryArgs","prependHTTP","isURL","getProtocol","isValidProtocol","CACHE","Map","fetchUrlData","url","options","endpoint","args","Promise","reject","protocol","startsWith","test","has","get","path","then","res","set"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAP,MAAqB,sBAArB;AACA,SACCC,YADD,EAECC,WAFD,EAGCC,KAHD,EAICC,WAJD,EAKCC,eALD,QAMO,gBANP;AAQA;AACA;AACA;AACA;AACA;;AACA,MAAMC,KAAK,GAAG,IAAIC,GAAJ,EAAd;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,YAAY,GAAG,gBAAQC,GAAR,EAA+B;AAAA,MAAlBC,OAAkB,uEAAR,EAAQ;AACnD,QAAMC,QAAQ,GAAG,iCAAjB;AAEA,QAAMC,IAAI,GAAG;AACZH,IAAAA,GAAG,EAAEP,WAAW,CAAEO,GAAF;AADJ,GAAb;;AAIA,MAAK,CAAEN,KAAK,CAAEM,GAAF,CAAZ,EAAsB;AACrB,WAAOI,OAAO,CAACC,MAAR,CAAiB,GAAGL,GAAK,sBAAzB,CAAP;AACA,GATkD,CAWnD;AACA;;;AACA,QAAMM,QAAQ,GAAGX,WAAW,CAAEK,GAAF,CAA5B;;AAEA,MACC,CAAEM,QAAF,IACA,CAAEV,eAAe,CAAEU,QAAF,CADjB,IAEA,CAAEA,QAAQ,CAACC,UAAT,CAAqB,MAArB,CAFF,IAGA,CAAE,uBAAuBC,IAAvB,CAA6BR,GAA7B,CAJH,EAKE;AACD,WAAOI,OAAO,CAACC,MAAR,CACL,GAAGL,GAAK,4DADH,CAAP;AAGA;;AAED,MAAKH,KAAK,CAACY,GAAN,CAAWT,GAAX,CAAL,EAAwB;AACvB,WAAOH,KAAK,CAACa,GAAN,CAAWV,GAAX,CAAP;AACA;;AAED,SAAOT,QAAQ,CAAE;AAChBoB,IAAAA,IAAI,EAAEnB,YAAY,CAAEU,QAAF,EAAYC,IAAZ,CADF;AAEhB,OAAGF;AAFa,GAAF,CAAR,CAGHW,IAHG,CAGKC,GAAF,IAAW;AACpBhB,IAAAA,KAAK,CAACiB,GAAN,CAAWd,GAAX,EAAgBa,GAAhB;AACA,WAAOA,GAAP;AACA,GANM,CAAP;AAOA,CArCD;;AAuCA,eAAed,YAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport {\n\taddQueryArgs,\n\tprependHTTP,\n\tisURL,\n\tgetProtocol,\n\tisValidProtocol,\n} from '@wordpress/url';\n\n/**\n * A simple in-memory cache for requests.\n * This avoids repeat HTTP requests which may be beneficial\n * for those wishing to preserve low-bandwidth.\n */\nconst CACHE = new Map();\n\n/**\n * @typedef WPRemoteUrlData\n *\n * @property {string} title contents of the remote URL's `<title>` tag.\n */\n\n/**\n * Fetches data about a remote URL.\n * eg: <title> tag, favicon...etc.\n *\n * @async\n * @param {string} url the URL to request details from.\n * @param {Object?} options any options to pass to the underlying fetch.\n * @example\n * ```js\n * import { __experimentalFetchUrlData as fetchUrlData } from '@wordpress/core-data';\n *\n * //...\n *\n * export function initialize( id, settings ) {\n *\n * settings.__experimentalFetchUrlData = (\n * url\n * ) => fetchUrlData( url );\n * ```\n * @return {Promise< WPRemoteUrlData[] >} Remote URL data.\n */\nconst fetchUrlData = async ( url, options = {} ) => {\n\tconst endpoint = '/wp-block-editor/v1/url-details';\n\n\tconst args = {\n\t\turl: prependHTTP( url ),\n\t};\n\n\tif ( ! isURL( url ) ) {\n\t\treturn Promise.reject( `${ url } is not a valid URL.` );\n\t}\n\n\t// Test for \"http\" based URL as it is possible for valid\n\t// yet unusable URLs such as `tel:123456` to be passed.\n\tconst protocol = getProtocol( url );\n\n\tif (\n\t\t! protocol ||\n\t\t! isValidProtocol( protocol ) ||\n\t\t! protocol.startsWith( 'http' ) ||\n\t\t! /^https?:\\/\\/[^\\/\\s]/i.test( url )\n\t) {\n\t\treturn Promise.reject(\n\t\t\t`${ url } does not have a valid protocol. URLs must be \"http\" based`\n\t\t);\n\t}\n\n\tif ( CACHE.has( url ) ) {\n\t\treturn CACHE.get( url );\n\t}\n\n\treturn apiFetch( {\n\t\tpath: addQueryArgs( endpoint, args ),\n\t\t...options,\n\t} ).then( ( res ) => {\n\t\tCACHE.set( url, res );\n\t\treturn res;\n\t} );\n};\n\nexport default fetchUrlData;\n"]}
@@ -7,10 +7,11 @@ import { store as coreStore } from '../';
7
7
  /**
8
8
  * Resolves the specified entity record.
9
9
  *
10
- * @param kind Kind of the requested entity.
11
- * @param name Name of the requested entity.
12
- * @param recordId Record ID of the requested entity.
13
- *
10
+ * @param kind Kind of the requested entity.
11
+ * @param name Name of the requested entity.
12
+ * @param recordId Record ID of the requested entity.
13
+ * @param options Hook options.
14
+ * @param [options.enabled=true] Whether to run the query or short-circuit and return null. Defaults to true.
14
15
  * @example
15
16
  * ```js
16
17
  * import { useEntityRecord } from '@wordpress/core-data';
@@ -37,10 +38,19 @@ import { store as coreStore } from '../';
37
38
  * @template RecordType
38
39
  */
39
40
  export default function __experimentalUseEntityRecord(kind, name, recordId) {
41
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
42
+ enabled: true
43
+ };
40
44
  const {
41
45
  data: record,
42
46
  ...rest
43
- } = useQuerySelect(query => query(coreStore).getEntityRecord(kind, name, recordId), [kind, name, recordId]);
47
+ } = useQuerySelect(query => {
48
+ if (!options.enabled) {
49
+ return null;
50
+ }
51
+
52
+ return query(coreStore).getEntityRecord(kind, name, recordId);
53
+ }, [kind, name, recordId, options.enabled]);
44
54
  return {
45
55
  record,
46
56
  ...rest
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-record.ts"],"names":["useQuerySelect","store","coreStore","__experimentalUseEntityRecord","kind","name","recordId","data","record","rest","query","getEntityRecord"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,cAAP,MAA2B,oBAA3B;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,KAAnC;;AAqBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,6BAAT,CACdC,IADc,EAEdC,IAFc,EAGdC,QAHc,EAIyB;AACvC,QAAM;AAAEC,IAAAA,IAAI,EAAEC,MAAR;AAAgB,OAAGC;AAAnB,MAA4BT,cAAc,CAC7CU,KAAF,IAAaA,KAAK,CAAER,SAAF,CAAL,CAAmBS,eAAnB,CAAoCP,IAApC,EAA0CC,IAA1C,EAAgDC,QAAhD,CADkC,EAE/C,CAAEF,IAAF,EAAQC,IAAR,EAAcC,QAAd,CAF+C,CAAhD;AAKA,SAAO;AACNE,IAAAA,MADM;AAEN,OAAGC;AAFG,GAAP;AAIA","sourcesContent":["/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\n\ninterface EntityRecordResolution< RecordType > {\n\t/** The requested entity record */\n\trecord: RecordType | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n}\n\n/**\n * Resolves the specified entity record.\n *\n * @param kind Kind of the requested entity.\n * @param name Name of the requested entity.\n * @param recordId Record ID of the requested entity.\n *\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitleDisplay( { id } ) {\n * const { record, isResolving } = useEntityRecord( 'postType', 'page', id );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return record.title;\n * }\n *\n * // Rendered in the application:\n * // <PageTitleDisplay id={ 1 } />\n * ```\n *\n * In the above example, when `PageTitleDisplay` is rendered into an\n * application, the page and the resolution details will be retrieved from\n * the store state using `getEntityRecord()`, or resolved if missing.\n *\n * @return {EntityRecordResolution<RecordType>} Entity record data.\n * @template RecordType\n */\nexport default function __experimentalUseEntityRecord< RecordType >(\n\tkind: string,\n\tname: string,\n\trecordId: string | number\n): EntityRecordResolution< RecordType > {\n\tconst { data: record, ...rest } = useQuerySelect(\n\t\t( query ) => query( coreStore ).getEntityRecord( kind, name, recordId ),\n\t\t[ kind, name, recordId ]\n\t);\n\n\treturn {\n\t\trecord,\n\t\t...rest,\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-record.ts"],"names":["useQuerySelect","store","coreStore","__experimentalUseEntityRecord","kind","name","recordId","options","enabled","data","record","rest","query","getEntityRecord"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,cAAP,MAA2B,oBAA3B;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,KAAnC;;AAyBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,6BAAT,CACdC,IADc,EAEdC,IAFc,EAGdC,QAHc,EAKyB;AAAA,MADvCC,OACuC,uEADpB;AAAEC,IAAAA,OAAO,EAAE;AAAX,GACoB;AACvC,QAAM;AAAEC,IAAAA,IAAI,EAAEC,MAAR;AAAgB,OAAGC;AAAnB,MAA4BX,cAAc,CAC7CY,KAAF,IAAa;AACZ,QAAK,CAAEL,OAAO,CAACC,OAAf,EAAyB;AACxB,aAAO,IAAP;AACA;;AACD,WAAOI,KAAK,CAAEV,SAAF,CAAL,CAAmBW,eAAnB,CAAoCT,IAApC,EAA0CC,IAA1C,EAAgDC,QAAhD,CAAP;AACA,GAN8C,EAO/C,CAAEF,IAAF,EAAQC,IAAR,EAAcC,QAAd,EAAwBC,OAAO,CAACC,OAAhC,CAP+C,CAAhD;AAUA,SAAO;AACNE,IAAAA,MADM;AAEN,OAAGC;AAFG,GAAP;AAIA","sourcesContent":["/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Status } from './constants';\n\ninterface EntityRecordResolution< RecordType > {\n\t/** The requested entity record */\n\trecord: RecordType | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n}\n\ninterface Options {\n\tenabled: boolean;\n}\n\n/**\n * Resolves the specified entity record.\n *\n * @param kind Kind of the requested entity.\n * @param name Name of the requested entity.\n * @param recordId Record ID of the requested entity.\n * @param options Hook options.\n * @param [options.enabled=true] Whether to run the query or short-circuit and return null. Defaults to true.\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitleDisplay( { id } ) {\n * const { record, isResolving } = useEntityRecord( 'postType', 'page', id );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return record.title;\n * }\n *\n * // Rendered in the application:\n * // <PageTitleDisplay id={ 1 } />\n * ```\n *\n * In the above example, when `PageTitleDisplay` is rendered into an\n * application, the page and the resolution details will be retrieved from\n * the store state using `getEntityRecord()`, or resolved if missing.\n *\n * @return {EntityRecordResolution<RecordType>} Entity record data.\n * @template RecordType\n */\nexport default function __experimentalUseEntityRecord< RecordType >(\n\tkind: string,\n\tname: string,\n\trecordId: string | number,\n\toptions: Options = { enabled: true }\n): EntityRecordResolution< RecordType > {\n\tconst { data: record, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecord( kind, name, recordId );\n\t\t},\n\t\t[ kind, name, recordId, options.enabled ]\n\t);\n\n\treturn {\n\t\trecord,\n\t\t...rest,\n\t};\n}\n"]}
@@ -15,6 +15,7 @@ import { store as coreStore } from '../';
15
15
  * @param kind Kind of the requested entities.
16
16
  * @param name Name of the requested entities.
17
17
  * @param queryArgs HTTP query for the requested entities.
18
+ * @param options Hook options.
18
19
  * @example
19
20
  * ```js
20
21
  * import { useEntityRecord } from '@wordpress/core-data';
@@ -43,11 +44,14 @@ import { store as coreStore } from '../';
43
44
  * application, the list of records and the resolution details will be retrieved from
44
45
  * the store state using `getEntityRecords()`, or resolved if missing.
45
46
  *
46
- * @return {EntityRecordsResolution<RecordType>} Entity records data.
47
+ * @return Entity records data.
47
48
  * @template RecordType
48
49
  */
49
50
  export default function __experimentalUseEntityRecords(kind, name) {
50
51
  let queryArgs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
52
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
53
+ enabled: true
54
+ };
51
55
  // Serialize queryArgs to a string that can be safely used as a React dep.
52
56
  // We can't just pass queryArgs as one of the deps, because if it is passed
53
57
  // as an object literal, then it will be a different object on each call even
@@ -56,7 +60,15 @@ export default function __experimentalUseEntityRecords(kind, name) {
56
60
  const {
57
61
  data: records,
58
62
  ...rest
59
- } = useQuerySelect(query => query(coreStore).getEntityRecords(kind, name, queryArgs), [kind, name, queryAsString]);
63
+ } = useQuerySelect(query => {
64
+ if (!options.enabled) {
65
+ return {
66
+ data: []
67
+ };
68
+ }
69
+
70
+ return query(coreStore).getEntityRecords(kind, name, queryArgs);
71
+ }, [kind, name, queryAsString, options.enabled]);
60
72
  return {
61
73
  records,
62
74
  ...rest
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-records.ts"],"names":["addQueryArgs","useQuerySelect","store","coreStore","__experimentalUseEntityRecords","kind","name","queryArgs","queryAsString","data","records","rest","query","getEntityRecords"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAT,QAA6B,gBAA7B;AAEA;AACA;AACA;;AACA,OAAOC,cAAP,MAA2B,oBAA3B;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,KAAnC;;AAqBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,8BAAT,CACdC,IADc,EAEdC,IAFc,EAI0B;AAAA,MADxCC,SACwC,uEADnB,EACmB;AACxC;AACA;AACA;AACA;AACA,QAAMC,aAAa,GAAGR,YAAY,CAAE,EAAF,EAAMO,SAAN,CAAlC;AAEA,QAAM;AAAEE,IAAAA,IAAI,EAAEC,OAAR;AAAiB,OAAGC;AAApB,MAA6BV,cAAc,CAC9CW,KAAF,IACCA,KAAK,CAAET,SAAF,CAAL,CAAmBU,gBAAnB,CAAqCR,IAArC,EAA2CC,IAA3C,EAAiDC,SAAjD,CAF+C,EAGhD,CAAEF,IAAF,EAAQC,IAAR,EAAcE,aAAd,CAHgD,CAAjD;AAMA,SAAO;AACNE,IAAAA,OADM;AAEN,OAAGC;AAFG,GAAP;AAIA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\n\ninterface EntityRecordsResolution< RecordType > {\n\t/** The requested entity record */\n\trecords: RecordType[] | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n}\n\n/**\n * Resolves the specified entity records.\n *\n * @param kind Kind of the requested entities.\n * @param name Name of the requested entities.\n * @param queryArgs HTTP query for the requested entities.\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return {EntityRecordsResolution<RecordType>} Entity records data.\n * @template RecordType\n */\nexport default function __experimentalUseEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: unknown = {}\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) =>\n\t\t\tquery( coreStore ).getEntityRecords( kind, name, queryArgs ),\n\t\t[ kind, name, queryAsString ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\t...rest,\n\t};\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-records.ts"],"names":["addQueryArgs","useQuerySelect","store","coreStore","__experimentalUseEntityRecords","kind","name","queryArgs","options","enabled","queryAsString","data","records","rest","query","getEntityRecords"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAT,QAA6B,gBAA7B;AAEA;AACA;AACA;;AACA,OAAOC,cAAP,MAA2B,oBAA3B;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,KAAnC;;AA8BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,8BAAT,CACdC,IADc,EAEdC,IAFc,EAK0B;AAAA,MAFxCC,SAEwC,uEAFD,EAEC;AAAA,MADxCC,OACwC,uEADrB;AAAEC,IAAAA,OAAO,EAAE;AAAX,GACqB;AACxC;AACA;AACA;AACA;AACA,QAAMC,aAAa,GAAGV,YAAY,CAAE,EAAF,EAAMO,SAAN,CAAlC;AAEA,QAAM;AAAEI,IAAAA,IAAI,EAAEC,OAAR;AAAiB,OAAGC;AAApB,MAA6BZ,cAAc,CAC9Ca,KAAF,IAAa;AACZ,QAAK,CAAEN,OAAO,CAACC,OAAf,EAAyB;AACxB,aAAO;AACNE,QAAAA,IAAI,EAAE;AADA,OAAP;AAGA;;AACD,WAAOG,KAAK,CAAEX,SAAF,CAAL,CAAmBY,gBAAnB,CAAqCV,IAArC,EAA2CC,IAA3C,EAAiDC,SAAjD,CAAP;AACA,GAR+C,EAShD,CAAEF,IAAF,EAAQC,IAAR,EAAcI,aAAd,EAA6BF,OAAO,CAACC,OAArC,CATgD,CAAjD;AAYA,SAAO;AACNG,IAAAA,OADM;AAEN,OAAGC;AAFG,GAAP;AAIA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Status } from './constants';\n\ninterface EntityRecordsResolution< RecordType > {\n\t/** The requested entity record */\n\trecords: RecordType[] | null;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n}\n\ninterface Options {\n\t/**\n\t * Whether to run the query or short-circuit and return null.\n\t *\n\t * @default true\n\t */\n\tenabled: boolean;\n}\n\n/**\n * Resolves the specified entity records.\n *\n * @param kind Kind of the requested entities.\n * @param name Name of the requested entities.\n * @param queryArgs HTTP query for the requested entities.\n * @param options Hook options.\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template RecordType\n */\nexport default function __experimentalUseEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\tdata: [],\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecords( kind, name, queryArgs );\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\t...rest,\n\t};\n}\n"]}