"use strict";

module.exports = apiLinksMiddleware;

/**
 * If the apiBaseUrl argument is given then configure the models to include
 * links in their output.
 *
 * @param {Object} options The options to configure this function
 * @param {String} options.baseUrl The url where the main app is located
 * @param {String} options.apiBaseUrl The url where the API is mounted
 * @return {Function} Express compatible middleware.
 */
function apiLinksMiddleware(options) {
  return function apiLinks(req, res, next) {
    var schema = req.collection.schema;
    if (options.baseUrl) {
      schema.options.toJSON.baseUrl = options.baseUrl;
    }
    if (options.apiBaseUrl) {
      schema.options.toJSON.apiBaseUrl = options.apiBaseUrl;
    }
    next();
  };
}

"use strict";

module.exports = apiLinksMiddleware;

/**
 * If the apiBaseUrl argument is given then configure the models to include
 * links in their output.
 *
 * @param {Object} options The options to configure this function
 * @param {String} options.baseUrl The url where the main app is located
 * @param {String} options.apiBaseUrl The url where the API is mounted
 * @return {Function} Express compatible middleware.
 */
function apiLinksMiddleware(options) {
  return function apiLinks(req, res, next) {
    var schema = req.collection.schema;
    schema.options.toJSON.baseUrl = options.baseUrl;
    schema.options.toJSON.apiBaseUrl = options.apiBaseUrl;
    if (options.baseUrl) {
      schema.options.toJSON.baseUrl = options.baseUrl;
    }
    if (options.apiBaseUrl) {
      schema.options.toJSON.apiBaseUrl = options.apiBaseUrl;
    }
    next();
  };
}

Reset baseUrl/apiBaseUrl before each request
Reset baseUrl/apiBaseUrl before each request

If we don't do this then mongoose will preserve the previous setting.
This caused tests to fail when run in a certain order as earlier tests
set baseUrl/apiBaseUrl but later tests weren't expecting it.
