From 987e41e8d6da0512d2d7cab4480488b8c5866859 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Wed, 17 Oct 2018 09:23:57 +0200 Subject: [PATCH] Attached api version to `res.locals` context refs #9866 - each request get's the ghost api version attached - this makes it possible to access the version in all steps (routing, theme helpers) --- .../web/shared/middlewares/ghost-locals.js | 3 +++ .../unit/web/middleware/ghost-locals_spec.js | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/server/web/shared/middlewares/ghost-locals.js b/core/server/web/shared/middlewares/ghost-locals.js index 989ebe5417..863ca75e9b 100644 --- a/core/server/web/shared/middlewares/ghost-locals.js +++ b/core/server/web/shared/middlewares/ghost-locals.js @@ -1,4 +1,5 @@ const ghostVersion = require('../../../lib/ghost-version'); +const themeService = require('../../../services/themes'); // ### GhostLocals Middleware // Expose the standard locals that every request will need to have available @@ -11,6 +12,8 @@ module.exports = function ghostLocals(req, res, next) { res.locals.safeVersion = ghostVersion.safe; // relative path from the URL res.locals.relativeUrl = req.path; + // make ghost api version available for the theme + routing + res.locals.apiVersion = themeService.getActive().engine('ghost-api'); next(); }; diff --git a/core/test/unit/web/middleware/ghost-locals_spec.js b/core/test/unit/web/middleware/ghost-locals_spec.js index 8ba35e6ca2..c8908c5541 100644 --- a/core/test/unit/web/middleware/ghost-locals_spec.js +++ b/core/test/unit/web/middleware/ghost-locals_spec.js @@ -1,16 +1,24 @@ -var should = require('should'), - sinon = require('sinon'), - ghostLocals = require('../../../../server/web/shared/middlewares/ghost-locals'), - - sandbox = sinon.sandbox.create(); +const should = require('should'); +const sinon = require('sinon'); +const ghostLocals = require('../../../../server/web/shared/middlewares/ghost-locals'); +const themeService = require('../../../../server/services/themes'); +const sandbox = sinon.sandbox.create(); describe('Theme Handler', function () { - var req, res, next; + let req, res, next; beforeEach(function () { req = sandbox.spy(); res = sandbox.spy(); next = sandbox.spy(); + + sandbox.stub(themeService, 'getActive').callsFake(() => { + return { + engine() { + return 'v0.1'; + } + }; + }); }); afterEach(function () { @@ -26,7 +34,9 @@ describe('Theme Handler', function () { res.locals.should.be.an.Object(); should.exist(res.locals.version); should.exist(res.locals.safeVersion); + should.exist(res.locals.apiVersion); res.locals.relativeUrl.should.equal(req.path); + res.locals.apiVersion.should.equal('v0.1'); next.called.should.be.true(); }); });