@wordpress/api-fetch 6.36.0 → 6.37.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 +2 -0
- package/build/index.js +26 -43
- package/build/index.js.map +1 -1
- package/build/middlewares/fetch-all-middleware.js +16 -32
- package/build/middlewares/fetch-all-middleware.js.map +1 -1
- package/build/middlewares/http-v1.js +6 -8
- package/build/middlewares/http-v1.js.map +1 -1
- package/build/middlewares/media-upload.js +4 -14
- package/build/middlewares/media-upload.js.map +1 -1
- package/build/middlewares/namespace-endpoint.js +2 -6
- package/build/middlewares/namespace-endpoint.js.map +1 -1
- package/build/middlewares/nonce.js +7 -8
- package/build/middlewares/nonce.js.map +1 -1
- package/build/middlewares/preloading.js +5 -14
- package/build/middlewares/preloading.js.map +1 -1
- package/build/middlewares/root-url.js +4 -11
- package/build/middlewares/root-url.js.map +1 -1
- package/build/middlewares/theme-preview.js +0 -5
- package/build/middlewares/theme-preview.js.map +1 -1
- package/build/middlewares/user-locale.js +0 -5
- package/build/middlewares/user-locale.js.map +1 -1
- package/build/types.js.map +1 -1
- package/build/utils/response.js +3 -14
- package/build/utils/response.js.map +1 -1
- package/build-module/index.js +28 -32
- package/build-module/index.js.map +1 -1
- package/build-module/middlewares/fetch-all-middleware.js +18 -30
- package/build-module/middlewares/fetch-all-middleware.js.map +1 -1
- package/build-module/middlewares/http-v1.js +6 -7
- package/build-module/middlewares/http-v1.js.map +1 -1
- package/build-module/middlewares/media-upload.js +6 -13
- package/build-module/middlewares/media-upload.js.map +1 -1
- package/build-module/middlewares/namespace-endpoint.js +2 -5
- package/build-module/middlewares/namespace-endpoint.js.map +1 -1
- package/build-module/middlewares/nonce.js +7 -7
- package/build-module/middlewares/nonce.js.map +1 -1
- package/build-module/middlewares/preloading.js +6 -13
- package/build-module/middlewares/preloading.js.map +1 -1
- package/build-module/middlewares/root-url.js +5 -9
- package/build-module/middlewares/root-url.js.map +1 -1
- package/build-module/middlewares/theme-preview.js +1 -4
- package/build-module/middlewares/theme-preview.js.map +1 -1
- package/build-module/middlewares/user-locale.js +1 -4
- package/build-module/middlewares/user-locale.js.map +1 -1
- package/build-module/types.js.map +1 -1
- package/build-module/utils/response.js +4 -11
- package/build-module/utils/response.js.map +1 -1
- package/build-types/index.d.ts +6 -6
- package/build-types/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["namespaceAndEndpointMiddleware","options","next","path","namespaceTrimmed","endpointTrimmed","namespace","endpoint","replace","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/namespace-endpoint.js"],"sourcesContent":["/**\n * @type {import('../types').APIFetchMiddleware}\n */\nconst namespaceAndEndpointMiddleware = ( options, next ) => {\n\tlet path = options.path;\n\tlet namespaceTrimmed, endpointTrimmed;\n\n\tif (\n\t\ttypeof options.namespace === 'string' &&\n\t\ttypeof options.endpoint === 'string'\n\t) {\n\t\tnamespaceTrimmed = options.namespace.replace( /^\\/|\\/$/g, '' );\n\t\tendpointTrimmed = options.endpoint.replace( /^\\//, '' );\n\t\tif ( endpointTrimmed ) {\n\t\t\tpath = namespaceTrimmed + '/' + endpointTrimmed;\n\t\t} else {\n\t\t\tpath = namespaceTrimmed;\n\t\t}\n\t}\n\n\tdelete options.namespace;\n\tdelete options.endpoint;\n\n\treturn next( {\n\t\t...options,\n\t\tpath,\n\t} );\n};\n\nexport default namespaceAndEndpointMiddleware;\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA,MAAMA,8BAA8B,GAAGA,CAAEC,OAAO,EAAEC,IAAI,KAAM;EAC3D,IAAIC,IAAI,GAAGF,OAAO,CAACE,IAAI;EACvB,IAAIC,gBAAgB,EAAEC,eAAe;EAErC,IACC,OAAOJ,OAAO,CAACK,SAAS,KAAK,QAAQ,IACrC,OAAOL,OAAO,CAACM,QAAQ,KAAK,QAAQ,EACnC;IACDH,gBAAgB,GAAGH,OAAO,CAACK,SAAS,CAACE,OAAO,CAAE,UAAU,EAAE,EAAG,CAAC;IAC9DH,eAAe,GAAGJ,OAAO,CAACM,QAAQ,CAACC,OAAO,CAAE,KAAK,EAAE,EAAG,CAAC;IACvD,IAAKH,eAAe,EAAG;MACtBF,IAAI,GAAGC,gBAAgB,GAAG,GAAG,GAAGC,eAAe;IAChD,CAAC,MAAM;MACNF,IAAI,GAAGC,gBAAgB;IACxB;EACD;EAEA,OAAOH,OAAO,CAACK,SAAS;EACxB,OAAOL,OAAO,CAACM,QAAQ;EAEvB,OAAOL,IAAI,CAAE;IACZ,GAAGD,OAAO;IACVE;EACD,CAAE,CAAC;AACJ,CAAC;AAAC,IAAAM,QAAA,GAEaT,8BAA8B;AAAAU,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
|
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* @param {string} nonce
|
|
10
9
|
* @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.
|
|
@@ -16,26 +15,26 @@ function createNonceMiddleware(nonce) {
|
|
|
16
15
|
const middleware = (options, next) => {
|
|
17
16
|
const {
|
|
18
17
|
headers = {}
|
|
19
|
-
} = options;
|
|
20
|
-
// thereof) was specified, no need to add a nonce header.
|
|
18
|
+
} = options;
|
|
21
19
|
|
|
20
|
+
// If an 'X-WP-Nonce' header (or any case-insensitive variation
|
|
21
|
+
// thereof) was specified, no need to add a nonce header.
|
|
22
22
|
for (const headerName in headers) {
|
|
23
23
|
if (headerName.toLowerCase() === 'x-wp-nonce' && headers[headerName] === middleware.nonce) {
|
|
24
24
|
return next(options);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
headers: {
|
|
27
|
+
return next({
|
|
28
|
+
...options,
|
|
29
|
+
headers: {
|
|
30
|
+
...headers,
|
|
30
31
|
'X-WP-Nonce': middleware.nonce
|
|
31
32
|
}
|
|
32
33
|
});
|
|
33
34
|
};
|
|
34
|
-
|
|
35
35
|
middleware.nonce = nonce;
|
|
36
36
|
return middleware;
|
|
37
37
|
}
|
|
38
|
-
|
|
39
38
|
var _default = createNonceMiddleware;
|
|
40
39
|
exports.default = _default;
|
|
41
40
|
//# sourceMappingURL=nonce.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["createNonceMiddleware","nonce","middleware","options","next","headers","headerName","toLowerCase","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/nonce.js"],"sourcesContent":["/**\n * @param {string} nonce\n * @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.\n */\nfunction createNonceMiddleware( nonce ) {\n\t/**\n\t * @type {import('../types').APIFetchMiddleware & { nonce: string }}\n\t */\n\tconst middleware = ( options, next ) => {\n\t\tconst { headers = {} } = options;\n\n\t\t// If an 'X-WP-Nonce' header (or any case-insensitive variation\n\t\t// thereof) was specified, no need to add a nonce header.\n\t\tfor ( const headerName in headers ) {\n\t\t\tif (\n\t\t\t\theaderName.toLowerCase() === 'x-wp-nonce' &&\n\t\t\t\theaders[ headerName ] === middleware.nonce\n\t\t\t) {\n\t\t\t\treturn next( options );\n\t\t\t}\n\t\t}\n\n\t\treturn next( {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t...headers,\n\t\t\t\t'X-WP-Nonce': middleware.nonce,\n\t\t\t},\n\t\t} );\n\t};\n\n\tmiddleware.nonce = nonce;\n\n\treturn middleware;\n}\n\nexport default createNonceMiddleware;\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA,SAASA,qBAAqBA,CAAEC,KAAK,EAAG;EACvC;AACD;AACA;EACC,MAAMC,UAAU,GAAGA,CAAEC,OAAO,EAAEC,IAAI,KAAM;IACvC,MAAM;MAAEC,OAAO,GAAG,CAAC;IAAE,CAAC,GAAGF,OAAO;;IAEhC;IACA;IACA,KAAM,MAAMG,UAAU,IAAID,OAAO,EAAG;MACnC,IACCC,UAAU,CAACC,WAAW,CAAC,CAAC,KAAK,YAAY,IACzCF,OAAO,CAAEC,UAAU,CAAE,KAAKJ,UAAU,CAACD,KAAK,EACzC;QACD,OAAOG,IAAI,CAAED,OAAQ,CAAC;MACvB;IACD;IAEA,OAAOC,IAAI,CAAE;MACZ,GAAGD,OAAO;MACVE,OAAO,EAAE;QACR,GAAGA,OAAO;QACV,YAAY,EAAEH,UAAU,CAACD;MAC1B;IACD,CAAE,CAAC;EACJ,CAAC;EAEDC,UAAU,CAACD,KAAK,GAAGA,KAAK;EAExB,OAAOC,UAAU;AAClB;AAAC,IAAAM,QAAA,GAEcR,qBAAqB;AAAAS,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
|
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _url = require("@wordpress/url");
|
|
9
|
-
|
|
10
8
|
/**
|
|
11
9
|
* WordPress dependencies
|
|
12
10
|
*/
|
|
@@ -22,42 +20,38 @@ function createPreloadingMiddleware(preloadedData) {
|
|
|
22
20
|
parse = true
|
|
23
21
|
} = options;
|
|
24
22
|
/** @type {string | void} */
|
|
25
|
-
|
|
26
23
|
let rawPath = options.path;
|
|
27
|
-
|
|
28
24
|
if (!rawPath && options.url) {
|
|
29
25
|
const {
|
|
30
26
|
rest_route: pathFromQuery,
|
|
31
27
|
...queryArgs
|
|
32
28
|
} = (0, _url.getQueryArgs)(options.url);
|
|
33
|
-
|
|
34
29
|
if (typeof pathFromQuery === 'string') {
|
|
35
30
|
rawPath = (0, _url.addQueryArgs)(pathFromQuery, queryArgs);
|
|
36
31
|
}
|
|
37
32
|
}
|
|
38
|
-
|
|
39
33
|
if (typeof rawPath !== 'string') {
|
|
40
34
|
return next(options);
|
|
41
35
|
}
|
|
42
|
-
|
|
43
36
|
const method = options.method || 'GET';
|
|
44
37
|
const path = (0, _url.normalizePath)(rawPath);
|
|
45
|
-
|
|
46
38
|
if ('GET' === method && cache[path]) {
|
|
47
|
-
const cacheData = cache[path];
|
|
39
|
+
const cacheData = cache[path];
|
|
48
40
|
|
|
41
|
+
// Unsetting the cache key ensures that the data is only used a single time.
|
|
49
42
|
delete cache[path];
|
|
50
43
|
return prepareResponse(cacheData, !!parse);
|
|
51
44
|
} else if ('OPTIONS' === method && cache[method] && cache[method][path]) {
|
|
52
|
-
const cacheData = cache[method][path];
|
|
45
|
+
const cacheData = cache[method][path];
|
|
53
46
|
|
|
47
|
+
// Unsetting the cache key ensures that the data is only used a single time.
|
|
54
48
|
delete cache[method][path];
|
|
55
49
|
return prepareResponse(cacheData, !!parse);
|
|
56
50
|
}
|
|
57
|
-
|
|
58
51
|
return next(options);
|
|
59
52
|
};
|
|
60
53
|
}
|
|
54
|
+
|
|
61
55
|
/**
|
|
62
56
|
* This is a helper function that sends a success response.
|
|
63
57
|
*
|
|
@@ -65,8 +59,6 @@ function createPreloadingMiddleware(preloadedData) {
|
|
|
65
59
|
* @param {boolean} parse
|
|
66
60
|
* @return {Promise<any>} Promise with the response.
|
|
67
61
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
62
|
function prepareResponse(responseData, parse) {
|
|
71
63
|
return Promise.resolve(parse ? responseData.body : new window.Response(JSON.stringify(responseData.body), {
|
|
72
64
|
status: 200,
|
|
@@ -74,7 +66,6 @@ function prepareResponse(responseData, parse) {
|
|
|
74
66
|
headers: responseData.headers
|
|
75
67
|
}));
|
|
76
68
|
}
|
|
77
|
-
|
|
78
69
|
var _default = createPreloadingMiddleware;
|
|
79
70
|
exports.default = _default;
|
|
80
71
|
//# sourceMappingURL=preloading.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["_url","require","createPreloadingMiddleware","preloadedData","cache","Object","fromEntries","entries","map","path","data","normalizePath","options","next","parse","rawPath","url","rest_route","pathFromQuery","queryArgs","getQueryArgs","addQueryArgs","method","cacheData","prepareResponse","responseData","Promise","resolve","body","window","Response","JSON","stringify","status","statusText","headers","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/preloading.js"],"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"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,SAASC,0BAA0BA,CAAEC,aAAa,EAAG;EACpD,MAAMC,KAAK,GAAGC,MAAM,CAACC,WAAW,CAC/BD,MAAM,CAACE,OAAO,CAAEJ,aAAc,CAAC,CAACK,GAAG,CAAE,CAAE,CAAEC,IAAI,EAAEC,IAAI,CAAE,KAAM,CAC1D,IAAAC,kBAAa,EAAEF,IAAK,CAAC,EACrBC,IAAI,CACH,CACH,CAAC;EAED,OAAO,CAAEE,OAAO,EAAEC,IAAI,KAAM;IAC3B,MAAM;MAAEC,KAAK,GAAG;IAAK,CAAC,GAAGF,OAAO;IAChC;IACA,IAAIG,OAAO,GAAGH,OAAO,CAACH,IAAI;IAC1B,IAAK,CAAEM,OAAO,IAAIH,OAAO,CAACI,GAAG,EAAG;MAC/B,MAAM;QAAEC,UAAU,EAAEC,aAAa;QAAE,GAAGC;MAAU,CAAC,GAAG,IAAAC,iBAAY,EAC/DR,OAAO,CAACI,GACT,CAAC;MAED,IAAK,OAAOE,aAAa,KAAK,QAAQ,EAAG;QACxCH,OAAO,GAAG,IAAAM,iBAAY,EAAEH,aAAa,EAAEC,SAAU,CAAC;MACnD;IACD;IAEA,IAAK,OAAOJ,OAAO,KAAK,QAAQ,EAAG;MAClC,OAAOF,IAAI,CAAED,OAAQ,CAAC;IACvB;IAEA,MAAMU,MAAM,GAAGV,OAAO,CAACU,MAAM,IAAI,KAAK;IACtC,MAAMb,IAAI,GAAG,IAAAE,kBAAa,EAAEI,OAAQ,CAAC;IAErC,IAAK,KAAK,KAAKO,MAAM,IAAIlB,KAAK,CAAEK,IAAI,CAAE,EAAG;MACxC,MAAMc,SAAS,GAAGnB,KAAK,CAAEK,IAAI,CAAE;;MAE/B;MACA,OAAOL,KAAK,CAAEK,IAAI,CAAE;MAEpB,OAAOe,eAAe,CAAED,SAAS,EAAE,CAAC,CAAET,KAAM,CAAC;IAC9C,CAAC,MAAM,IACN,SAAS,KAAKQ,MAAM,IACpBlB,KAAK,CAAEkB,MAAM,CAAE,IACflB,KAAK,CAAEkB,MAAM,CAAE,CAAEb,IAAI,CAAE,EACtB;MACD,MAAMc,SAAS,GAAGnB,KAAK,CAAEkB,MAAM,CAAE,CAAEb,IAAI,CAAE;;MAEzC;MACA,OAAOL,KAAK,CAAEkB,MAAM,CAAE,CAAEb,IAAI,CAAE;MAE9B,OAAOe,eAAe,CAAED,SAAS,EAAE,CAAC,CAAET,KAAM,CAAC;IAC9C;IAEA,OAAOD,IAAI,CAAED,OAAQ,CAAC;EACvB,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASY,eAAeA,CAAEC,YAAY,EAAEX,KAAK,EAAG;EAC/C,OAAOY,OAAO,CAACC,OAAO,CACrBb,KAAK,GACFW,YAAY,CAACG,IAAI,GACjB,IAAIC,MAAM,CAACC,QAAQ,CAAEC,IAAI,CAACC,SAAS,CAAEP,YAAY,CAACG,IAAK,CAAC,EAAE;IAC1DK,MAAM,EAAE,GAAG;IACXC,UAAU,EAAE,IAAI;IAChBC,OAAO,EAAEV,YAAY,CAACU;EACtB,CAAE,CACN,CAAC;AACF;AAAC,IAAAC,QAAA,GAEclC,0BAA0B;AAAAmC,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _namespaceEndpoint = _interopRequireDefault(require("./namespace-endpoint"));
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* Internal dependencies
|
|
14
11
|
*/
|
|
@@ -22,30 +19,26 @@ const createRootURLMiddleware = rootURL => (options, next) => {
|
|
|
22
19
|
let url = optionsWithPath.url;
|
|
23
20
|
let path = optionsWithPath.path;
|
|
24
21
|
let apiRoot;
|
|
25
|
-
|
|
26
22
|
if (typeof path === 'string') {
|
|
27
23
|
apiRoot = rootURL;
|
|
28
|
-
|
|
29
24
|
if (-1 !== rootURL.indexOf('?')) {
|
|
30
25
|
path = path.replace('?', '&');
|
|
31
26
|
}
|
|
27
|
+
path = path.replace(/^\//, '');
|
|
32
28
|
|
|
33
|
-
|
|
29
|
+
// API root may already include query parameter prefix if site is
|
|
34
30
|
// configured to use plain permalinks.
|
|
35
|
-
|
|
36
31
|
if ('string' === typeof apiRoot && -1 !== apiRoot.indexOf('?')) {
|
|
37
32
|
path = path.replace('?', '&');
|
|
38
33
|
}
|
|
39
|
-
|
|
40
34
|
url = apiRoot + path;
|
|
41
35
|
}
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
return next({
|
|
37
|
+
...optionsWithPath,
|
|
44
38
|
url
|
|
45
39
|
});
|
|
46
40
|
});
|
|
47
41
|
};
|
|
48
|
-
|
|
49
42
|
var _default = createRootURLMiddleware;
|
|
50
43
|
exports.default = _default;
|
|
51
44
|
//# sourceMappingURL=root-url.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["_namespaceEndpoint","_interopRequireDefault","require","createRootURLMiddleware","rootURL","options","next","namespaceAndEndpointMiddleware","optionsWithPath","url","path","apiRoot","indexOf","replace","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/root-url.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport namespaceAndEndpointMiddleware from './namespace-endpoint';\n\n/**\n * @param {string} rootURL\n * @return {import('../types').APIFetchMiddleware} Root URL middleware.\n */\nconst createRootURLMiddleware = ( rootURL ) => ( options, next ) => {\n\treturn namespaceAndEndpointMiddleware( options, ( optionsWithPath ) => {\n\t\tlet url = optionsWithPath.url;\n\t\tlet path = optionsWithPath.path;\n\t\tlet apiRoot;\n\n\t\tif ( typeof path === 'string' ) {\n\t\t\tapiRoot = rootURL;\n\n\t\t\tif ( -1 !== rootURL.indexOf( '?' ) ) {\n\t\t\t\tpath = path.replace( '?', '&' );\n\t\t\t}\n\n\t\t\tpath = path.replace( /^\\//, '' );\n\n\t\t\t// API root may already include query parameter prefix if site is\n\t\t\t// configured to use plain permalinks.\n\t\t\tif (\n\t\t\t\t'string' === typeof apiRoot &&\n\t\t\t\t-1 !== apiRoot.indexOf( '?' )\n\t\t\t) {\n\t\t\t\tpath = path.replace( '?', '&' );\n\t\t\t}\n\n\t\t\turl = apiRoot + path;\n\t\t}\n\n\t\treturn next( {\n\t\t\t...optionsWithPath,\n\t\t\turl,\n\t\t} );\n\t} );\n};\n\nexport default createRootURLMiddleware;\n"],"mappings":";;;;;;;AAGA,IAAAA,kBAAA,GAAAC,sBAAA,CAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA,MAAMC,uBAAuB,GAAKC,OAAO,IAAM,CAAEC,OAAO,EAAEC,IAAI,KAAM;EACnE,OAAO,IAAAC,0BAA8B,EAAEF,OAAO,EAAIG,eAAe,IAAM;IACtE,IAAIC,GAAG,GAAGD,eAAe,CAACC,GAAG;IAC7B,IAAIC,IAAI,GAAGF,eAAe,CAACE,IAAI;IAC/B,IAAIC,OAAO;IAEX,IAAK,OAAOD,IAAI,KAAK,QAAQ,EAAG;MAC/BC,OAAO,GAAGP,OAAO;MAEjB,IAAK,CAAC,CAAC,KAAKA,OAAO,CAACQ,OAAO,CAAE,GAAI,CAAC,EAAG;QACpCF,IAAI,GAAGA,IAAI,CAACG,OAAO,CAAE,GAAG,EAAE,GAAI,CAAC;MAChC;MAEAH,IAAI,GAAGA,IAAI,CAACG,OAAO,CAAE,KAAK,EAAE,EAAG,CAAC;;MAEhC;MACA;MACA,IACC,QAAQ,KAAK,OAAOF,OAAO,IAC3B,CAAC,CAAC,KAAKA,OAAO,CAACC,OAAO,CAAE,GAAI,CAAC,EAC5B;QACDF,IAAI,GAAGA,IAAI,CAACG,OAAO,CAAE,GAAG,EAAE,GAAI,CAAC;MAChC;MAEAJ,GAAG,GAAGE,OAAO,GAAGD,IAAI;IACrB;IAEA,OAAOJ,IAAI,CAAE;MACZ,GAAGE,eAAe;MAClBC;IACD,CAAE,CAAC;EACJ,CAAE,CAAC;AACJ,CAAC;AAAC,IAAAK,QAAA,GAEaX,uBAAuB;AAAAY,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
|
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _url = require("@wordpress/url");
|
|
9
|
-
|
|
10
8
|
/**
|
|
11
9
|
* WordPress dependencies
|
|
12
10
|
*/
|
|
@@ -24,16 +22,13 @@ const createThemePreviewMiddleware = themePath => (options, next) => {
|
|
|
24
22
|
wp_theme_preview: themePath
|
|
25
23
|
});
|
|
26
24
|
}
|
|
27
|
-
|
|
28
25
|
if (typeof options.path === 'string' && !(0, _url.hasQueryArg)(options.path, 'wp_theme_preview')) {
|
|
29
26
|
options.path = (0, _url.addQueryArgs)(options.path, {
|
|
30
27
|
wp_theme_preview: themePath
|
|
31
28
|
});
|
|
32
29
|
}
|
|
33
|
-
|
|
34
30
|
return next(options);
|
|
35
31
|
};
|
|
36
|
-
|
|
37
32
|
var _default = createThemePreviewMiddleware;
|
|
38
33
|
exports.default = _default;
|
|
39
34
|
//# sourceMappingURL=theme-preview.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["_url","require","createThemePreviewMiddleware","themePath","options","next","url","hasQueryArg","addQueryArgs","wp_theme_preview","path","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/theme-preview.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, hasQueryArg } from '@wordpress/url';\n\n/**\n * This appends a `wp_theme_preview` parameter to the REST API request URL if\n * the admin URL contains a `theme` GET parameter.\n *\n * @param {Record<string, any>} themePath\n * @return {import('../types').APIFetchMiddleware} Preloading middleware.\n */\nconst createThemePreviewMiddleware = ( themePath ) => ( options, next ) => {\n\tif (\n\t\ttypeof options.url === 'string' &&\n\t\t! hasQueryArg( options.url, 'wp_theme_preview' )\n\t) {\n\t\toptions.url = addQueryArgs( options.url, {\n\t\t\twp_theme_preview: themePath,\n\t\t} );\n\t}\n\n\tif (\n\t\ttypeof options.path === 'string' &&\n\t\t! hasQueryArg( options.path, 'wp_theme_preview' )\n\t) {\n\t\toptions.path = addQueryArgs( options.path, {\n\t\t\twp_theme_preview: themePath,\n\t\t} );\n\t}\n\n\treturn next( options );\n};\n\nexport default createThemePreviewMiddleware;\n"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,4BAA4B,GAAKC,SAAS,IAAM,CAAEC,OAAO,EAAEC,IAAI,KAAM;EAC1E,IACC,OAAOD,OAAO,CAACE,GAAG,KAAK,QAAQ,IAC/B,CAAE,IAAAC,gBAAW,EAAEH,OAAO,CAACE,GAAG,EAAE,kBAAmB,CAAC,EAC/C;IACDF,OAAO,CAACE,GAAG,GAAG,IAAAE,iBAAY,EAAEJ,OAAO,CAACE,GAAG,EAAE;MACxCG,gBAAgB,EAAEN;IACnB,CAAE,CAAC;EACJ;EAEA,IACC,OAAOC,OAAO,CAACM,IAAI,KAAK,QAAQ,IAChC,CAAE,IAAAH,gBAAW,EAAEH,OAAO,CAACM,IAAI,EAAE,kBAAmB,CAAC,EAChD;IACDN,OAAO,CAACM,IAAI,GAAG,IAAAF,iBAAY,EAAEJ,OAAO,CAACM,IAAI,EAAE;MAC1CD,gBAAgB,EAAEN;IACnB,CAAE,CAAC;EACJ;EAEA,OAAOE,IAAI,CAAED,OAAQ,CAAC;AACvB,CAAC;AAAC,IAAAO,QAAA,GAEaT,4BAA4B;AAAAU,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
|
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _url = require("@wordpress/url");
|
|
9
|
-
|
|
10
8
|
/**
|
|
11
9
|
* WordPress dependencies
|
|
12
10
|
*/
|
|
@@ -20,16 +18,13 @@ const userLocaleMiddleware = (options, next) => {
|
|
|
20
18
|
_locale: 'user'
|
|
21
19
|
});
|
|
22
20
|
}
|
|
23
|
-
|
|
24
21
|
if (typeof options.path === 'string' && !(0, _url.hasQueryArg)(options.path, '_locale')) {
|
|
25
22
|
options.path = (0, _url.addQueryArgs)(options.path, {
|
|
26
23
|
_locale: 'user'
|
|
27
24
|
});
|
|
28
25
|
}
|
|
29
|
-
|
|
30
26
|
return next(options);
|
|
31
27
|
};
|
|
32
|
-
|
|
33
28
|
var _default = userLocaleMiddleware;
|
|
34
29
|
exports.default = _default;
|
|
35
30
|
//# sourceMappingURL=user-locale.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["_url","require","userLocaleMiddleware","options","next","url","hasQueryArg","addQueryArgs","_locale","path","_default","exports","default"],"sources":["@wordpress/api-fetch/src/middlewares/user-locale.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs, hasQueryArg } from '@wordpress/url';\n\n/**\n * @type {import('../types').APIFetchMiddleware}\n */\nconst userLocaleMiddleware = ( options, next ) => {\n\tif (\n\t\ttypeof options.url === 'string' &&\n\t\t! hasQueryArg( options.url, '_locale' )\n\t) {\n\t\toptions.url = addQueryArgs( options.url, { _locale: 'user' } );\n\t}\n\n\tif (\n\t\ttypeof options.path === 'string' &&\n\t\t! hasQueryArg( options.path, '_locale' )\n\t) {\n\t\toptions.path = addQueryArgs( options.path, { _locale: 'user' } );\n\t}\n\n\treturn next( options );\n};\n\nexport default userLocaleMiddleware;\n"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA,MAAMC,oBAAoB,GAAGA,CAAEC,OAAO,EAAEC,IAAI,KAAM;EACjD,IACC,OAAOD,OAAO,CAACE,GAAG,KAAK,QAAQ,IAC/B,CAAE,IAAAC,gBAAW,EAAEH,OAAO,CAACE,GAAG,EAAE,SAAU,CAAC,EACtC;IACDF,OAAO,CAACE,GAAG,GAAG,IAAAE,iBAAY,EAAEJ,OAAO,CAACE,GAAG,EAAE;MAAEG,OAAO,EAAE;IAAO,CAAE,CAAC;EAC/D;EAEA,IACC,OAAOL,OAAO,CAACM,IAAI,KAAK,QAAQ,IAChC,CAAE,IAAAH,gBAAW,EAAEH,OAAO,CAACM,IAAI,EAAE,SAAU,CAAC,EACvC;IACDN,OAAO,CAACM,IAAI,GAAG,IAAAF,iBAAY,EAAEJ,OAAO,CAACM,IAAI,EAAE;MAAED,OAAO,EAAE;IAAO,CAAE,CAAC;EACjE;EAEA,OAAOJ,IAAI,CAAED,OAAQ,CAAC;AACvB,CAAC;AAAC,IAAAO,QAAA,GAEaR,oBAAoB;AAAAS,OAAA,CAAAC,OAAA,GAAAF,QAAA"}
|
package/build/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":[],"sources":["@wordpress/api-fetch/src/types.ts"],"sourcesContent":["export interface APIFetchOptions extends RequestInit {\n\t// Override headers, we only accept it as an object due to the `nonce` middleware\n\theaders?: Record< string, string >;\n\tpath?: string;\n\turl?: string;\n\t/**\n\t * @default true\n\t */\n\tparse?: boolean;\n\tdata?: any;\n\tnamespace?: string;\n\tendpoint?: string;\n}\n\nexport type APIFetchMiddleware = (\n\toptions: APIFetchOptions,\n\tnext: ( nextOptions: APIFetchOptions ) => Promise< any >\n) => Promise< any >;\n"],"mappings":""}
|
package/build/utils/response.js
CHANGED
|
@@ -5,9 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.parseAndThrowError = parseAndThrowError;
|
|
7
7
|
exports.parseResponseAndNormalizeError = void 0;
|
|
8
|
-
|
|
9
8
|
var _i18n = require("@wordpress/i18n");
|
|
10
|
-
|
|
11
9
|
/**
|
|
12
10
|
* WordPress dependencies
|
|
13
11
|
*/
|
|
@@ -25,12 +23,11 @@ const parseResponse = (response, shouldParseResponse = true) => {
|
|
|
25
23
|
if (response.status === 204) {
|
|
26
24
|
return null;
|
|
27
25
|
}
|
|
28
|
-
|
|
29
26
|
return response.json ? response.json() : Promise.reject(response);
|
|
30
27
|
}
|
|
31
|
-
|
|
32
28
|
return response;
|
|
33
29
|
};
|
|
30
|
+
|
|
34
31
|
/**
|
|
35
32
|
* Calls the `json` function on the Response, throwing an error if the response
|
|
36
33
|
* doesn't have a json function or if parsing the json itself fails.
|
|
@@ -38,22 +35,19 @@ const parseResponse = (response, shouldParseResponse = true) => {
|
|
|
38
35
|
* @param {Response} response
|
|
39
36
|
* @return {Promise<any>} Parsed response.
|
|
40
37
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
38
|
const parseJsonAndNormalizeError = response => {
|
|
44
39
|
const invalidJsonError = {
|
|
45
40
|
code: 'invalid_json',
|
|
46
41
|
message: (0, _i18n.__)('The response is not a valid JSON response.')
|
|
47
42
|
};
|
|
48
|
-
|
|
49
43
|
if (!response || !response.json) {
|
|
50
44
|
throw invalidJsonError;
|
|
51
45
|
}
|
|
52
|
-
|
|
53
46
|
return response.json().catch(() => {
|
|
54
47
|
throw invalidJsonError;
|
|
55
48
|
});
|
|
56
49
|
};
|
|
50
|
+
|
|
57
51
|
/**
|
|
58
52
|
* Parses the apiFetch response properly and normalize response errors.
|
|
59
53
|
*
|
|
@@ -62,11 +56,10 @@ const parseJsonAndNormalizeError = response => {
|
|
|
62
56
|
*
|
|
63
57
|
* @return {Promise<any>} Parsed response.
|
|
64
58
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
59
|
const parseResponseAndNormalizeError = (response, shouldParseResponse = true) => {
|
|
68
60
|
return Promise.resolve(parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse));
|
|
69
61
|
};
|
|
62
|
+
|
|
70
63
|
/**
|
|
71
64
|
* Parses a response, throwing an error if parsing the response fails.
|
|
72
65
|
*
|
|
@@ -74,15 +67,11 @@ const parseResponseAndNormalizeError = (response, shouldParseResponse = true) =>
|
|
|
74
67
|
* @param {boolean} shouldParseResponse
|
|
75
68
|
* @return {Promise<any>} Parsed response.
|
|
76
69
|
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
70
|
exports.parseResponseAndNormalizeError = parseResponseAndNormalizeError;
|
|
80
|
-
|
|
81
71
|
function parseAndThrowError(response, shouldParseResponse = true) {
|
|
82
72
|
if (!shouldParseResponse) {
|
|
83
73
|
throw response;
|
|
84
74
|
}
|
|
85
|
-
|
|
86
75
|
return parseJsonAndNormalizeError(response).then(error => {
|
|
87
76
|
const unknownError = {
|
|
88
77
|
code: 'unknown_error',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["_i18n","require","parseResponse","response","shouldParseResponse","status","json","Promise","reject","parseJsonAndNormalizeError","invalidJsonError","code","message","__","catch","parseResponseAndNormalizeError","resolve","res","parseAndThrowError","exports","then","error","unknownError"],"sources":["@wordpress/api-fetch/src/utils/response.js"],"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"],"mappings":";;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,aAAa,GAAGA,CAAEC,QAAQ,EAAEC,mBAAmB,GAAG,IAAI,KAAM;EACjE,IAAKA,mBAAmB,EAAG;IAC1B,IAAKD,QAAQ,CAACE,MAAM,KAAK,GAAG,EAAG;MAC9B,OAAO,IAAI;IACZ;IAEA,OAAOF,QAAQ,CAACG,IAAI,GAAGH,QAAQ,CAACG,IAAI,CAAC,CAAC,GAAGC,OAAO,CAACC,MAAM,CAAEL,QAAS,CAAC;EACpE;EAEA,OAAOA,QAAQ;AAChB,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMM,0BAA0B,GAAKN,QAAQ,IAAM;EAClD,MAAMO,gBAAgB,GAAG;IACxBC,IAAI,EAAE,cAAc;IACpBC,OAAO,EAAE,IAAAC,QAAE,EAAE,4CAA6C;EAC3D,CAAC;EAED,IAAK,CAAEV,QAAQ,IAAI,CAAEA,QAAQ,CAACG,IAAI,EAAG;IACpC,MAAMI,gBAAgB;EACvB;EAEA,OAAOP,QAAQ,CAACG,IAAI,CAAC,CAAC,CAACQ,KAAK,CAAE,MAAM;IACnC,MAAMJ,gBAAgB;EACvB,CAAE,CAAC;AACJ,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMK,8BAA8B,GAAGA,CAC7CZ,QAAQ,EACRC,mBAAmB,GAAG,IAAI,KACtB;EACJ,OAAOG,OAAO,CAACS,OAAO,CACrBd,aAAa,CAAEC,QAAQ,EAAEC,mBAAoB,CAC9C,CAAC,CAACU,KAAK,CAAIG,GAAG,IAAMC,kBAAkB,CAAED,GAAG,EAAEb,mBAAoB,CAAE,CAAC;AACrE,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AANAe,OAAA,CAAAJ,8BAAA,GAAAA,8BAAA;AAOO,SAASG,kBAAkBA,CAAEf,QAAQ,EAAEC,mBAAmB,GAAG,IAAI,EAAG;EAC1E,IAAK,CAAEA,mBAAmB,EAAG;IAC5B,MAAMD,QAAQ;EACf;EAEA,OAAOM,0BAA0B,CAAEN,QAAS,CAAC,CAACiB,IAAI,CAAIC,KAAK,IAAM;IAChE,MAAMC,YAAY,GAAG;MACpBX,IAAI,EAAE,eAAe;MACrBC,OAAO,EAAE,IAAAC,QAAE,EAAE,4BAA6B;IAC3C,CAAC;IAED,MAAMQ,KAAK,IAAIC,YAAY;EAC5B,CAAE,CAAC;AACJ"}
|
package/build-module/index.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { __ } from '@wordpress/i18n';
|
|
5
|
+
|
|
5
6
|
/**
|
|
6
7
|
* Internal dependencies
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
9
|
import createNonceMiddleware from './middlewares/nonce';
|
|
10
10
|
import createRootURLMiddleware from './middlewares/root-url';
|
|
11
11
|
import createPreloadingMiddleware from './middlewares/preloading';
|
|
@@ -16,13 +16,13 @@ import userLocaleMiddleware from './middlewares/user-locale';
|
|
|
16
16
|
import mediaUploadMiddleware from './middlewares/media-upload';
|
|
17
17
|
import createThemePreviewMiddleware from './middlewares/theme-preview';
|
|
18
18
|
import { parseResponseAndNormalizeError, parseAndThrowError } from './utils/response';
|
|
19
|
+
|
|
19
20
|
/**
|
|
20
21
|
* Default set of header values which should be sent with every request unless
|
|
21
22
|
* explicitly provided through apiFetch options.
|
|
22
23
|
*
|
|
23
24
|
* @type {Record<string, string>}
|
|
24
25
|
*/
|
|
25
|
-
|
|
26
26
|
const DEFAULT_HEADERS = {
|
|
27
27
|
// The backend uses the Accept header as a condition for considering an
|
|
28
28
|
// incoming request as a REST request.
|
|
@@ -30,34 +30,34 @@ const DEFAULT_HEADERS = {
|
|
|
30
30
|
// See: https://core.trac.wordpress.org/ticket/44534
|
|
31
31
|
Accept: 'application/json, */*;q=0.1'
|
|
32
32
|
};
|
|
33
|
+
|
|
33
34
|
/**
|
|
34
35
|
* Default set of fetch option values which should be sent with every request
|
|
35
36
|
* unless explicitly provided through apiFetch options.
|
|
36
37
|
*
|
|
37
38
|
* @type {Object}
|
|
38
39
|
*/
|
|
39
|
-
|
|
40
40
|
const DEFAULT_OPTIONS = {
|
|
41
41
|
credentials: 'include'
|
|
42
42
|
};
|
|
43
|
-
/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
|
|
44
43
|
|
|
44
|
+
/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
|
|
45
45
|
/** @typedef {import('./types').APIFetchOptions} APIFetchOptions */
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* @type {import('./types').APIFetchMiddleware[]}
|
|
49
49
|
*/
|
|
50
|
-
|
|
51
50
|
const middlewares = [userLocaleMiddleware, namespaceEndpointMiddleware, httpV1Middleware, fetchAllMiddleware];
|
|
51
|
+
|
|
52
52
|
/**
|
|
53
53
|
* Register a middleware
|
|
54
54
|
*
|
|
55
55
|
* @param {import('./types').APIFetchMiddleware} middleware
|
|
56
56
|
*/
|
|
57
|
-
|
|
58
57
|
function registerMiddleware(middleware) {
|
|
59
58
|
middlewares.unshift(middleware);
|
|
60
59
|
}
|
|
60
|
+
|
|
61
61
|
/**
|
|
62
62
|
* Checks the status of a response, throwing the Response as an error if
|
|
63
63
|
* it is outside the 200 range.
|
|
@@ -65,22 +65,18 @@ function registerMiddleware(middleware) {
|
|
|
65
65
|
* @param {Response} response
|
|
66
66
|
* @return {Response} The response if the status is in the 200 range.
|
|
67
67
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
68
|
const checkStatus = response => {
|
|
71
69
|
if (response.status >= 200 && response.status < 300) {
|
|
72
70
|
return response;
|
|
73
71
|
}
|
|
74
|
-
|
|
75
72
|
throw response;
|
|
76
73
|
};
|
|
74
|
+
|
|
77
75
|
/** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/
|
|
78
76
|
|
|
79
77
|
/**
|
|
80
78
|
* @type {FetchHandler}
|
|
81
79
|
*/
|
|
82
|
-
|
|
83
|
-
|
|
84
80
|
const defaultFetchHandler = nextOptions => {
|
|
85
81
|
const {
|
|
86
82
|
url,
|
|
@@ -92,19 +88,23 @@ const defaultFetchHandler = nextOptions => {
|
|
|
92
88
|
let {
|
|
93
89
|
body,
|
|
94
90
|
headers
|
|
95
|
-
} = nextOptions;
|
|
91
|
+
} = nextOptions;
|
|
96
92
|
|
|
97
|
-
headers
|
|
93
|
+
// Merge explicitly-provided headers with default values.
|
|
94
|
+
headers = {
|
|
95
|
+
...DEFAULT_HEADERS,
|
|
98
96
|
...headers
|
|
99
|
-
};
|
|
97
|
+
};
|
|
100
98
|
|
|
99
|
+
// The `data` property is a shorthand for sending a JSON body.
|
|
101
100
|
if (data) {
|
|
102
101
|
body = JSON.stringify(data);
|
|
103
102
|
headers['Content-Type'] = 'application/json';
|
|
104
103
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
url || path || window.location.href, {
|
|
104
|
+
const responsePromise = window.fetch(
|
|
105
|
+
// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
|
|
106
|
+
url || path || window.location.href, {
|
|
107
|
+
...DEFAULT_OPTIONS,
|
|
108
108
|
...remainingOptions,
|
|
109
109
|
body,
|
|
110
110
|
headers
|
|
@@ -113,55 +113,52 @@ const defaultFetchHandler = nextOptions => {
|
|
|
113
113
|
// Re-throw AbortError for the users to handle it themselves.
|
|
114
114
|
if (err && err.name === 'AbortError') {
|
|
115
115
|
throw err;
|
|
116
|
-
}
|
|
117
|
-
// Unfortunately the message might depend on the browser.
|
|
118
|
-
|
|
116
|
+
}
|
|
119
117
|
|
|
118
|
+
// Otherwise, there is most likely no network connection.
|
|
119
|
+
// Unfortunately the message might depend on the browser.
|
|
120
120
|
throw {
|
|
121
121
|
code: 'fetch_error',
|
|
122
122
|
message: __('You are probably offline.')
|
|
123
123
|
};
|
|
124
124
|
});
|
|
125
125
|
};
|
|
126
|
-
/** @type {FetchHandler} */
|
|
127
|
-
|
|
128
126
|
|
|
127
|
+
/** @type {FetchHandler} */
|
|
129
128
|
let fetchHandler = defaultFetchHandler;
|
|
129
|
+
|
|
130
130
|
/**
|
|
131
131
|
* Defines a custom fetch handler for making the requests that will override
|
|
132
132
|
* the default one using window.fetch
|
|
133
133
|
*
|
|
134
134
|
* @param {FetchHandler} newFetchHandler The new fetch handler
|
|
135
135
|
*/
|
|
136
|
-
|
|
137
136
|
function setFetchHandler(newFetchHandler) {
|
|
138
137
|
fetchHandler = newFetchHandler;
|
|
139
138
|
}
|
|
139
|
+
|
|
140
140
|
/**
|
|
141
141
|
* @template T
|
|
142
142
|
* @param {import('./types').APIFetchOptions} options
|
|
143
143
|
* @return {Promise<T>} A promise representing the request processed via the registered middlewares.
|
|
144
144
|
*/
|
|
145
|
-
|
|
146
|
-
|
|
147
145
|
function apiFetch(options) {
|
|
148
146
|
// creates a nested function chain that calls all middlewares and finally the `fetchHandler`,
|
|
149
147
|
// converting `middlewares = [ m1, m2, m3 ]` into:
|
|
150
148
|
// ```
|
|
151
149
|
// opts1 => m1( opts1, opts2 => m2( opts2, opts3 => m3( opts3, fetchHandler ) ) );
|
|
152
150
|
// ```
|
|
153
|
-
const enhancedHandler = middlewares.reduceRight((
|
|
154
|
-
/** @type {FetchHandler} */
|
|
155
|
-
next, middleware) => {
|
|
151
|
+
const enhancedHandler = middlewares.reduceRight(( /** @type {FetchHandler} */next, middleware) => {
|
|
156
152
|
return workingOptions => middleware(workingOptions, next);
|
|
157
153
|
}, fetchHandler);
|
|
158
154
|
return enhancedHandler(options).catch(error => {
|
|
159
155
|
if (error.code !== 'rest_cookie_invalid_nonce') {
|
|
160
156
|
return Promise.reject(error);
|
|
161
|
-
}
|
|
157
|
+
}
|
|
162
158
|
|
|
163
|
-
|
|
164
|
-
return window
|
|
159
|
+
// If the nonce is invalid, refresh it and try again.
|
|
160
|
+
return window
|
|
161
|
+
// @ts-ignore
|
|
165
162
|
.fetch(apiFetch.nonceEndpoint).then(checkStatus).then(data => data.text()).then(text => {
|
|
166
163
|
// @ts-ignore
|
|
167
164
|
apiFetch.nonceMiddleware.nonce = text;
|
|
@@ -169,7 +166,6 @@ function apiFetch(options) {
|
|
|
169
166
|
});
|
|
170
167
|
});
|
|
171
168
|
}
|
|
172
|
-
|
|
173
169
|
apiFetch.use = registerMiddleware;
|
|
174
170
|
apiFetch.setFetchHandler = setFetchHandler;
|
|
175
171
|
apiFetch.createNonceMiddleware = createNonceMiddleware;
|