diff --git a/examples/using-drupal/package.json b/examples/using-drupal/package.json index f5e42adee0f6c..7a71600deac77 100644 --- a/examples/using-drupal/package.json +++ b/examples/using-drupal/package.json @@ -12,7 +12,7 @@ "gatsby-plugin-offline": "^2.0.5", "gatsby-plugin-sharp": "^2.0.5", "gatsby-plugin-typography": "^2.2.0", - "gatsby-source-drupal": "^2.2.0", + "gatsby-source-drupal": "^3.0.9", "gatsby-transformer-sharp": "^2.1.1", "glamor": "^2.20.40", "gray-percentage": "^2.0.0", diff --git a/packages/gatsby-source-drupal/README.md b/packages/gatsby-source-drupal/README.md index 02c09dbcf620d..56e5313a32b67 100644 --- a/packages/gatsby-source-drupal/README.md +++ b/packages/gatsby-source-drupal/README.md @@ -21,15 +21,41 @@ been used since jsonapi version `8.x-1.0-alpha4`. ```javascript // In your gatsby-config.js -plugins: [ - { - resolve: `gatsby-source-drupal`, - options: { - baseUrl: `https://live-contentacms.pantheonsite.io/`, - apiBase: `api`, // optional, defaults to `jsonapi` +module.exports = { + plugins: [ + { + resolve: `gatsby-source-drupal`, + options: { + baseUrl: `https://live-contentacms.pantheonsite.io/`, + apiBase: `api`, // optional, defaults to `jsonapi` + }, }, - }, -] + ], +} +``` + +### Basic Auth + +You can use `basicAuth` option if your site is protected by basicauth. +First, you need a way to pass environment variables to the build process, so secrets and other secured data aren't committed to source control. We recommend using [`dotenv`][dotenv] which will then expose environment variables. [Read more about dotenv and using environment variables here][envvars]. Then we can _use_ these environment variables and configure our plugin. + +```javascript +// In your gatsby-config.js +module.exports = { + plugins: [ + { + resolve: `gatsby-source-drupal`, + options: { + baseUrl: `https://live-contentacms.pantheonsite.io/`, + apiBase: `api`, // optional, defaults to `jsonapi` + basicAuth: { + username: process.env.BASIC_AUTH_USERNAME, + password: process.env.BASIC_AUTH_PASSWORD, + }, + }, + }, + ], +} ``` ## How to query @@ -49,3 +75,6 @@ You can query nodes created from Drupal like the following: } } ``` + +[dotenv]: https://github.com/motdotla/dotenv +[envvars]: https://www.gatsbyjs.org/docs/environment-variables diff --git a/packages/gatsby-source-drupal/src/gatsby-node.js b/packages/gatsby-source-drupal/src/gatsby-node.js index 217fe331f50f5..7ca564a6b3659 100644 --- a/packages/gatsby-source-drupal/src/gatsby-node.js +++ b/packages/gatsby-source-drupal/src/gatsby-node.js @@ -14,7 +14,7 @@ const createContentDigest = obj => exports.sourceNodes = async ( { actions, getNode, hasNodeChanged, store, cache, createNodeId }, - { baseUrl, apiBase } + { baseUrl, apiBase, basicAuth } ) => { const { createNode } = actions @@ -40,7 +40,7 @@ exports.sourceNodes = async ( // .lastFetched // } - const data = await axios.get(`${baseUrl}/${apiBase}`) + const data = await axios.get(`${baseUrl}/${apiBase}`, { auth: basicAuth }) const allData = await Promise.all( _.map(data.data.links, async (url, type) => { if (type === `self`) return @@ -54,7 +54,7 @@ exports.sourceNodes = async ( let d try { - d = await axios.get(url) + d = await axios.get(url, { auth: basicAuth }) } catch (error) { if (error.response && error.response.status == 405) { // The endpoint doesn't support the GET method, so just skip it.