Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 3e795af

Browse files
ThomasdenHJustinBeckwith
authored andcommitted
fix: Add typing for File#download() (#409)
1 parent b207a36 commit 3e795af

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"Stephen Sawchuk <sawchuk@gmail.com>",
5151
"Stephen Sawchuk <stephenplusplus@users.noreply.github.com>",
5252
"Tyler Johnson <mail@tyler-johnson.ca>",
53+
"Thomas den Hollander <denhollander.thomas@gmail.com>",
5354
"Zach Bjornson <bjornson@stanford.edu>",
5455
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>"
5556
],

src/file.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ export interface CopyOptions {
122122
userProject?: string;
123123
}
124124

125+
/**
126+
* @typedef {array} DownloadResponse
127+
* @property [0] The contents of a File.
128+
*/
129+
export type DownloadResponse = [Buffer];
130+
131+
/**
132+
* @callback DownloadCallback
133+
* @param err Request error, if any.
134+
* @param contents The contents of a File.
135+
*/
136+
export type DownloadCallback = (err: Error|undefined, contents: Buffer) => void;
137+
138+
export interface DownloadOptions extends CreateReadStreamOptions {
139+
destination?: string;
140+
}
141+
125142
interface CopyQuery {
126143
sourceGeneration?: number;
127144
rewriteToken?: string;
@@ -1306,15 +1323,6 @@ class File extends ServiceObject {
13061323
(this.parent as ServiceObject).delete.call(this, options, callback);
13071324
}
13081325

1309-
/**
1310-
* @typedef {array} DownloadResponse
1311-
* @property {object} [0] The contents of a File.
1312-
*/
1313-
/**
1314-
* @callback DownloadCallback
1315-
* @param {?Error} err Request error, if any.
1316-
* @param {buffer} [contents] The contents of a File.
1317-
*/
13181326
/**
13191327
* Convenience method to download a file into memory or to a local
13201328
* destination.
@@ -1368,13 +1376,21 @@ class File extends ServiceObject {
13681376
* region_tag:storage_download_file_requester_pays
13691377
* Example of downloading a file where the requester pays:
13701378
*/
1371-
download(options?, callback?) {
1372-
if (is.fn(options)) {
1373-
callback = options;
1379+
download(options?: DownloadOptions): Promise<DownloadResponse>;
1380+
download(options: DownloadOptions, callback: DownloadCallback): void;
1381+
download(callback: DownloadCallback): void;
1382+
download(
1383+
optionsOrCallback?: DownloadOptions|DownloadCallback,
1384+
callback?: DownloadCallback): Promise<DownloadResponse>|void {
1385+
let options: DownloadOptions;
1386+
if (is.fn(optionsOrCallback)) {
1387+
callback = optionsOrCallback as DownloadCallback;
13741388
options = {};
1389+
} else {
1390+
options = optionsOrCallback as DownloadOptions;
13751391
}
13761392

1377-
callback = once(callback);
1393+
callback = once(callback as DownloadCallback);
13781394

13791395
const destination = options.destination;
13801396
delete options.destination;

0 commit comments

Comments
 (0)