Skip to content

Instantly share code, notes, and snippets.

@galenweber
Last active June 29, 2018 01:59
Show Gist options
  • Save galenweber/9b499c1a2bb7b5a9ef65b998288cbddc to your computer and use it in GitHub Desktop.
Save galenweber/9b499c1a2bb7b5a9ef65b998288cbddc to your computer and use it in GitHub Desktop.
Middleware to inject Prismic "context" into every request
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