diff --git a/app.js b/app.js index 9e86dc4..c5d0161 100644 --- a/app.js +++ b/app.js @@ -7,14 +7,6 @@ var PORT = app.get('port'); var PConfig = require('./prismic-configuration'); var request = require('request'); -function handleError(err, req, res) { - if (err.status == 404) { - res.status(404).send('404 not found'); - } else { - res.status(500).send('Error 500: ' + err.message); - } -} - app.listen(PORT, function() { const repoEndpoint = PConfig.apiEndpoint.replace('/api', ''); request.post(repoEndpoint + '/app/settings/onboarding/run', {}); @@ -22,18 +14,23 @@ app.listen(PORT, function() { }); /** -* initialize prismic context and api +* initialize prismic context and api in a middleware */ -function api(req, res) { - res.locals.ctx = { // So we can use this information in the views - endpoint: PConfig.apiEndpoint, - linkResolver: PConfig.linkResolver - }; - return Prismic.api(PConfig.apiEndpoint, { - accessToken: PConfig.accessToken, - req: req - }); -} +app.use((req, res, next) => { + Prismic.api(PConfig.apiEndpoint,{accessToken: PConfig.accessToken, req: req}) + .then((api) => { + req.prismic = {api: api}; + res.locals.ctx = { + endpoint: PConfig.apiEndpoint, + snipcartKey: PConfig.snipcartKey, + linkResolver: PConfig.linkResolver + }; + next(); + }).catch(function(err) { + console.error('missing Prismic API Configuration') + next(); + }); +}); // INSERT YOUR ROUTES HERE @@ -61,9 +58,5 @@ app.get('/help', function(req, res) { * preconfigured prismic preview */ app.get('/preview', function(req, res) { - api(req, res).then(function(api) { - return Prismic.preview(api, PConfig.linkResolver, req, res); - }).catch(function(err) { - handleError(err, req, res); - }); + return Prismic.preview(req.prismic.api, PConfig.linkResolver, req, res); }); diff --git a/views/help.pug b/views/help.pug index b1b1079..feeffdb 100644 --- a/views/help.pug +++ b/views/help.pug @@ -91,10 +91,9 @@ body |app.get('/page/:uid', function(req, res) { | // We store the param uid in a variable | var uid = req.params.uid; - | api(req, res).then(function(api) { - | // We are using the function to get a document by its uid - | return api.getByUID('#{''}', uid); - | }).then(function(pageContent) { + | // We are using the function to get a document by its uid + | req.prismic.api.getByUID('#{''}', uid) + | .then(function(pageContent) { | // pageContent is a document, or null if there is no match | res.render('page', { | // Where 'page' is the name of your pug template file (page.pug)