Skip to content

Commit b086d04

Browse files
bechatmanjescalan
authored andcommitted
Support preview API (#85)
Adds a `preview` boolean option that will switch the host to pull from the preview API
1 parent 3f98a6b commit b086d04

5 files changed

Lines changed: 72 additions & 1 deletion

File tree

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
accessToken=b4c0n73n7fu1
22
spaceId=cfexampleapi
3+
previewToken=e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
.DS_Store
33
.nyc_output
4+
test/fixtures/error/public/*
5+
test/fixtures/template/public/*

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,46 @@ Your template must use the `item` variable as seen below. Also note that this fe
171171
<p>{{item.title}}</p>
172172
```
173173

174+
### Preview Environment
175+
176+
Using [Spike Environments](https://spike.readme.io/docs/environments), you can change your default `app.js` to use [Contentful's preview API](https://www.contentful.com/developers/docs/references/content-preview-api/) by using your project's preview key (in this example, `yyy`).
177+
178+
```js
179+
new Contentful({
180+
addDataTo: locals,
181+
accessToken: 'yyy',
182+
preview: true;
183+
spaceId: 'xxx'
184+
})
185+
```
186+
187+
Then set your `app.production.js` to:
188+
189+
```js
190+
new Contentful({
191+
addDataTo: locals,
192+
accessToken: 'xxx',
193+
preview: false;
194+
spaceId: 'xxx'
195+
})
196+
```
197+
From there, running `spike compile` will use the preview API, and `spike compile -e production` will use the regular content delivery API.
198+
199+
This can also be accomplished with a single `app.js` with the help of [Dotenv](https://www.npmjs.com/package/dotenv).
200+
201+
Require Dotenv in your `app.js` file, then modify the Contentful call to use your preview key (in this example, `yyy`).
202+
203+
```js
204+
const env = process.env.SPIKE_ENV;
205+
206+
new Contentful({
207+
addDataTo: locals,
208+
accessToken: env !== 'production' ? 'yyy' : 'xxx' ,
209+
preview: env !== 'production';
210+
spaceId: 'xxx'
211+
})
212+
```
213+
174214
### JSON Output
175215

176216
Finally, if you'd like to have the output written locally to a JSON file so that it's cached locally, you can pass the name of the file, resolved relative to your project's output, as a `json` option to the plugin. For example:

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Contentful {
1919
// initialize the contentful api client
2020
this.client = contentful.createClient({
2121
accessToken: this.accessToken,
22-
space: this.spaceId
22+
space: this.spaceId,
23+
host: this.preview ? 'preview.contentful.com' : ''
2324
})
2425
bindAllClass(this, ['apply', 'run'])
2526
}
@@ -128,6 +129,7 @@ function validate(opts = {}) {
128129
const schema = Joi.object().keys({
129130
accessToken: Joi.string().required(),
130131
spaceId: Joi.string().required(),
132+
preview: Joi.boolean(),
131133
addDataTo: Joi.object().required(),
132134
json: Joi.string(),
133135
includeLevel: Joi.number().default(1),

test/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@ test.cb('returns valid content', t => {
138138
})
139139
})
140140

141+
test.cb('returns valid preview content', (t) => {
142+
const locals = {}
143+
const api = new Contentful({
144+
accessToken: process.env.previewToken,
145+
spaceId: process.env.spaceId,
146+
preview: true,
147+
addDataTo: locals,
148+
contentTypes: [
149+
{
150+
name: 'cats',
151+
id: 'cat'
152+
},
153+
{
154+
name: 'dogs',
155+
id: 'dog'
156+
}
157+
]
158+
})
159+
160+
api.run(undefined, () => {
161+
t.is(locals.contentful.dogs.length, 2)
162+
t.is(locals.contentful.cats.length, 3)
163+
t.end()
164+
})
165+
})
166+
141167
test.cb('defaults id to name if not present', t => {
142168
const locals = {}
143169
const api = new Contentful({

0 commit comments

Comments
 (0)