Skip to content

Commit

Permalink
feat(gatsby-source-drupal): Provide proxyUrl in addition to baseUrl t…
Browse files Browse the repository at this point in the history
…o allow using CDN, API gateway, etc. (#36819) (#37084)

Co-authored-by: Tomáš Fülöpp <tomi@vacilando.org>
  • Loading branch information
ViCo0TeCH and Vacilando committed Nov 23, 2022
1 parent 2bc5902 commit fac9fbc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/gatsby-source-drupal/README.md
Expand Up @@ -250,6 +250,29 @@ module.exports = {
}
```

## CDN

You can add an optional CDN or API gateway URL `proxyUrl` param. The URL can be a simple proxy of the Drupal
`baseUrl`, or another URL (even containing a path) where the Drupal JSON API resources can be retrieved.

This option is required as Drupal doesn't know about the CDN so it returns URLs pointing to the `baseUrl`. With `proxyUrl` set, the plugin will rewrite URLs returned from Drupal to keep pointing at the `proxyUrl`

```javascript
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-source-drupal`,
options: {
baseUrl: `https://live-contentacms.pantheonsite.io/`,
proxyUrl: `https://xyz.cloudfront.net/`, // optional, defaults to the value of baseUrl
apiBase: `api`, // optional, defaults to `jsonapi`
},
},
],
}
```

## GET Search Params

You can append optional GET request params to the request url using `params` option.
Expand Down
42 changes: 41 additions & 1 deletion packages/gatsby-source-drupal/src/__tests__/index.js
@@ -1,5 +1,14 @@
import got from "got"

const baseUrl = `http://fixture`
const proxyUrl = `http://fixture-proxy`

jest.mock(`got`, () =>
jest.fn(path => {
if (path.includes(proxyUrl)) {
path = path.replace(proxyUrl, baseUrl)
}

let last = ``
if (path.includes(`i18n-test`)) {
last = `i18n-test-`
Expand Down Expand Up @@ -59,7 +68,6 @@ const { handleWebhookUpdate } = require(`../utils`)
describe(`gatsby-source-drupal`, () => {
let nodes = {}
const createNodeId = id => `generated-id-${id}`
const baseUrl = `http://fixture`
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
const { objectContaining } = expect
const actions = {
Expand Down Expand Up @@ -450,6 +458,38 @@ describe(`gatsby-source-drupal`, () => {
expect(nodes[createNodeId(`und.article-3`)]).toBeDefined()
})

it(`Can use the proxyUrl plugin option to use a different API url for sourcing`, async () => {
got.mockClear()
nodes = {}
await sourceNodes(args, { baseUrl, proxyUrl })

let callSkipCount = 0
for (const [index, call] of got.mock.calls.entries()) {
if (call[0] === `http://fixture/jsonapi`) {
callSkipCount++
continue
}

expect(got).toHaveBeenNthCalledWith(
index + 1,
expect.stringContaining(proxyUrl),
expect.anything()
)
}

expect(callSkipCount).toBe(1)
expect(got).toBeCalledTimes(8)

expect(Object.keys(nodes).length).not.toEqual(0)
expect(nodes[createNodeId(`und.file-1`)]).toBeDefined()
expect(nodes[createNodeId(`und.file-2`)]).toBeDefined()
expect(nodes[createNodeId(`und.tag-1`)]).toBeDefined()
expect(nodes[createNodeId(`und.tag-2`)]).toBeDefined()
expect(nodes[createNodeId(`und.article-1`)]).toBeDefined()
expect(nodes[createNodeId(`und.article-2`)]).toBeDefined()
expect(nodes[createNodeId(`und.article-3`)]).toBeDefined()
})

it(`Verify JSON:API includes relationships`, async () => {
// Reset nodes and test includes relationships.
Object.keys(nodes).forEach(key => delete nodes[key])
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby-source-drupal/src/gatsby-node.js
Expand Up @@ -155,6 +155,7 @@ exports.sourceNodes = async (
globalReporter = reporter
const {
baseUrl,
proxyUrl = baseUrl,
apiBase = `jsonapi`,
basicAuth = {},
filters,
Expand Down Expand Up @@ -534,6 +535,11 @@ ${JSON.stringify(webhookBody, null, 4)}`
}
}

// If proxyUrl is defined, use it instead of baseUrl to get the content.
if (proxyUrl !== baseUrl) {
url = url.replace(baseUrl, proxyUrl)
}

let d
try {
d = await requestQueue.push([
Expand Down Expand Up @@ -833,6 +839,9 @@ exports.pluginOptionsSchema = ({ Joi }) =>
baseUrl: Joi.string()
.required()
.description(`The URL to root of your Drupal instance`),
proxyUrl: Joi.string().description(
`The CDN URL equivalent to your baseUrl`
),
apiBase: Joi.string().description(
`The path to the root of the JSONAPI — defaults to "jsonapi"`
),
Expand Down

0 comments on commit fac9fbc

Please sign in to comment.