@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
8 lines (7 loc) • 2.9 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/fetch/__experimental-fetch-url-data.js"],
"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"],
"mappings": ";AAGA,OAAO,cAAc;AACrB;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAOP,IAAM,QAAQ,oBAAI,IAAI;AA6BtB,IAAM,eAAe,OAAQ,KAAK,UAAU,CAAC,MAAO;AACnD,QAAM,WAAW;AAEjB,QAAM,OAAO;AAAA,IACZ,KAAK,YAAa,GAAI;AAAA,EACvB;AAEA,MAAK,CAAE,MAAO,GAAI,GAAI;AACrB,WAAO,QAAQ,OAAQ,GAAI,GAAI,sBAAuB;AAAA,EACvD;AAIA,QAAM,WAAW,YAAa,GAAI;AAElC,MACC,CAAE,YACF,CAAE,gBAAiB,QAAS,KAC5B,CAAE,SAAS,WAAY,MAAO,KAC9B,CAAE,uBAAuB,KAAM,GAAI,GAClC;AACD,WAAO,QAAQ;AAAA,MACd,GAAI,GAAI;AAAA,IACT;AAAA,EACD;AAEA,MAAK,MAAM,IAAK,GAAI,GAAI;AACvB,WAAO,MAAM,IAAK,GAAI;AAAA,EACvB;AAEA,SAAO,SAAU;AAAA,IAChB,MAAM,aAAc,UAAU,IAAK;AAAA,IACnC,GAAG;AAAA,EACJ,CAAE,EAAE,KAAM,CAAE,QAAS;AACpB,UAAM,IAAK,KAAK,GAAI;AACpB,WAAO;AAAA,EACR,CAAE;AACH;AAEA,IAAO,sCAAQ;",
"names": []
}