* refactor(routes): remove unused sitemap route * style(comments): enforce consistent comment style * style(exports): enforce consistent export style * chore: Add jsDoc for contentful service * chore: Add jsDoc for contentful service \n \n closes #23 * refactor(app): move query parameter comment to right position and mention comment route middleware * test(npm): add temporary test script * refactor(middlewares): split up bootstrap middleware - fixes #23759 * refactor(cookies): use constances to give context the maxAge cookie setting * refactor(variables): use more descriptive names for variables * space became spaceId when it was just the id not the full space instance * all (access) token variable name variants became [api-type]Token * all clients are now called deliveryClient or previewClient * cpa and cda only remain when they are used as actual API id * env variables names adjusted * perf(helpers): only run marked when content is not empty * refactor(comments): fix typos * refactor(comments): add hint why error is logged to console in settings * chore: Add comments to routes and services * refactor(requires): order and group requires * fix(settings): add validation for wrong preview token * chore: Add comments to routes * chore: fix typo * chore: console.error -> throw * chore: move cookie name to a constant * chore: Fix app.js comment * typo: removing t * chore: typos * chore: removed unnecessary comment line sign * chore: newline for readabillity * chore: remove dangling `t` * chore: remove `t`, add `l` * chore: typos * Fleshed out title * build(npm): remove unused dependencies * build(npm): upgrade to latest stable contentful sdk * chore: Addressing David feedbak * fix(credentials): update to match the new space * chore: Addressing code review comments * chore: Addressing code review comments * chore: res -> response, req-> request * chore: include exactly what we need * chore: Address JPs comments * chore: Address JPs comments * chore: Address Fredericks comments * chore: Address Fredericks comments * fixup! chore: Address Fredericks comments * fixup! fixup! chore: Address Fredericks comments * fixup! fixup! fixup! chore: Address Fredericks comments * fixup! fixup! fixup! fixup! chore: Address Fredericks comments * fixup! fixup! fixup! fixup! fixup! chore: Address Fredericks comments * fixup! fixup! fixup! fixup! fixup! fixup! chore: Address Fredericks comments
156 lines
4.9 KiB
JavaScript
156 lines
4.9 KiB
JavaScript
/**
|
|
* The purpose of this module is to get data from contentful
|
|
*/
|
|
const { createClient } = require('contentful')
|
|
|
|
let deliveryClient = null
|
|
let previewClient = null
|
|
|
|
/**
|
|
* Initialize the contentful Client
|
|
* @param options {space: string, cda: string, cpa: string}
|
|
*
|
|
* @returns {undefined}
|
|
*/
|
|
module.exports.initClients = (options) => {
|
|
// Getting the app version
|
|
const { version } = require('../package.json')
|
|
|
|
const config = options || {
|
|
spaceId: process.env.CF_SPACE_ID,
|
|
deliveryToken: process.env.CF_DELIVERY_TOKEN,
|
|
previewToken: process.env.CF_PREVIEW_TOKEN
|
|
}
|
|
deliveryClient = createClient({
|
|
application: `the-example-app.node/${version}`,
|
|
space: config.spaceId,
|
|
accessToken: config.deliveryToken
|
|
})
|
|
previewClient = createClient({
|
|
application: `the-example-app.node/${version}`,
|
|
space: config.spaceId,
|
|
accessToken: config.previewToken,
|
|
host: 'preview.contentful.com'
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Get the Space the app is connected to. Used for the settings form and to get all available locales
|
|
* @param api - string - the api to use, cda or cap. Default: 'cda'
|
|
* @returns {undefined}
|
|
*/
|
|
module.exports.getSpace = assert((api = `cda`) => {
|
|
return getClient(api).getSpace()
|
|
}, 'Space')
|
|
|
|
/**
|
|
* Gets an entry. Used to detect the `Draft` or `Pending Changes` state
|
|
* @param entryId - string - the entry id
|
|
* @param api - string - the api to use fetching the entry
|
|
*
|
|
* @returns {Object}
|
|
*/
|
|
|
|
module.exports.getEntry = assert((entryId, api = `cda`) => {
|
|
return getClient(api).getEntry(entryId)
|
|
}, 'Entry')
|
|
|
|
/**
|
|
* Get all entries with content_type `course`
|
|
* @param locale - string - the locale of the entry [default: 'en-US']
|
|
* @param api - string the api enpoint to use when fetching the data
|
|
* @returns {Array<Object>}
|
|
*/
|
|
module.exports.getCourses = assert((locale = 'en-US', api = `cda`) => {
|
|
return getClient(api).getEntries({
|
|
content_type: 'course',
|
|
locale,
|
|
order: 'sys.createdAt', // Ordering the entries by creation date
|
|
include: 6 // We use include param to increase the link level, the include value goes from 1 to 6
|
|
})
|
|
.then((response) => response.items)
|
|
}, 'Course')
|
|
|
|
/**
|
|
* Get entries of content_type `layout` e.g. Landing page
|
|
* @param slug - string - the slug of the entry to use in the query
|
|
* @param locale - string - locale of the entry to request [default: 'en-US']
|
|
* @param api - string - the api enpoint to use when fetching the data
|
|
* @returns {Object}
|
|
*/
|
|
module.exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => {
|
|
// Even though we need a single entry, we request it using the collection endpoint
|
|
// To get all the linked refs in one go, the SDK will use the data and resolve the links automatically
|
|
return getClient(api).getEntries({
|
|
content_type: 'layout',
|
|
locale,
|
|
'fields.slug': slug,
|
|
include: 6
|
|
})
|
|
.then((response) => response.items[0])
|
|
}
|
|
|
|
/**
|
|
* Get an entry with content_type `course`
|
|
* @param slug - string - the slug of the entry to use in the query
|
|
* @param locale - string - locale of the entry to request [default: 'en-US']
|
|
* @param api - string - the api enpoint to use when fetching the data
|
|
* @returns {Object}
|
|
*/
|
|
module.exports.getCourse = assert((slug, locale = 'en-US', api = `cda`) => {
|
|
// Even though we need a single entry, we request it using the collection endpoint
|
|
// To get all the linked refs in one go, the SDK will use the data and resolve the links automatically
|
|
return getClient(api).getEntries({
|
|
content_type: 'course',
|
|
'fields.slug': slug,
|
|
locale,
|
|
include: 6
|
|
})
|
|
.then((response) => response.items[0])
|
|
}, 'Course')
|
|
|
|
module.exports.getCategories = assert((locale = 'en-US', api = `cda`) => {
|
|
return getClient(api).getEntries({content_type: 'category', locale})
|
|
.then((response) => response.items)
|
|
}, 'Course')
|
|
|
|
/**
|
|
* Get Courses by Categories
|
|
* To get a course by category, simply query all entries
|
|
* with a query params `fields.categories.sys.id` equal to the desired category id
|
|
* Note that you need to send the `content_type` param to be able to query the entry
|
|
* @param category - string - the id of the category
|
|
* @param locale - string - locale of the entry to request [default: 'en-US']
|
|
* @param api - string - the api enpoint to use when fetching the data
|
|
* @returns {Object}
|
|
*/
|
|
module.exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`) => {
|
|
return getClient(api).getEntries({
|
|
content_type: 'course',
|
|
'fields.categories.sys.id': category,
|
|
locale,
|
|
order: '-sys.createdAt',
|
|
include: 6
|
|
})
|
|
.then((response) => response.items)
|
|
}, 'Category')
|
|
|
|
// Utility function
|
|
function getClient (api = 'cda') {
|
|
return api === 'cda' ? deliveryClient : previewClient
|
|
}
|
|
|
|
function assert (fn, context) {
|
|
return function (request, response, next) {
|
|
return fn(request, response, next)
|
|
.then((data) => {
|
|
if (!data) {
|
|
var err = new Error(`${context} Not Found`)
|
|
err.status = 404
|
|
throw err
|
|
}
|
|
return data
|
|
})
|
|
}
|
|
}
|