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

How to use google/protobuf/any.proto #39

Closed
SyedAsimAliSE opened this issue Nov 26, 2020 · 15 comments
Closed

How to use google/protobuf/any.proto #39

SyedAsimAliSE opened this issue Nov 26, 2020 · 15 comments
Assignees
Labels
bug Something isn't working

Comments

@SyedAsimAliSE
Copy link

// I am using Any type in one of my proto file 

import "google/protobuf/any.proto";

But when i am running the generator it is generating the import like :

import { Any } from "./google\\protobuf\\any";

image

@timostamm timostamm added the bug Something isn't working label Nov 26, 2020
@timostamm timostamm self-assigned this Nov 26, 2020
@timostamm
Copy link
Owner

That looks interesting :)

Do you happen to be on Windows?

@SyedAsimAliSE
Copy link
Author

yep i am using windows 10

@timostamm
Copy link
Owner

Ok. I'm pretty sure that this "importPath" has windows path separators (\) that have to be swapped.

const importPath = createRelativeImportPath(
this.source.getSourceFile().fileName,
symbolReg.file.getFilename()
);

But I have to setup a test env to confirm and fix. You'll have to wait a few days...

In the meantime, I can think of two workarounds:

  1. run protoc in the linux subsystem (WSL)
  2. replace paths in generated code with search and replace

@SyedAsimAliSE
Copy link
Author

it seems it need path normalization , i already tried to replace paths, but it says module not found .

import { Any } from "./google/protobuf/any";

image

Managed to compile it with :

import { Any } from "google-protobuf/google/protobuf/any_pb.js";

but don't know if it works or not because i am unable to go past this :

i am trying to migrate from grpc-js to protobuf-ts in my NestJS project, using nodejs 14.15.1 with tsconfigs set to ES2020.

after reading the documentation installed Twirp transport

and added the following call to the server.

                const transport = new TwirpFetchTransport({
			baseUrl: "0.0.0.0:50052"
		});

		const client = new ProcessTypeWritesServiceClient(transport);

		const payload:CreateProcessTypeInput_GRPC = {
			processTypeStatus:input.processTypeStatus
		}

		const {response} = await client.createProcessTypeGRPC(payload);
		console.log("Response! " + response)

but getting :

TypeError: globalThis.Headers is not a constructor at TwirpFetchTransport.unary 

thanks

@timostamm
Copy link
Owner

import { Any } from "./google/protobuf/any";

That should work. I guess the file is not there?

Use protoc argument '--ts_opt generate_dependencies':

  • "generate_dependencies"
    By default, only the PROTO_FILES passed as input to protoc are generated,
    not the files they import. Set this option to generate code for dependencies
    too.

Are you running the twirp transport in nodejs?

This transport requires the fetch API. globalThis.fetch, globalThis.Headers and globalThis.AbortController are expected. The transport is tested with the polyfill packages node-fetch and abort-controller.

@SyedAsimAliSE
Copy link
Author

You are right i missed the flag --ts_opt generate_dependencies i got the any file.

image

yes i am running it in nodejs, also installed node-fetch and abort-controller both of them but i have no idea what to do next with them :D because it still says TypeError: globalThis.Headers is not a constructor at TwirpFetchTransport.unary

@timostamm
Copy link
Owner

Have a look here:

import {default as fetch, Headers} from "node-fetch";
// fetch polyfill via https://github.com/node-fetch/node-fetch
globalThis.fetch = fetch;
globalThis.Headers = Headers;

And here:

import {AbortController} from "abort-controller";
import {Int32Value, StringValue} from "../ts-out/google/protobuf/wrappers";
globalThis.AbortController = AbortController; // AbortController polyfill via https://github.com/mysticatea/abort-controller

@SyedAsimAliSE
Copy link
Author

SyedAsimAliSE commented Nov 26, 2020

i tried everything but it seems to fail on me every time. if i don't set Headers then it is always giving :

TypeError: globalThis.Headers is not a constructor

if uncomment it ,getting the following type errors. did google for it allot but i guess i am the only one getting these :(

image

error TS2322: Type 'typeof Headers' is not assignable to type '{ new (init?: HeadersInit): Headers; prototype: Headers; }'.
  The types of 'prototype.forEach' are incompatible between these types.
    Type '(callback: (value: string, name: string) => void) => void' is not assignable to type '(callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: any) => void'.
      Types of parameters 'callback' and 'callbackfn' are incompatible.

37 globalThis.Headers = Headers;
   ~~~~~~~~~~~~~~~~~~

is there any other way i can send calls to the server ?

@timostamm
Copy link
Owner

The code snippets in the comments above are part of the CI workflow.
They run on every single commit to this repository, in node js.
They also run every single time I make a release.

Take a step back and get Headers working as expected, see examples here: https://developer.mozilla.org/en-US/docs/Web/API/Headers

The fetch API is nice and node-fetch is an excellent implementation.
Try making some simple requests, then add some custom headers.

Once you have that working, the TypeError is either gone - or you should be confident enough to just do globalThis.Headers = Headers as unknown as any; This will suppress the TypeError. But if you have some import error or broken node-fetch version, it will not help you at all. So get fetch and Headers running first.

@SyedAsimAliSE
Copy link
Author

found out that the problem is not with the node-fetch lib.

The globalThis.Headers = Headers; is taking me to the nestjs Header const declaration

export declare const Headers: (property?: string) => ParameterDecorator;

and the error is always taking me to this

globalThis.fetch(url, {
            --
            headers: createTwirpRequestHeader(new globalThis.Headers(), !!opt.sendJson, opt.meta),
            --
            --
        })

now need to find out how do i switch this global for the protobuf-ts.

@SyedAsimAliSE
Copy link
Author

Alright the problem with globalThis.Headers = Headers; is really not a problem but Intellij IDE isn't picking up the TS Typings, the project is working in VSCode perfectly fine.

@timostamm
Copy link
Owner

I'm using jetbrains IDEs as well. Some times the TypeScript service shows invalid errors. Helps to restart it in this case.

Bildschirmfoto 2020-11-27 um 21 50 40

But your issue might have been something else. Glad you solved it.

@timostamm
Copy link
Owner

But when i am running the generator it is generating the import like :

import { Any } from "./google\\protobuf\\any";

Fixed in v1.0.11

@SyedAsimAliSE
Copy link
Author

Thank you for taking your time :)

For anyone encountering the globalThis.Headers = Headers; problem in Intellij IDE, can solve it like this:

unknown

@hamming
Copy link

hamming commented Jan 25, 2021

// I am using Any type in one of my proto file 

import "google/protobuf/any.proto";

But when i am running the generator it is generating the import like :

import { Any } from "./google\\protobuf\\any";

image

  1. npm install node-fetch

  2. npm install abort-controller

  3. and --ts_opt generate_dependencies in your cmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants