Skip to content

question: How to get route.param on a RoutingController? #688

@rafaell-lycan

Description

@rafaell-lycan

I was trying to...

I have a very specific scenario giving the following situation where I need to check on all my routes if a param resourceType exists or not add some params to the request object giving my param value:

app.param('resourceType', (req, res, next, resourceType) => {
  try {
     const { 
        getResource,
        getResources,
        getUsers,
        getRoles,
        updateRole,
        deleteRole,
        createSuperAdmin,
        deleteSuperAdmin,
      } = getResourceRoutersParams(resourceType);

    req.resource = {
        getResource,
        getResources,
        getUsers,
        getRoles,
        updateRole,
        deleteRole,
        createSuperAdmin,
        deleteSuperAdmin,
    };

     next();
    } catch (e) {
      logger.error(e);
      throw new ResourceNotFoundError();
    }
})

I'm solving the issue using the app.param API

The problem:

All my routes contain a :resourceType param:

  • GET /resources/:resourceType/:resourceId/users
  • GET /resources/:resourceType/roles
  • GET/PUT/DELETE /users/:userId/:resourceType
  • GET/PUT/DELETE /users/:userId/:resourceType/:resourceId

There is any way to implement it other than adding a middleware for each route?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: awaiting answerAwaiting answer from the author of issue or PR.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions