Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wordpress/api-fetch

Package Overview
Dependencies
Maintainers
25
Versions
186
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 6.36.0 to 6.37.0

60

build-module/index.js

@@ -5,6 +5,6 @@ /**

import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import createNonceMiddleware from './middlewares/nonce';

@@ -20,2 +20,3 @@ import createRootURLMiddleware from './middlewares/root-url';

import { parseResponseAndNormalizeError, parseAndThrowError } from './utils/response';
/**

@@ -27,3 +28,2 @@ * Default set of header values which should be sent with every request unless

*/
const DEFAULT_HEADERS = {

@@ -36,2 +36,3 @@ // The backend uses the Accept header as a condition for considering an

};
/**

@@ -43,8 +44,7 @@ * Default set of fetch option values which should be sent with every request

*/
const DEFAULT_OPTIONS = {
credentials: 'include'
};
/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
/** @typedef {import('./types').APIFetchOptions} APIFetchOptions */

@@ -55,4 +55,4 @@

*/
const middlewares = [userLocaleMiddleware, namespaceEndpointMiddleware, httpV1Middleware, fetchAllMiddleware];
const middlewares = [userLocaleMiddleware, namespaceEndpointMiddleware, httpV1Middleware, fetchAllMiddleware];
/**

@@ -63,6 +63,6 @@ * Register a middleware

*/
function registerMiddleware(middleware) {
middlewares.unshift(middleware);
}
/**

@@ -75,4 +75,2 @@ * Checks the status of a response, throwing the Response as an error if

*/
const checkStatus = response => {

@@ -82,5 +80,5 @@ if (response.status >= 200 && response.status < 300) {

}
throw response;
};
/** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/

@@ -91,4 +89,2 @@

*/
const defaultFetchHandler = nextOptions => {

@@ -105,8 +101,11 @@ const {

headers
} = nextOptions; // Merge explicitly-provided headers with default values.
} = nextOptions;
headers = { ...DEFAULT_HEADERS,
// Merge explicitly-provided headers with default values.
headers = {
...DEFAULT_HEADERS,
...headers
}; // The `data` property is a shorthand for sending a JSON body.
};
// The `data` property is a shorthand for sending a JSON body.
if (data) {

@@ -116,5 +115,6 @@ body = JSON.stringify(data);

}
const responsePromise = window.fetch( // Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
url || path || window.location.href, { ...DEFAULT_OPTIONS,
const responsePromise = window.fetch(
// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
url || path || window.location.href, {
...DEFAULT_OPTIONS,
...remainingOptions,

@@ -128,6 +128,6 @@ body,

throw err;
} // Otherwise, there is most likely no network connection.
}
// Otherwise, there is most likely no network connection.
// Unfortunately the message might depend on the browser.
throw {

@@ -139,6 +139,6 @@ code: 'fetch_error',

};
/** @type {FetchHandler} */
let fetchHandler = defaultFetchHandler;
let fetchHandler = defaultFetchHandler;
/**

@@ -150,6 +150,6 @@ * Defines a custom fetch handler for making the requests that will override

*/
function setFetchHandler(newFetchHandler) {
fetchHandler = newFetchHandler;
}
/**

@@ -160,4 +160,2 @@ * @template T

*/
function apiFetch(options) {

@@ -169,5 +167,3 @@ // creates a nested function chain that calls all middlewares and finally the `fetchHandler`,

// ```
const enhancedHandler = middlewares.reduceRight((
/** @type {FetchHandler} */
next, middleware) => {
const enhancedHandler = middlewares.reduceRight(( /** @type {FetchHandler} */next, middleware) => {
return workingOptions => middleware(workingOptions, next);

@@ -178,6 +174,7 @@ }, fetchHandler);

return Promise.reject(error);
} // If the nonce is invalid, refresh it and try again.
}
return window // @ts-ignore
// If the nonce is invalid, refresh it and try again.
return window
// @ts-ignore
.fetch(apiFetch.nonceEndpoint).then(checkStatus).then(data => data.text()).then(text => {

@@ -190,3 +187,2 @@ // @ts-ignore

}
apiFetch.use = registerMiddleware;

@@ -193,0 +189,0 @@ apiFetch.setFetchHandler = setFetchHandler;

@@ -5,7 +5,8 @@ /**

import { addQueryArgs } from '@wordpress/url';
/**
* Internal dependencies
*/
import apiFetch from '..';
import apiFetch from '..';
/**

@@ -18,3 +19,2 @@ * Apply query arguments to both URL and Path, whichever is present.

*/
const modifyQuery = ({

@@ -24,6 +24,8 @@ path,

...options
}, queryArgs) => ({ ...options,
}, queryArgs) => ({
...options,
url: url && addQueryArgs(url, queryArgs),
path: path && addQueryArgs(path, queryArgs)
});
/**

@@ -35,5 +37,4 @@ * Duplicates parsing functionality from apiFetch.

*/
const parseResponse = response => response.json ? response.json() : Promise.reject(response);
const parseResponse = response => response.json ? response.json() : Promise.reject(response);
/**

@@ -43,4 +44,2 @@ * @param {string | null} linkHeader

*/
const parseLinkHeader = linkHeader => {

@@ -50,3 +49,2 @@ if (!linkHeader) {

}
const match = linkHeader.match(/<([^>]+)>; rel="next"/);

@@ -57,2 +55,3 @@ return match ? {

};
/**

@@ -62,4 +61,2 @@ * @param {Response} response

*/
const getNextPageUrl = response => {

@@ -71,2 +68,3 @@ const {

};
/**

@@ -76,4 +74,2 @@ * @param {import('../types').APIFetchOptions} options

*/
const requestContainsUnboundedQuery = options => {

@@ -84,2 +80,3 @@ const pathIsUnbounded = !!options.path && options.path.indexOf('per_page=-1') !== -1;

};
/**

@@ -92,4 +89,2 @@ * The REST API enforces an upper limit on the per_page option. To handle large

*/
const fetchAllMiddleware = async (options, next) => {

@@ -100,10 +95,10 @@ if (options.parse === false) {

}
if (!requestContainsUnboundedQuery(options)) {
// If neither url nor path is requesting all items, do not apply middleware.
return next(options);
} // Retrieve requested page of results.
}
const response = await apiFetch({ ...modifyQuery(options, {
// Retrieve requested page of results.
const response = await apiFetch({
...modifyQuery(options, {
per_page: 100

@@ -115,3 +110,2 @@ }),

const results = await parseResponse(response);
if (!Array.isArray(results)) {

@@ -121,17 +115,13 @@ // We have no reliable way of merging non-array results.

}
let nextPage = getNextPageUrl(response);
if (!nextPage) {
// There are no further pages to request.
return results;
} // Iteratively fetch all remaining pages until no "next" header is found.
}
let mergedResults =
/** @type {any[]} */
[].concat(results);
// Iteratively fetch all remaining pages until no "next" header is found.
let mergedResults = /** @type {any[]} */[].concat(results);
while (nextPage) {
const nextResponse = await apiFetch({ ...options,
const nextResponse = await apiFetch({
...options,
// Ensure the URL for the next page is used instead of any provided path.

@@ -147,7 +137,5 @@ path: undefined,

}
return mergedResults;
};
export default fetchAllMiddleware;
//# sourceMappingURL=fetch-all-middleware.js.map

@@ -7,2 +7,3 @@ /**

const OVERRIDE_METHODS = new Set(['PATCH', 'PUT', 'DELETE']);
/**

@@ -18,4 +19,4 @@ * Default request method.

*/
const DEFAULT_METHOD = 'GET';
const DEFAULT_METHOD = 'GET';
/**

@@ -27,3 +28,2 @@ * API Fetch middleware which overrides the request method for HTTP v1

*/
const httpV1Middleware = (options, next) => {

@@ -33,6 +33,7 @@ const {

} = options;
if (OVERRIDE_METHODS.has(method.toUpperCase())) {
options = { ...options,
headers: { ...options.headers,
options = {
...options,
headers: {
...options.headers,
'X-HTTP-Method-Override': method,

@@ -44,7 +45,5 @@ 'Content-Type': 'application/json'

}
return next(options);
};
export default httpV1Middleware;
//# sourceMappingURL=http-v1.js.map

@@ -5,7 +5,8 @@ /**

import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { parseAndThrowError, parseResponseAndNormalizeError } from '../utils/response';
import { parseAndThrowError, parseResponseAndNormalizeError } from '../utils/response';
/**

@@ -15,3 +16,2 @@ * @param {import('../types').APIFetchOptions} options

*/
function isMediaUploadRequest(options) {

@@ -22,2 +22,3 @@ const isCreateMethod = !!options.method && options.method === 'POST';

}
/**

@@ -28,4 +29,2 @@ * Middleware handling media upload failures and retries.

*/
const mediaUploadMiddleware = (options, next) => {

@@ -35,5 +34,5 @@ if (!isMediaUploadRequest(options)) {

}
let retries = 0;
const maxRetries = 5;
/**

@@ -43,3 +42,2 @@ * @param {string} attachmentId

*/
const postProcess = attachmentId => {

@@ -58,3 +56,2 @@ retries++;

}
next({

@@ -67,8 +64,7 @@ path: `/wp/v2/media/${attachmentId}?force=true`,

};
return next({ ...options,
return next({
...options,
parse: false
}).catch(response => {
const attachmentId = response.headers.get('x-wp-upload-attachment-id');
if (response.status >= 500 && response.status < 600 && attachmentId) {

@@ -82,12 +78,9 @@ return postProcess(attachmentId).catch(() => {

}
return Promise.reject(response);
});
}
return parseAndThrowError(response, options.parse);
}).then(response => parseResponseAndNormalizeError(response, options.parse));
};
export default mediaUploadMiddleware;
//# sourceMappingURL=media-upload.js.map

@@ -7,7 +7,5 @@ /**

let namespaceTrimmed, endpointTrimmed;
if (typeof options.namespace === 'string' && typeof options.endpoint === 'string') {
namespaceTrimmed = options.namespace.replace(/^\/|\/$/g, '');
endpointTrimmed = options.endpoint.replace(/^\//, '');
if (endpointTrimmed) {

@@ -19,11 +17,10 @@ path = namespaceTrimmed + '/' + endpointTrimmed;

}
delete options.namespace;
delete options.endpoint;
return next({ ...options,
return next({
...options,
path
});
};
export default namespaceAndEndpointMiddleware;
//# sourceMappingURL=namespace-endpoint.js.map

@@ -12,5 +12,6 @@ /**

headers = {}
} = options; // If an 'X-WP-Nonce' header (or any case-insensitive variation
} = options;
// If an 'X-WP-Nonce' header (or any case-insensitive variation
// thereof) was specified, no need to add a nonce header.
for (const headerName in headers) {

@@ -21,5 +22,6 @@ if (headerName.toLowerCase() === 'x-wp-nonce' && headers[headerName] === middleware.nonce) {

}
return next({ ...options,
headers: { ...headers,
return next({
...options,
headers: {
...headers,
'X-WP-Nonce': middleware.nonce

@@ -29,8 +31,6 @@ }

};
middleware.nonce = nonce;
return middleware;
}
export default createNonceMiddleware;
//# sourceMappingURL=nonce.js.map

@@ -5,2 +5,3 @@ /**

import { addQueryArgs, getQueryArgs, normalizePath } from '@wordpress/url';
/**

@@ -10,3 +11,2 @@ * @param {Record<string, any>} preloadedData

*/
function createPreloadingMiddleware(preloadedData) {

@@ -19,5 +19,3 @@ const cache = Object.fromEntries(Object.entries(preloadedData).map(([path, data]) => [normalizePath(path), data]));

/** @type {string | void} */
let rawPath = options.path;
if (!rawPath && options.url) {

@@ -28,3 +26,2 @@ const {

} = getQueryArgs(options.url);
if (typeof pathFromQuery === 'string') {

@@ -34,25 +31,24 @@ rawPath = addQueryArgs(pathFromQuery, queryArgs);

}
if (typeof rawPath !== 'string') {
return next(options);
}
const method = options.method || 'GET';
const path = normalizePath(rawPath);
if ('GET' === method && cache[path]) {
const cacheData = cache[path]; // Unsetting the cache key ensures that the data is only used a single time.
const cacheData = cache[path];
// Unsetting the cache key ensures that the data is only used a single time.
delete cache[path];
return prepareResponse(cacheData, !!parse);
} else if ('OPTIONS' === method && cache[method] && cache[method][path]) {
const cacheData = cache[method][path]; // Unsetting the cache key ensures that the data is only used a single time.
const cacheData = cache[method][path];
// Unsetting the cache key ensures that the data is only used a single time.
delete cache[method][path];
return prepareResponse(cacheData, !!parse);
}
return next(options);
};
}
/**

@@ -65,4 +61,2 @@ * This is a helper function that sends a success response.

*/
function prepareResponse(responseData, parse) {

@@ -75,4 +69,3 @@ return Promise.resolve(parse ? responseData.body : new window.Response(JSON.stringify(responseData.body), {

}
export default createPreloadingMiddleware;
//# sourceMappingURL=preloading.js.map

@@ -5,2 +5,3 @@ /**

import namespaceAndEndpointMiddleware from './namespace-endpoint';
/**

@@ -10,3 +11,2 @@ * @param {string} rootURL

*/
const createRootURLMiddleware = rootURL => (options, next) => {

@@ -17,21 +17,18 @@ return namespaceAndEndpointMiddleware(options, optionsWithPath => {

let apiRoot;
if (typeof path === 'string') {
apiRoot = rootURL;
if (-1 !== rootURL.indexOf('?')) {
path = path.replace('?', '&');
}
path = path.replace(/^\//, '');
path = path.replace(/^\//, ''); // API root may already include query parameter prefix if site is
// API root may already include query parameter prefix if site is
// configured to use plain permalinks.
if ('string' === typeof apiRoot && -1 !== apiRoot.indexOf('?')) {
path = path.replace('?', '&');
}
url = apiRoot + path;
}
return next({ ...optionsWithPath,
return next({
...optionsWithPath,
url

@@ -41,4 +38,3 @@ });

};
export default createRootURLMiddleware;
//# sourceMappingURL=root-url.js.map

@@ -5,2 +5,3 @@ /**

import { addQueryArgs, hasQueryArg } from '@wordpress/url';
/**

@@ -13,3 +14,2 @@ * This appends a `wp_theme_preview` parameter to the REST API request URL if

*/
const createThemePreviewMiddleware = themePath => (options, next) => {

@@ -21,3 +21,2 @@ if (typeof options.url === 'string' && !hasQueryArg(options.url, 'wp_theme_preview')) {

}
if (typeof options.path === 'string' && !hasQueryArg(options.path, 'wp_theme_preview')) {

@@ -28,7 +27,5 @@ options.path = addQueryArgs(options.path, {

}
return next(options);
};
export default createThemePreviewMiddleware;
//# sourceMappingURL=theme-preview.js.map

@@ -5,6 +5,6 @@ /**

import { addQueryArgs, hasQueryArg } from '@wordpress/url';
/**
* @type {import('../types').APIFetchMiddleware}
*/
const userLocaleMiddleware = (options, next) => {

@@ -16,3 +16,2 @@ if (typeof options.url === 'string' && !hasQueryArg(options.url, '_locale')) {

}
if (typeof options.path === 'string' && !hasQueryArg(options.path, '_locale')) {

@@ -23,7 +22,5 @@ options.path = addQueryArgs(options.path, {

}
return next(options);
};
export default userLocaleMiddleware;
//# sourceMappingURL=user-locale.js.map

@@ -5,2 +5,3 @@ /**

import { __ } from '@wordpress/i18n';
/**

@@ -14,3 +15,2 @@ * Parses the apiFetch response.

*/
const parseResponse = (response, shouldParseResponse = true) => {

@@ -21,8 +21,7 @@ if (shouldParseResponse) {

}
return response.json ? response.json() : Promise.reject(response);
}
return response;
};
/**

@@ -35,4 +34,2 @@ * Calls the `json` function on the Response, throwing an error if the response

*/
const parseJsonAndNormalizeError = response => {

@@ -43,7 +40,5 @@ const invalidJsonError = {

};
if (!response || !response.json) {
throw invalidJsonError;
}
return response.json().catch(() => {

@@ -53,2 +48,3 @@ throw invalidJsonError;

};
/**

@@ -62,7 +58,6 @@ * Parses the apiFetch response properly and normalize response errors.

*/
export const parseResponseAndNormalizeError = (response, shouldParseResponse = true) => {
return Promise.resolve(parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse));
};
/**

@@ -75,3 +70,2 @@ * Parses a response, throwing an error if parsing the response fails.

*/
export function parseAndThrowError(response, shouldParseResponse = true) {

@@ -81,3 +75,2 @@ if (!shouldParseResponse) {

}
return parseJsonAndNormalizeError(response).then(error => {

@@ -84,0 +77,0 @@ const unknownError = {

@@ -34,8 +34,8 @@ export default apiFetch;

declare function setFetchHandler(newFetchHandler: FetchHandler): void;
import createNonceMiddleware from "./middlewares/nonce";
import createPreloadingMiddleware from "./middlewares/preloading";
import createRootURLMiddleware from "./middlewares/root-url";
import fetchAllMiddleware from "./middlewares/fetch-all-middleware";
import mediaUploadMiddleware from "./middlewares/media-upload";
import createThemePreviewMiddleware from "./middlewares/theme-preview";
import createNonceMiddleware from './middlewares/nonce';
import createPreloadingMiddleware from './middlewares/preloading';
import createRootURLMiddleware from './middlewares/root-url';
import fetchAllMiddleware from './middlewares/fetch-all-middleware';
import mediaUploadMiddleware from './middlewares/media-upload';
import createThemePreviewMiddleware from './middlewares/theme-preview';
//# sourceMappingURL=index.d.ts.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,25 +8,13 @@ value: true

exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _nonce = _interopRequireDefault(require("./middlewares/nonce"));
var _rootUrl = _interopRequireDefault(require("./middlewares/root-url"));
var _preloading = _interopRequireDefault(require("./middlewares/preloading"));
var _fetchAllMiddleware = _interopRequireDefault(require("./middlewares/fetch-all-middleware"));
var _namespaceEndpoint = _interopRequireDefault(require("./middlewares/namespace-endpoint"));
var _httpV = _interopRequireDefault(require("./middlewares/http-v1"));
var _userLocale = _interopRequireDefault(require("./middlewares/user-locale"));
var _mediaUpload = _interopRequireDefault(require("./middlewares/media-upload"));
var _themePreview = _interopRequireDefault(require("./middlewares/theme-preview"));
var _response = require("./utils/response");
/**

@@ -54,2 +41,3 @@ * WordPress dependencies

};
/**

@@ -61,8 +49,7 @@ * Default set of fetch option values which should be sent with every request

*/
const DEFAULT_OPTIONS = {
credentials: 'include'
};
/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
/** @typedef {import('./types').APIFetchOptions} APIFetchOptions */

@@ -73,4 +60,4 @@

*/
const middlewares = [_userLocale.default, _namespaceEndpoint.default, _httpV.default, _fetchAllMiddleware.default];
const middlewares = [_userLocale.default, _namespaceEndpoint.default, _httpV.default, _fetchAllMiddleware.default];
/**

@@ -81,6 +68,6 @@ * Register a middleware

*/
function registerMiddleware(middleware) {
middlewares.unshift(middleware);
}
/**

@@ -93,4 +80,2 @@ * Checks the status of a response, throwing the Response as an error if

*/
const checkStatus = response => {

@@ -100,5 +85,5 @@ if (response.status >= 200 && response.status < 300) {

}
throw response;
};
/** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/

@@ -109,4 +94,2 @@

*/
const defaultFetchHandler = nextOptions => {

@@ -123,8 +106,11 @@ const {

headers
} = nextOptions; // Merge explicitly-provided headers with default values.
} = nextOptions;
headers = { ...DEFAULT_HEADERS,
// Merge explicitly-provided headers with default values.
headers = {
...DEFAULT_HEADERS,
...headers
}; // The `data` property is a shorthand for sending a JSON body.
};
// The `data` property is a shorthand for sending a JSON body.
if (data) {

@@ -134,5 +120,6 @@ body = JSON.stringify(data);

}
const responsePromise = window.fetch( // Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
url || path || window.location.href, { ...DEFAULT_OPTIONS,
const responsePromise = window.fetch(
// Fall back to explicitly passing `window.location` which is the behavior if `undefined` is passed.
url || path || window.location.href, {
...DEFAULT_OPTIONS,
...remainingOptions,

@@ -146,6 +133,6 @@ body,

throw err;
} // Otherwise, there is most likely no network connection.
}
// Otherwise, there is most likely no network connection.
// Unfortunately the message might depend on the browser.
throw {

@@ -157,6 +144,6 @@ code: 'fetch_error',

};
/** @type {FetchHandler} */
let fetchHandler = defaultFetchHandler;
let fetchHandler = defaultFetchHandler;
/**

@@ -168,6 +155,6 @@ * Defines a custom fetch handler for making the requests that will override

*/
function setFetchHandler(newFetchHandler) {
fetchHandler = newFetchHandler;
}
/**

@@ -178,4 +165,2 @@ * @template T

*/
function apiFetch(options) {

@@ -187,5 +172,3 @@ // creates a nested function chain that calls all middlewares and finally the `fetchHandler`,

// ```
const enhancedHandler = middlewares.reduceRight((
/** @type {FetchHandler} */
next, middleware) => {
const enhancedHandler = middlewares.reduceRight(( /** @type {FetchHandler} */next, middleware) => {
return workingOptions => middleware(workingOptions, next);

@@ -196,6 +179,7 @@ }, fetchHandler);

return Promise.reject(error);
} // If the nonce is invalid, refresh it and try again.
}
return window // @ts-ignore
// If the nonce is invalid, refresh it and try again.
return window
// @ts-ignore
.fetch(apiFetch.nonceEndpoint).then(checkStatus).then(data => data.text()).then(text => {

@@ -208,3 +192,2 @@ // @ts-ignore

}
apiFetch.use = registerMiddleware;

@@ -211,0 +194,0 @@ apiFetch.setFetchHandler = setFetchHandler;

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,7 +8,4 @@ value: true

exports.default = void 0;
var _url = require("@wordpress/url");
var _ = _interopRequireDefault(require(".."));
/**

@@ -34,6 +30,8 @@ * WordPress dependencies

...options
}, queryArgs) => ({ ...options,
}, queryArgs) => ({
...options,
url: url && (0, _url.addQueryArgs)(url, queryArgs),
path: path && (0, _url.addQueryArgs)(path, queryArgs)
});
/**

@@ -45,5 +43,4 @@ * Duplicates parsing functionality from apiFetch.

*/
const parseResponse = response => response.json ? response.json() : Promise.reject(response);
const parseResponse = response => response.json ? response.json() : Promise.reject(response);
/**

@@ -53,4 +50,2 @@ * @param {string | null} linkHeader

*/
const parseLinkHeader = linkHeader => {

@@ -60,3 +55,2 @@ if (!linkHeader) {

}
const match = linkHeader.match(/<([^>]+)>; rel="next"/);

@@ -67,2 +61,3 @@ return match ? {

};
/**

@@ -72,4 +67,2 @@ * @param {Response} response

*/
const getNextPageUrl = response => {

@@ -81,2 +74,3 @@ const {

};
/**

@@ -86,4 +80,2 @@ * @param {import('../types').APIFetchOptions} options

*/
const requestContainsUnboundedQuery = options => {

@@ -94,2 +86,3 @@ const pathIsUnbounded = !!options.path && options.path.indexOf('per_page=-1') !== -1;

};
/**

@@ -102,4 +95,2 @@ * The REST API enforces an upper limit on the per_page option. To handle large

*/
const fetchAllMiddleware = async (options, next) => {

@@ -110,10 +101,10 @@ if (options.parse === false) {

}
if (!requestContainsUnboundedQuery(options)) {
// If neither url nor path is requesting all items, do not apply middleware.
return next(options);
} // Retrieve requested page of results.
}
const response = await (0, _.default)({ ...modifyQuery(options, {
// Retrieve requested page of results.
const response = await (0, _.default)({
...modifyQuery(options, {
per_page: 100

@@ -125,3 +116,2 @@ }),

const results = await parseResponse(response);
if (!Array.isArray(results)) {

@@ -131,17 +121,13 @@ // We have no reliable way of merging non-array results.

}
let nextPage = getNextPageUrl(response);
if (!nextPage) {
// There are no further pages to request.
return results;
} // Iteratively fetch all remaining pages until no "next" header is found.
}
let mergedResults =
/** @type {any[]} */
[].concat(results);
// Iteratively fetch all remaining pages until no "next" header is found.
let mergedResults = /** @type {any[]} */[].concat(results);
while (nextPage) {
const nextResponse = await (0, _.default)({ ...options,
const nextResponse = await (0, _.default)({
...options,
// Ensure the URL for the next page is used instead of any provided path.

@@ -157,8 +143,6 @@ path: undefined,

}
return mergedResults;
};
var _default = fetchAllMiddleware;
exports.default = _default;
//# sourceMappingURL=fetch-all-middleware.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -15,2 +14,3 @@ * Set of HTTP methods which are eligible to be overridden.

const OVERRIDE_METHODS = new Set(['PATCH', 'PUT', 'DELETE']);
/**

@@ -26,4 +26,4 @@ * Default request method.

*/
const DEFAULT_METHOD = 'GET';
const DEFAULT_METHOD = 'GET';
/**

@@ -35,3 +35,2 @@ * API Fetch middleware which overrides the request method for HTTP v1

*/
const httpV1Middleware = (options, next) => {

@@ -41,6 +40,7 @@ const {

} = options;
if (OVERRIDE_METHODS.has(method.toUpperCase())) {
options = { ...options,
headers: { ...options.headers,
options = {
...options,
headers: {
...options.headers,
'X-HTTP-Method-Override': method,

@@ -52,8 +52,6 @@ 'Content-Type': 'application/json'

}
return next(options);
};
var _default = httpV1Middleware;
exports.default = _default;
//# sourceMappingURL=http-v1.js.map

@@ -7,7 +7,4 @@ "use strict";

exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _response = require("../utils/response");
/**

@@ -30,2 +27,3 @@ * WordPress dependencies

}
/**

@@ -36,4 +34,2 @@ * Middleware handling media upload failures and retries.

*/
const mediaUploadMiddleware = (options, next) => {

@@ -43,5 +39,5 @@ if (!isMediaUploadRequest(options)) {

}
let retries = 0;
const maxRetries = 5;
/**

@@ -51,3 +47,2 @@ * @param {string} attachmentId

*/
const postProcess = attachmentId => {

@@ -66,3 +61,2 @@ retries++;

}
next({

@@ -75,8 +69,7 @@ path: `/wp/v2/media/${attachmentId}?force=true`,

};
return next({ ...options,
return next({
...options,
parse: false
}).catch(response => {
const attachmentId = response.headers.get('x-wp-upload-attachment-id');
if (response.status >= 500 && response.status < 600 && attachmentId) {

@@ -90,13 +83,10 @@ return postProcess(attachmentId).catch(() => {

}
return Promise.reject(response);
});
}
return (0, _response.parseAndThrowError)(response, options.parse);
}).then(response => (0, _response.parseResponseAndNormalizeError)(response, options.parse));
};
var _default = mediaUploadMiddleware;
exports.default = _default;
//# sourceMappingURL=media-upload.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -15,7 +14,5 @@ * @type {import('../types').APIFetchMiddleware}

let namespaceTrimmed, endpointTrimmed;
if (typeof options.namespace === 'string' && typeof options.endpoint === 'string') {
namespaceTrimmed = options.namespace.replace(/^\/|\/$/g, '');
endpointTrimmed = options.endpoint.replace(/^\//, '');
if (endpointTrimmed) {

@@ -27,12 +24,11 @@ path = namespaceTrimmed + '/' + endpointTrimmed;

}
delete options.namespace;
delete options.endpoint;
return next({ ...options,
return next({
...options,
path
});
};
var _default = namespaceAndEndpointMiddleware;
exports.default = _default;
//# sourceMappingURL=namespace-endpoint.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -20,5 +19,6 @@ * @param {string} nonce

headers = {}
} = options; // If an 'X-WP-Nonce' header (or any case-insensitive variation
} = options;
// If an 'X-WP-Nonce' header (or any case-insensitive variation
// thereof) was specified, no need to add a nonce header.
for (const headerName in headers) {

@@ -29,5 +29,6 @@ if (headerName.toLowerCase() === 'x-wp-nonce' && headers[headerName] === middleware.nonce) {

}
return next({ ...options,
headers: { ...headers,
return next({
...options,
headers: {
...headers,
'X-WP-Nonce': middleware.nonce

@@ -37,9 +38,7 @@ }

};
middleware.nonce = nonce;
return middleware;
}
var _default = createNonceMiddleware;
exports.default = _default;
//# sourceMappingURL=nonce.js.map

@@ -7,5 +7,3 @@ "use strict";

exports.default = void 0;
var _url = require("@wordpress/url");
/**

@@ -26,5 +24,3 @@ * WordPress dependencies

/** @type {string | void} */
let rawPath = options.path;
if (!rawPath && options.url) {

@@ -35,3 +31,2 @@ const {

} = (0, _url.getQueryArgs)(options.url);
if (typeof pathFromQuery === 'string') {

@@ -41,25 +36,24 @@ rawPath = (0, _url.addQueryArgs)(pathFromQuery, queryArgs);

}
if (typeof rawPath !== 'string') {
return next(options);
}
const method = options.method || 'GET';
const path = (0, _url.normalizePath)(rawPath);
if ('GET' === method && cache[path]) {
const cacheData = cache[path]; // Unsetting the cache key ensures that the data is only used a single time.
const cacheData = cache[path];
// Unsetting the cache key ensures that the data is only used a single time.
delete cache[path];
return prepareResponse(cacheData, !!parse);
} else if ('OPTIONS' === method && cache[method] && cache[method][path]) {
const cacheData = cache[method][path]; // Unsetting the cache key ensures that the data is only used a single time.
const cacheData = cache[method][path];
// Unsetting the cache key ensures that the data is only used a single time.
delete cache[method][path];
return prepareResponse(cacheData, !!parse);
}
return next(options);
};
}
/**

@@ -72,4 +66,2 @@ * This is a helper function that sends a success response.

*/
function prepareResponse(responseData, parse) {

@@ -82,5 +74,4 @@ return Promise.resolve(parse ? responseData.body : new window.Response(JSON.stringify(responseData.body), {

}
var _default = createPreloadingMiddleware;
exports.default = _default;
//# sourceMappingURL=preloading.js.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,5 +8,3 @@ value: true

exports.default = void 0;
var _namespaceEndpoint = _interopRequireDefault(require("./namespace-endpoint"));
/**

@@ -26,21 +23,18 @@ * Internal dependencies

let apiRoot;
if (typeof path === 'string') {
apiRoot = rootURL;
if (-1 !== rootURL.indexOf('?')) {
path = path.replace('?', '&');
}
path = path.replace(/^\//, '');
path = path.replace(/^\//, ''); // API root may already include query parameter prefix if site is
// API root may already include query parameter prefix if site is
// configured to use plain permalinks.
if ('string' === typeof apiRoot && -1 !== apiRoot.indexOf('?')) {
path = path.replace('?', '&');
}
url = apiRoot + path;
}
return next({ ...optionsWithPath,
return next({
...optionsWithPath,
url

@@ -50,5 +44,4 @@ });

};
var _default = createRootURLMiddleware;
exports.default = _default;
//# sourceMappingURL=root-url.js.map

@@ -7,5 +7,3 @@ "use strict";

exports.default = void 0;
var _url = require("@wordpress/url");
/**

@@ -28,3 +26,2 @@ * WordPress dependencies

}
if (typeof options.path === 'string' && !(0, _url.hasQueryArg)(options.path, 'wp_theme_preview')) {

@@ -35,8 +32,6 @@ options.path = (0, _url.addQueryArgs)(options.path, {

}
return next(options);
};
var _default = createThemePreviewMiddleware;
exports.default = _default;
//# sourceMappingURL=theme-preview.js.map

@@ -7,5 +7,3 @@ "use strict";

exports.default = void 0;
var _url = require("@wordpress/url");
/**

@@ -24,3 +22,2 @@ * WordPress dependencies

}
if (typeof options.path === 'string' && !(0, _url.hasQueryArg)(options.path, '_locale')) {

@@ -31,8 +28,6 @@ options.path = (0, _url.addQueryArgs)(options.path, {

}
return next(options);
};
var _default = userLocaleMiddleware;
exports.default = _default;
//# sourceMappingURL=user-locale.js.map

@@ -8,5 +8,3 @@ "use strict";

exports.parseResponseAndNormalizeError = void 0;
var _i18n = require("@wordpress/i18n");
/**

@@ -29,8 +27,7 @@ * WordPress dependencies

}
return response.json ? response.json() : Promise.reject(response);
}
return response;
};
/**

@@ -43,4 +40,2 @@ * Calls the `json` function on the Response, throwing an error if the response

*/
const parseJsonAndNormalizeError = response => {

@@ -51,7 +46,5 @@ const invalidJsonError = {

};
if (!response || !response.json) {
throw invalidJsonError;
}
return response.json().catch(() => {

@@ -61,2 +54,3 @@ throw invalidJsonError;

};
/**

@@ -70,7 +64,6 @@ * Parses the apiFetch response properly and normalize response errors.

*/
const parseResponseAndNormalizeError = (response, shouldParseResponse = true) => {
return Promise.resolve(parseResponse(response, shouldParseResponse)).catch(res => parseAndThrowError(res, shouldParseResponse));
};
/**

@@ -83,6 +76,3 @@ * Parses a response, throwing an error if parsing the response fails.

*/
exports.parseResponseAndNormalizeError = parseResponseAndNormalizeError;
function parseAndThrowError(response, shouldParseResponse = true) {

@@ -92,3 +82,2 @@ if (!shouldParseResponse) {

}
return parseJsonAndNormalizeError(response).then(error => {

@@ -95,0 +84,0 @@ const unknownError = {

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

## 6.37.0 (2023-08-16)
## 6.36.0 (2023-08-10)

@@ -7,0 +9,0 @@

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

@@ -31,4 +31,4 @@ "author": "The WordPress Contributors",

"@babel/runtime": "^7.16.0",
"@wordpress/i18n": "^4.39.0",
"@wordpress/url": "^3.40.0"
"@wordpress/i18n": "^4.40.0",
"@wordpress/url": "^3.41.0"
},

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

},
"gitHead": "b898cf1dc8e70841d1647ea0994ac6278acc18a7"
"gitHead": "78a288d55b83a713b2f7d98d5a855c0771a2afc6"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc