-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental util to allow fetch remote url data from REST API (#…
…31085) * Add new helper to utilise new REST endpoint * Provide docblock
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/core-data/src/fetch/__experimental-fetch-remote-url-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters