all files / server/ controller-hapi-v1.js

100% Statements 7/7
60.87% Branches 28/46
22.22% Functions 2/9
100% Lines 7/7
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                                      24×             24×                       24×                       24×                       24×                       24×                                                                  
'use strict'
 
import prompter from './credentials-prompter'
import Api from '../lib/index'
import routes from './routes-v1'
import config from 'config'
 
exports.register = function (server, options, next) {
  // This const is instantiated once at init,
  // to keep the same instance of this object for all requests.
  // This is mandatory to share authenticated session.
  // But from this point, we must always ensure an instance will handle calls for only one Myfox account!
  const api = Api(config.get('myfox-wrapper-api.server.myfox'), prompter())
 
  // Routes
  for (let routeName in routes) {
    let routeParams = routes[routeName]
    let preHandlers = []
 
    // pre-action middleware
    if (routeParams.pre !== null && routeParams.pre !== undefined) {
      const preHandler = routeParams.pre.hapi || routeParams.pre
      preHandlers.push({method: (request, reply) => {
        return preHandler(request, reply, api, () => { reply.continue() })
      }})
    }
 
    if (routeParams.get !== null && routeParams.get !== undefined) {
      server.route([{
        path: routeParams.path.hapi || routeParams.path,
        method: 'get',
        config: {
          pre: preHandlers,
          handler: (request, reply) => {
            (routeParams.get.hapi || routeParams.get)(request, reply, api)
          }
        }
      }])
    }
    if (routeParams.post !== null && routeParams.post !== undefined) {
      server.route([{
        path: routeParams.path.hapi || routeParams.path,
        method: 'post',
        config: {
          pre: preHandlers,
          handler: (request, reply) => {
            (routeParams.post.hapi || routeParams.post)(request, reply, api)
          }
        }
      }])
    }
    Iif (routeParams.patch !== null && routeParams.patch !== undefined) {
      server.route([{
        path: routeParams.path.hapi || routeParams.path,
        method: 'patch',
        config: {
          pre: preHandlers,
          handler: (request, reply) => {
            (routeParams.patch.hapi || routeParams.patch)(request, reply, api)
          }
        }
      }])
    }
    if (routeParams.put !== null && routeParams.put !== undefined) {
      server.route([{
        path: routeParams.path.hapi || routeParams.path,
        method: 'put',
        config: {
          pre: preHandlers,
          handler: (request, reply) => {
            (routeParams.put.hapi || routeParams.put)(request, reply, api)
          }
        }
      }])
    }
    Iif (routeParams.delete !== null && routeParams.delete !== undefined) {
      server.route([{
        path: routeParams.path.hapi || routeParams.path,
        method: 'delete',
        config: {
          pre: preHandlers,
          handler: (request, reply) => {
            (routeParams.delete.hapi || routeParams.delete)(request, reply, api)
          }
        }
      }])
    }
  }
 
  // Tea pot test route. To keep for test purpose.
  server.route([{
    path: '/',
    method: 'get',
    config: {
      handler: (request, reply) => {
        reply('Hello world!').code(418)
      }
    }
  }])
 
  next()
}
 
exports.register.attributes = {
  name: 'controller-hapi-v1',
  version: '1.0.0'
}