locals.handler.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const query = require("../queries");
  2. const utils = require("../utils");
  3. const env = require("../env");
  4. function isHTML(req, res, next) {
  5. const accepts = req.accepts(["json", "html"]);
  6. req.isHTML = accepts === "html";
  7. next();
  8. }
  9. function addNoLayoutLocals(req, res, next) {
  10. res.locals.layout = null;
  11. next();
  12. }
  13. function viewTemplate(template) {
  14. return function (req, res, next) {
  15. req.viewTemplate = template;
  16. next();
  17. }
  18. }
  19. function addConfigLocals(req, res, next) {
  20. res.locals.default_domain = env.DEFAULT_DOMAIN;
  21. res.locals.site_name = env.SITE_NAME;
  22. next();
  23. }
  24. async function addUserLocals(req, res, next) {
  25. const user = req.user;
  26. res.locals.user = user;
  27. res.locals.domains = user && (await query.domain.get({ user_id: user.id })).map(utils.sanitize.domain);
  28. next();
  29. }
  30. function createLink(req, res, next) {
  31. res.locals.show_advanced = !!req.body.show_advanced;
  32. next();
  33. }
  34. function editLink(req, res, next) {
  35. res.locals.id = req.params.id;
  36. res.locals.class = "no-animation";
  37. next();
  38. }
  39. function protected(req, res, next) {
  40. res.locals.id = req.params.id;
  41. next();
  42. }
  43. module.exports = {
  44. addConfigLocals,
  45. addNoLayoutLocals,
  46. addUserLocals,
  47. createLink,
  48. editLink,
  49. isHTML,
  50. protected,
  51. viewTemplate,
  52. }