Skip to content

Commit

Permalink
refactor typing to use with cli
Browse files Browse the repository at this point in the history
- export more info for core nestjs server and client
- fix incorrect value for grpcTimestampToDate
- fix type for fastify server to use with cli
- add cli to example to generate typing from protobuf files
  • Loading branch information
zgid123 committed Apr 18, 2023
1 parent b32950f commit 64f438f
Show file tree
Hide file tree
Showing 26 changed files with 428 additions and 154 deletions.
9 changes: 9 additions & 0 deletions examples/fastify/grpc-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { IConfigProps } from '@grpc.ts/cli';

const config: IConfigProps = {
paths: ['../proto/*.proto'],
external: ['google.protobuf'],
output: './src/protobufTypings',
};

export default config;
6 changes: 4 additions & 2 deletions examples/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
"type": "module",
"scripts": {
"dev": "vite-node -w src/main.ts",
"build": "vite build"
"build": "vite build",
"gen-type": "gen-grpc-typing"
},
"dependencies": {
"@grpc.ts/fastify-client": "workspace:*",
"@grpc.ts/fastify-server": "workspace:*",
"detect-port": "^1.5.1",
"fastify": "^4.15.0",
"vite": "^4.2.1"
"vite": "^4.2.2"
},
"devDependencies": {
"@fastify/vite": "^4.0.0",
"@grpc.ts/cli": "workspace:*",
"@types/detect-port": "^1.3.2",
"fastify-cli": "^5.7.1",
"vite-node": "^0.30.1"
Expand Down
37 changes: 25 additions & 12 deletions examples/fastify/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import FastifyGrpcServer, {

import { fastify } from 'config/fastify';

import type {
IExampleService,
ISendMessageRequest,
} from './protobufTypings/example3.interface';

async function bootstrap(): Promise<typeof fastify> {
fastify.register(FastifyGrpcServer, {
url: 'localhost:3010',
Expand Down Expand Up @@ -61,9 +66,12 @@ async function bootstrap(): Promise<typeof fastify> {

fastify.get('/', async (_request, reply) => {
const result = await fastify.grpcClient
.getService('ExampleService')
.getService<IExampleService>('ExampleService')
.sendMessage(
{ message: 'hello', createdAt: dateToGrpcTimestamp(new Date()) },
{
message: 'hello',
createdAt: dateToGrpcTimestamp(new Date()),
},
createMetadata({
meta: 'test',
}),
Expand All @@ -81,16 +89,21 @@ async function bootstrap(): Promise<typeof fastify> {

fastify.grpcServer
.getServer()
.addUnaryHandler('ExampleService', 'sendMessage', (request, metadata) => {
console.log(request);
console.log(metadata);
return {
message: {
message: 'hola',
createdAt: dateToGrpcTimestamp(new Date()),
},
};
});
.addUnaryHandler<ISendMessageRequest>(
'ExampleService',
'sendMessage',
(request, metadata) => {
console.log(request);
console.log(metadata);

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

return fastify;
}
Expand Down
32 changes: 32 additions & 0 deletions examples/fastify/src/protobufTypings/example.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type {
Metadata,
GrpcTimestamp,
ServiceClient,
} from '@grpc.ts/cli/node_modules/@grpc.ts/core';

export const PACKAGE_NAME = 'example.v1';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
}

export interface ISendMessageRequest {
message: string;
createdAt: GrpcTimestamp;
}

export interface IGetMessageResponse {
message: IMessage;
}

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
32 changes: 32 additions & 0 deletions examples/fastify/src/protobufTypings/example2.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type {
Metadata,
GrpcTimestamp,
ServiceClient,
} from '@grpc.ts/cli/node_modules/@grpc.ts/core';

export const PACKAGE_NAME = 'example2.v1';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
}

export interface ISendMessageRequest {
message: string;
createdAt: GrpcTimestamp;
}

export interface IGetMessageResponse {
message: IMessage;
}

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
30 changes: 30 additions & 0 deletions examples/fastify/src/protobufTypings/example3.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {
Metadata,
GrpcTimestamp,
ServiceClient,
} from '@grpc.ts/cli/node_modules/@grpc.ts/core';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
}

export interface ISendMessageRequest {
message: string;
createdAt: GrpcTimestamp;
}

export interface IGetMessageResponse {
message: IMessage;
}

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
8 changes: 8 additions & 0 deletions examples/nestjs/grpc-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { IConfigProps } from '@grpc.ts/cli';

const config: IConfigProps = {
paths: ['../proto/*.proto'],
external: ['google.protobuf'],
};

export default config;
4 changes: 3 additions & 1 deletion examples/nestjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"scripts": {
"build": "nest build",
"dev": "nest start --watch",
"start:prod": "node dist/main"
"start:prod": "node dist/main",
"gen-type": "gen-grpc-typing"
},
"dependencies": {
"@grpc.ts/nestjs-client": "workspace:*",
Expand All @@ -22,6 +23,7 @@
"rxjs": "^7.8.0"
},
"devDependencies": {
"@grpc.ts/cli": "workspace:*",
"@nestjs/cli": "^9.4.0",
"@nestjs/schematics": "^9.1.0",
"@types/detect-port": "^1.3.2",
Expand Down
32 changes: 32 additions & 0 deletions examples/nestjs/protobufTypings/example.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type {
Metadata,
GrpcTimestamp,
ServiceClient,
} from '@grpc.ts/cli/node_modules/@grpc.ts/core';

export const PACKAGE_NAME = 'example.v1';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
}

export interface ISendMessageRequest {
message: string;
createdAt: GrpcTimestamp;
}

export interface IGetMessageResponse {
message: IMessage;
}

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
32 changes: 32 additions & 0 deletions examples/nestjs/protobufTypings/example2.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type {
Metadata,
GrpcTimestamp,
ServiceClient,
} from '@grpc.ts/cli/node_modules/@grpc.ts/core';

export const PACKAGE_NAME = 'example2.v1';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
}

export interface ISendMessageRequest {
message: string;
createdAt: GrpcTimestamp;
}

export interface IGetMessageResponse {
message: IMessage;
}

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
30 changes: 30 additions & 0 deletions examples/nestjs/protobufTypings/example3.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {
Metadata,
GrpcTimestamp,
ServiceClient,
} from '@grpc.ts/cli/node_modules/@grpc.ts/core';

export interface IMessage {
message: string;
createdAt: GrpcTimestamp;
}

export interface ISendMessageRequest {
message: string;
createdAt: GrpcTimestamp;
}

export interface IGetMessageResponse {
message: IMessage;
}

export interface IExampleService extends ServiceClient {
SendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
sendMessage(
params: ISendMessageRequest,
metadata?: Metadata,
): Promise<IGetMessageResponse>;
}
9 changes: 7 additions & 2 deletions examples/nestjs/src/client/client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ import {
dateToGrpcTimestamp,
} from '@grpc.ts/nestjs-client';

import { IExampleService } from '../../protobufTypings/example3.interface';

@Controller('/client')
export class ClientController {
constructor(
@GrpcService({
serviceName: 'ExampleService',
})
private readonly exampleService: any,
private readonly exampleService: IExampleService,
) {}

@Get()
public async sendMessage(): Promise<string> {
this.exampleService
.sendMessage(
{ message: 'hello', createdAt: dateToGrpcTimestamp(new Date()) },
{
message: 'hello',
createdAt: dateToGrpcTimestamp(new Date()),
},
createMetadata({
meta: 'test',
}),
Expand Down
15 changes: 7 additions & 8 deletions examples/nestjs/src/server/server.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ import {
GrpcUnaryMethod,
dateToGrpcTimestamp,
type Metadata,
type Timestamp,
type ServerUnaryCall,
} from '@grpc.ts/nestjs-server';

import type {
IGetMessageResponse,
ISendMessageRequest,
} from '../../protobufTypings/example3.interface';

@Controller()
export class ServerController {
@GrpcUnaryMethod({
serviceName: 'ExampleService',
})
public async sendMessage(
request: unknown,
request: ISendMessageRequest,
metadata: Metadata,
call: ServerUnaryCall<unknown, unknown>,
): Promise<{
message: {
message: string;
createdAt: Timestamp.AsObject;
};
}> {
): Promise<IGetMessageResponse> {
console.log('request', request);
console.log('metadata', metadata);
console.log('call', call);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"husky": "^8.0.3",
"lint-staged": "^13.2.1",
"prettier": "^2.8.7",
"rollup": "^3.20.5",
"rollup": "^3.20.6",
"turbo": "^1.9.3",
"typescript": "^5.0.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grpc.ts/core",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"directories": {
"lib": "lib"
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './utils';
export type {
IClientProps,
IServerProps,
TUnaryHandlerFunc,
IServerWrapperProps,
TAddUnaryHandlerFunc,
IAddUnaryHandlerOptionsProps,
Expand Down
Loading

0 comments on commit 64f438f

Please sign in to comment.