Skip to content

Commit

Permalink
Improve TypeScript types (part 4) (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Sep 19, 2018
1 parent 8b036c0 commit fac2897
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 158 deletions.
4 changes: 3 additions & 1 deletion src/acl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as arrify from 'arrify';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import * as is from 'is';
import {DecorateRequestOptions, BodyResponseCallback} from '@google-cloud/common';

interface AclQuery {
generation: number;
Expand Down Expand Up @@ -757,7 +758,8 @@ class Acl extends AclRoleAccessorMethods {
* @param {*} body Request body contents.
* @param {function} callback Callback function.
*/
request(reqOpts, callback) {
request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback):
void {
reqOpts.uri = this.pathPrefix + reqOpts.uri;
this.request_(reqOpts, callback);
}
Expand Down
76 changes: 44 additions & 32 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

import * as arrify from 'arrify';
import * as async from 'async';
import {ExistsCallback, ServiceObject, Metadata, util, DeleteCallback, InstanceResponseCallback, GetConfig, GetMetadataCallback} from '@google-cloud/common';
import {ExistsCallback, ServiceObject, Metadata, util, DeleteCallback, InstanceResponseCallback, GetConfig, GetMetadataCallback, DecorateRequestOptions, BodyResponseCallback} from '@google-cloud/common';
import {paginator} from '@google-cloud/paginator';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import * as fs from 'fs';
import * as is from 'is';
import * as mime from 'mime-types';
import * as path from 'path';
import * as snakeize from 'snakeize';
const snakeize = require('snakeize');
import * as request from 'request';

import {Acl} from './acl';
Expand Down Expand Up @@ -54,6 +54,15 @@ interface BucketOptions {
userProject?: string;
}

/**
* @callback GetFilesCallback
* @param {?Error} err Request error, if any.
* @param {File[]} files Array of {@link File} instances.
*/
export interface GetFilesCallback {
(err: Error|null, files?: File[]): void;
}

/**
* See a [Objects:
* watchAll request
Expand Down Expand Up @@ -128,7 +137,7 @@ export interface CombineOptions {
* @param {object} apiResponse The full API response.
*/
export interface CombineCallback {
(err: Error|null, newFile: File|null, apiResponse: request.Response);
(err: Error|null, newFile: File|null, apiResponse: request.Response): void;
}

/**
Expand Down Expand Up @@ -174,7 +183,7 @@ export type CreateChannelResponse = [Channel, request.Response];
* @param {object} apiResponse The full API response.
*/
export interface CreateChannelCallback {
(err: Error|null, channel: Channel|null, apiResponse: request.Response);
(err: Error|null, channel: Channel|null, apiResponse: request.Response): void;
}

/**
Expand Down Expand Up @@ -214,7 +223,7 @@ export interface CreateNotificationOptions {
*/
export interface CreateNotificationCallback {
(err: Error|null, notification: Notification|null,
apiResponse: request.Response);
apiResponse: request.Response): void;
}

/**
Expand Down Expand Up @@ -245,7 +254,7 @@ export type DeleteBucketResponse = [request.Response];
* @param {object} apiResponse The full API response.
*/
export interface DeleteBucketCallback extends DeleteCallback {
(err: Error|null, apiResponse: request.Response);
(err: Error|null, apiResponse: request.Response): void;
}

/**
Expand All @@ -265,7 +274,7 @@ export interface DeleteFilesOptions extends GetFilesOptions {
* @param {object} [apiResponse] The full API response.
*/
export interface DeleteFilesCallback {
(err: Error|Error[]|null, apiResponse?: object);
(err: Error|Error[]|null, apiResponse?: object): void;
}

/**
Expand All @@ -280,7 +289,7 @@ export type DeleteLabelsResponse = [request.Response];
* @param {object} apiResponse The full API response.
*/
export interface DeleteLabelsCallback {
(err: Error|null, apiResponse?: object);
(err: Error|null, apiResponse?: object): void;
}

/**
Expand All @@ -295,7 +304,7 @@ export type DisableRequesterPaysResponse = [request.Response];
* @param {object} apiResponse The full API response.
*/
export interface DisableRequesterPaysCallback {
(err: Error|null, apiResponse?: object);
(err: Error|null, apiResponse?: object): void;
}

/**
Expand All @@ -310,7 +319,7 @@ export type EnableRequesterPaysResponse = [request.Response];
* @param {object} apiResponse The full API response.
*/
export interface EnableRequesterPaysCallback {
(err: Error|null, apiResponse: request.Response);
(err: Error|null, apiResponse: request.Response): void;
}

/**
Expand Down Expand Up @@ -360,7 +369,7 @@ export type GetBucketResponse = [Bucket, request.Response];
* @param {object} apiResponse The full API response.
*/
export interface GetBucketCallback extends InstanceResponseCallback {
(err: Error|null, bucket: Bucket|null, apiResponse: request.Response);
(err: Error|null, bucket: Bucket|null, apiResponse: request.Response): void;
}

/**
Expand All @@ -384,7 +393,7 @@ export type GetLabelsResponse = [request.Response];
* @param {object} labels Object of labels currently set on this bucket.
*/
export interface GetLabelsCallback {
(err: Error|null, labels: object|null);
(err: Error|null, labels: object|null): void;
}

/**
Expand All @@ -401,7 +410,8 @@ export type GetBucketMetadataResponse = [object, request.Response];
* @param {object} apiResponse The full API response.
*/
export interface GetBucketMetadataCallback extends GetMetadataCallback {
(err: Error|null, metadata: Metadata|null, apiResponse: request.Response);
(err: Error|null, metadata: Metadata|null,
apiResponse: request.Response): void;
}

/**
Expand Down Expand Up @@ -431,7 +441,7 @@ export interface GetNotificationsOptions {
*/
export interface GetNotificationsCallback {
(err: Error|null, notifications: Notification[]|null,
apiResponse: request.Response);
apiResponse: request.Response): void;
}

/**
Expand Down Expand Up @@ -1201,7 +1211,7 @@ class Bucket extends ServiceObject {

// Iterate through each file and attempt to delete it.
async.eachLimit<File, Error>(
files, MAX_PARALLEL_LIMIT, deleteFile, err => {
files!, MAX_PARALLEL_LIMIT, deleteFile, err => {
if (err || errors.length > 0) {
callback!(err || errors);
return;
Expand Down Expand Up @@ -1551,11 +1561,6 @@ class Bucket extends ServiceObject {
* @typedef {array} GetFilesResponse
* @property {File[]} 0 Array of {@link File} instances.
*/
/**
* @callback GetFilesCallback
* @param {?Error} err Request error, if any.
* @param {File[]} files Array of {@link File} instances.
*/
/**
* Get {@link File} objects for the files currently in the bucket.
*
Expand Down Expand Up @@ -1624,12 +1629,16 @@ class Bucket extends ServiceObject {
* region_tag:storage_list_files_with_prefix
* Example of listing files, filtered by a prefix:
*/
getFiles(query: GetFilesOptions, callback?) {
getFiles(query?: GetFilesOptions): Promise<[File[]]>;
getFiles(query: GetFilesOptions, callback: GetFilesCallback): void;
getFiles(callback: GetFilesCallback): void;
getFiles(
queryOrCallback?: GetFilesOptions|GetFilesCallback,
callback?: GetFilesCallback): void|Promise<[File[]]> {
let query = typeof queryOrCallback === 'object' ? queryOrCallback : {};
if (!callback) {
callback = query;
query = {};
callback = queryOrCallback as GetFilesCallback;
}

query = extend({}, query);

if (query.directory) {
Expand All @@ -1644,7 +1653,8 @@ class Bucket extends ServiceObject {
},
(err, resp) => {
if (err) {
callback(err, null, null, resp);
// tslint:disable-next-line:no-any
(callback as any)(err, null, null, resp);
return;
}

Expand All @@ -1671,8 +1681,8 @@ class Bucket extends ServiceObject {
pageToken: resp.nextPageToken,
});
}

callback(null, files, nextQuery, resp);
// tslint:disable-next-line:no-any
(callback as any)(null, files, nextQuery, resp);
});
}

Expand Down Expand Up @@ -2145,13 +2155,15 @@ class Bucket extends ServiceObject {
* @param {object} reqOpts - The request options.
* @param {function} callback - The callback function.
*/
request(reqOpts): Promise<request.Response>;
request(reqOpts, callback): void;
request(reqOpts, callback?): void|Promise<request.Response> {
request(reqOpts: DecorateRequestOptions): Promise<request.Response>;
request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback):
void;
request(reqOpts: DecorateRequestOptions, callback?: BodyResponseCallback):
void|Promise<request.Response> {
if (this.userProject && (!reqOpts.qs || !reqOpts.qs.userProject)) {
reqOpts.qs = extend(reqOpts.qs, {userProject: this.userProject});
}
return super.request(reqOpts, callback);
return super.request(reqOpts, callback!);
}

/**
Expand Down Expand Up @@ -2691,7 +2703,7 @@ class Bucket extends ServiceObject {
};

// Iterate through each file and make it public or private.
async.eachLimit(files, MAX_PARALLEL_LIMIT, processFile, err => {
async.eachLimit(files!, MAX_PARALLEL_LIMIT, processFile, err => {
if (err || errors.length > 0) {
callback(err || errors, updatedFiles);
return;
Expand Down
22 changes: 14 additions & 8 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ import {ServiceObject, util} from '@google-cloud/common';
import {promisifyAll} from '@google-cloud/promisify';
import * as request from 'request';
import {Storage} from '.';
import {Response} from 'request';

/**
* @callback StopCallback
* @param {?Error} err Request error, if any.
* @param {object} apiResponse The full API response.
*/
export interface StopCallback {
(err: Error|null, apiResponse?: request.Response): void;
}

/**
* Create a channel object to interact with a Cloud Storage channel.
Expand Down Expand Up @@ -66,11 +76,6 @@ class Channel extends ServiceObject {
* @typedef {array} StopResponse
* @property {object} 0 The full API response.
*/
/**
* @callback StopCallback
* @param {?Error} err Request error, if any.
* @param {object} apiResponse The full API response.
*/
/**
* Stop this channel.
*
Expand All @@ -94,17 +99,18 @@ class Channel extends ServiceObject {
* const apiResponse = data[0];
* });
*/
stop(callback?) {
stop(): Promise<Response>;
stop(callback: StopCallback): void;
stop(callback?: StopCallback): Promise<Response>|void {
callback = callback || util.noop;

this.request(
{
method: 'POST',
uri: '/stop',
json: this.metadata,
},
(err, apiResponse) => {
callback(err, apiResponse);
callback!(err, apiResponse);
});
}
}
Expand Down
Loading

0 comments on commit fac2897

Please sign in to comment.