Skip to content

Commit

Permalink
#57
Browse files Browse the repository at this point in the history
- created code generator
- use plugin parameter "client_grpc1"
- created example project
  • Loading branch information
timostamm committed Jan 30, 2021
1 parent 357c8ba commit 63c82d0
Show file tree
Hide file tree
Showing 10 changed files with 2,461 additions and 189 deletions.
7 changes: 3 additions & 4 deletions packages/example-node-grpc-client/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
.PHONY: default npm-install generate run-client


default: npm-install run-client
#default: npm-install generate run-client
default: npm-install generate run-client

npm-install:
npm i

#generate:
# npx protoc -I . --ts_out . --ts_opt client_none,generate_dependencies,optimize_code_size service-example.proto
generate:
npx protoc -I . --ts_out . --ts_opt client_grpc1,generate_dependencies,optimize_code_size service-example.proto

run-client:
npx ts-node client.ts
Expand Down
193 changes: 186 additions & 7 deletions packages/example-node-grpc-client/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ChannelCredentials} from "@grpc/grpc-js";
import {FailRequest} from "./service-example";
import {ExampleServiceClient, IExampleServiceClient} from "./todo-generate.service-example.grpc-client";
import {ExampleServiceClient, IExampleServiceClient} from "./service-example.grpc-client";


const client = new ExampleServiceClient(
Expand All @@ -15,16 +15,16 @@ async function main() {

await callUnary(client);

// await callServerStream(client);
//
// await callClientStream(client);
//
// await callBidi(client);
await callServerStream(client);

await callClientStream(client);

await callBidi(client);

}


async function callUnary(client: IExampleServiceClient) {
function callUnary(client: IExampleServiceClient) {

console.log(`### calling method "unary"...`)

Expand Down Expand Up @@ -56,5 +56,184 @@ async function callUnary(client: IExampleServiceClient) {
}


function callServerStream(client: IExampleServiceClient) {

console.log(`### calling method "serverStream"...`)

const call = client.serverStream({
question: 'whats up?',
pleaseDelayResponseMs: 50,
pleaseFail: FailRequest.FAIL_REQUEST_NONE,
disableSendingExampleResponseHeaders: false,
});

call.on('data', arg1 => {
console.log("got response message: ", arg1)
});

call.on('metadata', arg1 => {
console.log("got response headers: ", arg1)
});

call.on('error', arg1 => {
console.log("got err: ", arg1)
});

call.on('status', arg1 => {
console.log("got status: ", arg1)
});

call.on('close', () => {
console.log("got closed event")
});

call.on('end', () => {
console.log("got end event")
});

return new Promise(resolve => {
call.on('end', () => resolve());
});
}


function callClientStream(client: IExampleServiceClient) {

console.log(`### calling method "clientStream"...`)

const call = client.clientStream((err, value) => {
if (err) {
console.log("got err: ", err)
}
if (value) {
console.log("got response message: ", value)
}
});


call.on('data', arg1 => {
console.log("got response message: ", arg1)
});

call.on('metadata', arg1 => {
console.log("got response headers: ", arg1)
});

call.on('error', arg1 => {
console.log("got err: ", arg1)
});

call.on('status', arg1 => {
console.log("got status: ", arg1)
});

call.on('close', () => {
console.log("got closed event")
});

call.on('end', () => {
console.log("got end event")
});


console.log("sending message...");
call.write({
question: 'whats up? #1',
pleaseDelayResponseMs: 50,
pleaseFail: FailRequest.FAIL_REQUEST_NONE,
disableSendingExampleResponseHeaders: false,
});

console.log("sending message...");
call.write({
question: 'whats up? #2',
pleaseDelayResponseMs: 50,
pleaseFail: FailRequest.FAIL_REQUEST_NONE,
disableSendingExampleResponseHeaders: false,
});

console.log("sending message...");
call.write({
question: 'whats up? #3',
pleaseDelayResponseMs: 50,
pleaseFail: FailRequest.FAIL_REQUEST_NONE,
disableSendingExampleResponseHeaders: false,
});

console.log("done sending");
call.end();


return new Promise(resolve => {
call.on('status', () => resolve());
});
}


function callBidi(client: IExampleServiceClient) {

console.log(`### calling method bidi...`)

const call = client.bidi();


console.log("sending message...");
call.write({
question: 'whats up? #1',
pleaseDelayResponseMs: 50,
pleaseFail: FailRequest.FAIL_REQUEST_NONE,
disableSendingExampleResponseHeaders: false,
});

console.log("sending message...");
call.write({
question: 'whats up? #2',
pleaseDelayResponseMs: 50,
pleaseFail: FailRequest.FAIL_REQUEST_NONE,
disableSendingExampleResponseHeaders: false,
});

console.log("sending message...");
call.write({
question: 'whats up? #3',
pleaseDelayResponseMs: 50,
pleaseFail: FailRequest.FAIL_REQUEST_NONE,
disableSendingExampleResponseHeaders: false,
});

console.log("done sending");
call.end();


call.on('data', arg1 => {
console.log("got response message: ", arg1)
});

call.on('metadata', arg1 => {
console.log("got response headers: ", arg1)
});

call.on('error', arg1 => {
console.log("got err: ", arg1)
});

call.on('status', arg1 => {
console.log("got status: ", arg1)
});

call.on('close', () => {
console.log("got closed event")
});

call.on('end', () => {
console.log("got end event")
});

return new Promise(resolve => {
call.on('end', () => resolve());
});
}


main().catch(e => console.error(e)).finally(() => process.exit());

76 changes: 76 additions & 0 deletions packages/example-node-grpc-client/service-example.grpc-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// @generated by protobuf-ts 2.0.0-alpha.12 with parameters client_grpc1,generate_dependencies,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ExampleService } from "./service-example";
import { BinaryWriteOptions } from "@protobuf-ts/runtime";
import { BinaryReadOptions } from "@protobuf-ts/runtime";
import { ExampleResponse } from "./service-example";
import { ExampleRequest } from "./service-example";
import * as grpc from "@grpc/grpc-js";
/**
* @generated from protobuf service spec.ExampleService
*/
export interface IExampleServiceClient {
/**
* @generated from protobuf rpc: Unary(spec.ExampleRequest) returns (spec.ExampleResponse);
*/
unary(input: ExampleRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientUnaryCall;
unary(input: ExampleRequest, metadata: grpc.Metadata, callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientUnaryCall;
unary(input: ExampleRequest, options: grpc.CallOptions, callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientUnaryCall;
unary(input: ExampleRequest, callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientUnaryCall;
/**
* @generated from protobuf rpc: ServerStream(spec.ExampleRequest) returns (stream spec.ExampleResponse);
*/
serverStream(input: ExampleRequest, metadata?: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<ExampleResponse>;
serverStream(input: ExampleRequest, options?: grpc.CallOptions): grpc.ClientReadableStream<ExampleResponse>;
/**
* @generated from protobuf rpc: ClientStream(stream spec.ExampleRequest) returns (spec.ExampleResponse);
*/
clientStream(metadata: grpc.Metadata, options: grpc.CallOptions, callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientWritableStream<ExampleRequest>;
clientStream(metadata: grpc.Metadata, callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientWritableStream<ExampleRequest>;
clientStream(options: grpc.CallOptions, callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientWritableStream<ExampleRequest>;
clientStream(callback: (err: grpc.ServiceError | null, value?: ExampleResponse) => void): grpc.ClientWritableStream<ExampleRequest>;
/**
* @generated from protobuf rpc: Bidi(stream spec.ExampleRequest) returns (stream spec.ExampleResponse);
*/
bidi(metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientDuplexStream<ExampleRequest, ExampleResponse>;
bidi(options?: grpc.CallOptions): grpc.ClientDuplexStream<ExampleRequest, ExampleResponse>;
}
/**
* @generated from protobuf service spec.ExampleService
*/
export class ExampleServiceClient extends grpc.Client implements IExampleServiceClient {
private readonly _binaryOptions: Partial<BinaryReadOptions & BinaryWriteOptions>;
constructor(address: string, credentials: grpc.ChannelCredentials, options: grpc.ClientOptions = {}, binaryOptions: Partial<BinaryReadOptions & BinaryWriteOptions> = {}) {
super(address, credentials, options);
this._binaryOptions = binaryOptions;
}
/**
* @generated from protobuf rpc: Unary(spec.ExampleRequest) returns (spec.ExampleResponse);
*/
unary(input: ExampleRequest, metadata: grpc.Metadata | grpc.CallOptions | ((err: grpc.ServiceError | null, value?: ExampleResponse) => void), options?: grpc.CallOptions | ((err: grpc.ServiceError | null, value?: ExampleResponse) => void), callback?: ((err: grpc.ServiceError | null, value?: ExampleResponse) => void)): grpc.ClientUnaryCall {
const method = ExampleService.methods[0];
return this.makeUnaryRequest<ExampleRequest, ExampleResponse>(`/${ExampleService.typeName}/${method.name}`, (value: ExampleRequest): Buffer => Buffer.from(method.I.toBinary(value, this._binaryOptions)), (value: Buffer): ExampleResponse => method.O.fromBinary(value, this._binaryOptions), input, (metadata as any), (options as any), (callback as any));
}
/**
* @generated from protobuf rpc: ServerStream(spec.ExampleRequest) returns (stream spec.ExampleResponse);
*/
serverStream(input: ExampleRequest, metadata?: grpc.Metadata | grpc.CallOptions, options?: grpc.CallOptions): grpc.ClientReadableStream<ExampleResponse> {
const method = ExampleService.methods[1];
return this.makeServerStreamRequest<ExampleRequest, ExampleResponse>(`/${ExampleService.typeName}/${method.name}`, (value: ExampleRequest): Buffer => Buffer.from(method.I.toBinary(value, this._binaryOptions)), (value: Buffer): ExampleResponse => method.O.fromBinary(value, this._binaryOptions), input, (metadata as any), options);
}
/**
* @generated from protobuf rpc: ClientStream(stream spec.ExampleRequest) returns (spec.ExampleResponse);
*/
clientStream(metadata: grpc.Metadata | grpc.CallOptions | ((err: grpc.ServiceError | null, value?: ExampleResponse) => void), options?: grpc.CallOptions | ((err: grpc.ServiceError | null, value?: ExampleResponse) => void), callback?: ((err: grpc.ServiceError | null, value?: ExampleResponse) => void)): grpc.ClientWritableStream<ExampleRequest> {
const method = ExampleService.methods[2];
return this.makeClientStreamRequest<ExampleRequest, ExampleResponse>(`/${ExampleService.typeName}/${method.name}`, (value: ExampleRequest): Buffer => Buffer.from(method.I.toBinary(value, this._binaryOptions)), (value: Buffer): ExampleResponse => method.O.fromBinary(value, this._binaryOptions), (metadata as any), (options as any), (callback as any));
}
/**
* @generated from protobuf rpc: Bidi(stream spec.ExampleRequest) returns (stream spec.ExampleResponse);
*/
bidi(metadata?: grpc.Metadata | grpc.CallOptions, options?: grpc.CallOptions): grpc.ClientDuplexStream<ExampleRequest, ExampleResponse> {
const method = ExampleService.methods[3];
return this.makeBidiStreamRequest<ExampleRequest, ExampleResponse>(`/${ExampleService.typeName}/${method.name}`, (value: ExampleRequest): Buffer => Buffer.from(method.I.toBinary(value, this._binaryOptions)), (value: Buffer): ExampleResponse => method.O.fromBinary(value, this._binaryOptions), (metadata as any), options);
}
}
2 changes: 1 addition & 1 deletion packages/example-node-grpc-client/service-example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protobuf-ts 2.0.0-alpha.12 with parameters client_none,generate_dependencies,optimize_code_size
// @generated by protobuf-ts 2.0.0-alpha.12 with parameters client_grpc1,generate_dependencies,optimize_code_size
// @generated from protobuf file "service-example.proto" (package "spec", syntax proto3)
// tslint:disable
import { ServiceType } from "@protobuf-ts/runtime-rpc";
Expand Down
Loading

0 comments on commit 63c82d0

Please sign in to comment.