Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/api-fetch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### New Features

- Add `apiFetch.unregister` to remove a registered middleware, and expose `apiFetch.httpV1Middleware` so the `X-HTTP-Method-Override` behavior can be opted out of. ([#82408](https://github.com/WordPress/gutenberg/pull/82408))
- Add `apiFetch.unregister` to remove a registered middleware, and expose `apiFetch.httpV1Middleware` so the `X-HTTP-Method-Override` behavior can be opted out of ([#82408](https://github.com/WordPress/gutenberg/pull/82408)).
- Expose `apiFetch.defaultFetchHandler` so the default fetch handler can be restored after `setFetchHandler` overrides it ([#82553](https://github.com/WordPress/gutenberg/pull/82553))

## 7.54.0 (2026-08-26)

Expand Down
6 changes: 6 additions & 0 deletions packages/api-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ apiFetch.setFetchHandler( ( options ) => {
} );
```

The default handler remains available, so it can be restored later.

```js
apiFetch.setFetchHandler( apiFetch.defaultFetchHandler );
```

## Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
Expand Down
8 changes: 8 additions & 0 deletions packages/api-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ function clearPreloadedData() {
}
}

/**
* The default fetch handler, using `window.fetch`. Exposed so it can be
* restored after `setFetchHandler` overrides it.
*
* @param nextOptions The options for the fetch.
*/
const defaultFetchHandler: FetchHandler = ( nextOptions ) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now when this becomes public API, let's improve the nextOptions name. It should be just options. The "next" prefix is appropriate for middlewares, as they work with a prev/next chain of options, and even our middlewares call the parameter options.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do a quick follow-up shortly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's PR #82588.

const { url, path, data, parse = true, ...remainingOptions } = nextOptions;
let { body, headers } = nextOptions;
Expand Down Expand Up @@ -168,6 +174,7 @@ export interface ApiFetch {
use: ( middleware: APIFetchMiddleware ) => void;
unregister: ( middleware: APIFetchMiddleware ) => boolean;
setFetchHandler: ( newFetchHandler: FetchHandler ) => void;
defaultFetchHandler: FetchHandler;
createNonceMiddleware: typeof createNonceMiddleware;
createPreloadingMiddleware: typeof createPreloadingMiddleware;
createRootURLMiddleware: typeof createRootURLMiddleware;
Expand Down Expand Up @@ -224,6 +231,7 @@ const apiFetch: ApiFetch = ( options ) => {
apiFetch.use = registerMiddleware;
apiFetch.unregister = unregisterMiddleware;
apiFetch.setFetchHandler = setFetchHandler;
apiFetch.defaultFetchHandler = defaultFetchHandler;

// Attached to the function (rather than a named export) because
// `wpScriptDefaultExport: true` flattens this module to its default
Expand Down
Loading