Skip to content

Commit

Permalink
fix(gatsby-source-contentful): validation with environment (#29228)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Jan 28, 2021
1 parent 7c38997 commit fb38f8a
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions packages/gatsby-source-contentful/src/gatsby-node.js
Expand Up @@ -29,22 +29,28 @@ exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`).extendNodeTyp
const validateContentfulAccess = async pluginOptions => {
if (process.env.NODE_ENV === `test`) return undefined

await fetch(`https://${pluginOptions.host}/spaces/${pluginOptions.spaceId}`, {
headers: {
Authorization: `Bearer ${pluginOptions.accessToken}`,
"Content-Type": `application/json`,
},
})
await fetch(
`https://${pluginOptions.host}/spaces/${pluginOptions.spaceId}/environments/${pluginOptions.environment}/content_types`,
{
headers: {
Authorization: `Bearer ${pluginOptions.accessToken}`,
"Content-Type": `application/json`,
},
}
)
.then(res => res.ok)
.then(ok => {
if (!ok)
throw new Error(
`Cannot access Contentful space "${maskText(
pluginOptions.spaceId
)}" with access token "${maskText(
pluginOptions.accessToken
)}". Make sure to double check them!`
)
if (!ok) {
let errorMessage = `Cannot access Contentful space "${maskText(
pluginOptions.spaceId
)}" on environment "${
pluginOptions.environment
} with access token "${maskText(
pluginOptions.accessToken
)}". Make sure to double check them!`

throw new Error(errorMessage)
}
})

return undefined
Expand Down

0 comments on commit fb38f8a

Please sign in to comment.