Socket
Book a DemoSign in
Socket

@wordpress/api-fetch

Package Overview
Dependencies
Maintainers
23
Versions
262
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wordpress/api-fetch - npm Package Compare versions

Comparing version
5.2.4
to
5.2.5
+16
-27
build-module/middlewares/preloading.js
/**
* Given a path, returns a normalized path where equal query parameter values
* will be treated as identical, regardless of order they appear in the original
* text.
*
* @param {string} path Original path.
*
* @return {string} Normalized path.
* WordPress dependencies
*/
export function getStablePath(path) {
const splitted = path.split('?');
const query = splitted[1];
const base = splitted[0];
if (!query) {
return base;
} // 'b=1&c=2&a=5'
return base + '?' + query // [ 'b=1', 'c=2', 'a=5' ]
.split('&') // [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]
.map(entry => entry.split('=')) // [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]
.sort((a, b) => a[0].localeCompare(b[0])) // [ 'a=5', 'b=1', 'c=2' ]
.map(pair => pair.join('=')) // 'a=5&b=1&c=2'
.join('&');
}
import { getQueryArg, normalizePath } from '@wordpress/url';
/**

@@ -34,3 +12,3 @@ * @param {Record<string, any>} preloadedData

const cache = Object.keys(preloadedData).reduce((result, path) => {
result[getStablePath(path)] = preloadedData[path];
result[normalizePath(path)] = preloadedData[path];
return result;

@@ -44,6 +22,17 @@ },

} = options;
/** @type {string | void} */
if (typeof options.path === 'string') {
let rawPath = options.path;
if (!rawPath && options.url) {
const pathFromQuery = getQueryArg(options.url, 'rest_route');
if (typeof pathFromQuery === 'string') {
rawPath = pathFromQuery;
}
}
if (typeof rawPath === 'string') {
const method = options.method || 'GET';
const path = getStablePath(options.path);
const path = normalizePath(rawPath);

@@ -50,0 +39,0 @@ if ('GET' === method && cache[path]) {

@@ -1,1 +0,1 @@

{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["getStablePath","path","splitted","split","query","base","map","entry","sort","a","b","localeCompare","pair","join","createPreloadingMiddleware","preloadedData","cache","Object","keys","reduce","result","options","next","parse","method","cacheData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAT,CAAwBC,IAAxB,EAA+B;AACrC,QAAMC,QAAQ,GAAGD,IAAI,CAACE,KAAL,CAAY,GAAZ,CAAjB;AACA,QAAMC,KAAK,GAAGF,QAAQ,CAAE,CAAF,CAAtB;AACA,QAAMG,IAAI,GAAGH,QAAQ,CAAE,CAAF,CAArB;;AACA,MAAK,CAAEE,KAAP,EAAe;AACd,WAAOC,IAAP;AACA,GANoC,CAQrC;;;AACA,SACCA,IAAI,GACJ,GADA,GAEAD,KAAK,CACJ;AADI,GAEHD,KAFF,CAES,GAFT,EAGC;AAHD,GAIEG,GAJF,CAISC,KAAF,IAAaA,KAAK,CAACJ,KAAN,CAAa,GAAb,CAJpB,EAKC;AALD,GAMEK,IANF,CAMQ,CAAEC,CAAF,EAAKC,CAAL,KAAYD,CAAC,CAAE,CAAF,CAAD,CAAOE,aAAP,CAAsBD,CAAC,CAAE,CAAF,CAAvB,CANpB,EAOC;AAPD,GAQEJ,GARF,CAQSM,IAAF,IAAYA,IAAI,CAACC,IAAL,CAAW,GAAX,CARnB,EASC;AATD,GAUEA,IAVF,CAUQ,GAVR,CAHD;AAeA;AAED;AACA;AACA;AACA;;AACA,SAASC,0BAAT,CAAqCC,aAArC,EAAqD;AACpD,QAAMC,KAAK,GAAGC,MAAM,CAACC,IAAP,CAAaH,aAAb,EAA6BI,MAA7B,CAAqC,CAAEC,MAAF,EAAUnB,IAAV,KAAoB;AACtEmB,IAAAA,MAAM,CAAEpB,aAAa,CAAEC,IAAF,CAAf,CAAN,GAAkCc,aAAa,CAAEd,IAAF,CAA/C;AACA,WAAOmB,MAAP;AACA,GAHa;AAGX;AAAqC,IAH1B,CAAd;AAKA,SAAO,CAAEC,OAAF,EAAWC,IAAX,KAAqB;AAC3B,UAAM;AAAEC,MAAAA,KAAK,GAAG;AAAV,QAAmBF,OAAzB;;AACA,QAAK,OAAOA,OAAO,CAACpB,IAAf,KAAwB,QAA7B,EAAwC;AACvC,YAAMuB,MAAM,GAAGH,OAAO,CAACG,MAAR,IAAkB,KAAjC;AACA,YAAMvB,IAAI,GAAGD,aAAa,CAAEqB,OAAO,CAACpB,IAAV,CAA1B;;AAEA,UAAK,UAAUuB,MAAV,IAAoBR,KAAK,CAAEf,IAAF,CAA9B,EAAyC;AACxC,cAAMwB,SAAS,GAAGT,KAAK,CAAEf,IAAF,CAAvB,CADwC,CAGxC;;AACA,eAAOe,KAAK,CAAEf,IAAF,CAAZ;AAEA,eAAOyB,OAAO,CAACC,OAAR,CACNJ,KAAK,GACFE,SAAS,CAACG,IADR,GAEF,IAAIC,MAAM,CAACC,QAAX,CACAC,IAAI,CAACC,SAAL,CAAgBP,SAAS,CAACG,IAA1B,CADA,EAEA;AACCK,UAAAA,MAAM,EAAE,GADT;AAECC,UAAAA,UAAU,EAAE,IAFb;AAGCC,UAAAA,OAAO,EAAEV,SAAS,CAACU;AAHpB,SAFA,CAHG,CAAP;AAYA,OAlBD,MAkBO,IACN,cAAcX,MAAd,IACAR,KAAK,CAAEQ,MAAF,CADL,IAEAR,KAAK,CAAEQ,MAAF,CAAL,CAAiBvB,IAAjB,CAHM,EAIL;AACD,cAAMwB,SAAS,GAAGT,KAAK,CAAEQ,MAAF,CAAL,CAAiBvB,IAAjB,CAAlB,CADC,CAGD;;AACA,eAAOe,KAAK,CAAEQ,MAAF,CAAL,CAAiBvB,IAAjB,CAAP;AAEA,eAAOyB,OAAO,CAACC,OAAR,CAAiBJ,KAAK,GAAGE,SAAS,CAACG,IAAb,GAAoBH,SAA1C,CAAP;AACA;AACD;;AAED,WAAOH,IAAI,CAAED,OAAF,CAAX;AACA,GAvCD;AAwCA;;AAED,eAAeP,0BAAf","sourcesContent":["/**\n * Given a path, returns a normalized path where equal query parameter values\n * will be treated as identical, regardless of order they appear in the original\n * text.\n *\n * @param {string} path Original path.\n *\n * @return {string} Normalized path.\n */\nexport function getStablePath( path ) {\n\tconst splitted = path.split( '?' );\n\tconst query = splitted[ 1 ];\n\tconst base = splitted[ 0 ];\n\tif ( ! query ) {\n\t\treturn base;\n\t}\n\n\t// 'b=1&c=2&a=5'\n\treturn (\n\t\tbase +\n\t\t'?' +\n\t\tquery\n\t\t\t// [ 'b=1', 'c=2', 'a=5' ]\n\t\t\t.split( '&' )\n\t\t\t// [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]\n\t\t\t.map( ( entry ) => entry.split( '=' ) )\n\t\t\t// [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]\n\t\t\t.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) )\n\t\t\t// [ 'a=5', 'b=1', 'c=2' ]\n\t\t\t.map( ( pair ) => pair.join( '=' ) )\n\t\t\t// 'a=5&b=1&c=2'\n\t\t\t.join( '&' )\n\t);\n}\n\n/**\n * @param {Record<string, any>} preloadedData\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nfunction createPreloadingMiddleware( preloadedData ) {\n\tconst cache = Object.keys( preloadedData ).reduce( ( result, path ) => {\n\t\tresult[ getStablePath( path ) ] = preloadedData[ path ];\n\t\treturn result;\n\t}, /** @type {Record<string, any>} */ ( {} ) );\n\n\treturn ( options, next ) => {\n\t\tconst { parse = true } = options;\n\t\tif ( typeof options.path === 'string' ) {\n\t\t\tconst method = options.method || 'GET';\n\t\t\tconst path = getStablePath( options.path );\n\n\t\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ path ];\n\n\t\t\t\treturn Promise.resolve(\n\t\t\t\t\tparse\n\t\t\t\t\t\t? cacheData.body\n\t\t\t\t\t\t: new window.Response(\n\t\t\t\t\t\t\t\tJSON.stringify( cacheData.body ),\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tstatus: 200,\n\t\t\t\t\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\t\t\t\t\theaders: cacheData.headers,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t )\n\t\t\t\t);\n\t\t\t} else if (\n\t\t\t\t'OPTIONS' === method &&\n\t\t\t\tcache[ method ] &&\n\t\t\t\tcache[ method ][ path ]\n\t\t\t) {\n\t\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ method ][ path ];\n\n\t\t\t\treturn Promise.resolve( parse ? cacheData.body : cacheData );\n\t\t\t}\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\nexport default createPreloadingMiddleware;\n"]}
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["getQueryArg","normalizePath","createPreloadingMiddleware","preloadedData","cache","Object","keys","reduce","result","path","options","next","parse","rawPath","url","pathFromQuery","method","cacheData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAT,EAAsBC,aAAtB,QAA2C,gBAA3C;AAEA;AACA;AACA;AACA;;AACA,SAASC,0BAAT,CAAqCC,aAArC,EAAqD;AACpD,QAAMC,KAAK,GAAGC,MAAM,CAACC,IAAP,CAAaH,aAAb,EAA6BI,MAA7B,CAAqC,CAAEC,MAAF,EAAUC,IAAV,KAAoB;AACtED,IAAAA,MAAM,CAAEP,aAAa,CAAEQ,IAAF,CAAf,CAAN,GAAkCN,aAAa,CAAEM,IAAF,CAA/C;AACA,WAAOD,MAAP;AACA,GAHa;AAGX;AAAqC,IAH1B,CAAd;AAKA,SAAO,CAAEE,OAAF,EAAWC,IAAX,KAAqB;AAC3B,UAAM;AAAEC,MAAAA,KAAK,GAAG;AAAV,QAAmBF,OAAzB;AACA;;AACA,QAAIG,OAAO,GAAGH,OAAO,CA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;AACD;;AAED,WAAON,IAAI,CAAED,OAAF,CAAX;AACA,GA/CD;AAgDA;;AAED,eAAeR,0BAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { getQueryArg, normalizePath } from '@wordpress/url';\n\n/**\n * @param {Record<string, any>} preloadedData\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nfunction createPreloadingMiddleware( preloadedData ) {\n\tconst cache = Object.keys( preloadedData ).reduce( ( result, path ) => {\n\t\tresult[ normalizePath( path ) ] = preloadedData[ path ];\n\t\treturn result;\n\t}, /** @type {Record<string, any>} */ ( {} ) );\n\n\treturn ( options, next ) => {\n\t\tconst { parse = true } = options;\n\t\t/** @type {string | void} */\n\t\tlet rawPath = options.path;\n\t\tif ( ! rawPath && options.url ) {\n\t\t\tconst pathFromQuery = getQueryArg( options.url, 'rest_route' );\n\t\t\tif ( typeof pathFromQuery === 'string' ) {\n\t\t\t\trawPath = pathFromQuery;\n\t\t\t}\n\t\t}\n\t\tif ( typeof rawPath === 'string' ) {\n\t\t\tconst method = options.method || 'GET';\n\t\t\tconst path = normalizePath( rawPath );\n\n\t\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ path ];\n\n\t\t\t\treturn Promise.resolve(\n\t\t\t\t\tparse\n\t\t\t\t\t\t? cacheData.body\n\t\t\t\t\t\t: new window.Response(\n\t\t\t\t\t\t\t\tJSON.stringify( cacheData.body ),\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tstatus: 200,\n\t\t\t\t\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\t\t\t\t\theaders: cacheData.headers,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t )\n\t\t\t\t);\n\t\t\t} else if (\n\t\t\t\t'OPTIONS' === method &&\n\t\t\t\tcache[ method ] &&\n\t\t\t\tcache[ method ][ path ]\n\t\t\t) {\n\t\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ method ][ path ];\n\n\t\t\t\treturn Promise.resolve( parse ? cacheData.body : cacheData );\n\t\t\t}\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\nexport default createPreloadingMiddleware;\n"]}

@@ -1,11 +0,1 @@

/**
* Given a path, returns a normalized path where equal query parameter values
* will be treated as identical, regardless of order they appear in the original
* text.
*
* @param {string} path Original path.
*
* @return {string} Normalized path.
*/
export function getStablePath(path: string): string;
export default createPreloadingMiddleware;

@@ -12,0 +2,0 @@ /**

@@ -1,1 +0,1 @@

{"version":3,"file":"preloading.d.ts","sourceRoot":"","sources":["../../src/middlewares/preloading.js"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,oCAJW,MAAM,GAEL,MAAM,CA0BjB;;AAED;;;GAGG;AACH,2DAHW,OAAO,MAAM,EAAE,GAAG,CAAC,GAClB,OAAO,UAAU,EAAE,kBAAkB,CAgDhD"}
{"version":3,"file":"preloading.d.ts","sourceRoot":"","sources":["../../src/middlewares/preloading.js"],"names":[],"mappings":";AAKA;;;GAGG;AACH,2DAHW,OAAO,MAAM,EAAE,GAAG,CAAC,GAClB,OAAO,UAAU,EAAE,kBAAkB,CAwDhD"}

@@ -6,31 +6,10 @@ "use strict";

});
exports.getStablePath = getStablePath;
exports.default = void 0;
var _url = require("@wordpress/url");
/**
* Given a path, returns a normalized path where equal query parameter values
* will be treated as identical, regardless of order they appear in the original
* text.
*
* @param {string} path Original path.
*
* @return {string} Normalized path.
* WordPress dependencies
*/
function getStablePath(path) {
const splitted = path.split('?');
const query = splitted[1];
const base = splitted[0];
if (!query) {
return base;
} // 'b=1&c=2&a=5'
return base + '?' + query // [ 'b=1', 'c=2', 'a=5' ]
.split('&') // [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]
.map(entry => entry.split('=')) // [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]
.sort((a, b) => a[0].localeCompare(b[0])) // [ 'a=5', 'b=1', 'c=2' ]
.map(pair => pair.join('=')) // 'a=5&b=1&c=2'
.join('&');
}
/**

@@ -40,7 +19,5 @@ * @param {Record<string, any>} preloadedData

*/
function createPreloadingMiddleware(preloadedData) {
const cache = Object.keys(preloadedData).reduce((result, path) => {
result[getStablePath(path)] = preloadedData[path];
result[(0, _url.normalizePath)(path)] = preloadedData[path];
return result;

@@ -54,6 +31,17 @@ },

} = options;
/** @type {string | void} */
if (typeof options.path === 'string') {
let rawPath = options.path;
if (!rawPath && options.url) {
const pathFromQuery = (0, _url.getQueryArg)(options.url, 'rest_route');
if (typeof pathFromQuery === 'string') {
rawPath = pathFromQuery;
}
}
if (typeof rawPath === 'string') {
const method = options.method || 'GET';
const path = getStablePath(options.path);
const path = (0, _url.normalizePath)(rawPath);

@@ -60,0 +48,0 @@ if ('GET' === method && cache[path]) {

@@ -1,1 +0,1 @@

{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["getStablePath","path","splitted","split","query","base","map","entry","sort","a","b","localeCompare","pair","join","createPreloadingMiddleware","preloadedData","cache","Object","keys","reduce","result","options","next","parse","method","cacheData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,aAAT,CAAwBC,IAAxB,EAA+B;AACrC,QAAMC,QAAQ,GAAGD,IAAI,CAACE,KAAL,CAAY,GAAZ,CAAjB;AACA,QAAMC,KAAK,GAAGF,QAAQ,CAAE,CAAF,CAAtB;AACA,QAAMG,IAAI,GAAGH,QAAQ,CAAE,CAAF,CAArB;;AACA,MAAK,CAAEE,KAAP,EAAe;AACd,WAAOC,IAAP;AACA,GANoC,CAQrC;;;AACA,SACCA,IAAI,GACJ,GADA,GAEAD,KAAK,CACJ;AADI,GAEHD,KAFF,CAES,GAFT,EAGC;AAHD,GAI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,QAAX,CACAC,IAAI,CAACC,SAAL,CAAgBP,SAAS,CAACG,IAA1B,CADA,EAEA;AACCK,UAAAA,MAAM,EAAE,GADT;AAECC,UAAAA,UAAU,EAAE,IAFb;AAGCC,UAAAA,OAAO,EAAEV,SAAS,CAACU;AAHpB,SAFA,CAHG,CAAP;AAYA,OAlBD,MAkBO,IACN,cAAcX,MAAd,IACAR,KAAK,CAAEQ,MAAF,CADL,IAEAR,KAAK,CAAEQ,MAAF,CAAL,CAAiBvB,IAAjB,CAHM,EAIL;AACD,cAAMwB,SAAS,GAAGT,KAAK,CAAEQ,MAAF,CAAL,CAAiBvB,IAAjB,CAAlB,CADC,CAGD;;AACA,eAAOe,KAAK,CAAEQ,MAAF,CAAL,CAAiBvB,IAAjB,CAAP;AAEA,eAAOyB,OAAO,CAACC,OAAR,CAAiBJ,KAAK,GAAGE,SAAS,CAACG,IAAb,GAAoBH,SAA1C,CAAP;AACA;AACD;;AAED,WAAOH,IAAI,CAAED,OAAF,CAAX;AACA,GAvCD;AAwCA;;eAEcP,0B","sourcesContent":["/**\n * Given a path, returns a normalized path where equal query parameter values\n * will be treated as identical, regardless of order they appear in the original\n * text.\n *\n * @param {string} path Original path.\n *\n * @return {string} Normalized path.\n */\nexport function getStablePath( path ) {\n\tconst splitted = path.split( '?' );\n\tconst query = splitted[ 1 ];\n\tconst base = splitted[ 0 ];\n\tif ( ! query ) {\n\t\treturn base;\n\t}\n\n\t// 'b=1&c=2&a=5'\n\treturn (\n\t\tbase +\n\t\t'?' +\n\t\tquery\n\t\t\t// [ 'b=1', 'c=2', 'a=5' ]\n\t\t\t.split( '&' )\n\t\t\t// [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]\n\t\t\t.map( ( entry ) => entry.split( '=' ) )\n\t\t\t// [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]\n\t\t\t.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) )\n\t\t\t// [ 'a=5', 'b=1', 'c=2' ]\n\t\t\t.map( ( pair ) => pair.join( '=' ) )\n\t\t\t// 'a=5&b=1&c=2'\n\t\t\t.join( '&' )\n\t);\n}\n\n/**\n * @param {Record<string, any>} preloadedData\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nfunction createPreloadingMiddleware( preloadedData ) {\n\tconst cache = Object.keys( preloadedData ).reduce( ( result, path ) => {\n\t\tresult[ getStablePath( path ) ] = preloadedData[ path ];\n\t\treturn result;\n\t}, /** @type {Record<string, any>} */ ( {} ) );\n\n\treturn ( options, next ) => {\n\t\tconst { parse = true } = options;\n\t\tif ( typeof options.path === 'string' ) {\n\t\t\tconst method = options.method || 'GET';\n\t\t\tconst path = getStablePath( options.path );\n\n\t\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ path ];\n\n\t\t\t\treturn Promise.resolve(\n\t\t\t\t\tparse\n\t\t\t\t\t\t? cacheData.body\n\t\t\t\t\t\t: new window.Response(\n\t\t\t\t\t\t\t\tJSON.stringify( cacheData.body ),\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tstatus: 200,\n\t\t\t\t\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\t\t\t\t\theaders: cacheData.headers,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t )\n\t\t\t\t);\n\t\t\t} else if (\n\t\t\t\t'OPTIONS' === method &&\n\t\t\t\tcache[ method ] &&\n\t\t\t\tcache[ method ][ path ]\n\t\t\t) {\n\t\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ method ][ path ];\n\n\t\t\t\treturn Promise.resolve( parse ? cacheData.body : cacheData );\n\t\t\t}\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\nexport default createPreloadingMiddleware;\n"]}
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["createPreloadingMiddleware","preloadedData","cache","Object","keys","reduce","result","path","options","next","parse","rawPath","url","pathFromQuery","method","cacheData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,SAASA,0BAAT,CAAqCC,aAArC,EAAqD;AACpD,QAAMC,KAAK,GAAGC,MAAM,CAACC,IAAP,CAAaH,aAAb,EAA6BI,MAA7B,CAAqC,CAAEC,MAAF,EAAUC,IAAV,KAAoB;AACtED,IAAAA,MAAM,CAAE,wBAAeC,IAAf,CAAF,CAAN,GAAkCN,aAAa,CAAEM,IAAF,CAA/C;AACA,WAAOD,MAAP;AACA,GAHa;AAGX;AAAqC,IAH1B,CAAd;AAKA,SAAO,CAAEE,OAAF,EAAWC,IAAX,KAAqB;AAC3B,UAAM;AAAEC,MAAAA,KAAK,GAAG;AAAV,QAAmBF,OAAzB;AACA;;AACA,QAAIG,OAAO,GAAGH,OAAO,CAACD,IAAtB;;AACA,QAAK,CAAEI,OAAF,IAAaH,OAAO,CAACI,GAA1B,EAAgC;AAC/B,YAAMC,aAAa,GAAG,sBAAaL,OAAO,CAACI,GAArB,EAA0B,YAA1B,CAAtB;;AACA,UAAK,OAAOC,aAAP,KAAyB,QAA9B,EAAyC;AACxCF,QAAAA,OAAO,GAAGE,aAAV;AACA;AACD;;AACD,QAAK,OAAOF,OAAP,KAAmB,QAAxB,EAAmC;AAClC,YAAMG,MAAM,GAAGN,OAAO,CAACM,MAAR,IAAkB,KAAjC;AACA,YAAMP,IAAI,GAAG,wBAAeI,OAAf,CAAb;;AAEA,UAAK,UAAUG,MAAV,IAAoBZ,KAAK,CAAEK,IAAF,CAA9B,EAAyC;AACxC,cAAMQ,SAAS,GAAGb,KAAK,CAAEK,IAAF,CAAvB,CADwC,CAGxC;;AACA,eAAOL,KAAK,CAAEK,IAAF,CAAZ;AAEA,eAAOS,OAAO,CAACC,OAAR,CACNP,KAAK,GACFK,SAAS,CAACG,IADR,GAEF,IAAIC,MAAM,CAACC,QAAX,CACAC,IAAI,CAACC,SAAL,CAAgBP,SAAS,CAACG,IAA1B,CADA,EAEA;AACCK,UAAAA,MAAM,EAAE,GADT;AAECC,UAAAA,UAAU,EAAE,IAFb;AAGCC,UAAAA,OAAO,EAAEV,SAAS,CAACU;AAHpB,SAFA,CAHG,CAAP;AAYA,OAlBD,MAkBO,IACN,cAAcX,MAAd,IACAZ,KAAK,CAAEY,MAAF,CADL,IAEAZ,KAAK,CAAEY,MAAF,CAAL,CAAiBP,IAAjB,CAHM,EAIL;AACD,cAAMQ,SAAS,GAAGb,KAAK,CAAEY,MAAF,CAAL,CAAiBP,IAAjB,CAAlB,CADC,CAGD;;AACA,eAAOL,KAAK,CAAEY,MAAF,CAAL,CAAiBP,IAAjB,CAAP;AAEA,eAAOS,OAAO,CAACC,OAAR,CAAiBP,KAAK,GAAGK,SAAS,CAACG,IAAb,GAAoBH,SAA1C,CAAP;AACA;AACD;;AAED,WAAON,IAAI,CAAED,OAAF,CAAX;AACA,GA/CD;AAgDA;;eAEcR,0B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { getQueryArg, normalizePath } from '@wordpress/url';\n\n/**\n * @param {Record<string, any>} preloadedData\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nfunction createPreloadingMiddleware( preloadedData ) {\n\tconst cache = Object.keys( preloadedData ).reduce( ( result, path ) => {\n\t\tresult[ normalizePath( path ) ] = preloadedData[ path ];\n\t\treturn result;\n\t}, /** @type {Record<string, any>} */ ( {} ) );\n\n\treturn ( options, next ) => {\n\t\tconst { parse = true } = options;\n\t\t/** @type {string | void} */\n\t\tlet rawPath = options.path;\n\t\tif ( ! rawPath && options.url ) {\n\t\t\tconst pathFromQuery = getQueryArg( options.url, 'rest_route' );\n\t\t\tif ( typeof pathFromQuery === 'string' ) {\n\t\t\t\trawPath = pathFromQuery;\n\t\t\t}\n\t\t}\n\t\tif ( typeof rawPath === 'string' ) {\n\t\t\tconst method = options.method || 'GET';\n\t\t\tconst path = normalizePath( rawPath );\n\n\t\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ path ];\n\n\t\t\t\treturn Promise.resolve(\n\t\t\t\t\tparse\n\t\t\t\t\t\t? cacheData.body\n\t\t\t\t\t\t: new window.Response(\n\t\t\t\t\t\t\t\tJSON.stringify( cacheData.body ),\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tstatus: 200,\n\t\t\t\t\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\t\t\t\t\theaders: cacheData.headers,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t )\n\t\t\t\t);\n\t\t\t} else if (\n\t\t\t\t'OPTIONS' === method &&\n\t\t\t\tcache[ method ] &&\n\t\t\t\tcache[ method ][ path ]\n\t\t\t) {\n\t\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t\t// Unsetting the cache key ensures that the data is only used a single time\n\t\t\t\tdelete cache[ method ][ path ];\n\n\t\t\t\treturn Promise.resolve( parse ? cacheData.body : cacheData );\n\t\t\t}\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\nexport default createPreloadingMiddleware;\n"]}

@@ -5,2 +5,8 @@ <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## 5.2.5 (2021-11-07)
### Internal
- Removed `getStablePath` function. Please use `normalizePath` from `@wordpress/url` package instead ([#35992](https://github.com/WordPress/gutenberg/pull/35992)).``
## 5.2.0 (2021-07-21)

@@ -7,0 +13,0 @@

{
"name": "@wordpress/api-fetch",
"version": "5.2.4",
"version": "5.2.5",
"description": "Utility to make WordPress REST API requests.",

@@ -32,3 +32,3 @@ "author": "The WordPress Contributors",

"@wordpress/i18n": "^4.2.3",
"@wordpress/url": "^3.2.3"
"@wordpress/url": "^3.3.0"
},

@@ -38,3 +38,3 @@ "publishConfig": {

},
"gitHead": "157f4ae53ab98e574af01f72213ae5a9613159ff"
"gitHead": "393c2b5533837fd637e998d23f0124c081a10df0"
}
/**
* Given a path, returns a normalized path where equal query parameter values
* will be treated as identical, regardless of order they appear in the original
* text.
*
* @param {string} path Original path.
*
* @return {string} Normalized path.
* WordPress dependencies
*/
export function getStablePath( path ) {
const splitted = path.split( '?' );
const query = splitted[ 1 ];
const base = splitted[ 0 ];
if ( ! query ) {
return base;
}
import { getQueryArg, normalizePath } from '@wordpress/url';
// 'b=1&c=2&a=5'
return (
base +
'?' +
query
// [ 'b=1', 'c=2', 'a=5' ]
.split( '&' )
// [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]
.map( ( entry ) => entry.split( '=' ) )
// [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]
.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) )
// [ 'a=5', 'b=1', 'c=2' ]
.map( ( pair ) => pair.join( '=' ) )
// 'a=5&b=1&c=2'
.join( '&' )
);
}
/**

@@ -42,3 +12,3 @@ * @param {Record<string, any>} preloadedData

const cache = Object.keys( preloadedData ).reduce( ( result, path ) => {
result[ getStablePath( path ) ] = preloadedData[ path ];
result[ normalizePath( path ) ] = preloadedData[ path ];
return result;

@@ -49,5 +19,13 @@ }, /** @type {Record<string, any>} */ ( {} ) );

const { parse = true } = options;
if ( typeof options.path === 'string' ) {
/** @type {string | void} */
let rawPath = options.path;
if ( ! rawPath && options.url ) {
const pathFromQuery = getQueryArg( options.url, 'rest_route' );
if ( typeof pathFromQuery === 'string' ) {
rawPath = pathFromQuery;
}
}
if ( typeof rawPath === 'string' ) {
const method = options.method || 'GET';
const path = getStablePath( options.path );
const path = normalizePath( rawPath );

@@ -54,0 +32,0 @@ if ( 'GET' === method && cache[ path ] ) {

/**
* Internal dependencies
*/
import createPreloadingMiddleware, { getStablePath } from '../preloading';
import createPreloadingMiddleware from '../preloading';
describe( 'Preloading Middleware', () => {
describe( 'getStablePath', () => {
it( 'returns same value if no query parameters', () => {
const path = '/foo/bar';
expect( getStablePath( path ) ).toBe( path );
} );
it( 'returns a stable path', () => {
const abc = getStablePath( '/foo/bar?a=5&b=1&c=2' );
const bca = getStablePath( '/foo/bar?b=1&c=2&a=5' );
const bac = getStablePath( '/foo/bar?b=1&a=5&c=2' );
const acb = getStablePath( '/foo/bar?a=5&c=2&b=1' );
const cba = getStablePath( '/foo/bar?c=2&b=1&a=5' );
const cab = getStablePath( '/foo/bar?c=2&a=5&b=1' );
expect( abc ).toBe( bca );
expect( bca ).toBe( bac );
expect( bac ).toBe( acb );
expect( acb ).toBe( cba );
expect( cba ).toBe( cab );
} );
} );
describe( 'given preloaded data', () => {

@@ -31,0 +8,0 @@ describe( 'when data is requested from a preloaded endpoint', () => {

Sorry, the diff of this file is not supported yet