From 03ee2db0861f9cf9d9b698f40ef82afebee46a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C6=B0=C6=A1ng=20T=E1=BA=A5n=20Hu=E1=BB=B3nh=20Phong?= Date: Wed, 19 Apr 2023 13:43:09 +0700 Subject: [PATCH] fix addUnaryHandler to support add individual handler --- packages/core/package.json | 2 +- packages/core/src/server.ts | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 155af06..5313092 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@grpc.ts/core", - "version": "1.1.1", + "version": "1.1.2", "license": "MIT", "directories": { "lib": "lib" diff --git a/packages/core/src/server.ts b/packages/core/src/server.ts index 33d964e..8b7585c 100644 --- a/packages/core/src/server.ts +++ b/packages/core/src/server.ts @@ -1,10 +1,5 @@ import { promisify } from 'util'; -import { - Server, - ServerCredentials, - type ServerUnaryCall, - type ServiceDefinition, -} from '@grpc/grpc-js'; +import { Server, ServerCredentials, type ServerUnaryCall } from '@grpc/grpc-js'; import type { UnaryCallback } from '@grpc/grpc-js/build/src/client'; @@ -60,10 +55,12 @@ export async function createServer({ serviceMethodTracking[key] = true; } - const rpcNameUpper = rpcName[0].toUpperCase() + rpcName.substring(1); + const subName = rpcName.substring(1); + const rpcNameUpper = rpcName[0].toUpperCase() + subName; + const rpcNameLower = rpcName[0].toLowerCase() + subName; if ( - !Object.prototype.hasOwnProperty.call(service, rpcName) && + !Object.prototype.hasOwnProperty.call(service, rpcNameLower) && !Object.prototype.hasOwnProperty.call(service, rpcNameUpper) ) { throw `${serviceName} service does not have ${rpcName} rpc method`; @@ -77,10 +74,15 @@ export async function createServer({ callback(null, result); }; - server.addService(service as unknown as ServiceDefinition, { - [rpcName]: implFunc, - [rpcNameUpper]: implFunc, - }); + const attrs = service[rpcNameLower] || service[rpcNameUpper]; + + server.register( + attrs.path, + implFunc, + attrs.responseSerialize, + attrs.requestDeserialize, + 'unary', + ); }; return {