'Accessing a variable from a service without passing it as parameters to a method

I have a middleware/guard that gets a part of JWT as a string. After retrieving, normally I would just pass the string as a parameter to the methods that I consume from the injected service. However, adding a parameter would introduce a breaking change to the service.

Q: How would I be able to access that string/variable - that is determined in the middleware/guard - from the service level correctly without introducing a new parameter to the methods in the injected service?

What I mean by "correctly" is... that every call to these endpoints. It passes through the middleware/guard. The JWT payload is always different. So the resulting string is also different for every request.

Should I need to refactor/change my approach for this? Or is it possible to do so?

My sample code:

  @UseGuards(JwtAuthGuard, ProjectGuard) //where the string is getting retrieved
  @ApiQuery({ required: false, name: 'email' })
  @ApiQuery({ required: false, name: 'clientId' })
  @Get()
  findAll(
    @Query('email') email?: string,
    @Query('clientId') clientId?: string,
  ): Promise<User[]> {
    if (email) {
      return this.userService.findByEmail(email); // the service that needs to access the string
    }
    if (clientId) {
      return this.userService.findByClientId(clientId); // the service that needs to access the string
    }
    return this.userService.findAll(); // the service that needs to access the string
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source