UNPKG

@viewdo/dxp-story-cli

Version:
95 lines (87 loc) 3.38 kB
const request = require('request'); /** * Gets the information for the stories to use with the home view * layout. * * @param {Object} iVXConfig * @param {Object} appConfig */ const _getStories = (iVXConfig, appConfig) => { const { stories = {} } = iVXConfig; const { urls, api } = appConfig; const { xapi: xapiUrl } = api; const { storyPlayer: storyPlayerUrl } = urls; const storyPreviewPromises = Object.keys(stories) .map((storyKey) => { const { episodes: iVXConfigEpisodes = {} } = stories[storyKey]; const options = { method: 'GET', url: `${xapiUrl}/story/${storyKey}`, json: true }; const storyPreviewPromise = new Promise((resolve, reject) => { request(options, (err, res, body) => { if (err || res.statusCode != 200) reject(err || body); const { story = {} } = body; const { data = {}, events = [], milestones = [], inputs = [], episodes = [] } = story; const { metadata = {} } = data; const previewUrl = `${storyPlayerUrl}/${storyKey}`; const { image = story.thumbnailUrl, title = story.name, description = story.description } = metadata; const storyInfo = { image, title, description, storyKey, events, milestones, inputs, previewUrl }; const iVXConfigEpisodesKeys = Object.keys(iVXConfigEpisodes); const linkToEpisodes = episodes.filter(episode => { return iVXConfigEpisodesKeys.indexOf(episode.key) >= 0; }); if (linkToEpisodes.length > 0) { storyInfo.episodes = linkToEpisodes.map(episode => { return Object.assign(episode, { episodePreviewUrl: `${previewUrl}?eid=${episode.key}` }) }); } resolve(storyInfo); }); }) return storyPreviewPromise .catch(err => { console.log(err.red); }) }); return Promise.all(storyPreviewPromises); } /** * Sets up the default route for the local intance of xapi. * This main page will create cards for each story used in this * repo and a link to their previews in the preview version of * the ivx-story-player module. * * @param {Express} app * @param {Object} iVXConfig * @param {Object} appConfig */ module.exports = (app, iVXConfig, appConfig) => { const { build } = iVXConfig; app.get('/', (req, res) => { _getStories(iVXConfig, appConfig) .then(stories => { stories.sort((a, b) => { if (a.storyKey < b.storyKey) return -1; if (a.storyKey > b.storyKey) return 1; return 0; }) res.render('home', { stories }); }); }); }