Fetcher

Using a custom fetcher with DrupalClient.


⚠️

The `DrupalClient` is available in `next-drupal ^1.3.0` as experimental.

The `DrupalClient` uses Next.js polyfilled `fetch` as the default fetcher.

You can provide your own using the `fetcher` option.

Example

Here's how you can replace the default fetcher with cross-fetch.

We're using cross-fetch instead of node-fetch because it is compatible with the built-in fetch and uses the same signature.

lib/drupal.ts

import { Experiment_DrupalClient as DrupalClient } from "next-drupal"
import crossFetch from "cross-fetch"
const customFetcher = (url, options) => {
const { withAuth, ...opts } = options
if (withAuth) {
// Make additional requests to fetch a bearer token
// Or any other Authorization headers.
}
return crossFetch(url, {
...opts,
// Pass in additional options. Example: agent.
})
}
export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
fetcher: customFetcher,
}
)