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

feat(microservices): Add grpc metadata to server responce #5143

Merged
merged 5 commits into from
Oct 30, 2020

Conversation

AlexDaSoul
Copy link

#5098 PR Checklist
Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Cannot send metadata from @GrpcMethod

Issue Number: #1897

What is the new behavior?

import { Metadata } from 'grpc';

@GrpcMethod('Service', 'Method')
public method(request: any, metadata: Metadata, sendMetadata: Function): any {
   // 1. nothing to do with request that is empty in this case
   // 2. may something with metadata from client if needed
   // 3. send metadata with responce........
   const srvMetadata = new Metadata();

   srvMetadata.add('Set-Cookie', 'yummy_cookie=choco');

   sendMetadata(srvMetadata);

   return 'Hello World!!!';
}

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

example of usage:

import { Metadata } from 'grpc';

@GrpcMethod('Service', 'Method')
public method(request: any, metadata: Metadata, sendMetadata: any): any {
   // 1. nothing to do with request that is empty in this case
   // 2. may something with metadata from client if needed
   // 3. send metadata with responce........
   const srvMetadata = new Metadata();

   srvMetadata.add('Set-Cookie', 'yummy_cookie=choco');

   sendMetadata(srvMetadata);

   return 'Hello World!!!';
}
@coveralls
Copy link

coveralls commented Jul 24, 2020

Pull Request Test Coverage Report for Build d8106f42-dec7-49a5-bd41-01fe82ee1392

  • 4 of 5 (80.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 94.8%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/microservices/factories/rpc-params-factory.ts 0 1 0.0%
Totals Coverage Status
Change from base Build df8fd14f-cb6a-4c1f-a4b6-051ea09e4baa: -0.02%
Covered Lines: 4922
Relevant Lines: 5192

💛 - Coveralls

alex added 2 commits July 29, 2020 11:14
example of usage:

import { Metadata } from 'grpc';

@GrpcMethod('Service', 'Method')
public method(request: any, metadata: Metadata, call: any): any {
   // 1. nothing to do with request that is empty in this case
   // 2. may something with metadata from client if needed
   // 3. send metadata with responce........
   const srvMetadata = new Metadata();

   srvMetadata.add('Set-Cookie', 'yummy_cookie=choco');

   call.sendMetadata(srvMetadata);

   return 'Hello World!!!';
}
@kamilmysliwiec
Copy link
Member

Would you like to create a PR to the docs with this feature?

@AlexDaSoul
Copy link
Author

Would you like to create a PR to the docs with this feature?

Sure, I will do it

@AlexDaSoul
Copy link
Author

Would you like to create a PR to the docs with this feature?

@kamilmysliwiec done nestjs/docs.nestjs.com#1386

@kamilmysliwiec kamilmysliwiec added this to the 7.5.0 milestone Oct 5, 2020
@kamilmysliwiec kamilmysliwiec merged commit 0d7b13e into nestjs:master Oct 30, 2020
@tchambard
Copy link

@AlexDaSoul Is there a way to get these metadata from the response on the client side when using nestjs grpc client ? When I look into the code, it seems not, but maybe I'm missing something ?

@AlexDaSoul
Copy link
Author

AlexDaSoul commented Feb 11, 2021

@AlexDaSoul Is there a way to get these metadata from the response on the client side when using nestjs grpc client ? When I look into the code, it seems not, but maybe I'm missing something ?

There is no direct option, but we can get metadata using the event 'metadata' of client

const client = new proto.MyService(host, creds);

call = client.GrpcMethod({ //... });
call.on('metadata', (metadata) => console.log(metadata));
call.on('data', fillSnapshot);
call.on('end', () => console.log('end'));
call.on('cancelled', () => console.log('cancelled'));
call.on('error', (err) => console.log(err));

@tchambard
Copy link

Ok. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants