Last active
June 29, 2018 01:59
-
-
Save galenweber/9b499c1a2bb7b5a9ef65b998288cbddc to your computer and use it in GitHub Desktop.
Middleware to inject Prismic "context" into every request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express'; | |
import Prismic from 'prismic-javascript'; | |
import PrismicDOM from 'prismic-dom'; | |
// Middleware to inject prismic context | |
const injectContext: express.RequestHandler = function (req, res, next) { | |
res.locals.ctx = { | |
endpoint: process.env.PRISMIC_URL, | |
}; | |
Prismic.api(apiEndpoint, { | |
req, | |
}).then((api) => { | |
res.locals.ctx.api = api; | |
next(); | |
}).catch((error) => { | |
next(error.message); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment