All files / src/server/middleware version-validation.middleware.js

93.33% Statements 14/15
86.66% Branches 13/15
100% Functions 4/4
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 375x                 5x 735x   735x 1x 1x   1x 1x         1x       734x 107x   107x 87x     20x      
const errors = require('../utils/error.utils');
 
/**
 * @description Middleware for validating the correct spec version is being accessed
 * If the user is trying to access R4 but the server is route is only supposed
 * to allow STU3, then we need to trigger a 404 error. Otherwise, we can continue.
 * @param {Object} profile - Configurations for the profile from the wrapping library
 * @return {function} valid express middleware
 */
module.exports = function versionValidationMiddleware(profile = {}) {
  const { versions = [], baseUrls = [] } = profile;
 
  if (baseUrls.length) {
    return (req, res, next) => {
      const base_version = req.params && req.params.base_version;
 
      baseUrls.forEach((baseUrl) => {
        Iif (baseUrl.indexOf(`/${base_version}`) > -1) {
          return next();
        }
      });
 
      return next(errors.notFound(undefined, base_version));
    };
  }
 
  return (req, res, next) => {
    const base_version = req.params && req.params.base_version;
 
    if (!base_version || versions.indexOf(base_version) === -1) {
      return next(errors.notFound(undefined, base_version));
    }
 
    next();
  };
};