-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 wrong types on drive.files.get method return value #1768
Comments
The problem is in the types here: Return should always include Stream, ArrayBuffer, Blob alternative. https://github.com/googleapis/google-api-nodejs-client/blob/master/src/apis/drive/v3.ts get(
params?: Params$Resource$Files$Get,
options?: MethodOptions
): GaxiosPromise<Schema$File>;
get(
params: Params$Resource$Files$Get,
options: MethodOptions | BodyResponseCallback<Schema$File>,
callback: BodyResponseCallback<Schema$File>
): void;
get(
params: Params$Resource$Files$Get,
callback: BodyResponseCallback<Schema$File>
): void;
get(callback: BodyResponseCallback<Schema$File>): void;
get(
paramsOrCallback?:
| Params$Resource$Files$Get
| BodyResponseCallback<Schema$File>,
optionsOrCallback?: MethodOptions | BodyResponseCallback<Schema$File>,
callback?: BodyResponseCallback<Schema$File>
): void | GaxiosPromise<Schema$File> How to work with the file contents, since the docs do not provide any further details:
const dest = fs.createWriteStream('p5.jpg');
await drive.files.get(
{
fileId: '<file id>',
alt: 'media',
},
{
responseType: 'arraybuffer', // MUST set responseType (eg. Buffer)
},
(err, res: any): void => { // set res: any to overwrite the type
if (res) {
console.log(res.data); // arraybuffer
dest.write(Buffer.from(res.data));
}
}) |
Greetings folks! This ain't perfect, but I honestly don't know if it's something we can directly fix. The good news is that - you don't need to cast to const drive = google.drive({ version: "v3"});
const dest = fs.createWriteStream('picture.jpg');
const res = await drive.files.get({
fileId: '<file id here>',
alt: 'media',
}, {
responseType: 'stream'
});
const streamy = res.data as Readable;
streamy.pipe(dest); Does that cast work? The reason this is hard - I can't quite seem to get the return type to differ based on the specific values within an object. I will keep poking at it, but this should be a pretty simple work around. |
me salvou |
Hey @JustinBeckwith. Just wanted to say great work with this lib! I had a quick question. What would I cast to if I were specifying a |
Environment details
googleapis
version: 41.0.1 (drive v3, JWT auth)Steps to reproduce
responseType: 'stream'
I am trying to download files (a JPG image in my case) from a GDrive with a service account. Authentication etc. works fine, but it seems there is an issue with the types when it comes to the drive.files.get method.
This request below is written in JavaScript and works fine, although the original google docs are not up to date this is the current workaround:
=> works fine, jpg is created and viewable
Using the same code working with Typescript:
Receiving the following error:
Property 'on' does not exist on type 'Schema$File'.
res.data
should be of type 'Stream' in this request configuration.Finally I started to try&error around this issue and wanted to first wait for the Promise to finish and then write it to file without
responseType: stream
At this point the only workaround for me is a crappy splitting of code into a JS module to fetch files and import the module in my TS code, because TS is all over the place with wrong types for the drive API.
And finally what does drive.files.get => res.data return? Is it a binary string? Is it something else? No explanation in the docs at all.
The text was updated successfully, but these errors were encountered: