Skip to content

Allow injecting extra arguments to middleware #6

@yowu

Description

@yowu

Hi, I am searching for a middleware implement for our project. The solution looks promising.

In our case, sometimes, the middleware need to know some context information to fulfill its task. So, I am wondering if it's possible to enable inject some extra argument to middleware to make the information available.
The proposal looks like:

// middleware constructor
//  @param {object} extraArgument - data will be injected into middleware
 constructor(target, extraArgument, ...middlewareObjects) 

// middleware _applyToMethod
middlewares.forEach(middleware =>
       typeof middleware === 'function' &&
       this._methodMiddlewares[methodName].push(middleware(this._target, this._extraArgument))
);

// middleware implementation
 const walk = (target, extraArgument) => next => (...args) => {
    // the extraArgument is now accessible in the middleware implementation
    return result;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions