Skip to content

Commit

Permalink
a variety of changes (#47)
Browse files Browse the repository at this point in the history
- remove 'ordered' prop, can be implemented using transform
- set default transform to false by default, it causes errors
- change tests over to use public contentful test data
  • Loading branch information
jescalan authored Jan 11, 2017
1 parent 8c81c3a commit 92aff6b
Show file tree
Hide file tree
Showing 13 changed files with 5,805 additions and 150 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
accessToken=b4c0n73n7fu1
spaceId=cfexampleapi
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
.DS_Store
.env
.nyc_output
42 changes: 3 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,6 @@ ul
li {{ JSON.stringify(post) }}
```


### Ordered Content

In some cases you'll need to use a content model to maintain order of another content model. Take the following for example:

```
Case Study
- Title [Short Text]
- Client [Short Text]
...
```

```
Case Study Order
- Case Studies [References, many]
```

In order to pull formatted and ordered `case studies`, you'll need to use the following parameters:

```js
new Contentful({
addDataTo: locals,
accessToken: 'xxx',
spaceId: 'xxx',
contentTypes: [
{
name: 'case_studies', // Arbitrary name used from within templates
id: '633fTeiMaxxxxxxxxx', // This should point to the case_studies_order content type
ordered: true // Required. Appropriately format the related ordered content type
}
]
})
```

### Filters

#### Limit
Expand Down Expand Up @@ -150,7 +114,7 @@ new Contentful({

### Transforms

Contentful returns a lot of associated data and, as a result, we clean that up by default. You also have the ability to pass your own custom `transform` option to each content type allowing you to transform the data however you like before it's sent to your views.
Contentful returns a lot of associated data and, as a result, we include a way to clean it up. You also have the ability to pass your own custom `transform` option to each content type allowing you to transform the data however you like before it's sent to your views.

```js
new Contentful({
Expand All @@ -170,7 +134,7 @@ new Contentful({
})
```

You also have the ability to disable the default `transform` and receive the raw JSON data.
This plugin ships with a default transform function that will run some basic cleanup. However, be warned that the transform will enter an infinite loop and crash if there are circular references within the data, which is not an uncommon occurance, so please be very careful utilizing this transform. The enable our default transform, you can pass `true` as such:

```js
new Contentful({
Expand All @@ -181,7 +145,7 @@ new Contentful({
{
name: 'posts',
id: '633fTeiMaxxxxxxxxx',
transform: false
transform: true
}
]
})
Expand Down
6 changes: 1 addition & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ class Contentful {

return W(this.client.getEntries(options))
.then(response => {
if (ct.ordered) {
response.items = response.items[0].fields[Object.keys(response.items[0].fields)[0]]
}

return W.map(response.items, (entry) => transformFn(entry))
})
.tap((res) => { m[ct.name] = res })
Expand Down Expand Up @@ -86,7 +82,7 @@ function validate (opts = {}) {
filters: Joi.object().keys({
limit: Joi.number().integer().min(1).max(100).default(100)
}),
transform: Joi.alternatives().try(Joi.boolean(), Joi.func()).default(true)
transform: Joi.alternatives().try(Joi.boolean(), Joi.func()).default(false)
})
)
})
Expand Down
2 changes: 0 additions & 2 deletions test/.env.sample

This file was deleted.

4 changes: 2 additions & 2 deletions test/fixtures/default/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = {
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
name: 'cats',
id: 'cat',
filters: {
limit: 1,
order: 'sys.createdAt'
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/default/index.sgr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
each(loop='blog of contentful.blogs')
p {{ blog.id }}
each(loop='cat of contentful.cats')
p {{ cat.fields.name }}
4 changes: 2 additions & 2 deletions test/fixtures/json/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = {
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk'
name: 'dogs',
id: 'dog'
}
],
json: 'data.json'
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/json/index.sgr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
each(loop='blog of contentful.blogs')
p {{ blog.name }}
each(loop='dog of contentful.dogs')
p {{ dog.fields.name }}
2 changes: 1 addition & 1 deletion test/fixtures/template/error.sgr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
p {{ notItem.title }}
p {{ notItem.fields.name }}
2 changes: 1 addition & 1 deletion test/fixtures/template/template.sgr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
p {{ item.title }}
p {{ item.fields.name }}
127 changes: 34 additions & 93 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,40 @@ test.cb('returns valid content', (t) => {
addDataTo: locals,
contentTypes: [
{
name: 'press',
id: '4Em9bQeIQowM0QM8o40yOA'
},
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk'
name: 'cats',
id: 'cat'
}, {
name: 'dogs',
id: 'dog'
}
]
})

api.run(compilerMock, undefined, () => {
t.is(locals.contentful.press.length, 91)
t.is(locals.contentful.blogs.length, 100)
t.is(locals.contentful.dogs.length, 2)
t.is(locals.contentful.cats.length, 3)
t.end()
})
})

test.skip.cb('implements request options', (t) => {
test.cb('implements request options', (t) => {
const locals = {}
const api = new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
filters: {
limit: 1
}
name: 'cats',
id: 'cat',
filters: { limit: 1 }
}
]
})

api.run(compilerMock, undefined, () => {
t.is(locals.contentful.blogs.length, 1)
t.is(locals.contentful.blogs[0].title, 'This Webby Saves Elephants ...')
t.is(locals.contentful.cats.length, 1)
t.is(locals.contentful.cats[0].fields.name, 'Garfield')
t.end()
})
})
Expand All @@ -139,8 +136,8 @@ test.cb('works with custom transform function', (t) => {
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
name: 'cats',
id: 'cat',
filters: {
limit: 1
},
Expand All @@ -153,21 +150,22 @@ test.cb('works with custom transform function', (t) => {
})

api.run(compilerMock, undefined, () => {
t.is(locals.contentful.blogs[0].doge, 'wow')
t.is(locals.contentful.cats[0].doge, 'wow')
t.end()
})
})

test.cb('implements default transform function', (t) => {
test.cb('can implement default transform function', (t) => {
const locals = {}
const api = new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
name: 'cats',
id: 'cat',
transform: true,
filters: {
limit: 1
}
Expand All @@ -176,32 +174,7 @@ test.cb('implements default transform function', (t) => {
})

api.run(compilerMock, undefined, () => {
t.truthy(typeof locals.contentful.blogs[0].title === 'string')
t.end()
})
})

test.cb('can disable transform function', (t) => {
const locals = {}
const api = new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
filters: {
limit: 1
},
transform: false
}
]
})

api.run(compilerMock, undefined, () => {
t.truthy(typeof locals.contentful.blogs[0].sys === 'object')
t.truthy(typeof locals.contentful.blogs[0].fields === 'object')
t.truthy(typeof locals.contentful.cats[0].name === 'string')
t.end()
})
})
Expand All @@ -217,7 +190,7 @@ test.cb('works as a plugin to spike', (t) => {
project.on('warning', t.end)
project.on('compile', () => {
const src = fs.readFileSync(path.join(projectPath, 'public/index.html'), 'utf8')
t.truthy(src === '<p>fqhi1USjAIuogSS2AKEKu</p>') // IDs listed in output, sans spaces
t.truthy(src === '<p>Nyan Cat</p>')
rimraf.sync(path.join(projectPath, 'public'))
t.end()
})
Expand All @@ -238,7 +211,7 @@ test.cb('writes json output', (t) => {
const file = path.join(projectPath, 'public/data.json')
t.falsy(fs.accessSync(file))
const src = JSON.parse(fs.readFileSync(path.join(projectPath, 'public/data.json'), 'utf8'))
t.truthy(src.blogs.length > 1)
t.truthy(src.dogs.length > 1)
rimraf.sync(path.join(projectPath, 'public'))
t.end()
})
Expand All @@ -254,15 +227,15 @@ test.cb('accepts template object and generates html', (t) => {
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
name: 'cats',
id: 'cat',
filters: {
limit: 2,
order: 'sys.createdAt'
},
template: {
path: '../template/template.sgr',
output: (item) => `blog_posts/${item.title}.html`
output: (item) => `cats/${item.fields.name}.html`
}
}
]
Expand All @@ -280,10 +253,10 @@ test.cb('accepts template object and generates html', (t) => {
project.on('error', t.end)
project.on('warning', t.end)
project.on('compile', () => {
const file1 = fs.readFileSync(path.join(projectPath, 'public/blog_posts/Save The Elephants.html'), 'utf8')
const file2 = fs.readFileSync(path.join(projectPath, 'public/blog_posts/Unlocking The Grid.html'), 'utf8')
t.is(file1.trim(), '<p>Save The Elephants</p>')
t.is(file2.trim(), '<p>Unlocking The Grid</p>')
const file1 = fs.readFileSync(path.join(projectPath, 'public/cats/Happy Cat.html'), 'utf8')
const file2 = fs.readFileSync(path.join(projectPath, 'public/cats/Nyan Cat.html'), 'utf8')
t.is(file1.trim(), '<p>Happy Cat</p>')
t.is(file2.trim(), '<p>Nyan Cat</p>')
rimraf.sync(path.join(projectPath, 'public'))
t.end()
})
Expand All @@ -299,14 +272,14 @@ test.cb('generates error if template has an error', (t) => {
addDataTo: locals,
contentTypes: [
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
name: 'cats',
id: 'cat',
filters: {
limit: 1
},
template: {
path: '../template/error.sgr',
output: (item) => `blog_posts/${item.title}.html`
output: (item) => `cats/${item.fields.name}.html`
}
}
]
Expand All @@ -324,42 +297,10 @@ test.cb('generates error if template has an error', (t) => {
project.on('warning', t.end)
project.on('compile', () => t.end('no error'))
project.on('error', (error) => {
t.is(error.message.message, "Cannot read property 'title' of undefined")
t.is(error.message.message, "Cannot read property 'fields' of undefined")
rimraf.sync(path.join(projectPath, 'public'))
t.end()
})

project.compile()
})

test.cb('generates appropriate output for ordered content types', (t) => {
const locals = {}
const api = new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
addDataTo: locals,
contentTypes: [
{
name: 'case_studies',
ordered: true,
id: '4FmWzuxZb2EcgyU44weG0Q',
filters: {
limit: 100
}
},
{
name: 'blogs',
id: '633fTeiMaQwE44OsIqSimk',
filters: {
limit: 10
}
}
]
})

api.run(compilerMock, undefined, () => {
t.is(locals.contentful.case_studies.length, 42)
t.is(locals.contentful.blogs.length, 10)
t.end()
})
})
Loading

0 comments on commit 92aff6b

Please sign in to comment.