Extracted `version-match` middleware to separate package

- this middlware is standalone and has 100% test coverage, therefore we
  can move it out to a separate package and remap the requires
  accordingly
This commit is contained in:
Daniel Lockyer 2023-02-27 18:34:03 +01:00
parent 025ebb7890
commit 93e0898c12
No known key found for this signature in database
10 changed files with 76 additions and 8 deletions

View File

@ -1,10 +1,11 @@
const debug = require('@tryghost/debug')('web:endpoints:admin:app');
const boolParser = require('express-query-boolean');
const express = require('../../../../../shared/express');
const bodyParser = require('body-parser');
const shared = require('../../../shared');
const apiMw = require('../../middleware');
const errorHandler = require('@tryghost/mw-error-handler');
const versionMatch = require('@tryghost/mw-version-match');
const shared = require('../../../shared');
const express = require('../../../../../shared/express');
const sentry = require('../../../../../shared/sentry');
const routes = require('./routes');
const APIVersionCompatibilityService = require('../../../../services/api-version-compatibility');
@ -24,7 +25,7 @@ module.exports = function setupApiApp() {
// Check version matches for API requests, depends on res.locals.safeVersion being set
// Therefore must come after themeHandler.ghostLocals, for now
apiApp.use(apiMw.versionMatch);
apiApp.use(versionMatch);
// Admin API shouldn't be cached
apiApp.use(shared.middleware.cacheControl('private'));

View File

@ -1,6 +1,5 @@
module.exports = {
cors: require('./cors'),
updateUserLastSeen: require('./update-user-last-seen'),
upload: require('./upload'),
versionMatch: require('./version-match')
upload: require('./upload')
};

View File

@ -119,6 +119,7 @@
"@tryghost/mw-cache-control": "0.0.0",
"@tryghost/mw-error-handler": "0.0.0",
"@tryghost/mw-session-from-token": "0.0.0",
"@tryghost/mw-version-match": "0.0.0",
"@tryghost/mw-vhost": "0.0.0",
"@tryghost/nodemailer": "0.3.32",
"@tryghost/nql": "0.11.0",

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/node'
]
};

View File

@ -0,0 +1,23 @@
# Mw Version Match
Version compatibility middleware for Ghost
## Usage
## Develop
This is a monorepo package.
Follow the instructions for the top-level repo.
1. `git clone` this repo & `cd` into it as usual
2. Run `yarn` to install top-level dependencies.
## Test
- `yarn lint` run just eslint
- `yarn test` run lint and tests

View File

@ -0,0 +1 @@
module.exports = require('./lib/mw-version-match');

View File

@ -0,0 +1,30 @@
{
"name": "@tryghost/mw-version-match",
"version": "0.0.0",
"repository": "https://github.com/TryGhost/Ghost/tree/main/packages/mw-version-match",
"author": "Ghost Foundation",
"private": true,
"main": "index.js",
"scripts": {
"dev": "echo \"Implement me!\"",
"test:unit": "NODE_ENV=testing c8 --all --check-coverage --100 --reporter text --reporter cobertura mocha './test/**/*.test.js'",
"test": "yarn test:unit",
"lint:code": "eslint *.js lib/ --ext .js --cache",
"lint": "yarn lint:code && yarn lint:test",
"lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache"
},
"files": [
"index.js",
"lib"
],
"devDependencies": {
"c8": "7.13.0",
"mocha": "10.2.0",
"sinon": "15.0.1"
},
"dependencies": {
"@tryghost/errors": "1.2.21",
"@tryghost/tpl": "0.1.21",
"semver": "7.3.8"
}
}

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/test'
]
};

View File

@ -1,6 +1,7 @@
const should = require('should');
require('should');
const sinon = require('sinon');
const versionMatch = require('../../../../../../core/server/web/api/middleware/version-match');
const versionMatch = require('../');
describe('Version Mismatch', function () {
let req;