Skip to content

Commit

Permalink
update nestjs example for using multiple protobufs from multiple pack…
Browse files Browse the repository at this point in the history
…ages and servers
  • Loading branch information
zgid123 committed Apr 19, 2023
1 parent 03ee2db commit 4bd49aa
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 9 deletions.
78 changes: 77 additions & 1 deletion examples/nestjs/src/client/client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
dateToGrpcTimestamp,
} from '@grpc.ts/nestjs-client';

import { IExampleService } from '../../protobufTypings/example3.interface';
import type { IExampleService } from '../../protobufTypings/example3.interface';
import type { IExampleService as IAService } from '../../protobufTypings/example.interface';
import type { IExampleService as IEx2Service } from '../../protobufTypings/example2.interface';

@Controller('/client')
export class ClientController {
Expand All @@ -15,6 +17,17 @@ export class ClientController {
serviceName: 'ExampleService',
})
private readonly exampleService: IExampleService,
@GrpcService({
packageName: 'example.v1',
serviceName: 'ExampleService',
})
private readonly aService: IAService,
@GrpcService({
clientName: 'test',
packageName: 'example2.v1',
serviceName: 'ExampleService',
})
private readonly ex2Service: IEx2Service,
) {}

@Get()
Expand All @@ -38,4 +51,67 @@ export class ClientController {

return 'Ok!';
}

@Get('/empty')
public async sendEmptyMessage(): Promise<string> {
this.exampleService
.sendEmptyMessage(
{},
createMetadata({
meta: 'test',
}),
)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(err);
});

return 'Ok!';
}

@Get('/a')
public async sendMessageA(): Promise<string> {
this.aService
.sendMessage(
{
message: 'hello',
createdAt: dateToGrpcTimestamp(new Date()),
},
createMetadata({
meta: 'test',
}),
)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(err);
});

return 'Ok!';
}

@Get('/2')
public async sendMessage2(): Promise<string> {
this.ex2Service
.sendMessage(
{
message: 'hello',
createdAt: dateToGrpcTimestamp(new Date()),
},
createMetadata({
meta: 'test',
}),
)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(err);
});

return 'Ok!';
}
}
21 changes: 18 additions & 3 deletions examples/nestjs/src/client/client.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,26 @@ import { ClientController } from './client.controller';
protoPath: '../proto/example.proto',
},
{
packageName: 'example2.v1',
protoPath: '../proto/example2.proto',
protoPath: '../proto/example3.proto',
},
],
packageDefinitionOptions: {
oneofs: true,
longs: String,
enums: String,
defaults: true,
},
options: {
keepaliveTimeMs: 5_000,
},
},
{
clientName: 'test',
url: 'localhost:3011',
package: [
{
protoPath: '../proto/example3.proto',
packageName: 'example2.v1',
protoPath: '../proto/example2.proto',
},
],
packageDefinitionOptions: {
Expand Down
21 changes: 18 additions & 3 deletions examples/nestjs/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ async function bootstrap() {
protoPath: '../proto/example.proto',
},
{
packageName: 'example2.v1',
protoPath: '../proto/example2.proto',
protoPath: '../proto/example3.proto',
},
],
packageDefinitionOptions: {
oneofs: true,
longs: String,
enums: String,
defaults: true,
},
options: {
keepaliveTimeMs: 5_000,
},
},
{
serverName: 'test',
url: 'localhost:3011',
package: [
{
protoPath: '../proto/example3.proto',
packageName: 'example2.v1',
protoPath: '../proto/example2.proto',
},
],
packageDefinitionOptions: {
Expand Down
72 changes: 70 additions & 2 deletions examples/nestjs/src/server/server.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,79 @@ export class ServerController {
public async sendMessage(
request: ISendMessageRequest,
metadata: Metadata,
call: ServerUnaryCall<unknown, unknown>,
_call: ServerUnaryCall<unknown, unknown>,
): Promise<IGetMessageResponse> {
console.log('request', request);
console.log('metadata', metadata);
console.log('call', call);

return {
message: {
message: 'hola',
createdAt: dateToGrpcTimestamp(new Date()),
},
};
}

@GrpcUnaryMethod({
serviceName: 'ExampleService',
})
public async sendEmptyMessage(
request: ISendMessageRequest,
metadata: Metadata,
_call: ServerUnaryCall<unknown, unknown>,
): Promise<IGetMessageResponse> {
console.log('from empty message');
console.log('request', request);
console.log('metadata', metadata);

return {
message: {
message: 'hola',
createdAt: dateToGrpcTimestamp(new Date()),
},
};
}

@GrpcUnaryMethod({
rpcName: 'sendMessage',
serviceName: 'ExampleService',
options: {
package: 'example.v1',
},
})
public async sendMessageA(
request: ISendMessageRequest,
metadata: Metadata,
_call: ServerUnaryCall<unknown, unknown>,
): Promise<IGetMessageResponse> {
console.log('from sendMessageA');
console.log('request', request);
console.log('metadata', metadata);

return {
message: {
message: 'hola',
createdAt: dateToGrpcTimestamp(new Date()),
},
};
}

@GrpcUnaryMethod({
serverName: 'test',
rpcName: 'sendMessage',
serviceName: 'ExampleService',
options: {
package: 'example2.v1',
},
})
public async sendMessage2(
request: ISendMessageRequest,
metadata: Metadata,
_call: ServerUnaryCall<unknown, unknown>,
): Promise<IGetMessageResponse> {
console.log('from sendMessage2');
console.log('request', request);
console.log('metadata', metadata);

return {
message: {
Expand Down

0 comments on commit 4bd49aa

Please sign in to comment.