improbable-eng / ts-protoc-gen

Protocol Buffers Compiler (protoc) plugin for TypeScript and gRPC-Web.
Apache License 2.0
1.36k stars 172 forks source link

Add Server interface #247

Closed badsyntax closed 3 years ago

badsyntax commented 4 years ago

Unless i'm completely missing something, it's not possible to use the types generated by this package for a server implementation. This change fixes that.

I've had to make various upstream changes to allow the types to work across the grpc and @grpc/grpc-js packages. I've been following & contributing to the proto-loader typescript generator changes in https://github.com/grpc/grpc-node/pull/1474, and changes in this PR align with those changes.

If using @grpc/grpc-js, a minimum version of 1.2.0 is required. I'm not sure how best to handle this, whether we would mention this in the readme, or add it as a peerDependency?

Changes

Example usage

import * as grpc from '@grpc/grpc-js';
import { ChatService, IChatServer } from './proto/chat_grpc_pb';
import { ClientMessage, ServerMessage } from './proto/chat_pb';

const chatServer: IChatServer = {
  join(call: grpc.ServerWritableStream<ClientMessage, ServerMessage>): void {
    // implementation
  },
  send(
    call: grpc.ServerUnaryCall<ClientMessage, ServerMessage>,
    callback: grpc.sendUnaryData<ServerMessage>
  ): void {
    // implementation
  },
};

const server = new grpc.Server();
server.addService(ChatService, chatServer);

Verification

I've used the generated files in an example app and they seem ok. I briefly explored writing tests but all the existing tests are testing the javascript implementation and not the generated types, so not sure how best to create tests for typescript interfaces.

I believe we can ignore the the failing build (linting) as this new code is consistent with existing code.

Related

https://github.com/improbable-eng/ts-protoc-gen/pull/221

IchordeDionysos commented 4 years ago

@badsyntax what's the status of this PR? 🤔

badsyntax commented 4 years ago

@IchordeDionysos unfortunately i'm still waiting on these changes to be released:

https://github.com/grpc/grpc-node/pull/1587 https://github.com/grpc/grpc-node/pull/1590

i need those changes to build a generic service interface that will work with both grpc and @grpc/grpc-js packages

badsyntax commented 3 years ago

https://github.com/grpc/grpc-node/pull/1587 is published so this PR is good for a review!

Any thoughts about this @MarcusLongmuir?

badsyntax commented 3 years ago

@MarcusLongmuir Any thoughts about this one? It's not a huge change but would be helpful for people i reckon.

improbable-prow-robot commented 3 years ago

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: MarcusLongmuir To complete the pull request process, please assign You can assign the PR to them by writing /assign in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files: - **[OWNERS](https://github.com/improbable-eng/ts-protoc-gen/blob/master/OWNERS)** Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
badsyntax commented 3 years ago

Thanks for the merge @MarcusLongmuir. Is there an ETA for a release?

MarcusLongmuir commented 3 years ago

Apologies for the delay. We've just been overhauling a lot of permissions and repo configurations that made this rather laborious.

This has gone out in 0.14.0: https://www.npmjs.com/package/ts-protoc-gen/v/0.14.0

badsyntax commented 3 years ago

Awesome thank you!