Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request (context) provider in Microservices for request-scoped providers #2111

Closed
ivibe opened this issue Apr 29, 2019 · 4 comments
Closed

Comments

@ivibe
Copy link
Contributor

ivibe commented Apr 29, 2019

I'm submitting a...

[x] Feature request

Current behavior

In the HTTP application it's possible to inject an original request reference into request-scoped providers. However, there's no such functionality for microservices.

Expected behavior

It would be great to have something similar to Request provider but in Microservices. For example, Context provider.

In case of Microservices, context might be an object that contains pattern and transmitted data, at least for TCP Microservice.

CONTEXT {
  pattern,
  data
}

What is the motivation / use case for changing the behavior?

Having access to Request (context) provider permit to instantiate services per request based on the context.
For example:

import { Module, Scope } from '@nestjs/common';
import { CONTEXT } from '@nestjs/microservices';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { ExternalCatsService } from './external-cats.service';
import { LocalCatsService } from './local-cats.service';

@Module({
  controllers: [CatsController],
  providers: [
    {
      provide: CatsService,
      scope: Scope.REQUEST,
      useFactory: ctx =>
        ctx.data.isLocal ? new LocalCatsService() : new ExternalCatsService(),
      inject: [CONTEXT],
    },
  ],
})
export class CatsModule { }

Environment


Nest version: 6.1.1

 
For Tooling issues:
- Node version: 11.10.0
- Platform:  Windows 10

Others:

What do you think?

@kamilmysliwiec
Copy link
Member

Added in 6.30. We need to update the docs accordingly (let's track this here nestjs/docs.nestjs.com#461)

import { CONTEXT } from '@nestjs/microservices';

CONTEXT implements RequestContext<T>:

pattern: string | Record<string, any>;
data: T;

@ivibe
Copy link
Contributor Author

ivibe commented Jun 5, 2019

Awesome!
Unfortunately, didn't have time at all to participate in making a PR.
Thank you!

@rhlsthrm
Copy link

Can you please provide a simple example of how to print out the request context? I am trying to print the subject of the received message in my MessagePattern controller.

For example if I subscribe to @MessagePattern("user.id.>") I want to be able to get the subject of the request so I can split the string get get the send ID, for example if subject is user.id.abcdef I need the abcdef part.

@lock
Copy link

lock bot commented Sep 23, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants