Skip to content

Commit

Permalink
Implement setFetchHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivica Batinic committed Sep 25, 2019
1 parent 5327c03 commit 72e3505
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 14 additions & 2 deletions packages/api-fetch/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# API Fetch

Utility to make WordPress REST API requests. It's a wrapper around `window.fetch`.
Utility to make WordPress REST API requests. It's a wrapper around `fetch`.

## Installation

Expand Down Expand Up @@ -89,7 +89,7 @@ apiFetch.use( apiFetch.createRootURLMiddleware( rootURL ) );

### Custom fetch handler

The `api-fetch` package uses `window.fetch` for making the requests but you can use a custom fetch handler by using the `setFetchHandler` method. The custom fetch handler will receive the `options` passed to the `apiFetch` calls.
The `api-fetch` package uses `fetch` for making the requests but you can use a custom fetch handler by using the `setFetchHandler` method. The custom fetch handler will receive the `options` passed to the `apiFetch` calls.

**Example**

Expand All @@ -111,3 +111,15 @@ apiFetch.setFetchHandler( ( options ) => {
```

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>

### Custom fetch reference
For polyfilling fetch on Node environment.

**Example**

```js
import apiFetch from '@wordpress/api-fetch';
import fetch from 'isomorphic-fetch';

apiFetch.setFetchReference( fetch );
```
18 changes: 15 additions & 3 deletions packages/api-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const middlewares = [
fetchAllMiddleware,
];

let fetchReference = typeof window !== 'undefined' && 'fetch' in window && typeof window.fetch === 'function' && window.fetch.bind( window );

/**
* Defines a custom fetch reference to support fetch on Node
*
* @param {Function} newFetchReference The new fetch reference
*/
function setFetchReference( newFetchReference ) {
fetchReference = newFetchReference;
}

function registerMiddleware( middleware ) {
middlewares.unshift( middleware );
}
Expand All @@ -70,7 +81,7 @@ const defaultFetchHandler = ( nextOptions ) => {
headers[ 'Content-Type' ] = 'application/json';
}

const responsePromise = window.fetch(
const responsePromise = fetchReference(
url || path,
{
...DEFAULT_OPTIONS,
Expand Down Expand Up @@ -128,7 +139,7 @@ let fetchHandler = defaultFetchHandler;

/**
* Defines a custom fetch handler for making the requests that will override
* the default one using window.fetch
* the default one using fetch
*
* @param {Function} newFetchHandler The new fetch handler
*/
Expand Down Expand Up @@ -158,7 +169,7 @@ function apiFetch( options ) {
}

// If the nonce is invalid, refresh it and try again.
window.fetch( apiFetch.nonceEndpoint )
fetchReference( apiFetch.nonceEndpoint )
.then( checkStatus )
.then( ( data ) => data.text() )
.then( ( text ) => {
Expand All @@ -174,6 +185,7 @@ function apiFetch( options ) {

apiFetch.use = registerMiddleware;
apiFetch.setFetchHandler = setFetchHandler;
apiFetch.setFetchReference = setFetchReference;

apiFetch.createNonceMiddleware = createNonceMiddleware;
apiFetch.createPreloadingMiddleware = createPreloadingMiddleware;
Expand Down

0 comments on commit 72e3505

Please sign in to comment.