Servify

class
 new Servify() 

This class detects the server
and applies the API to the
request and response objects

class Servify {
  constructor(core) {
      log.debug(`class: ${Servify.name}`, `process: constructor`);
      this.server = '';
      this.core = core;
    }

apply

method
 Servify.prototype.apply() 

Option name Type Description
req Object

The request object

res Object

The request object

next Function

The next function

return Servify

The context of Servify

Applies the API to the objects

apply(req, res, next) {
  log.debug(`class: ${Servify.name}`, `process: apply`);
  var _this = this.core;
  // Koa?
  if (this.isKoa(req) && !_.isEmpty(req)) {
    this.server = 'koa';
    // Apply api to koa
    _this.assign(req);
    _this.assign(req.request, req.response);
    if (req.req || req.res) _this.assign(req.req, req.res);
    if (req.state) _this.assign(req.state);
  }
  // Hapi?
  if (this.isHapi(req) && !_.isEmpty(req)) {
    this.server = 'hapi';
    if (req.response)
      if (req.response.variety === 'view')
        _this.assign(req.response.source.context);
    _this.assign(req);
  }
  // Express ?
  if (this.isExpress(req) && !_.isEmpty(req)) {
    this.server = 'express';
    _this.assign(req, res);
    // Apply to API to the view
    if (res && res.locals) _this.assign(res.locals);
  }
  log.info(`class: ${Servify.name}`, `server: ${this.server}`);
  // Make sure next exists and call it.
  if (_.isFunction(next)) next();
  return this;
}

isKoa

method
 Servify.prototype.isKoa() 

Option name Type Description
req Object

The request object

return Boolean

True if the server is Koa.

Determines if the current server is Koa.

isKoa(req) {
    return req && !req.raw ? (req.response && req.request) :
      !_.isEmpty(this.server) ? this.server === 'koa' : false;
  }

isHapi

method
 Servify.prototype.isHapi() 

Option name Type Description
req Object

The request object

return Boolean

True if the server is Hapi.

Determines if the current server is Hapi.

isHapi(req) {
    return req ? (req.raw) :
      !_.isEmpty(this.server) ? this.server === 'hapi' : false;
  }

isExpress

method
 Servify.prototype.isExpress() 

Option name Type Description
req Object

The request object

return Boolean

True if the server is Express.

Determines if the current server is Express.

isExpress(req) {
  return req && !req.raw ? (req && !req.raw && !req.response) :
    !_.isEmpty(this.server) ? this.server === 'express' : false;
}
}

servify

function
 servify() 

Option name Type Description
core Core

The context of Core

return Servify

An instance of Servify

Creates an instance of Servify

function servify(core) {
  'use strict';
  return new Servify(core);
}
export default servify;