Skip to content

Commit

Permalink
Add experimental util to allow fetch remote url data from REST API (#…
Browse files Browse the repository at this point in the history
…31085)

* Add new helper to utilise new REST endpoint

* Provide docblock
  • Loading branch information
getdave authored May 4, 2021
1 parent 1826160 commit 90f67f9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs, prependHTTP } from '@wordpress/url';

/**
* @typedef WPRemoteUrlData
*
* @property {string} title contents of the remote URL's `<title>` tag.
*/

/**
* Fetches data about a remote URL.
* eg: <title> tag, favicon...etc.
*
* @async
* @param {string} url
*
* @example
* ```js
* import { __experimentalFetchRemoteUrlData as fetchRemoteUrlData } from '@wordpress/core-data';
*
* //...
*
* export function initialize( id, settings ) {
*
* settings.__experimentalFetchRemoteUrlData = (
* url
* ) => fetchRemoteUrlData( url );
* ```
* @return {Promise< WPRemoteUrlData[] >} Remote URL data.
*/
const fetchRemoteUrlData = async ( url ) => {
const endpoint = '/__experimental/url-details';

const args = {
url: prependHTTP( url ),
};

return apiFetch( {
path: addQueryArgs( endpoint, args ),
} );
};

export default fetchRemoteUrlData;
1 change: 1 addition & 0 deletions packages/core-data/src/fetch/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as __experimentalFetchLinkSuggestions } from './__experimental-fetch-link-suggestions';
export { default as __experimentalFetchRemoteUrlData } from './__experimental-fetch-remote-url-data';
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
import {
store as coreStore,
__experimentalFetchLinkSuggestions as fetchLinkSuggestions,
__experimentalFetchRemoteUrlData as fetchRemoteUrlData,
} from '@wordpress/core-data';

/**
Expand Down Expand Up @@ -107,6 +108,8 @@ function useBlockEditorSettings( settings, hasTemplate ) {
__experimentalReusableBlocks: reusableBlocks,
__experimentalFetchLinkSuggestions: ( search, searchOptions ) =>
fetchLinkSuggestions( search, searchOptions, settings ),
__experimentalFetchRemoteUrlData: ( url ) =>
fetchRemoteUrlData( url ),
__experimentalCanUserUseUnfilteredHTML: canUseUnfilteredHTML,
__experimentalUndo: undo,
__experimentalShouldInsertAtTheTop: isTitleSelected,
Expand Down

0 comments on commit 90f67f9

Please sign in to comment.