Skip to content

Commit 7a8e41a

Browse files
iFlameingwardpeet
authored andcommitted
feat(createRemoteFileNode): allow passing headers to request (#11682)
## Description This allows a user to pass header to create-remote-file-node for fetching data which is in private mode in cms. thanks to @datakurre who also helped me in creating this pr.
1 parent 65693d3 commit 7a8e41a

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

packages/gatsby-source-filesystem/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ createRemoteFileNode({
182182
// Adds htaccess authentication to the download request if passed in.
183183
auth: { htaccess_user: `USER`, htaccess_pass: `PASSWORD` },
184184

185+
// OPTIONAL
186+
// Adds extra http headers to download request if passed in.
187+
httpHeaders: { Authorization: `Bearer someAccessToken` },
188+
185189
// OPTIONAL
186190
// Sets the file extension
187191
ext: ".jpg",

packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js

+17
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ describe(`create-remote-file-node`, () => {
174174
})
175175
)
176176
})
177+
178+
it(`passes custom http heades, if defined`, async () => {
179+
await setup({
180+
httpHeaders: {
181+
Authorization: `Bearer foobar`,
182+
},
183+
})
184+
185+
expect(got.stream).toHaveBeenCalledWith(
186+
expect.any(String),
187+
expect.objectContaining({
188+
headers: expect.objectContaining({
189+
Authorization: `Bearer foobar`,
190+
}),
191+
})
192+
)
193+
})
177194
})
178195
})
179196

packages/gatsby-source-filesystem/src/create-remote-file-node.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ async function processRemoteNode({
186186
createNode,
187187
parentNodeId,
188188
auth = {},
189+
httpHeaders = {},
189190
createNodeId,
190191
ext,
191192
name,
@@ -201,7 +202,7 @@ async function processRemoteNode({
201202
// See if there's response headers for this url
202203
// from a previous request.
203204
const cachedHeaders = await cache.get(cacheId(url))
204-
const headers = {}
205+
const headers = { ...httpHeaders }
205206
if (cachedHeaders && cachedHeaders.etag) {
206207
headers[`If-None-Match`] = cachedHeaders.etag
207208
}
@@ -314,6 +315,7 @@ module.exports = ({
314315
createNode,
315316
parentNodeId = null,
316317
auth = {},
318+
httpHeaders = {},
317319
createNodeId,
318320
ext = null,
319321
name = null,
@@ -359,6 +361,7 @@ module.exports = ({
359361
parentNodeId,
360362
createNodeId,
361363
auth,
364+
httpHeaders,
362365
ext,
363366
name,
364367
})

0 commit comments

Comments
 (0)