From 103e4ac057bf97af8fc595d92389b9368de479fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BChler?= Date: Fri, 27 Jan 2023 10:53:00 +0700 Subject: [PATCH] feat(grpc): add possibility to fetch user by id --- kreya/gRPC/Get User By Id-request.json | 3 +++ kreya/gRPC/Get User By Id.krop | 12 ++++++++++++ src/grpc/protos/users.proto | 8 +++++++- src/grpc/users.grpc.ts | 13 ++++++++++++- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 kreya/gRPC/Get User By Id-request.json create mode 100644 kreya/gRPC/Get User By Id.krop diff --git a/kreya/gRPC/Get User By Id-request.json b/kreya/gRPC/Get User By Id-request.json new file mode 100644 index 0000000..26ecb2d --- /dev/null +++ b/kreya/gRPC/Get User By Id-request.json @@ -0,0 +1,3 @@ +{ + "id": "179828644301046017" +} \ No newline at end of file diff --git a/kreya/gRPC/Get User By Id.krop b/kreya/gRPC/Get User By Id.krop new file mode 100644 index 0000000..07b13a0 --- /dev/null +++ b/kreya/gRPC/Get User By Id.krop @@ -0,0 +1,12 @@ +{ + "details": { + "methodFqn": "users.UsersService.Get" + }, + "requests": [ + { + "location": "Get User By Id-request.json" + } + ], + "operationType": "unary", + "invokerName": "grpc" +} \ No newline at end of file diff --git a/src/grpc/protos/users.proto b/src/grpc/protos/users.proto index 4dd6bf9..036942b 100644 --- a/src/grpc/protos/users.proto +++ b/src/grpc/protos/users.proto @@ -30,6 +30,9 @@ service UsersService { // The result is paginated. rpc List(ListRequest) returns (ListResponse); + // Fetch a user by ID. + rpc Get(GetRequest) returns (User); + // Get the authenticated own user. rpc Me(google.protobuf.Empty) returns (User); } @@ -37,7 +40,10 @@ service UsersService { message ListRequest { uint32 offset = 1; uint32 limit = 2; - string newer_than = 3; +} + +message GetRequest { + string id = 1; } message ListResponse { diff --git a/src/grpc/users.grpc.ts b/src/grpc/users.grpc.ts index 882699b..89537de 100644 --- a/src/grpc/users.grpc.ts +++ b/src/grpc/users.grpc.ts @@ -1,12 +1,13 @@ import { Metadata } from '@grpc/grpc-js'; import { UseGuards } from '@nestjs/common'; -import { GrpcMethod, GrpcService } from '@nestjs/microservices'; +import { GrpcMethod, GrpcService, RpcException } from '@nestjs/microservices'; import { User } from 'src/auth/user'; import { UsersService as DataUsersService, ZitadelUser, } from '../data/users.service'; import { Empty } from './gen/google/protobuf/Empty'; +import { GetRequest } from './gen/users/GetRequest'; import { ListRequest } from './gen/users/ListRequest'; import { ListResponse } from './gen/users/ListResponse'; import { User as GrpcUser } from './gen/users/User'; @@ -51,6 +52,16 @@ export class UsersService { }; } + @GrpcMethod() + async get({ id }: GetRequest): Promise { + if (!id) { + throw new RpcException('id is required'); + } + + const user = await this.users.get(id); + return mapUser(user); + } + @GrpcMethod() async me(_: Empty, metadata: Metadata): Promise { const user = grpcUser(metadata);