Files
the-example-app-nodejs/services/contentful.js
2017-11-07 17:33:32 +01:00

49 lines
1.5 KiB
JavaScript

const { createClient } = require('contentful')
let client = null
let previewClient = null
exports.initClient = (config = {space: process.env.CF_SPACE, accessToken: process.env.CF_ACCESS_TOKEN}) => {
client = createClient(config)
previewClient = createClient({...config, host: 'preview.contentful.com'})
}
exports.getCourses = () => {
// to get all the courses we simply request from Contentful all the entries
// with the content_type `course`
return client.getEntries({content_type: 'course'})
.then((response) => response.items)
}
exports.getLandingPage = () => {
// our Home page is fully configureable via contentful
return client.getEntries({content_type: 'landingPage', 'fields.slug': 'contentful-university'})
.then((response) => response.items[0])
}
exports.getCourse = (slug) => {
// the SDK support link resolution only when you request the collection endpoint
// That's why we are using getEntries with a query instead of getEntry(entryId)
// make sure to specify the content_type whenever you want to perform a query
return client.getEntries({content_type: 'course', 'fields.slug': slug})
.then((response) => response.items[0])
}
exports.getLessons = (courseId) => {
// TODO
}
exports.getCategories = () => {
return client.getEntries({content_type: 'category'})
.then((response) => response.items)
}
exports.getCoursesByCategory = (category) => {
return client.getEntries({
content_type: 'course',
'fields.category.sys.contentType.sys.id': category
})
.then((response) => response.items)
}