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

refactor(typescript): noImplicityAny for app-profile.ts #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
137 changes: 93 additions & 44 deletions src/app-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import {promisifyAll} from '@google-cloud/promisify';
import * as is from 'is';
import snakeCase = require('lodash.snakecase');
import {Cluster} from './cluster';
import {Bigtable} from '.';
import {Bigtable, AppProfileOptions, CreateAppProfileCallback, CreateAppProfileResponse, DeleteAppProfileOptions, DeleteAppProfileCallback, EmptyResponse, ExistsCallback, ExistsResponse, GetAppProfileCallback, GetAppProfileResponse, GetAppProfileMetadataCallback, GetAppProfileMetadataResponse, Arguments, SetAppProfileMetadataCallback, SetAppProfileMetadataResponse} from '.';
import {Instance} from './instance';

import {Metadata} from '@google-cloud/common';
import {google} from '../proto/bigtable';
import {CallOptions} from 'grpc';
/**
* Create an app profile object to interact with your app profile.
*
Expand All @@ -39,7 +41,7 @@ export class AppProfile {
instance: Instance;
name: string;
id: string;
metadata;
metadata: Metadata;
constructor(instance: Instance, id: string) {
this.bigtable = instance.bigtable;
this.instance = instance;
Expand All @@ -58,7 +60,7 @@ Please use the format 'my-app-profile' or '${
name = `${instance.name}/appProfiles/${id}`;
}

this.id = name.split('/').pop();
this.id = name.split('/').pop()!;
this.name = name;
}

Expand Down Expand Up @@ -96,8 +98,9 @@ Please use the format 'my-app-profile' or '${
* // description: 'My App Profile',
* // }
*/
static formatAppProfile_(options) {
const appProfile: any = {};
static formatAppProfile_(options: AppProfileOptions):
google.bigtable.admin.v2.IAppProfile {
const appProfile: google.bigtable.admin.v2.IAppProfile = {};

if (options.routing) {
if (options.routing === 'any') {
Expand Down Expand Up @@ -132,11 +135,17 @@ Please use the format 'my-app-profile' or '${
* <caption>include:samples/document-snippets/app-profile.js</caption>
* region_tag:bigtable_create_app_profile
*/
create(options, callback) {
if (is.fn(options)) {
callback = options;
options = {};
}
create(options?: AppProfileOptions): Promise<CreateAppProfileResponse>;
create(callback: CreateAppProfileCallback): void;
create(options: AppProfileOptions, callback: CreateAppProfileCallback): void;
create(
optionsOrCallback?: AppProfileOptions|CreateAppProfileCallback,
callback?: CreateAppProfileCallback):
void|Promise<CreateAppProfileResponse> {
const options =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
callback =
typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
this.instance.createAppProfile(this.id, options, callback);
}

Expand All @@ -157,12 +166,18 @@ Please use the format 'my-app-profile' or '${
* <caption>include:samples/document-snippets/app-profile.js</caption>
* region_tag:bigtable_delete_app_profile
*/
delete(options, callback) {
if (is.fn(options)) {
callback = options;
options = {};
}

delete(options?: DeleteAppProfileOptions): Promise<EmptyResponse>;
delete(callback: DeleteAppProfileCallback): void;
delete(options: DeleteAppProfileOptions, callback: DeleteAppProfileCallback):
void;
delete(
optionsOrCallback?: DeleteAppProfileOptions|DeleteAppProfileCallback,
callback?: DeleteAppProfileCallback): void|Promise<EmptyResponse> {
const options =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
callback =
typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
// tslint:disable-next-line no-any
const reqOpts: any = {
name: this.name,
};
Expand Down Expand Up @@ -195,24 +210,31 @@ Please use the format 'my-app-profile' or '${
* <caption>include:samples/document-snippets/app-profile.js</caption>
* region_tag:bigtable_exists_app_profile
*/
exists(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
exists(gaxOptions?: CallOptions): Promise<ExistsResponse>;
exists(callback: ExistsCallback): void;
exists(gaxOptions: CallOptions, callback: ExistsCallback): void;
exists(
gaxOptionsOrcallback?: CallOptions|ExistsCallback,
callback?: ExistsCallback): void|Promise<ExistsResponse> {
const gaxOptions =
(typeof gaxOptionsOrcallback === 'object' ? gaxOptionsOrcallback :
{}) as CallOptions;
callback = typeof gaxOptionsOrcallback === 'function' ?
gaxOptionsOrcallback :
callback;

this.getMetadata(gaxOptions, err => {
if (err) {
if (err.code === 5) {
callback(null, false);
callback!(null, false);
return;
}

callback(err);
callback!(err);
return;
}

callback(null, true);
callback!(null, true);
});
}

Expand All @@ -226,14 +248,20 @@ Please use the format 'my-app-profile' or '${
* <caption>include:samples/document-snippets/app-profile.js</caption>
* region_tag:bigtable_get_app_profile
*/
get(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
get(gaxOptions?: CallOptions): Promise<GetAppProfileResponse>;
get(callback: GetAppProfileCallback): void;
get(gaxOptions: CallOptions, callback: GetAppProfileCallback): void;
get(gaxOptionsOrcallback?: CallOptions|GetAppProfileCallback,
callback?: GetAppProfileCallback): void|Promise<GetAppProfileResponse> {
const gaxOptions =
(typeof gaxOptionsOrcallback === 'object' ? gaxOptionsOrcallback :
{}) as CallOptions;
callback = typeof gaxOptionsOrcallback === 'function' ?
gaxOptionsOrcallback :
callback;

this.getMetadata(gaxOptions, (err, metadata) => {
callback(err, err ? null : this, metadata);
callback!(err, err ? null : this, metadata);
});
}

Expand All @@ -252,11 +280,19 @@ Please use the format 'my-app-profile' or '${
* <caption>include:samples/document-snippets/app-profile.js</caption>
* region_tag:bigtable_app_profile_get_meta
*/
getMetadata(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
getMetadata(gaxOptions?: CallOptions): Promise<GetAppProfileMetadataResponse>;
getMetadata(callback: GetAppProfileMetadataCallback): void;
getMetadata(gaxOptions: CallOptions, callback: GetAppProfileMetadataCallback):
void;
getMetadata(
gaxOptionsOrcallback?: CallOptions|GetAppProfileMetadataCallback,
callback?: GetAppProfileMetadataCallback):
void|Promise<GetAppProfileMetadataResponse> {
const gaxOptions =
typeof gaxOptionsOrcallback === 'object' ? gaxOptionsOrcallback : {};
callback = typeof gaxOptionsOrcallback === 'function' ?
gaxOptionsOrcallback :
callback;

this.bigtable.request(
{
Expand All @@ -267,12 +303,12 @@ Please use the format 'my-app-profile' or '${
},
gaxOpts: gaxOptions,
},
(...args) => {
(...args: Arguments<AppProfile>) => {
if (args[1]) {
this.metadata = args[1];
}

callback(...args);
callback!(...args);
});
}

Expand All @@ -291,12 +327,25 @@ Please use the format 'my-app-profile' or '${
* <caption>include:samples/document-snippets/app-profile.js</caption>
* region_tag:bigtable_app_profile_set_meta
*/
setMetadata(metadata, gaxOptions, callback) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}

setMetadata(metadata: AppProfileOptions, gaxOptions?: CallOptions):
Promise<SetAppProfileMetadataResponse>;
setMetadata(
metadata: AppProfileOptions,
callback: SetAppProfileMetadataCallback): void;
setMetadata(
metadata: AppProfileOptions, gaxOptions: CallOptions,
callback: SetAppProfileMetadataCallback): void;
setMetadata(
metadata: AppProfileOptions,
gaxOptionsOrcallback?: CallOptions|SetAppProfileMetadataCallback,
callback?: SetAppProfileMetadataCallback):
void|Promise<SetAppProfileMetadataResponse> {
const gaxOptions =
typeof gaxOptionsOrcallback === 'object' ? gaxOptionsOrcallback : {};
callback = typeof gaxOptionsOrcallback === 'function' ?
gaxOptionsOrcallback :
callback;
// tslint:disable-next-line no-any
const reqOpts: any = {
appProfile: AppProfile.formatAppProfile_(metadata),
updateMask: {
Expand Down
2 changes: 1 addition & 1 deletion src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

import {promisifyAll} from '@google-cloud/promisify';
import {ServiceError} from '@grpc/grpc-js';
import {CallOptions, Operation as GaxOperation} from 'google-gax';
import {ServiceError} from 'grpc';

import {google as btTypes} from '../proto/bigtable';

Expand Down
53 changes: 53 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,58 @@ import * as through from 'through2';
import {AppProfile} from './app-profile';
import {Cluster} from './cluster';
import {Instance} from './instance';
import {ServiceError} from 'grpc';
import {google as btTypes} from '../proto/bigtable';

type RequestCallback<T, R = void> =
R extends void ? NormalCallback<T>: FullCallback<T, R>;
type NormalCallback<T> = (err: ServiceError|null, response?: T|null) => void;
type FullCallback<T, R> =
(err: ServiceError|null, response?: T|null, apiResponse?: R|null) => void;

type ApiResponse<T, R = void> =
R extends void ? NormalResponse<T>: FullResponse<T, R>;
type NormalResponse<T> = [T];
type FullResponse<T, R> = [T, R];
interface OptionInterface {
gaxOptions?: gax.CallOptions;
}
export type EmptyResponse = ApiResponse<btTypes.protobuf.IEmpty>;
export type ExistsCallback = RequestCallback<boolean>;
export type ExistsResponse = ApiResponse<boolean>;
export type Arguments<T> = [(ServiceError | null),(T| null)?,any?];

export type CreateAppProfileCallback =
RequestCallback<AppProfile, btTypes.bigtable.admin.v2.IAppProfile>;
export type CreateAppProfileResponse =
ApiResponse<AppProfile, btTypes.bigtable.admin.v2.IAppProfile>;
export type DeleteAppProfileCallback =
btTypes.bigtable.admin.v2.BigtableInstanceAdmin.DeleteAppProfileCallback;
export type GetAppProfileCallback =
RequestCallback<AppProfile, btTypes.bigtable.admin.v2.IAppProfile>;
export type GetAppProfileResponse =
ApiResponse<AppProfile, btTypes.bigtable.admin.v2.IAppProfile>;
export type GetAppProfileMetadataCallback =
RequestCallback<AppProfile, btTypes.bigtable.admin.v2.IAppProfile>;
export type GetAppProfileMetadataResponse =
ApiResponse<AppProfile, btTypes.bigtable.admin.v2.IAppProfile>;
export type SetAppProfileMetadataCallback =
RequestCallback<btTypes.bigtable.admin.v2.IAppProfile>;
export type SetAppProfileMetadataResponse =
ApiResponse<btTypes.bigtable.admin.v2.IAppProfile>;

export interface AppProfileOptions extends OptionInterface {
routing: Cluster|'any';
allowTransactionalWrites?: boolean;
description?: string;
ignoreWarnings?: string;
}

export interface DeleteAppProfileOptions extends OptionInterface {
ignoreWarnings?: string;
}



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


/**
* @typedef {object} ClientConfig
* @property {string} [apiEndpoint] Override the default API endpoint used
Expand Down
2 changes: 1 addition & 1 deletion system-test/bigtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as assert from 'assert';
import Q = require('p-queue');
const Q = require('p-queue');
import * as uuid from 'uuid';

import {Bigtable} from '../src';
Expand Down