2

I have followed the tutorial from:

But I do not find a way to inject dynamic parameters in @UseBefore/@UseAfter middleware call Like this:

@Get("/something/:id")
@UseBefore(checkPermission(['canReadThis','canWriteThis']))
getOne(@Param("id") id: number) {
    // ...
}

Can someone gide me doing this?

I found this :

But I dont know how to handle data from the Context "Class" and "{req, resp, next}" like this

function loggingMiddleware(request: any, response: any, next?: (err?: any) => any): any {
  console.log('do something...');
  next();
}
mduvernon
  • 498
  • 3
  • 13

2 Answers2

1

Finaly Find out

My middlerware:

@UseBefore((request: any, response: any, next: Function) =>
  PermissionsChecker(request, response, next)({
    permissions: ['ONE_PERMISSION', 'ANOTHER_ONE']
  })
)

My Handler Function

export const PermissionsChecker = (request: any, response: any, next: Function) => 
  async ({ permissions }: { permissions: string[] }): Promise<any> => { 
     // ... do something 
}
mduvernon
  • 498
  • 3
  • 13
0

Try this

function executeMiddlewareList (middlewareList, req, res, next) {
   async.eachSeries(middlewareList, function(middleware,callback) {
      middleware.bind(null,req,res,callback)()
   }, function(err) {
      if (err) return res.status(500).json({error: err});
      next();
   })
}

Nsamba Isaac
  • 357
  • 4
  • 14