Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support grpc es unimplemented service #25

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/grpc-es/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kanziw/grpc-es",
"version": "0.2.0",
"version": "0.2.1",
"description": "A collection of grpc libraries using connect-es",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions packages/grpc-es/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { stdoutUnaryServerInterceptor } from './interceptor.js'
export { GrpcEsServer } from './server.js'
export type { Handler, Interceptor } from './types.js'
export { Code, GrpcError } from './status.js'
19 changes: 17 additions & 2 deletions packages/grpc-es/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AnyMessage, ServiceType } from '@bufbuild/protobuf'
import type { HandlerContext, ServiceImpl } from '@connectrpc/connect'
import { connectNodeAdapter } from '@connectrpc/connect-node'
import { UniversalHandlerOptions } from '@connectrpc/connect/protocol'
import { Code, GrpcError } from './status.js'
import type { Handler, Interceptor } from './types.js'

export class GrpcEsServer {
Expand All @@ -16,8 +17,9 @@ export class GrpcEsServer {
return this
}

register<Service extends ServiceType>(service: Service, implementation: ServiceImpl<Service>): this {
const interceptorAppliedImplementation = {} as ServiceImpl<typeof service>
register<Service extends ServiceType>(service: Service, partialImplementation: Partial<ServiceImpl<Service>>): this {
const implementation = { ...makeUnimplementedService(service), ...partialImplementation } as ServiceImpl<Service>
const interceptorAppliedImplementation = {} as ServiceImpl<Service>

for (const [key, handler] of Object.entries(implementation)) {
let appliedHandler = handler
Expand Down Expand Up @@ -47,3 +49,16 @@ export class GrpcEsServer {
.listen(port)
}
}

function makeUnimplementedService<Service extends ServiceType>(service: Service): ServiceImpl<Service> {
return Object.keys(service.methods).reduce(
(us, method) => {
// @ts-ignore
us[method] = () => {
throw new GrpcError(`Unimplemented method: [${method}]`, Code.Unimplemented)
}
return us
},
{} as ServiceImpl<Service>,
)
}
3 changes: 3 additions & 0 deletions packages/grpc-es/src/server/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Code, ConnectError as GrpcError } from '@connectrpc/connect'

export { Code, GrpcError }
Loading