Skip to content

Commit

Permalink
fix addUnaryHandler to support add individual handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zgid123 committed Apr 19, 2023
1 parent cbc7468 commit 03ee2db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
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.1",
"version": "1.1.2",
"license": "MIT",
"directories": {
"lib": "lib"
Expand Down
26 changes: 14 additions & 12 deletions packages/core/src/server.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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`;
Expand All @@ -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 {
Expand Down

0 comments on commit 03ee2db

Please sign in to comment.