index.js
function invokeFunctionHandler(handler, args, checkArity, req, res, done) {
  // check if number of arguments provided matches the handler function arity
  if (checkArity && args.length < handler.length) { // < due to possible optionals
    done(new ModofunError(400, 'InvalidInput',
      `This operation requires ${handler.length} parameters. Received ${args.length}.`));
    return;
  }
  // set this in function call to the remaining relevant request data
  // could consider pushing the whole request object instead?
  // for now prefer to keep it to minimum to allow maximum flexibility
  const thisArg = {
    method: req.method,
    headers: req.headers,
    body: req.body,
    query: req.query,
    user: req.user
  };
  // call handler function with
  let result = handler.apply(thisArg, args);
  // handle results that are not a trusted Promise with Promise.resolve()
  // which also supports other then-ables
  if (result instanceof Promise === false) {
    result = Promise.resolve(result);
  }
  return result
    .then(value => {
      if (value === null) {
        done(new ModofunError(404, 'NullResponse', `${req.path} resource was not found`));
        return;
      }
      if (value === undefined || value === '') {
        res.status(204).end();
      } else {
        res.status(200).json(value);
      }
      done();
    })
    .catch(done);
}
function invokeFunctionHandler(handler, args, checkArity, req, res, done) {
  // check if number of arguments provided matches the handler function arity
  if (checkArity && args.length < handler.length) { // < due to possible optionals
    done(new ModofunError(400, 'InvalidInput',
      `This operation requires ${handler.length} parameters. Received ${args.length}.`));
    return;
  }
  // set this in function call to the remaining relevant request data
  // could consider pushing the whole request object instead?
  // for now prefer to keep it to minimum to allow maximum flexibility
  const thisArg = {
    method: req.method,
    headers: req.headers,
    body: req.body,
    query: req.query,
    user: req.user
  };
  // call handler function with
  let result = handler.apply(thisArg, args);
  // handle results that are not a trusted Promise with Promise.resolve()
  // which also supports other then-ables
  if (result instanceof Promise === false) {
    result = Promise.resolve(result);
  }
  return result
    .then(value => {
      if (value === null) {
        done(new ModofunError(404, 'NullResponse', `${req.path} resource was not found`));
        return;
      }
      if (value === undefined || value === '') {
        res.status(204).end();
      } else {
        res.status(200).json(value);
      }
      done();
    })
    .catch(done);
}
function invokeFunctionHandler ( handler , args , checkArity , req , res , done ) { // check if number of arguments provided matches the handler function arity if ( checkArity && args . length < handler . length ) { // < due to possible optionals done ( new ModofunError ( 400 , 'InvalidInput' , ` ${ handler . length } ${ args . length } ` ) ) ; return ; } // set this in function call to the remaining relevant request data // could consider pushing the whole request object instead? // for now prefer to keep it to minimum to allow maximum flexibility const thisArg = { method : req . method , headers : req . headers , body : req . body , query : req . query , user : req . user } ; // call handler function with let result = handler . apply ( thisArg , args ) ; // handle results that are not a trusted Promise with Promise.resolve() // which also supports other then-ables if ( result instanceof Promise === false ) { result = Promise . resolve ( result ) ; } return result . then ( value => { if ( value === null ) { done ( new ModofunError ( 404 , 'NullResponse' , ` ${ req . path } ` ) ) ; return ; } if ( value === undefined || value === '' ) { res . status ( 204 ) . end ( ) ; } else { res . status ( 200 ) . json ( value ) ; } done ( ) ; } ) . catch ( done ) ; }
Invoke the provided request handler function.
The handler should return a value or a Promised value
which will be added to the reponse automatically.
@private
Invoke the provided request handler function . The handler should return a value or a Promised value which will be added to the reponse automatically .