Skip to content

Commit

Permalink
Move Contentful access token logic to shared function so that service…
Browse files Browse the repository at this point in the history
…s/contentful.coffee can use it. Trying to fix bug where no routes were generated when CONTENTFUL_PREVIEW=true.
  • Loading branch information
jonjahr committed May 3, 2022
1 parent 499f3ec commit 88e8438
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 2 additions & 5 deletions config/concerns/cms.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Add configuration required to interact with CMS choice
###
{ join } = require 'path'
{ getAccessToken } = require '../../services/contentful'
module.exports = ({ cms }) ->
return unless cms

Expand All @@ -19,11 +20,7 @@ module.exports = ({ cms }) ->
CONTENTFUL_PREVIEW: process.env.CONTENTFUL_PREVIEW

# Don't expose the preview access token when it's not being used
CONTENTFUL_ACCESS_TOKEN: do ->
if process.env.CONTENTFUL_PREVIEW and
previewToken = process.env.CONTENTFUL_PREVIEW_ACCESS_TOKEN
then previewToken
else process.env.CONTENTFUL_ACCESS_TOKEN
CONTENTFUL_ACCESS_TOKEN: getAccessToken()

)
}
Expand Down
8 changes: 7 additions & 1 deletion services/contentful.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import axios from 'axios'
# when services/contentful was imported from a Nuxt module
nonEmpty = (array) -> array.filter (val) -> !!val

export getAccessToken = ->
if process.env.CONTENTFUL_PREVIEW and
previewToken = process.env.CONTENTFUL_PREVIEW_ACCESS_TOKEN
then previewToken
else process.env.CONTENTFUL_ACCESS_TOKEN

# Make a Contentful client
client = axios.create
baseURL: 'https://graphql.contentful.com/content/v1/spaces/' +
process.env.CONTENTFUL_SPACE
headers:
'Content-Type': 'application/json'
'Authorization': 'Bearer ' + process.env.CONTENTFUL_ACCESS_TOKEN
'Authorization': 'Bearer ' + getAccessToken()

# Retry requests when met with Contentful API rate limits.
# https://www.contentful.com/developers/docs/references/graphql/#/introduction/api-rate-limits
Expand Down

0 comments on commit 88e8438

Please sign in to comment.