Skip to content

Commit

Permalink
feat(createRemoteFileNode): allow passing headers to request (#11682)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
iFlameing authored and wardpeet committed Apr 5, 2019
1 parent 65693d3 commit 7a8e41a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/gatsby-source-filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ createRemoteFileNode({
// Adds htaccess authentication to the download request if passed in.
auth: { htaccess_user: `USER`, htaccess_pass: `PASSWORD` },

// OPTIONAL
// Adds extra http headers to download request if passed in.
httpHeaders: { Authorization: `Bearer someAccessToken` },

// OPTIONAL
// Sets the file extension
ext: ".jpg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ describe(`create-remote-file-node`, () => {
})
)
})

it(`passes custom http heades, if defined`, async () => {
await setup({
httpHeaders: {
Authorization: `Bearer foobar`,
},
})

expect(got.stream).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
headers: expect.objectContaining({
Authorization: `Bearer foobar`,
}),
})
)
})
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ async function processRemoteNode({
createNode,
parentNodeId,
auth = {},
httpHeaders = {},
createNodeId,
ext,
name,
Expand All @@ -201,7 +202,7 @@ async function processRemoteNode({
// See if there's response headers for this url
// from a previous request.
const cachedHeaders = await cache.get(cacheId(url))
const headers = {}
const headers = { ...httpHeaders }
if (cachedHeaders && cachedHeaders.etag) {
headers[`If-None-Match`] = cachedHeaders.etag
}
Expand Down Expand Up @@ -314,6 +315,7 @@ module.exports = ({
createNode,
parentNodeId = null,
auth = {},
httpHeaders = {},
createNodeId,
ext = null,
name = null,
Expand Down Expand Up @@ -359,6 +361,7 @@ module.exports = ({
parentNodeId,
createNodeId,
auth,
httpHeaders,
ext,
name,
})
Expand Down

0 comments on commit 7a8e41a

Please sign in to comment.