index.js
function handleOperation(handlers, mode, checkArity, req, res, done) {
  // parse path:
  // - first part is the operation name
  // - the following components of the path are used as arguments
  const parsedParts = parsePath(req);
  if (parsedParts[0].length === 0) {
    done(new ModofunError(403, 'NoOperation', 'Operation must be specified!'));
    return;
  }
  const [operation, ...args] = parsedParts;

  if (operation && handlers.hasOwnProperty(operation)) {
    // the stack of operation specific middleware
    let operationMiddleware = [];
    // operation handler
    let operationHandler = handlers[operation];

    if (Array.isArray(operationHandler)) {
      // if an array is passed, add operation specific middleware
      // last item in the array should be the operation handler
      operationMiddleware = operationHandler.slice(0, -1);
      operationHandler = operationHandler[operationHandler.length-1];

    } else if (typeof operationHandler !== 'function') {
      // otherwise return internal error
      done(new ModofunError(500, 'InvalidConfig', 'Handler must be a function or array'));
      return;
    }

    // call middleware stack with same req/res context
    runMiddlewareStack(operationMiddleware, req, res, (err) => {
      if (err) {
        done(err);
        return;
      }
      try {
        // call handler function
        if (mode === FUNCTION_MODE) {
          invokeFunctionHandler(operationHandler, args, checkArity, req, res, done);
        } else {
          invokeHTTPHandler(operationHandler, args, req, res, done);
        }
      } catch(err) {
        done(err);
      }
    });

  } else {
    // fail if requested operation cannot be resolved
    done(new ModofunError(404, 'NotFound', `No handler for: ${operation}`));
    return;
  }
}
function handleOperation(handlers, mode, checkArity, req, res, done) {
  // parse path:
  // - first part is the operation name
  // - the following components of the path are used as arguments
  const parsedParts = parsePath(req);
  if (parsedParts[0].length === 0) {
    done(new ModofunError(403, 'NoOperation', 'Operation must be specified!'));
    return;
  }
  const [operation, ...args] = parsedParts;

  if (operation && handlers.hasOwnProperty(operation)) {
    // the stack of operation specific middleware
    let operationMiddleware = [];
    // operation handler
    let operationHandler = handlers[operation];

    if (Array.isArray(operationHandler)) {
      // if an array is passed, add operation specific middleware
      // last item in the array should be the operation handler
      operationMiddleware = operationHandler.slice(0, -1);
      operationHandler = operationHandler[operationHandler.length-1];

    } else if (typeof operationHandler !== 'function') {
      // otherwise return internal error
      done(new ModofunError(500, 'InvalidConfig', 'Handler must be a function or array'));
      return;
    }

    // call middleware stack with same req/res context
    runMiddlewareStack(operationMiddleware, req, res, (err) => {
      if (err) {
        done(err);
        return;
      }
      try {
        // call handler function
        if (mode === FUNCTION_MODE) {
          invokeFunctionHandler(operationHandler, args, checkArity, req, res, done);
        } else {
          invokeHTTPHandler(operationHandler, args, req, res, done);
        }
      } catch(err) {
        done(err);
      }
    });

  } else {
    // fail if requested operation cannot be resolved
    done(new ModofunError(404, 'NotFound', `No handler for: ${operation}`));
    return;
  }
}
function handleOperation ( handlers , mode , checkArity , req , res , done ) { // parse path: // - first part is the operation name // - the following components of the path are used as arguments const parsedParts = parsePath ( req ) ; if ( parsedParts [ 0 ] . length === 0 ) { done ( new ModofunError ( 403 , 'NoOperation' , 'Operation must be specified!' ) ) ; return ; } const [ operation , ... args ] = parsedParts ; if ( operation && handlers . hasOwnProperty ( operation ) ) { // the stack of operation specific middleware let operationMiddleware = [ ] ; // operation handler let operationHandler = handlers [ operation ] ; if ( Array . isArray ( operationHandler ) ) { // if an array is passed, add operation specific middleware // last item in the array should be the operation handler operationMiddleware = operationHandler . slice ( 0 , - 1 ) ; operationHandler = operationHandler [ operationHandler . length - 1 ] ; } else if ( typeof operationHandler !== 'function' ) { // otherwise return internal error done ( new ModofunError ( 500 , 'InvalidConfig' , 'Handler must be a function or array' ) ) ; return ; } // call middleware stack with same req/res context runMiddlewareStack ( operationMiddleware , req , res , ( err ) => { if ( err ) { done ( err ) ; return ; } try { // call handler function if ( mode === FUNCTION_MODE ) { invokeFunctionHandler ( operationHandler , args , checkArity , req , res , done ) ; } else { invokeHTTPHandler ( operationHandler , args , req , res , done ) ; } } catch ( err ) { done ( err ) ; } } ) ; } else { // fail if requested operation cannot be resolved done ( new ModofunError ( 404 , 'NotFound' , ` ${ operation } ` ) ) ; return ; } }
Try to apply supplied handlers to the requested operation.
@private
Try to apply supplied handlers to the requested operation .