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

[TypeScript] The return type of drive.files.get is inferred wrongly when downloading as media #2052

Closed
TonalidadeHidrica opened this issue Apr 18, 2020 · 1 comment · Fixed by #2188
Assignees
Labels
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

@TonalidadeHidrica
Copy link

The following is a small snippet to download a file from Google Drive using this API.

import {createDriveClient} from "./utils";
import fs from "fs";
import * as stream from "stream";
import {promisify} from "util";

(async () => {
    const drive = await createDriveClient();
    const {data} = (await drive.files.get({
        fileId: "anyFileId",
        alt: "media",
    }, {responseType: "stream"})) as unknown as { data: stream.PassThrough };
    const out = fs.createWriteStream("destinationFile");
    await promisify(stream.pipeline)(data, out);
})().catch(console.error);

The problem is that drive.files.get is defined to be returning (primarily) GaxiosPromise<Schema$File> according to the type definitions around here. However, when alt: "media" is specified, the method returns string (by default) or stream.PassThrough (when {responseType: "stream"}, or maybe other various types.

Therefore, when I try to use the API with TypeScript properly, I have to apply an ugly cast ( as unknown as { data: stream.PassThrough } to the response of the function. Can it be improved?

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Apr 19, 2020
@bcoe bcoe added api: clouddebugger Issues related to the Cloud Debugger API. 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. and removed api: clouddebugger Issues related to the Cloud Debugger API. labels Apr 20, 2020
@yoshi-automation yoshi-automation removed the triage me I really want to be triaged. label Apr 20, 2020
@JustinBeckwith JustinBeckwith self-assigned this May 24, 2020
@JustinBeckwith
Copy link
Contributor

This is a real bug. Step 1 is to land this PR and cut a release:
googleapis/nodejs-googleapis-common#283

Next, we need to add method signature overloads to the generator that look like this:

    get(
      params?: Params$Resource$Sites$Get,
      options?: MethodOptions
    ): GaxiosPromise<Schema$SiteSummaryResponse>;
    get(
      params?: Params$Resource$Sites$Get,
      options?: StreamMethodOptions
    ): GaxiosPromise<Readable>;
    get(
      params: Params$Resource$Sites$Get,
      options: MethodOptions | BodyResponseCallback<Schema$SiteSummaryResponse>,
      callback: BodyResponseCallback<Schema$SiteSummaryResponse>
    ): void;
    get(
      params: Params$Resource$Sites$Get,
      options: StreamMethodOptions | BodyResponseCallback<Readable>,
      callback: BodyResponseCallback<Readable>
    ): void;
    get(
      params: Params$Resource$Sites$Get,
      callback: BodyResponseCallback<Schema$SiteSummaryResponse>
    ): void;
    get(callback: BodyResponseCallback<Schema$SiteSummaryResponse>): void;
    get(
      paramsOrCallback?:
        | Params$Resource$Sites$Get
        | BodyResponseCallback<Schema$SiteSummaryResponse>
        | BodyResponseCallback<Readable>,
      optionsOrCallback?:
        | MethodOptions
        | StreamMethodOptions
        | BodyResponseCallback<Schema$SiteSummaryResponse>
        | BodyResponseCallback<Readable>,
      callback?: BodyResponseCallback<Schema$SiteSummaryResponse> | BodyResponseCallback<Readable>
    ): void | GaxiosPromise<Schema$SiteSummaryResponse> | GaxiosPromise<Readable> {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

Successfully merging a pull request may close this issue.

4 participants