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

fix(types): overload for bigtable.getInstances #579

Merged
merged 8 commits into from
Dec 4, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 72 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {replaceProjectIdToken} from '@google-cloud/projectify';
import {promisifyAll} from '@google-cloud/promisify';
import arrify = require('arrify');
import * as extend from 'extend';
import {GoogleAuth} from 'google-gax';
import {GoogleAuth, CallOptions} from 'google-gax';
import * as gax from 'google-gax';
import * as is from 'is';
import * as through from 'through2';
Expand All @@ -54,6 +54,8 @@ import {AppProfile} from './app-profile';
import {Cluster} from './cluster';
import {Instance} from './instance';
import {shouldRetryRequest} from './decorateStatus';
import {google} from '../proto/bigtable';
import {ServiceError} from '@grpc/grpc-js';

const retryRequest = require('retry-request');
const streamEvents = require('stream-events');
Expand All @@ -62,6 +64,19 @@ const PKG = require('../../package.json');
const v2 = require('./v2');
const {grpc} = new gax.GrpcClient();

export interface GetInstancesCallback {
(
err: ServiceError | null,
result?: Instance[] | null,
nextQuery?: {} | null,
response?: google.bigtable.admin.v2.IListInstancesResponse | null
): void;
}
export type GetInstancesResponse = [
Instance[] | null,
laljikanjareeya marked this conversation as resolved.
Show resolved Hide resolved
{} | null,
google.bigtable.admin.v2.IListInstancesResponse | null
laljikanjareeya marked this conversation as resolved.
Show resolved Hide resolved
];
/**
* @typedef {object} ClientConfig
* @property {string} [apiEndpoint] Override the default API endpoint used
Expand Down Expand Up @@ -588,16 +603,39 @@ export class Bigtable {
);
}

getInstances(callback: GetInstancesCallback): void;
getInstances(gaxOptions: CallOptions, callback: GetInstancesCallback): void;
getInstances(gaxOptions?: CallOptions): Promise<GetInstancesResponse>;
laljikanjareeya marked this conversation as resolved.
Show resolved Hide resolved
/**
* Query object for listing instances.
*
* @typedef {object} CallOptions
laljikanjareeya marked this conversation as resolved.
Show resolved Hide resolved
* @property {boolean} [autoPaginate=true] Have pagination handled
* automatically.
* @property {number} [maxApiCalls] Maximum number of API calls to make.
* @property {number} [maxResults] Maximum number of items to return.
* @property {number} [pageSize] Maximum number of results per page.
* @property {string} [pageToken] A previously-returned page token
* representing part of the larger set of results to view.
*/
/**
* @typedef {array} GetInstancesResponse
* @property {Instance[]} 0 Array of {@link Instance} instances.
* @property {object} 1 The full API response.
*/
/**
* @callback GetInstancesCallback
* @param {?Error} err Request error, if any.
* @param {Instance[]} instances Array of {@link Instance} instances.
* @param {object} apiResponse The full API response.
*/
/**
* Get Instance objects for all of your Cloud Bigtable instances.
*
* @param {object} [gaxOptions] Request configuration options, outlined here:
* https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback The callback function.
* @param {?error} callback.error An error returned while making this request.
* @param {Instance[]} callback.instances List of all
* instances.
* @param {object} callback.apiResponse The full API response.
* https://googleapis.github.io/gax-nodejs/classes/CallSettings.html.
* @param {GetInstancesCallback} [callback] The callback function.
* @returns {Promise<GetInstancesResponse>}
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
Expand All @@ -610,17 +648,37 @@ export class Bigtable {
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
laljikanjareeya marked this conversation as resolved.
Show resolved Hide resolved
* function callback(err, instances, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* bigtable.getInstances(nextQuery, callback);
* }
* }
*
* bigtable.getInstances({
* autoPaginate: false
* }, callback);
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* bigtable.getInstances().then(function(data) {
* const instances = data[0];
* });
*/
getInstances(gaxOptions?, callback?) {
if (is.function(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
getInstances(
gaxOptionsOrCallback?: CallOptions | GetInstancesCallback,
callback?: GetInstancesCallback
): void | Promise<GetInstancesResponse> {
const gaxOptions =
typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
callback =
typeof gaxOptionsOrCallback === 'function'
? gaxOptionsOrCallback
: callback;

const reqOpts = {
parent: this.projectName,
Expand All @@ -635,7 +693,7 @@ export class Bigtable {
},
(err, resp) => {
if (err) {
callback(err);
callback!(err);
return;
}

Expand All @@ -645,7 +703,7 @@ export class Bigtable {
return instance;
});

callback(null, instances, resp);
callback!(null, instances, resp);
}
);
}
Expand Down