Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

HttpRequest's body only accepts Uint8Array in typescript. #334

Closed
cielo opened this issue Jan 9, 2020 · 4 comments
Closed

HttpRequest's body only accepts Uint8Array in typescript. #334

cielo opened this issue Jan 9, 2020 · 4 comments
Assignees
Labels
api: cloudtasks Issues related to the googleapis/nodejs-tasks API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@cielo
Copy link

cielo commented Jan 9, 2020

Hi,

I am using v2 namespace, but this problem seems to exist in v2beta3 namespace as well. Also, this issue might be related to #331.

I cannot seem to use HttpRequest because its body property is expecting Uint8Array. The part of proto.d.ts file looks like below, and you can see that body is only expecting Uint8Array. Is this an expected behavior? Because of below type definition, my ts file is throwing the following error.
Type 'string' is not assignable to type 'Uint8Array | null | undefined'

I think it would be much better if it accepts some form of string or variations of string... not Uint8array.

            /** Properties of a HttpRequest. */
            interface IHttpRequest {

                /** HttpRequest url */
                url?: (string|null);

                /** HttpRequest httpMethod */
                httpMethod?: (google.cloud.tasks.v2.HttpMethod|null);

                /** HttpRequest headers */
                headers?: ({ [k: string]: string }|null);

                /** HttpRequest body */
                body?: (Uint8Array|null);

                /** HttpRequest oauthToken */
                oauthToken?: (google.cloud.tasks.v2.IOAuthToken|null);

                /** HttpRequest oidcToken */
                oidcToken?: (google.cloud.tasks.v2.IOidcToken|null);
            }

What workaround can I use in this case?

Environment details

  • @google-cloud/tasks version: 1.7.1
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Jan 10, 2020
@bcoe bcoe added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Jan 14, 2020
@bcoe
Copy link
Contributor

bcoe commented Jan 14, 2020

@alexander-fenster @xiaozhenliu-gg5 this seems a bit odd, should that be a Buffer, and/or stream?

@yoshi-automation yoshi-automation removed the triage me I really want to be triaged. label Jan 14, 2020
@alexander-fenster
Copy link
Contributor

Hi @cielo, thank you for this report. This is indeed similar to #331, and the main problem here is caused by the inconsistent types generated by pbts tool (they are stricter than the type protobuf.js fromObject actually accept). The right solution to this would involve patching pbts (which is one of the options on the table). We are actively searching for a good enough solution for that and I expect to have it sorted out soon. Until that time, please try using my fromObject suggestion from #331:

import * as protos from '@google-cloud/tasks/build/protos/protos';

const httpRequest = protos.google.cloud.tasks.v2.HttpRequest.fromObject({
  . . .
});

The fromObject accepts objects in a less strict manner so you should be able to pass a string there.

Obviously, it only affects TypeScript users :)

@cielo
Copy link
Author

cielo commented Jan 14, 2020

Thanks. I was able to use fromObject to bypass this issue per your suggestion.

However, because that helper method accepts any object and type checking is no longer done, I misplaced my 'scheduleTime' parameter in the wrong place... took a while (carefully reading my parameters as if I have traveled back to old JavaScript days) to figure out why Google Tasks isn't accepting my future schedule requests, and firing tasks as soon as possible.

Anyhow, everything is resolved now on my end. Google Tasks seems to be a powerful service, and I already fell in love with it. Keep up the good work!

@alexander-fenster
Copy link
Contributor

This should be fixed in v1.7.3 (we updated the types to include string where UInt8Array was allowed).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: cloudtasks Issues related to the googleapis/nodejs-tasks API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

4 participants