Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-hats-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hashicorp/platform-cms': minor
---

Use headers for preview and environment config
30 changes: 6 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions packages/cms/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Return draft content from Dato app is in preview mode
// Default to production endpoint if undefined to avoid unexpectedly exposing draft content
let url = process.env.HASHI_DATO_ENVIRONMENT
? `https://graphql.datocms.com/environments/${process.env.HASHI_DATO_ENVIRONMENT}`
: 'https://graphql.datocms.com'

if (process.env.HASHI_ENV === 'preview') url += '/preview'

// https://www.datocms.com/docs/content-delivery-api/api-endpoints
const url = 'https://graphql.datocms.com'
const token = process.env.HASHI_DATO_TOKEN || '2f7896a6b4f1948af64900319aed60'
const isPreview = process.env.HASHI_ENV === 'preview'
const datoEnvironment = process.env.HASHI_DATO_ENVIRONMENT

module.exports = {
url,
headers: { Authorization: token },
headers: {
Authorization: token,
...(isPreview && { 'X-Include-Drafts': 'true' }),
...(datoEnvironment && { 'X-Environment:': datoEnvironment }),
},
}
2 changes: 1 addition & 1 deletion packages/cms/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('returns the correct url based on the environment', async () => {
jest.resetModules()
process.env.HASHI_ENV = 'preview'
const prodConfig = await require('./config')
expect(prodConfig.url).toBe('https://graphql.datocms.com/preview')
expect(prodConfig.headers['X-Include-Drafts']).toBe('true')

// finally we reset the HASHI_ENV for subsequent tests
process.env.HASHI_ENV = oldEnv
Expand Down