Skip to content

Commit 239101e

Browse files
aposudevskyDSchau
authored andcommitted
feat(gatsby-source-drupal): added Request Headers and GET Request Params options (#12180)
* feat(gatsby-source-drupal): added Request Headers and GET Request Params options * chore: format
1 parent e3971f9 commit 239101e

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

packages/gatsby-source-drupal/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,50 @@ module.exports = {
9898
}
9999
```
100100

101+
## Request Headers
102+
103+
You can add optional request headers to the request using `headers` param.
104+
105+
```javascript
106+
// In your gatsby-config.js
107+
module.exports = {
108+
plugins: [
109+
{
110+
resolve: `gatsby-source-drupal`,
111+
options: {
112+
baseUrl: `https://live-contentacms.pantheonsite.io/`,
113+
apiBase: `api`, // optional, defaults to `jsonapi`
114+
headers: {
115+
Host: "https://example.com", // any valid request header here
116+
},
117+
},
118+
},
119+
],
120+
}
121+
```
122+
123+
## GET Params
124+
125+
You can append optional GET request params to the request url using `params` option.
126+
127+
```javascript
128+
// In your gatsby-config.js
129+
module.exports = {
130+
plugins: [
131+
{
132+
resolve: `gatsby-source-drupal`,
133+
options: {
134+
baseUrl: `https://live-contentacms.pantheonsite.io/`,
135+
apiBase: `api`, // optional, defaults to `jsonapi`
136+
params: {
137+
"api-key": "your-api-key-header-here", // any valid key value pair here
138+
},
139+
},
140+
},
141+
],
142+
}
143+
```
144+
101145
## How to query
102146

103147
You can query nodes created from Drupal like the following:

packages/gatsby-source-drupal/src/gatsby-node.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,8 @@ const { URL } = require(`url`)
55
const { nodeFromData } = require(`./normalize`)
66

77
exports.sourceNodes = async (
8-
{
9-
actions,
10-
getNode,
11-
hasNodeChanged,
12-
store,
13-
cache,
14-
createNodeId,
15-
createContentDigest,
16-
},
17-
{ baseUrl, apiBase, basicAuth, filters }
8+
{ actions, store, cache, createNodeId, createContentDigest },
9+
{ baseUrl, apiBase, basicAuth, filters, headers, params }
1810
) => {
1911
const { createNode } = actions
2012

@@ -40,7 +32,11 @@ exports.sourceNodes = async (
4032
// .lastFetched
4133
// }
4234

43-
const data = await axios.get(`${baseUrl}/${apiBase}`, { auth: basicAuth })
35+
const data = await axios.get(`${baseUrl}/${apiBase}`, {
36+
auth: basicAuth,
37+
headers,
38+
params,
39+
})
4440
const allData = await Promise.all(
4541
_.map(data.data.links, async (url, type) => {
4642
if (type === `self`) return
@@ -63,7 +59,11 @@ exports.sourceNodes = async (
6359

6460
let d
6561
try {
66-
d = await axios.get(url, { auth: basicAuth })
62+
d = await axios.get(url, {
63+
auth: basicAuth,
64+
headers,
65+
params,
66+
})
6767
} catch (error) {
6868
if (error.response && error.response.status == 405) {
6969
// The endpoint doesn't support the GET method, so just skip it.

0 commit comments

Comments
 (0)