@wordpress/api-fetch 6.30.0 → 6.32.0
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.
- package/CHANGELOG.md +4 -0
- package/build/middlewares/fetch-all-middleware.js +8 -11
- package/build/middlewares/fetch-all-middleware.js.map +1 -1
- package/build/middlewares/preloading.js +1 -4
- package/build/middlewares/preloading.js.map +1 -1
- package/build/utils/response.js +3 -8
- package/build/utils/response.js.map +1 -1
- package/build-module/middlewares/fetch-all-middleware.js +8 -11
- package/build-module/middlewares/fetch-all-middleware.js.map +1 -1
- package/build-module/middlewares/preloading.js +1 -4
- package/build-module/middlewares/preloading.js.map +1 -1
- package/build-module/utils/response.js +3 -8
- package/build-module/utils/response.js.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -26,17 +26,14 @@ var _ = _interopRequireDefault(require(".."));
|
|
|
26
26
|
* @param {Record<string, string | number>} queryArgs
|
|
27
27
|
* @return {import('../types').APIFetchOptions} The request with the modified query args
|
|
28
28
|
*/
|
|
29
|
-
const modifyQuery = (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
path: path && (0, _url.addQueryArgs)(path, queryArgs)
|
|
38
|
-
};
|
|
39
|
-
};
|
|
29
|
+
const modifyQuery = ({
|
|
30
|
+
path,
|
|
31
|
+
url,
|
|
32
|
+
...options
|
|
33
|
+
}, queryArgs) => ({ ...options,
|
|
34
|
+
url: url && (0, _url.addQueryArgs)(url, queryArgs),
|
|
35
|
+
path: path && (0, _url.addQueryArgs)(path, queryArgs)
|
|
36
|
+
});
|
|
40
37
|
/**
|
|
41
38
|
* Duplicates parsing functionality from apiFetch.
|
|
42
39
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/fetch-all-middleware.js"],"names":["modifyQuery","
|
|
1
|
+
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/fetch-all-middleware.js"],"names":["modifyQuery","path","url","options","queryArgs","parseResponse","response","json","Promise","reject","parseLinkHeader","linkHeader","match","next","getNextPageUrl","headers","get","requestContainsUnboundedQuery","pathIsUnbounded","indexOf","urlIsUnbounded","fetchAllMiddleware","parse","per_page","results","Array","isArray","nextPage","mergedResults","concat","nextResponse","undefined","nextResults"],"mappings":";;;;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,WAAW,GAAG,CAAE;AAAEC,EAAAA,IAAF;AAAQC,EAAAA,GAAR;AAAa,KAAGC;AAAhB,CAAF,EAA6BC,SAA7B,MAA8C,EACjE,GAAGD,OAD8D;AAEjED,EAAAA,GAAG,EAAEA,GAAG,IAAI,uBAAcA,GAAd,EAAmBE,SAAnB,CAFqD;AAGjEH,EAAAA,IAAI,EAAEA,IAAI,IAAI,uBAAcA,IAAd,EAAoBG,SAApB;AAHmD,CAA9C,CAApB;AAMA;AACA;AACA;AACA;AACA;AACA;;;AACA,MAAMC,aAAa,GAAKC,QAAF,IACrBA,QAAQ,CAACC,IAAT,GAAgBD,QAAQ,CAACC,IAAT,EAAhB,GAAkCC,OAAO,CAACC,MAAR,CAAgBH,QAAhB,CADnC;AAGA;AACA;AACA;AACA;;;AACA,MAAMI,eAAe,GAAKC,UAAF,IAAkB;AACzC,MAAK,CAAEA,UAAP,EAAoB;AACnB,WAAO,EAAP;AACA;;AACD,QAAMC,KAAK,GAAGD,UAAU,CAACC,KAAX,CAAkB,uBAAlB,CAAd;AACA,SAAOA,KAAK,GACT;AACAC,IAAAA,IAAI,EAAED,KAAK,CAAE,CAAF;AADX,GADS,GAIT,EAJH;AAKA,CAVD;AAYA;AACA;AACA;AACA;;;AACA,MAAME,cAAc,GAAKR,QAAF,IAAgB;AACtC,QAAM;AAAEO,IAAAA;AAAF,MAAWH,eAAe,CAAEJ,QA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D,CAiCrD;;;AACA,MAAII,aAAa;AAAG;AAAuB,IAAF,CAAOC,MAAP,CAAeL,OAAf,CAAzC;;AACA,SAAQG,QAAR,EAAmB;AAClB,UAAMG,YAAY,GAAG,MAAM,eAAU,EACpC,GAAG3B,OADiC;AAEpC;AACAF,MAAAA,IAAI,EAAE8B,SAH8B;AAIpC7B,MAAAA,GAAG,EAAEyB,QAJ+B;AAKpC;AACAL,MAAAA,KAAK,EAAE;AAN6B,KAAV,CAA3B;AAQA,UAAMU,WAAW,GAAG,MAAM3B,aAAa,CAAEyB,YAAF,CAAvC;AACAF,IAAAA,aAAa,GAAGA,aAAa,CAACC,MAAd,CAAsBG,WAAtB,CAAhB;AACAL,IAAAA,QAAQ,GAAGb,cAAc,CAAEgB,YAAF,CAAzB;AACA;;AACD,SAAOF,aAAP;AACA,CAjDD;;eAmDeP,kB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport apiFetch from '..';\n\n/**\n * Apply query arguments to both URL and Path, whichever is present.\n *\n * @param {import('../types').APIFetchOptions} props\n * @param {Record<string, string | number>} queryArgs\n * @return {import('../types').APIFetchOptions} The request with the modified query args\n */\nconst modifyQuery = ( { path, url, ...options }, queryArgs ) => ( {\n\t...options,\n\turl: url && addQueryArgs( url, queryArgs ),\n\tpath: path && addQueryArgs( path, queryArgs ),\n} );\n\n/**\n * Duplicates parsing functionality from apiFetch.\n *\n * @param {Response} response\n * @return {Promise<any>} Parsed response json.\n */\nconst parseResponse = ( response ) =>\n\tresponse.json ? response.json() : Promise.reject( response );\n\n/**\n * @param {string | null} linkHeader\n * @return {{ next?: string }} The parsed link header.\n */\nconst parseLinkHeader = ( linkHeader ) => {\n\tif ( ! linkHeader ) {\n\t\treturn {};\n\t}\n\tconst match = linkHeader.match( /<([^>]+)>; rel=\"next\"/ );\n\treturn match\n\t\t? {\n\t\t\t\tnext: match[ 1 ],\n\t\t }\n\t\t: {};\n};\n\n/**\n * @param {Response} response\n * @return {string | undefined} The next page URL.\n */\nconst getNextPageUrl = ( response ) => {\n\tconst { next } = parseLinkHeader( response.headers.get( 'link' ) );\n\treturn next;\n};\n\n/**\n * @param {import('../types').APIFetchOptions} options\n * @return {boolean} True if the request contains an unbounded query.\n */\nconst requestContainsUnboundedQuery = ( options ) => {\n\tconst pathIsUnbounded =\n\t\t!! options.path && options.path.indexOf( 'per_page=-1' ) !== -1;\n\tconst urlIsUnbounded =\n\t\t!! options.url && options.url.indexOf( 'per_page=-1' ) !== -1;\n\treturn pathIsUnbounded || urlIsUnbounded;\n};\n\n/**\n * The REST API enforces an upper limit on the per_page option. To handle large\n * collections, apiFetch consumers can pass `per_page=-1`; this middleware will\n * then recursively assemble a full response array from all available pages.\n *\n * @type {import('../types').APIFetchMiddleware}\n */\nconst fetchAllMiddleware = async ( options, next ) => {\n\tif ( options.parse === false ) {\n\t\t// If a consumer has opted out of parsing, do not apply middleware.\n\t\treturn next( options );\n\t}\n\tif ( ! requestContainsUnboundedQuery( options ) ) {\n\t\t// If neither url nor path is requesting all items, do not apply middleware.\n\t\treturn next( options );\n\t}\n\n\t// Retrieve requested page of results.\n\tconst response = await apiFetch( {\n\t\t...modifyQuery( options, {\n\t\t\tper_page: 100,\n\t\t} ),\n\t\t// Ensure headers are returned for page 1.\n\t\tparse: false,\n\t} );\n\n\tconst results = await parseResponse( response );\n\n\tif ( ! Array.isArray( results ) ) {\n\t\t// We have no reliable way of merging non-array results.\n\t\treturn results;\n\t}\n\n\tlet nextPage = getNextPageUrl( response );\n\n\tif ( ! nextPage ) {\n\t\t// There are no further pages to request.\n\t\treturn results;\n\t}\n\n\t// Iteratively fetch all remaining pages until no \"next\" header is found.\n\tlet mergedResults = /** @type {any[]} */ ( [] ).concat( results );\n\twhile ( nextPage ) {\n\t\tconst nextResponse = await apiFetch( {\n\t\t\t...options,\n\t\t\t// Ensure the URL for the next page is used instead of any provided path.\n\t\t\tpath: undefined,\n\t\t\turl: nextPage,\n\t\t\t// Ensure we still get headers so we can identify the next page.\n\t\t\tparse: false,\n\t\t} );\n\t\tconst nextResults = await parseResponse( nextResponse );\n\t\tmergedResults = mergedResults.concat( nextResults );\n\t\tnextPage = getNextPageUrl( nextResponse );\n\t}\n\treturn mergedResults;\n};\n\nexport default fetchAllMiddleware;\n"]}
|
|
@@ -16,10 +16,7 @@ var _url = require("@wordpress/url");
|
|
|
16
16
|
* @return {import('../types').APIFetchMiddleware} Preloading middleware.
|
|
17
17
|
*/
|
|
18
18
|
function createPreloadingMiddleware(preloadedData) {
|
|
19
|
-
const cache = Object.fromEntries(Object.entries(preloadedData).map(
|
|
20
|
-
let [path, data] = _ref;
|
|
21
|
-
return [(0, _url.normalizePath)(path), data];
|
|
22
|
-
}));
|
|
19
|
+
const cache = Object.fromEntries(Object.entries(preloadedData).map(([path, data]) => [(0, _url.normalizePath)(path), data]));
|
|
23
20
|
return (options, next) => {
|
|
24
21
|
const {
|
|
25
22
|
parse = true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["createPreloadingMiddleware","preloadedData","cache","Object","fromEntries","entries","map","path","data","options","next","parse","rawPath","url","rest_route","pathFromQuery","queryArgs","method","cacheData","prepareResponse","responseData","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,WAAP,CACbD,MAAM,CAACE,OAAP,CAAgBJ,aAAhB,EAAgCK,GAAhC,CAAqC
|
|
1
|
+
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["createPreloadingMiddleware","preloadedData","cache","Object","fromEntries","entries","map","path","data","options","next","parse","rawPath","url","rest_route","pathFromQuery","queryArgs","method","cacheData","prepareResponse","responseData","Promise","resolve","body","window","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D,MAOO,IACN,cAAcM,MAAd,IACAf,KAAK,CAAEe,MAAF,CADL,IAEAf,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAHM,EAIL;AACD,YAAMW,SAAS,GAAGhB,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAAlB,CADC,CAGD;;AACA,aAAOL,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAAP;AAEA,aAAOY,eAAe,CAAED,SAAF,EAAa,CAAC,CAAEP,KAAhB,CAAtB;AACA;;AAED,WAAOD,IAAI,CAAED,OAAF,CAAX;AACA,GA1CD;AA2CA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASU,eAAT,CAA0BC,YAA1B,EAAwCT,KAAxC,EAAgD;AAC/C,SAAOU,OAAO,CAACC,OAAR,CACNX,KAAK,GACFS,YAAY,CAACG,IADX,GAEF,IAAIC,MAAM,CAACC,QAAX,CAAqBC,IAAI,CAACC,SAAL,CAAgBP,YAAY,CAACG,IAA7B,CAArB,EAA0D;AAC1DK,IAAAA,MAAM,EAAE,GADkD;AAE1DC,IAAAA,UAAU,EAAE,IAF8C;AAG1DC,IAAAA,OAAO,EAAEV,YAAY,CAACU;AAHoC,GAA1D,CAHG,CAAP;AASA;;eAEc9B,0B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, getQueryArgs, 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.fromEntries(\n\t\tObject.entries( preloadedData ).map( ( [ path, data ] ) => [\n\t\t\tnormalizePath( path ),\n\t\t\tdata,\n\t\t] )\n\t);\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 { rest_route: pathFromQuery, ...queryArgs } = getQueryArgs(\n\t\t\t\toptions.url\n\t\t\t);\n\n\t\t\tif ( typeof pathFromQuery === 'string' ) {\n\t\t\t\trawPath = addQueryArgs( pathFromQuery, queryArgs );\n\t\t\t}\n\t\t}\n\n\t\tif ( typeof rawPath !== 'string' ) {\n\t\t\treturn next( options );\n\t\t}\n\n\t\tconst method = options.method || 'GET';\n\t\tconst path = normalizePath( rawPath );\n\n\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t} else if (\n\t\t\t'OPTIONS' === method &&\n\t\t\tcache[ method ] &&\n\t\t\tcache[ method ][ path ]\n\t\t) {\n\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ method ][ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\n/**\n * This is a helper function that sends a success response.\n *\n * @param {Record<string, any>} responseData\n * @param {boolean} parse\n * @return {Promise<any>} Promise with the response.\n */\nfunction prepareResponse( responseData, parse ) {\n\treturn Promise.resolve(\n\t\tparse\n\t\t\t? responseData.body\n\t\t\t: new window.Response( JSON.stringify( responseData.body ), {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\theaders: responseData.headers,\n\t\t\t } )\n\t);\n}\n\nexport default createPreloadingMiddleware;\n"]}
|
package/build/utils/response.js
CHANGED
|
@@ -20,9 +20,7 @@ var _i18n = require("@wordpress/i18n");
|
|
|
20
20
|
*
|
|
21
21
|
* @return {Promise<any> | null | Response} Parsed response.
|
|
22
22
|
*/
|
|
23
|
-
const parseResponse =
|
|
24
|
-
let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
25
|
-
|
|
23
|
+
const parseResponse = (response, shouldParseResponse = true) => {
|
|
26
24
|
if (shouldParseResponse) {
|
|
27
25
|
if (response.status === 204) {
|
|
28
26
|
return null;
|
|
@@ -66,8 +64,7 @@ const parseJsonAndNormalizeError = response => {
|
|
|
66
64
|
*/
|
|
67
65
|
|
|
68
66
|
|
|
69
|
-
const parseResponseAndNormalizeError =
|
|
70
|
-
let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
67
|
+
const parseResponseAndNormalizeError = (response, shouldParseResponse = true) => {
|
|
71
68
|
return Promise.resolve(parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse));
|
|
72
69
|
};
|
|
73
70
|
/**
|
|
@@ -81,9 +78,7 @@ const parseResponseAndNormalizeError = function (response) {
|
|
|
81
78
|
|
|
82
79
|
exports.parseResponseAndNormalizeError = parseResponseAndNormalizeError;
|
|
83
80
|
|
|
84
|
-
function parseAndThrowError(response) {
|
|
85
|
-
let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
86
|
-
|
|
81
|
+
function parseAndThrowError(response, shouldParseResponse = true) {
|
|
87
82
|
if (!shouldParseResponse) {
|
|
88
83
|
throw response;
|
|
89
84
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/api-fetch/src/utils/response.js"],"names":["parseResponse","response","shouldParseResponse","status","json","Promise","reject","parseJsonAndNormalizeError","invalidJsonError","code","message","catch","parseResponseAndNormalizeError","resolve","res","parseAndThrowError","then","error","unknownError"],"mappings":";;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,aAAa,GAAG,
|
|
1
|
+
{"version":3,"sources":["@wordpress/api-fetch/src/utils/response.js"],"names":["parseResponse","response","shouldParseResponse","status","json","Promise","reject","parseJsonAndNormalizeError","invalidJsonError","code","message","catch","parseResponseAndNormalizeError","resolve","res","parseAndThr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,MAAK,CAAEA,mBAAP,EAA6B;AAC5B,UAAMD,QAAN;AACA;;AAED,SAAOM,0BAA0B,CAAEN,QAAF,CAA1B,CAAuCe,IAAvC,CAA+CC,KAAF,IAAa;AAChE,UAAMC,YAAY,GAAG;AACpBT,MAAAA,IAAI,EAAE,eADc;AAEpBC,MAAAA,OAAO,EAAE,cAAI,4BAAJ;AAFW,KAArB;AAKA,UAAMO,KAAK,IAAIC,YAAf;AACA,GAPM,CAAP;AAQA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Parses the apiFetch response.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any> | null | Response} Parsed response.\n */\nconst parseResponse = ( response, shouldParseResponse = true ) => {\n\tif ( shouldParseResponse ) {\n\t\tif ( response.status === 204 ) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn response.json ? response.json() : Promise.reject( response );\n\t}\n\n\treturn response;\n};\n\n/**\n * Calls the `json` function on the Response, throwing an error if the response\n * doesn't have a json function or if parsing the json itself fails.\n *\n * @param {Response} response\n * @return {Promise<any>} Parsed response.\n */\nconst parseJsonAndNormalizeError = ( response ) => {\n\tconst invalidJsonError = {\n\t\tcode: 'invalid_json',\n\t\tmessage: __( 'The response is not a valid JSON response.' ),\n\t};\n\n\tif ( ! response || ! response.json ) {\n\t\tthrow invalidJsonError;\n\t}\n\n\treturn response.json().catch( () => {\n\t\tthrow invalidJsonError;\n\t} );\n};\n\n/**\n * Parses the apiFetch response properly and normalize response errors.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any>} Parsed response.\n */\nexport const parseResponseAndNormalizeError = (\n\tresponse,\n\tshouldParseResponse = true\n) => {\n\treturn Promise.resolve(\n\t\tparseResponse( response, shouldParseResponse )\n\t).catch( ( res ) => parseAndThrowError( res, shouldParseResponse ) );\n};\n\n/**\n * Parses a response, throwing an error if parsing the response fails.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n * @return {Promise<any>} Parsed response.\n */\nexport function parseAndThrowError( response, shouldParseResponse = true ) {\n\tif ( ! shouldParseResponse ) {\n\t\tthrow response;\n\t}\n\n\treturn parseJsonAndNormalizeError( response ).then( ( error ) => {\n\t\tconst unknownError = {\n\t\t\tcode: 'unknown_error',\n\t\t\tmessage: __( 'An unknown error occurred.' ),\n\t\t};\n\n\t\tthrow error || unknownError;\n\t} );\n}\n"]}
|
|
@@ -15,17 +15,14 @@ import apiFetch from '..';
|
|
|
15
15
|
* @return {import('../types').APIFetchOptions} The request with the modified query args
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
const modifyQuery = (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
path: path && addQueryArgs(path, queryArgs)
|
|
27
|
-
};
|
|
28
|
-
};
|
|
18
|
+
const modifyQuery = ({
|
|
19
|
+
path,
|
|
20
|
+
url,
|
|
21
|
+
...options
|
|
22
|
+
}, queryArgs) => ({ ...options,
|
|
23
|
+
url: url && addQueryArgs(url, queryArgs),
|
|
24
|
+
path: path && addQueryArgs(path, queryArgs)
|
|
25
|
+
});
|
|
29
26
|
/**
|
|
30
27
|
* Duplicates parsing functionality from apiFetch.
|
|
31
28
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/fetch-all-middleware.js"],"names":["addQueryArgs","apiFetch","modifyQuery","
|
|
1
|
+
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/fetch-all-middleware.js"],"names":["addQueryArgs","apiFetch","modifyQuery","path","url","options","queryArgs","parseResponse","response","json","Promise","reject","parseLinkHeader","linkHeader","match","next","getNextPageUrl","headers","get","requestContainsUnboundedQuery","pathIsUnbounded","indexOf","urlIsUnbounded","fetchAllMiddleware","parse","per_page","results","Array","isArray","nextPage","mergedResults","concat","nextResponse","undefined","nextResults"],"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,IAAI,EAAE8B,SAH8B;AAIpC7B,MAAAA,GAAG,EAAEyB,QAJ+B;AAKpC;AACAL,MAAAA,KAAK,EAAE;AAN6B,KAAF,CAAnC;AAQA,UAAMU,WAAW,GAAG,MAAM3B,aAAa,CAAEyB,YAAF,CAAvC;AACAF,IAAAA,aAAa,GAAGA,aAAa,CAACC,MAAd,CAAsBG,WAAtB,CAAhB;AACAL,IAAAA,QAAQ,GAAGb,cAAc,CAAEgB,YAAF,CAAzB;AACA;;AACD,SAAOF,aAAP;AACA,CAjDD;;AAmDA,eAAeP,kBAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport apiFetch from '..';\n\n/**\n * Apply query arguments to both URL and Path, whichever is present.\n *\n * @param {import('../types').APIFetchOptions} props\n * @param {Record<string, string | number>} queryArgs\n * @return {import('../types').APIFetchOptions} The request with the modified query args\n */\nconst modifyQuery = ( { path, url, ...options }, queryArgs ) => ( {\n\t...options,\n\turl: url && addQueryArgs( url, queryArgs ),\n\tpath: path && addQueryArgs( path, queryArgs ),\n} );\n\n/**\n * Duplicates parsing functionality from apiFetch.\n *\n * @param {Response} response\n * @return {Promise<any>} Parsed response json.\n */\nconst parseResponse = ( response ) =>\n\tresponse.json ? response.json() : Promise.reject( response );\n\n/**\n * @param {string | null} linkHeader\n * @return {{ next?: string }} The parsed link header.\n */\nconst parseLinkHeader = ( linkHeader ) => {\n\tif ( ! linkHeader ) {\n\t\treturn {};\n\t}\n\tconst match = linkHeader.match( /<([^>]+)>; rel=\"next\"/ );\n\treturn match\n\t\t? {\n\t\t\t\tnext: match[ 1 ],\n\t\t }\n\t\t: {};\n};\n\n/**\n * @param {Response} response\n * @return {string | undefined} The next page URL.\n */\nconst getNextPageUrl = ( response ) => {\n\tconst { next } = parseLinkHeader( response.headers.get( 'link' ) );\n\treturn next;\n};\n\n/**\n * @param {import('../types').APIFetchOptions} options\n * @return {boolean} True if the request contains an unbounded query.\n */\nconst requestContainsUnboundedQuery = ( options ) => {\n\tconst pathIsUnbounded =\n\t\t!! options.path && options.path.indexOf( 'per_page=-1' ) !== -1;\n\tconst urlIsUnbounded =\n\t\t!! options.url && options.url.indexOf( 'per_page=-1' ) !== -1;\n\treturn pathIsUnbounded || urlIsUnbounded;\n};\n\n/**\n * The REST API enforces an upper limit on the per_page option. To handle large\n * collections, apiFetch consumers can pass `per_page=-1`; this middleware will\n * then recursively assemble a full response array from all available pages.\n *\n * @type {import('../types').APIFetchMiddleware}\n */\nconst fetchAllMiddleware = async ( options, next ) => {\n\tif ( options.parse === false ) {\n\t\t// If a consumer has opted out of parsing, do not apply middleware.\n\t\treturn next( options );\n\t}\n\tif ( ! requestContainsUnboundedQuery( options ) ) {\n\t\t// If neither url nor path is requesting all items, do not apply middleware.\n\t\treturn next( options );\n\t}\n\n\t// Retrieve requested page of results.\n\tconst response = await apiFetch( {\n\t\t...modifyQuery( options, {\n\t\t\tper_page: 100,\n\t\t} ),\n\t\t// Ensure headers are returned for page 1.\n\t\tparse: false,\n\t} );\n\n\tconst results = await parseResponse( response );\n\n\tif ( ! Array.isArray( results ) ) {\n\t\t// We have no reliable way of merging non-array results.\n\t\treturn results;\n\t}\n\n\tlet nextPage = getNextPageUrl( response );\n\n\tif ( ! nextPage ) {\n\t\t// There are no further pages to request.\n\t\treturn results;\n\t}\n\n\t// Iteratively fetch all remaining pages until no \"next\" header is found.\n\tlet mergedResults = /** @type {any[]} */ ( [] ).concat( results );\n\twhile ( nextPage ) {\n\t\tconst nextResponse = await apiFetch( {\n\t\t\t...options,\n\t\t\t// Ensure the URL for the next page is used instead of any provided path.\n\t\t\tpath: undefined,\n\t\t\turl: nextPage,\n\t\t\t// Ensure we still get headers so we can identify the next page.\n\t\t\tparse: false,\n\t\t} );\n\t\tconst nextResults = await parseResponse( nextResponse );\n\t\tmergedResults = mergedResults.concat( nextResults );\n\t\tnextPage = getNextPageUrl( nextResponse );\n\t}\n\treturn mergedResults;\n};\n\nexport default fetchAllMiddleware;\n"]}
|
|
@@ -8,10 +8,7 @@ import { addQueryArgs, getQueryArgs, normalizePath } from '@wordpress/url';
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
function createPreloadingMiddleware(preloadedData) {
|
|
11
|
-
const cache = Object.fromEntries(Object.entries(preloadedData).map(
|
|
12
|
-
let [path, data] = _ref;
|
|
13
|
-
return [normalizePath(path), data];
|
|
14
|
-
}));
|
|
11
|
+
const cache = Object.fromEntries(Object.entries(preloadedData).map(([path, data]) => [normalizePath(path), data]));
|
|
15
12
|
return (options, next) => {
|
|
16
13
|
const {
|
|
17
14
|
parse = true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["addQueryArgs","getQueryArgs","normalizePath","createPreloadingMiddleware","preloadedData","cache","Object","fromEntries","entries","map","path","data","options","next","parse","rawPath","url","rest_route","pathFromQuery","queryArgs","method","cacheData","prepareResponse","responseData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAT,EAAuBC,YAAvB,EAAqCC,aAArC,QAA0D,gBAA1D;AAEA;AACA;AACA;AACA;;AACA,SAASC,0BAAT,CAAqCC,aAArC,EAAqD;AACpD,QAAMC,KAAK,GAAGC,MAAM,CAACC,WAAP,CACbD,MAAM,CAACE,OAAP,CAAgBJ,aAAhB,EAAgCK,GAAhC,CAAqC
|
|
1
|
+
{"version":3,"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"names":["addQueryArgs","getQueryArgs","normalizePath","createPreloadingMiddleware","preloadedData","cache","Object","fromEntries","entries","map","path","data","options","next","parse","rawPath","url","rest_route","pathFromQuery","queryArgs","method","cacheData","prepareResponse","responseData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAT,EAAuBC,YAAvB,EAAqCC,aAArC,QAA0D,g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d,IACAf,KAAK,CAAEe,MAAF,CADL,IAEAf,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAHM,EAIL;AACD,YAAMW,SAAS,GAAGhB,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAAlB,CADC,CAGD;;AACA,aAAOL,KAAK,CAAEe,MAAF,CAAL,CAAiBV,IAAjB,CAAP;AAEA,aAAOY,eAAe,CAAED,SAAF,EAAa,CAAC,CAAEP,KAAhB,CAAtB;AACA;;AAED,WAAOD,IAAI,CAAED,OAAF,CAAX;AACA,GA1CD;AA2CA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASU,eAAT,CAA0BC,YAA1B,EAAwCT,KAAxC,EAAgD;AAC/C,SAAOU,OAAO,CAACC,OAAR,CACNX,KAAK,GACFS,YAAY,CAACG,IADX,GAEF,IAAIC,MAAM,CAACC,QAAX,CAAqBC,IAAI,CAACC,SAAL,CAAgBP,YAAY,CAACG,IAA7B,CAArB,EAA0D;AAC1DK,IAAAA,MAAM,EAAE,GADkD;AAE1DC,IAAAA,UAAU,EAAE,IAF8C;AAG1DC,IAAAA,OAAO,EAAEV,YAAY,CAACU;AAHoC,GAA1D,CAHG,CAAP;AASA;;AAED,eAAe9B,0BAAf","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, getQueryArgs, 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.fromEntries(\n\t\tObject.entries( preloadedData ).map( ( [ path, data ] ) => [\n\t\t\tnormalizePath( path ),\n\t\t\tdata,\n\t\t] )\n\t);\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 { rest_route: pathFromQuery, ...queryArgs } = getQueryArgs(\n\t\t\t\toptions.url\n\t\t\t);\n\n\t\t\tif ( typeof pathFromQuery === 'string' ) {\n\t\t\t\trawPath = addQueryArgs( pathFromQuery, queryArgs );\n\t\t\t}\n\t\t}\n\n\t\tif ( typeof rawPath !== 'string' ) {\n\t\t\treturn next( options );\n\t\t}\n\n\t\tconst method = options.method || 'GET';\n\t\tconst path = normalizePath( rawPath );\n\n\t\tif ( 'GET' === method && cache[ path ] ) {\n\t\t\tconst cacheData = cache[ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t} else if (\n\t\t\t'OPTIONS' === method &&\n\t\t\tcache[ method ] &&\n\t\t\tcache[ method ][ path ]\n\t\t) {\n\t\t\tconst cacheData = cache[ method ][ path ];\n\n\t\t\t// Unsetting the cache key ensures that the data is only used a single time.\n\t\t\tdelete cache[ method ][ path ];\n\n\t\t\treturn prepareResponse( cacheData, !! parse );\n\t\t}\n\n\t\treturn next( options );\n\t};\n}\n\n/**\n * This is a helper function that sends a success response.\n *\n * @param {Record<string, any>} responseData\n * @param {boolean} parse\n * @return {Promise<any>} Promise with the response.\n */\nfunction prepareResponse( responseData, parse ) {\n\treturn Promise.resolve(\n\t\tparse\n\t\t\t? responseData.body\n\t\t\t: new window.Response( JSON.stringify( responseData.body ), {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\tstatusText: 'OK',\n\t\t\t\t\theaders: responseData.headers,\n\t\t\t } )\n\t);\n}\n\nexport default createPreloadingMiddleware;\n"]}
|
|
@@ -11,9 +11,7 @@ import { __ } from '@wordpress/i18n';
|
|
|
11
11
|
* @return {Promise<any> | null | Response} Parsed response.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
const parseResponse =
|
|
15
|
-
let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
16
|
-
|
|
14
|
+
const parseResponse = (response, shouldParseResponse = true) => {
|
|
17
15
|
if (shouldParseResponse) {
|
|
18
16
|
if (response.status === 204) {
|
|
19
17
|
return null;
|
|
@@ -57,8 +55,7 @@ const parseJsonAndNormalizeError = response => {
|
|
|
57
55
|
*/
|
|
58
56
|
|
|
59
57
|
|
|
60
|
-
export const parseResponseAndNormalizeError =
|
|
61
|
-
let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
58
|
+
export const parseResponseAndNormalizeError = (response, shouldParseResponse = true) => {
|
|
62
59
|
return Promise.resolve(parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse));
|
|
63
60
|
};
|
|
64
61
|
/**
|
|
@@ -69,9 +66,7 @@ export const parseResponseAndNormalizeError = function (response) {
|
|
|
69
66
|
* @return {Promise<any>} Parsed response.
|
|
70
67
|
*/
|
|
71
68
|
|
|
72
|
-
export function parseAndThrowError(response) {
|
|
73
|
-
let shouldParseResponse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
74
|
-
|
|
69
|
+
export function parseAndThrowError(response, shouldParseResponse = true) {
|
|
75
70
|
if (!shouldParseResponse) {
|
|
76
71
|
throw response;
|
|
77
72
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/api-fetch/src/utils/response.js"],"names":["__","parseResponse","response","shouldParseResponse","status","json","Promise","reject","parseJsonAndNormalizeError","invalidJsonError","code","message","catch","parseResponseAndNormalizeError","resolve","res","parseAndThrowError","then","error","unknownError"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,EAAT,QAAmB,iBAAnB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,aAAa,GAAG,
|
|
1
|
+
{"version":3,"sources":["@wordpress/api-fetch/src/utils/response.js"],"names":["__","parseResponse","response","shouldParseResponse","status","json","Promise","reject","parseJsonAndNormalizeError","invalidJsonError","code","message","catch","parseResponseAndNormalizeError","resolve","res","parseAndThrowError","then","error","unknownError"],"mappings":"AA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;AAC1E,MAAK,CAAEA,mBAAP,EAA6B;AAC5B,UAAMD,QAAN;AACA;;AAED,SAAOM,0BAA0B,CAAEN,QAAF,CAA1B,CAAuCe,IAAvC,CAA+CC,KAAF,IAAa;AAChE,UAAMC,YAAY,GAAG;AACpBT,MAAAA,IAAI,EAAE,eADc;AAEpBC,MAAAA,OAAO,EAAEX,EAAE,CAAE,4BAAF;AAFS,KAArB;AAKA,UAAMkB,KAAK,IAAIC,YAAf;AACA,GAPM,CAAP;AAQA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Parses the apiFetch response.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any> | null | Response} Parsed response.\n */\nconst parseResponse = ( response, shouldParseResponse = true ) => {\n\tif ( shouldParseResponse ) {\n\t\tif ( response.status === 204 ) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn response.json ? response.json() : Promise.reject( response );\n\t}\n\n\treturn response;\n};\n\n/**\n * Calls the `json` function on the Response, throwing an error if the response\n * doesn't have a json function or if parsing the json itself fails.\n *\n * @param {Response} response\n * @return {Promise<any>} Parsed response.\n */\nconst parseJsonAndNormalizeError = ( response ) => {\n\tconst invalidJsonError = {\n\t\tcode: 'invalid_json',\n\t\tmessage: __( 'The response is not a valid JSON response.' ),\n\t};\n\n\tif ( ! response || ! response.json ) {\n\t\tthrow invalidJsonError;\n\t}\n\n\treturn response.json().catch( () => {\n\t\tthrow invalidJsonError;\n\t} );\n};\n\n/**\n * Parses the apiFetch response properly and normalize response errors.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n *\n * @return {Promise<any>} Parsed response.\n */\nexport const parseResponseAndNormalizeError = (\n\tresponse,\n\tshouldParseResponse = true\n) => {\n\treturn Promise.resolve(\n\t\tparseResponse( response, shouldParseResponse )\n\t).catch( ( res ) => parseAndThrowError( res, shouldParseResponse ) );\n};\n\n/**\n * Parses a response, throwing an error if parsing the response fails.\n *\n * @param {Response} response\n * @param {boolean} shouldParseResponse\n * @return {Promise<any>} Parsed response.\n */\nexport function parseAndThrowError( response, shouldParseResponse = true ) {\n\tif ( ! shouldParseResponse ) {\n\t\tthrow response;\n\t}\n\n\treturn parseJsonAndNormalizeError( response ).then( ( error ) => {\n\t\tconst unknownError = {\n\t\t\tcode: 'unknown_error',\n\t\t\tmessage: __( 'An unknown error occurred.' ),\n\t\t};\n\n\t\tthrow error || unknownError;\n\t} );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/api-fetch",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.32.0",
|
|
4
4
|
"description": "Utility to make WordPress REST API requests.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"types": "build-types",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@babel/runtime": "^7.16.0",
|
|
31
|
-
"@wordpress/i18n": "^4.
|
|
32
|
-
"@wordpress/url": "^3.
|
|
31
|
+
"@wordpress/i18n": "^4.35.0",
|
|
32
|
+
"@wordpress/url": "^3.36.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "a92f606309b1541b834ff9b0a76ed2a466fc45ed"
|
|
38
38
|
}
|