agreatfool / grpc_tools_node_protoc_ts

Generate TypeScript d.ts definitions for generated js files from grpc_tools_node_protoc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Examples for attributes in object style server implementation

paymog opened this issue · comments

The write up on https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/v5.1.1/doc/server_impl_signature.md is super, super useful with the error about the UntypedHandleCall error. However, I'm finding it hard to figure out how to add extra attributes to the object style implementation. Here's an example of what I've tried. Unfortunately, it seems that added attributes to the object-style implementation ends up with the same errors as the class-based implementation:

const ArticleServer: IArticleServerServer = {
    key: "",
    secret: "",

    async getArticle(_: ServerUnaryCall<Empty, Empty>, callback: sendUnaryData<Empty>): Promise<void> {
        const files = await fleekStorage.listFiles({
            apiKey: this.key,
            apiSecret: this.secret,
            getOptions: [
                'bucket',
                'key',
                'hash',
                'publicUrl'
            ],
        })
        console.log(files)
        callback(null, new Empty())
    }
}

However, when compile this with tsc I see several errors:

src/index.ts:11:5 - error TS2322: Type 'string' is not assignable to type 'UntypedHandleCall'.

11     key: "",
       ~~~

  node_modules/@grpc/grpc-js/build/src/server.d.ts:7:5
    7     [name: string]: UntypedHandleCall;
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from this index signature.

src/index.ts:12:5 - error TS2322: Type 'string' is not assignable to type 'UntypedHandleCall'.

12     secret: "",
       ~~~~~~

  node_modules/@grpc/grpc-js/build/src/server.d.ts:7:5
    7     [name: string]: UntypedHandleCall;
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from this index signature.

src/index.ts:16:13 - error TS2322: Type 'UntypedHandleCall | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

16             apiKey: this.key,
               ~~~~~~

  node_modules/@fleekhq/fleek-storage-js/index.d.ts:69:3
    69   apiKey: string,
         ~~~~~~
    The expected type comes from property 'apiKey' which is declared here on type 'listFilesInput'

src/index.ts:16:26 - error TS4111: Property 'key' comes from an index signature, so it must be accessed with ['key'].

16             apiKey: this.key,
                            ~~~

src/index.ts:17:13 - error TS2322: Type 'UntypedHandleCall | undefined' is not assignable to type 'string'.

17             apiSecret: this.secret,
               ~~~~~~~~~

  node_modules/@fleekhq/fleek-storage-js/index.d.ts:70:3
    70   apiSecret: string,
         ~~~~~~~~~
    The expected type comes from property 'apiSecret' which is declared here on type 'listFilesInput'

Is there another way to add attributes to the object style declaration as shown in https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/v5.1.1/doc/server_impl_signature.md#object-style? If so, how?

I'm using the following package versions:

  "dependencies": {
    "@fleekhq/fleek-storage-js": "^1.0.17",
    "@grpc/grpc-js": "^1.3.6",
    "@types/google-protobuf": "^3.15.3",
    "@types/yargs": "^17.0.2",
    "@typescript-eslint/eslint-plugin": "^4.28.5",
    "@typescript-eslint/parser": "^4.28.5",
    "eslint": "^7.31.0",
    "grpc-tools": "^1.11.2",
    "grpc_tools_node_protoc_ts": "^5.3.0",
    "typescript": "^4.3.5",
    "yargs": "^17.0.1"
  },

Interesting.

When I was writing the doc you mentioned, any attributes of any type you want could be added into the object style, as I remember It worked.

Today I just tested it again with the codes in the examples dir, same error as you encountered. Maybe I made some mistake when I was doing the test, object style can't work same as class style.

Anyway, as we can see the behavior of object style and class style are consistent now, you can't put anything (any additional attributes) doesn't existing in your ServerImpl definition into the class or object.

Then I suggest you could use the class style, and put codes below in your class.

[name: string]: grpc.UntypedHandleCall;

Hmm, weird. I wonder what changed between your original test and now.

Regardless, I found a way to add attributes to class based declarations. I'll put up a PR with that today or tomorrow.

@paymog how did you resolve this. I have a class with attributes that implements gRPC service. I am running into this issue.

thanks!