<commit_before>'use strict'

var ConnectMiddleware = function( apiCore ) {
  this.apiCore = apiCore;
};


/**
 * Call this before any route executes
 *
 * @this     FacetApiCore
 * 
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function in the node stack to call
 *
 * @return   {void}
 */
ConnectMiddleware.prototype.preAPIRoute = function(req, res, next) {
  this._req = req;
  this._res = res;
  this._next = next;

  next();
};


/**
 * Process route function that sets the items of the node stack
 *
 * @this FacetApiCore
 * 
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.processRoute = function (apiCore) {
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );

  }
};


/**
 * The route verb for GET
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbGET = function(apiCore) {
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );
  };
};


/**
 * The route verb for POST
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbPOST = function(apiCore) {
  var _this = this;

  return function (req, res, next) {
    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );
  };
};


/**
 * The route verb for PUT
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbPUT = function(apiCore){
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );

  };
};


/**
 * The route verb for DELETE 
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbDELETE = function(apiCore) {
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );

  };
};



/**
 * Get the request variables based on method
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void}
 */
ConnectMiddleware.prototype.getRequestVariables = function(req, res, next) {
  // console.log('req in getRequestVariables: ', req.body, req.params, req.query);

  var emitValue = null,
    apiModelId = this.apiCore.routerManifest.manifest.apiModelId;

  // setup the get or put values
  if( req.method == 'GET' || req.method == 'PUT' ){
    emitValue = {
      conditions: {},
      fields: '',
      options: {}
    };

    if( req.hasOwnProperty("params") && req.params.hasOwnProperty(apiModelId) ){
      emitValue.conditions['_id'] = req.params[apiModelId];
    }

    try {
      // process all the query params
      if( req.hasOwnProperty('query') ){
        // if the query param contains the q attribute
        if( req.query.hasOwnProperty('q') && req.query.q ){
          emitValue.conditions = JSON.parse(req.query.q);
        }
        // if the query param contains the f attribute
        if( req.query.hasOwnProperty('q') && req.query.f ){
          emitValue.fields = JSON.parse(req.query.f);
        }
        // if the query param contains the o attribute
        if( req.query.hasOwnProperty('o') && req.query.o ){
          emitValue.options = JSON.parse(req.query.o);
        }
      }  
    }
    catch(e) {
      this.apiCore.intercom.emit('facet:response:error', 400, 'Bad query formatting. Check your JSON or the encoded q parameter for errors.');
      return null;
    }
    

    // process all the body params
    if( req.hasOwnProperty('body') && Object.keys(req.body).length !== 0 ){
      // TODO: Not sure why we need to delete conditions if body is present?
      // .. How is the item Id supposed to be passed through without the conditions?
      
      // delete the conditions 
      // delete emitValue.conditions;
      
      // delete the options 
      delete emitValue.options;
      // if the body contains the fields, set those
      if( req.body.hasOwnProperty('fields') ){
        emitValue['fields'] = req.body.fields;
      }
      // if the body contains the updates, set those
      if( req.body.hasOwnProperty('updates') ){
        emitValue['updates'] = req.body.updates;
      }
    }

  }
  // set up the post values
  else if ( req.method == 'POST' ){
    emitValue = req.body;
  }
  
  return emitValue;
};

exports = module.exports = ConnectMiddleware;

<commit_msg>do not json parse fields param<commit_after>'use strict'

var ConnectMiddleware = function( apiCore ) {
  this.apiCore = apiCore;
};


/**
 * Call this before any route executes
 *
 * @this     FacetApiCore
 * 
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function in the node stack to call
 *
 * @return   {void}
 */
ConnectMiddleware.prototype.preAPIRoute = function(req, res, next) {
  this._req = req;
  this._res = res;
  this._next = next;

  next();
};


/**
 * Process route function that sets the items of the node stack
 *
 * @this FacetApiCore
 * 
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.processRoute = function (apiCore) {
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );

  }
};


/**
 * The route verb for GET
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbGET = function(apiCore) {
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );
  };
};


/**
 * The route verb for POST
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbPOST = function(apiCore) {
  var _this = this;

  return function (req, res, next) {
    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );
  };
};


/**
 * The route verb for PUT
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbPUT = function(apiCore){
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );

  };
};


/**
 * The route verb for DELETE 
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void} 
 */
ConnectMiddleware.prototype.routeVerbDELETE = function(apiCore) {
  var _this = this;

  return function (req, res, next) {

    var emitValue = null,
      uniqueRouteKey = req.method + '::' + req.route.path,
      emitEvent = apiCore._routeEventTracker[uniqueRouteKey];

    emitValue = _this.getRequestVariables(req, res, next);

    // emit the route event
    apiCore.intercom.emit( emitEvent, emitValue );

  };
};



/**
 * Get the request variables based on method
 *
 * @param    {Object}     req    The request object
 * @param    {Object}     res    The response object
 * @param    {Function}   next   The next function to call in the node stack
 *
 * @return   {void}
 */
ConnectMiddleware.prototype.getRequestVariables = function(req, res, next) {
  // console.log('req in getRequestVariables: ', req.body, req.params, req.query);

  var emitValue = null,
    apiModelId = this.apiCore.routerManifest.manifest.apiModelId;

  // setup the get or put values
  if( req.method == 'GET' || req.method == 'PUT' ){
    emitValue = {
      conditions: {},
      fields: '',
      options: {}
    };

    if( req.hasOwnProperty("params") && req.params.hasOwnProperty(apiModelId) ){
      emitValue.conditions['_id'] = req.params[apiModelId];
    }

    try {
      // process all the query params
      if( req.hasOwnProperty('query') ){
        // if the query param contains the q attribute
        if( req.query.hasOwnProperty('q') && req.query.q ){
          emitValue.conditions = JSON.parse(req.query.q);
        }
        // if the query param contains the f attribute
        if( req.query.hasOwnProperty('f') && req.query.f ){
          emitValue.fields = req.query.f;
        }
        // if the query param contains the o attribute
        if( req.query.hasOwnProperty('o') && req.query.o ){
          emitValue.options = JSON.parse(req.query.o);
        }
      }  
    }
    catch(e) {
      this.apiCore.intercom.emit('facet:response:error', 400, 'Bad query formatting. Check your JSON or the encoded q parameter for errors.');
      return null;
    }
    

    // process all the body params
    if( req.hasOwnProperty('body') && Object.keys(req.body).length !== 0 ){
      // TODO: Not sure why we need to delete conditions if body is present?
      // .. How is the item Id supposed to be passed through without the conditions?
      
      // delete the conditions 
      // delete emitValue.conditions;
      
      // delete the options 
      delete emitValue.options;
      // if the body contains the fields, set those
      if( req.body.hasOwnProperty('fields') ){
        emitValue['fields'] = req.body.fields;
      }
      // if the body contains the updates, set those
      if( req.body.hasOwnProperty('updates') ){
        emitValue['updates'] = req.body.updates;
      }
    }

  }
  // set up the post values
  else if ( req.method == 'POST' ){
    emitValue = req.body;
  }
  
  return emitValue;
};

exports = module.exports = ConnectMiddleware;


{'fraction_non_alphanumeric': 0.11606114140871035, 'fraction_numerical': 0.0006144865197019741, 'mean_word_length': 2.5779060181368507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3594047', 'n_tokens_mistral': 3942, 'n_tokens_neox': 3870, 'n_words': 1165}