Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat(grpc): add possibility to fetch user by id
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Bühler committed Jan 27, 2023
1 parent 881468c commit 103e4ac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions kreya/gRPC/Get User By Id-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "179828644301046017"
}
12 changes: 12 additions & 0 deletions kreya/gRPC/Get User By Id.krop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"details": {
"methodFqn": "users.UsersService.Get"
},
"requests": [
{
"location": "Get User By Id-request.json"
}
],
"operationType": "unary",
"invokerName": "grpc"
}
8 changes: 7 additions & 1 deletion src/grpc/protos/users.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ 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);
}

message ListRequest {
uint32 offset = 1;
uint32 limit = 2;
string newer_than = 3;
}

message GetRequest {
string id = 1;
}

message ListResponse {
Expand Down
13 changes: 12 additions & 1 deletion src/grpc/users.grpc.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -51,6 +52,16 @@ export class UsersService {
};
}

@GrpcMethod()
async get({ id }: GetRequest): Promise<GrpcUser> {
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<GrpcUser> {
const user = grpcUser(metadata);
Expand Down

0 comments on commit 103e4ac

Please sign in to comment.