From 7135f81c65d20aa0ff7f182144345a1733a0c27f Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 1 Apr 2020 14:31:47 -0700 Subject: [PATCH] feat!: drop node8 support (#562) * feat!: drop node8 support BREAKING CHANGE: The library now supports Node.js v10+. The last version to support Node.js v8 is tagged legacy-8 on NPM. * fix: lint --- packages/google-cloud-node/.eslintrc.json | 3 + packages/google-cloud-node/.eslintrc.yml | 15 - packages/google-cloud-node/.prettierrc | 8 - packages/google-cloud-node/.prettierrc.js | 17 + packages/google-cloud-node/package.json | 12 +- packages/google-cloud-node/src/helpers.ts | 2 +- .../google-cloud-node/src/v1/speech_client.ts | 150 +++--- .../src/v1p1beta1/speech_client.ts | 151 +++--- packages/google-cloud-node/synth.metadata | 20 +- packages/google-cloud-node/synth.py | 3 +- .../system-test/fixtures/sample/src/index.js | 1 - .../system-test/fixtures/sample/src/index.ts | 2 +- .../google-cloud-node/test/gapic-speech-v1.ts | 317 ------------ .../test/gapic-speech-v1p1beta1.ts | 317 ------------ .../google-cloud-node/test/gapic_speech_v1.ts | 489 ++++++++++++++++++ .../test/gapic_speech_v1p1beta1.ts | 489 ++++++++++++++++++ .../google-cloud-node/test/helpers.test.ts | 19 +- packages/google-cloud-node/webpack.config.js | 12 +- 18 files changed, 1191 insertions(+), 836 deletions(-) create mode 100644 packages/google-cloud-node/.eslintrc.json delete mode 100644 packages/google-cloud-node/.eslintrc.yml delete mode 100644 packages/google-cloud-node/.prettierrc create mode 100644 packages/google-cloud-node/.prettierrc.js delete mode 100644 packages/google-cloud-node/test/gapic-speech-v1.ts delete mode 100644 packages/google-cloud-node/test/gapic-speech-v1p1beta1.ts create mode 100644 packages/google-cloud-node/test/gapic_speech_v1.ts create mode 100644 packages/google-cloud-node/test/gapic_speech_v1p1beta1.ts diff --git a/packages/google-cloud-node/.eslintrc.json b/packages/google-cloud-node/.eslintrc.json new file mode 100644 index 000000000000..782153495464 --- /dev/null +++ b/packages/google-cloud-node/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "./node_modules/gts" +} diff --git a/packages/google-cloud-node/.eslintrc.yml b/packages/google-cloud-node/.eslintrc.yml deleted file mode 100644 index 73eeec27612d..000000000000 --- a/packages/google-cloud-node/.eslintrc.yml +++ /dev/null @@ -1,15 +0,0 @@ ---- -extends: - - 'eslint:recommended' - - 'plugin:node/recommended' - - prettier -plugins: - - node - - prettier -rules: - prettier/prettier: error - block-scoped-var: error - eqeqeq: error - no-warning-comments: warn - no-var: error - prefer-const: error diff --git a/packages/google-cloud-node/.prettierrc b/packages/google-cloud-node/.prettierrc deleted file mode 100644 index df6eac074460..000000000000 --- a/packages/google-cloud-node/.prettierrc +++ /dev/null @@ -1,8 +0,0 @@ ---- -bracketSpacing: false -printWidth: 80 -semi: true -singleQuote: true -tabWidth: 2 -trailingComma: es5 -useTabs: false diff --git a/packages/google-cloud-node/.prettierrc.js b/packages/google-cloud-node/.prettierrc.js new file mode 100644 index 000000000000..08cba3775be1 --- /dev/null +++ b/packages/google-cloud-node/.prettierrc.js @@ -0,0 +1,17 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +module.exports = { + ...require('gts/.prettierrc.json') +} diff --git a/packages/google-cloud-node/package.json b/packages/google-cloud-node/package.json index 429b9aa79bc5..d1d42d54a223 100644 --- a/packages/google-cloud-node/package.json +++ b/packages/google-cloud-node/package.json @@ -5,7 +5,7 @@ "license": "Apache-2.0", "author": "Google Inc.", "engines": { - "node": ">=8.10.0" + "node": ">=10" }, "repository": "googleapis/nodejs-speech", "main": "./build/src/index.js", @@ -44,7 +44,7 @@ }, "dependencies": { "@google-cloud/common": "^2.0.0", - "google-gax": "^1.11.1", + "google-gax": "^2.0.1", "protobufjs": "^6.8.6", "pumpify": "^2.0.0", "stream-events": "^1.0.4", @@ -53,14 +53,14 @@ "devDependencies": { "@types/mocha": "^7.0.0", "@types/node": "^12.0.0", - "@types/sinon": "^7.5.1", + "@types/sinon": "^7.5.2", "c8": "^7.0.0", "codecov": "^3.0.2", "eslint": "^6.0.0", "eslint-config-prettier": "^6.0.0", "eslint-plugin-node": "^11.0.0", "eslint-plugin-prettier": "^3.0.0", - "gts": "^1.1.2", + "gts": "2.0.0-alpha.9", "jsdoc": "^3.5.5", "jsdoc-fresh": "^1.0.1", "jsdoc-region-tag": "^1.0.2", @@ -69,9 +69,9 @@ "null-loader": "^3.0.0", "pack-n-play": "^1.0.0-2", "prettier": "^1.13.5", - "sinon": "^9.0.0", + "sinon": "^9.0.1", "ts-loader": "^6.2.1", - "typescript": "3.6.4", + "typescript": "^3.8.3", "webpack": "^4.41.2", "webpack-cli": "^3.3.10" } diff --git a/packages/google-cloud-node/src/helpers.ts b/packages/google-cloud-node/src/helpers.ts index b5e410c45a87..f22d5f3bca5c 100644 --- a/packages/google-cloud-node/src/helpers.ts +++ b/packages/google-cloud-node/src/helpers.ts @@ -68,7 +68,7 @@ export class ImprovedStreamingClient { // Format the audio content as input request for pipeline const recognizeStream = streamEvents(new pumpify.obj()); - // tslint:disable-next-line no-any + // eslint-disable-next-line @typescript-eslint/no-explicit-any const requestStream = (this as any) ._streamingRecognize(options) .on('error', (err: Error) => { diff --git a/packages/google-cloud-node/src/v1/speech_client.ts b/packages/google-cloud-node/src/v1/speech_client.ts index 613a1b926494..1726250d9eb1 100644 --- a/packages/google-cloud-node/src/v1/speech_client.ts +++ b/packages/google-cloud-node/src/v1/speech_client.ts @@ -18,7 +18,6 @@ import * as gax from 'google-gax'; import { - APICallback, Callback, CallOptions, Descriptors, @@ -27,7 +26,7 @@ import { } from 'google-gax'; import * as path from 'path'; -import * as protosTypes from '../../protos/protos'; +import * as protos from '../../protos/protos'; import * as gapicConfig from './speech_client_config.json'; const version = require('../../../package.json').version; @@ -38,13 +37,6 @@ const version = require('../../../package.json').version; * @memberof v1 */ export class SpeechClient { - private _descriptors: Descriptors = { - page: {}, - stream: {}, - longrunning: {}, - batching: {}, - }; - private _innerApiCalls: {[name: string]: Function}; private _terminated = false; private _opts: ClientOptions; private _gaxModule: typeof gax | typeof gax.fallback; @@ -52,6 +44,13 @@ export class SpeechClient { private _protos: {}; private _defaults: {[method: string]: gax.CallSettings}; auth: gax.GoogleAuth; + descriptors: Descriptors = { + page: {}, + stream: {}, + longrunning: {}, + batching: {}, + }; + innerApiCalls: {[name: string]: Function}; operationsClient: gax.OperationsClient; speechStub?: Promise<{[name: string]: Function}>; @@ -144,12 +143,15 @@ export class SpeechClient { 'protos.json' ); this._protos = this._gaxGrpc.loadProto( - opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath + opts.fallback + ? // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../../protos/protos.json') + : nodejsProtoPath ); // Some of the methods on this service provide streaming responses. // Provide descriptors for these. - this._descriptors.stream = { + this.descriptors.stream = { streamingRecognize: new this._gaxModule.StreamDescriptor( gax.StreamType.BIDI_STREAMING ), @@ -160,6 +162,7 @@ export class SpeechClient { // rather than holding a request open. const protoFilesRoot = opts.fallback ? this._gaxModule.protobuf.Root.fromJSON( + // eslint-disable-next-line @typescript-eslint/no-var-requires require('../../protos/protos.json') ) : this._gaxModule.protobuf.loadSync(nodejsProtoPath); @@ -177,7 +180,7 @@ export class SpeechClient { '.google.cloud.speech.v1.LongRunningRecognizeMetadata' ) as gax.protobuf.Type; - this._descriptors.longrunning = { + this.descriptors.longrunning = { longRunningRecognize: new this._gaxModule.LongrunningDescriptor( this.operationsClient, longRunningRecognizeResponse.decode.bind(longRunningRecognizeResponse), @@ -196,7 +199,7 @@ export class SpeechClient { // Set up a dictionary of "inner API calls"; the core implementation // of calling the API is handled in `google-gax`, with this code // merely providing the destination and request information. - this._innerApiCalls = {}; + this.innerApiCalls = {}; } /** @@ -223,7 +226,7 @@ export class SpeechClient { ? (this._protos as protobuf.Root).lookupService( 'google.cloud.speech.v1.Speech' ) - : // tslint:disable-next-line no-any + : // eslint-disable-next-line @typescript-eslint/no-explicit-any (this._protos as any).google.cloud.speech.v1.Speech, this._opts ) as Promise<{[method: string]: Function}>; @@ -235,9 +238,8 @@ export class SpeechClient { 'longRunningRecognize', 'streamingRecognize', ]; - for (const methodName of speechStubMethods) { - const innerCallPromise = this.speechStub.then( + const callPromise = this.speechStub.then( stub => (...args: Array<{}>) => { if (this._terminated) { return Promise.reject('The client has already been closed.'); @@ -251,20 +253,14 @@ export class SpeechClient { ); const apiCall = this._gaxModule.createApiCall( - innerCallPromise, + callPromise, this._defaults[methodName], - this._descriptors.page[methodName] || - this._descriptors.stream[methodName] || - this._descriptors.longrunning[methodName] + this.descriptors.page[methodName] || + this.descriptors.stream[methodName] || + this.descriptors.longrunning[methodName] ); - this._innerApiCalls[methodName] = ( - argument: {}, - callOptions?: CallOptions, - callback?: APICallback - ) => { - return apiCall(argument, callOptions, callback); - }; + this.innerApiCalls[methodName] = apiCall; } return this.speechStub; @@ -321,22 +317,30 @@ export class SpeechClient { // -- Service calls -- // ------------------- recognize( - request: protosTypes.google.cloud.speech.v1.IRecognizeRequest, + request: protos.google.cloud.speech.v1.IRecognizeRequest, options?: gax.CallOptions ): Promise< [ - protosTypes.google.cloud.speech.v1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1.IRecognizeRequest | undefined, + protos.google.cloud.speech.v1.IRecognizeResponse, + protos.google.cloud.speech.v1.IRecognizeRequest | undefined, {} | undefined ] >; recognize( - request: protosTypes.google.cloud.speech.v1.IRecognizeRequest, + request: protos.google.cloud.speech.v1.IRecognizeRequest, options: gax.CallOptions, callback: Callback< - protosTypes.google.cloud.speech.v1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1.IRecognizeRequest | undefined, - {} | undefined + protos.google.cloud.speech.v1.IRecognizeResponse, + protos.google.cloud.speech.v1.IRecognizeRequest | null | undefined, + {} | null | undefined + > + ): void; + recognize( + request: protos.google.cloud.speech.v1.IRecognizeRequest, + callback: Callback< + protos.google.cloud.speech.v1.IRecognizeResponse, + protos.google.cloud.speech.v1.IRecognizeRequest | null | undefined, + {} | null | undefined > ): void; /** @@ -357,23 +361,23 @@ export class SpeechClient { * The promise has a method named "cancel" which cancels the ongoing API call. */ recognize( - request: protosTypes.google.cloud.speech.v1.IRecognizeRequest, + request: protos.google.cloud.speech.v1.IRecognizeRequest, optionsOrCallback?: | gax.CallOptions | Callback< - protosTypes.google.cloud.speech.v1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1.IRecognizeRequest | undefined, - {} | undefined + protos.google.cloud.speech.v1.IRecognizeResponse, + protos.google.cloud.speech.v1.IRecognizeRequest | null | undefined, + {} | null | undefined >, callback?: Callback< - protosTypes.google.cloud.speech.v1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1.IRecognizeRequest | undefined, - {} | undefined + protos.google.cloud.speech.v1.IRecognizeResponse, + protos.google.cloud.speech.v1.IRecognizeRequest | null | undefined, + {} | null | undefined > ): Promise< [ - protosTypes.google.cloud.speech.v1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1.IRecognizeRequest | undefined, + protos.google.cloud.speech.v1.IRecognizeResponse, + protos.google.cloud.speech.v1.IRecognizeRequest | undefined, {} | undefined ] > | void { @@ -387,7 +391,7 @@ export class SpeechClient { } options = options || {}; this.initialize(); - return this._innerApiCalls.recognize(request, options, callback); + return this.innerApiCalls.recognize(request, options, callback); } /** @@ -403,32 +407,43 @@ export class SpeechClient { */ _streamingRecognize(options?: gax.CallOptions): gax.CancellableStream { this.initialize(); - return this._innerApiCalls.streamingRecognize(options); + return this.innerApiCalls.streamingRecognize(options); } longRunningRecognize( - request: protosTypes.google.cloud.speech.v1.ILongRunningRecognizeRequest, + request: protos.google.cloud.speech.v1.ILongRunningRecognizeRequest, options?: gax.CallOptions ): Promise< [ LROperation< - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, + protos.google.longrunning.IOperation | undefined, {} | undefined ] >; longRunningRecognize( - request: protosTypes.google.cloud.speech.v1.ILongRunningRecognizeRequest, + request: protos.google.cloud.speech.v1.ILongRunningRecognizeRequest, options: gax.CallOptions, callback: Callback< LROperation< - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, - {} | undefined + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined + > + ): void; + longRunningRecognize( + request: protos.google.cloud.speech.v1.ILongRunningRecognizeRequest, + callback: Callback< + LROperation< + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata + >, + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined > ): void; /** @@ -453,32 +468,32 @@ export class SpeechClient { * The promise has a method named "cancel" which cancels the ongoing API call. */ longRunningRecognize( - request: protosTypes.google.cloud.speech.v1.ILongRunningRecognizeRequest, + request: protos.google.cloud.speech.v1.ILongRunningRecognizeRequest, optionsOrCallback?: | gax.CallOptions | Callback< LROperation< - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, - {} | undefined + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined >, callback?: Callback< LROperation< - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, - {} | undefined + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined > ): Promise< [ LROperation< - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, + protos.google.longrunning.IOperation | undefined, {} | undefined ] > | void { @@ -492,7 +507,7 @@ export class SpeechClient { } options = options || {}; this.initialize(); - return this._innerApiCalls.longRunningRecognize(request, options, callback); + return this.innerApiCalls.longRunningRecognize(request, options, callback); } /** @@ -513,4 +528,5 @@ export class SpeechClient { } import {ImprovedStreamingClient} from '../helpers'; +// eslint-disable-next-line @typescript-eslint/no-empty-interface export interface SpeechClient extends ImprovedStreamingClient {} diff --git a/packages/google-cloud-node/src/v1p1beta1/speech_client.ts b/packages/google-cloud-node/src/v1p1beta1/speech_client.ts index f4634c4e5cc1..25a80f2dfe21 100644 --- a/packages/google-cloud-node/src/v1p1beta1/speech_client.ts +++ b/packages/google-cloud-node/src/v1p1beta1/speech_client.ts @@ -18,7 +18,6 @@ import * as gax from 'google-gax'; import { - APICallback, Callback, CallOptions, Descriptors, @@ -27,7 +26,7 @@ import { } from 'google-gax'; import * as path from 'path'; -import * as protosTypes from '../../protos/protos'; +import * as protos from '../../protos/protos'; import * as gapicConfig from './speech_client_config.json'; const version = require('../../../package.json').version; @@ -38,13 +37,6 @@ const version = require('../../../package.json').version; * @memberof v1p1beta1 */ export class SpeechClient { - private _descriptors: Descriptors = { - page: {}, - stream: {}, - longrunning: {}, - batching: {}, - }; - private _innerApiCalls: {[name: string]: Function}; private _terminated = false; private _opts: ClientOptions; private _gaxModule: typeof gax | typeof gax.fallback; @@ -52,6 +44,13 @@ export class SpeechClient { private _protos: {}; private _defaults: {[method: string]: gax.CallSettings}; auth: gax.GoogleAuth; + descriptors: Descriptors = { + page: {}, + stream: {}, + longrunning: {}, + batching: {}, + }; + innerApiCalls: {[name: string]: Function}; operationsClient: gax.OperationsClient; speechStub?: Promise<{[name: string]: Function}>; @@ -144,12 +143,15 @@ export class SpeechClient { 'protos.json' ); this._protos = this._gaxGrpc.loadProto( - opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath + opts.fallback + ? // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../../protos/protos.json') + : nodejsProtoPath ); // Some of the methods on this service provide streaming responses. // Provide descriptors for these. - this._descriptors.stream = { + this.descriptors.stream = { streamingRecognize: new this._gaxModule.StreamDescriptor( gax.StreamType.BIDI_STREAMING ), @@ -160,6 +162,7 @@ export class SpeechClient { // rather than holding a request open. const protoFilesRoot = opts.fallback ? this._gaxModule.protobuf.Root.fromJSON( + // eslint-disable-next-line @typescript-eslint/no-var-requires require('../../protos/protos.json') ) : this._gaxModule.protobuf.loadSync(nodejsProtoPath); @@ -177,7 +180,7 @@ export class SpeechClient { '.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata' ) as gax.protobuf.Type; - this._descriptors.longrunning = { + this.descriptors.longrunning = { longRunningRecognize: new this._gaxModule.LongrunningDescriptor( this.operationsClient, longRunningRecognizeResponse.decode.bind(longRunningRecognizeResponse), @@ -196,7 +199,7 @@ export class SpeechClient { // Set up a dictionary of "inner API calls"; the core implementation // of calling the API is handled in `google-gax`, with this code // merely providing the destination and request information. - this._innerApiCalls = {}; + this.innerApiCalls = {}; } /** @@ -223,7 +226,7 @@ export class SpeechClient { ? (this._protos as protobuf.Root).lookupService( 'google.cloud.speech.v1p1beta1.Speech' ) - : // tslint:disable-next-line no-any + : // eslint-disable-next-line @typescript-eslint/no-explicit-any (this._protos as any).google.cloud.speech.v1p1beta1.Speech, this._opts ) as Promise<{[method: string]: Function}>; @@ -235,9 +238,8 @@ export class SpeechClient { 'longRunningRecognize', 'streamingRecognize', ]; - for (const methodName of speechStubMethods) { - const innerCallPromise = this.speechStub.then( + const callPromise = this.speechStub.then( stub => (...args: Array<{}>) => { if (this._terminated) { return Promise.reject('The client has already been closed.'); @@ -251,20 +253,14 @@ export class SpeechClient { ); const apiCall = this._gaxModule.createApiCall( - innerCallPromise, + callPromise, this._defaults[methodName], - this._descriptors.page[methodName] || - this._descriptors.stream[methodName] || - this._descriptors.longrunning[methodName] + this.descriptors.page[methodName] || + this.descriptors.stream[methodName] || + this.descriptors.longrunning[methodName] ); - this._innerApiCalls[methodName] = ( - argument: {}, - callOptions?: CallOptions, - callback?: APICallback - ) => { - return apiCall(argument, callOptions, callback); - }; + this.innerApiCalls[methodName] = apiCall; } return this.speechStub; @@ -321,22 +317,30 @@ export class SpeechClient { // -- Service calls -- // ------------------- recognize( - request: protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest, + request: protos.google.cloud.speech.v1p1beta1.IRecognizeRequest, options?: gax.CallOptions ): Promise< [ - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest | undefined, + protos.google.cloud.speech.v1p1beta1.IRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.IRecognizeRequest | undefined, {} | undefined ] >; recognize( - request: protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest, + request: protos.google.cloud.speech.v1p1beta1.IRecognizeRequest, options: gax.CallOptions, callback: Callback< - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest | undefined, - {} | undefined + protos.google.cloud.speech.v1p1beta1.IRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.IRecognizeRequest | null | undefined, + {} | null | undefined + > + ): void; + recognize( + request: protos.google.cloud.speech.v1p1beta1.IRecognizeRequest, + callback: Callback< + protos.google.cloud.speech.v1p1beta1.IRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.IRecognizeRequest | null | undefined, + {} | null | undefined > ): void; /** @@ -357,24 +361,25 @@ export class SpeechClient { * The promise has a method named "cancel" which cancels the ongoing API call. */ recognize( - request: protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest, + request: protos.google.cloud.speech.v1p1beta1.IRecognizeRequest, optionsOrCallback?: | gax.CallOptions | Callback< - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeResponse, - | protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest + protos.google.cloud.speech.v1p1beta1.IRecognizeResponse, + | protos.google.cloud.speech.v1p1beta1.IRecognizeRequest + | null | undefined, - {} | undefined + {} | null | undefined >, callback?: Callback< - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest | undefined, - {} | undefined + protos.google.cloud.speech.v1p1beta1.IRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.IRecognizeRequest | null | undefined, + {} | null | undefined > ): Promise< [ - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest | undefined, + protos.google.cloud.speech.v1p1beta1.IRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.IRecognizeRequest | undefined, {} | undefined ] > | void { @@ -388,7 +393,7 @@ export class SpeechClient { } options = options || {}; this.initialize(); - return this._innerApiCalls.recognize(request, options, callback); + return this.innerApiCalls.recognize(request, options, callback); } /** @@ -404,32 +409,43 @@ export class SpeechClient { */ _streamingRecognize(options?: gax.CallOptions): gax.CancellableStream { this.initialize(); - return this._innerApiCalls.streamingRecognize(options); + return this.innerApiCalls.streamingRecognize(options); } longRunningRecognize( - request: protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest, + request: protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest, options?: gax.CallOptions ): Promise< [ LROperation< - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, + protos.google.longrunning.IOperation | undefined, {} | undefined ] >; longRunningRecognize( - request: protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest, + request: protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest, options: gax.CallOptions, callback: Callback< LROperation< - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, - {} | undefined + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined + > + ): void; + longRunningRecognize( + request: protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest, + callback: Callback< + LROperation< + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + >, + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined > ): void; /** @@ -454,32 +470,32 @@ export class SpeechClient { * The promise has a method named "cancel" which cancels the ongoing API call. */ longRunningRecognize( - request: protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest, + request: protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest, optionsOrCallback?: | gax.CallOptions | Callback< LROperation< - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, - {} | undefined + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined >, callback?: Callback< LROperation< - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, - {} | undefined + protos.google.longrunning.IOperation | null | undefined, + {} | null | undefined > ): Promise< [ LROperation< - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, - protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata >, - protosTypes.google.longrunning.IOperation | undefined, + protos.google.longrunning.IOperation | undefined, {} | undefined ] > | void { @@ -493,7 +509,7 @@ export class SpeechClient { } options = options || {}; this.initialize(); - return this._innerApiCalls.longRunningRecognize(request, options, callback); + return this.innerApiCalls.longRunningRecognize(request, options, callback); } /** @@ -514,4 +530,5 @@ export class SpeechClient { } import {ImprovedStreamingClient} from '../helpers'; +// eslint-disable-next-line @typescript-eslint/no-empty-interface export interface SpeechClient extends ImprovedStreamingClient {} diff --git a/packages/google-cloud-node/synth.metadata b/packages/google-cloud-node/synth.metadata index fada31646b58..9f33065a4db3 100644 --- a/packages/google-cloud-node/synth.metadata +++ b/packages/google-cloud-node/synth.metadata @@ -1,23 +1,5 @@ { - "updateTime": "2020-03-22T11:49:11.344546Z", - "sources": [ - { - "git": { - "name": "googleapis", - "remote": "https://github.com/googleapis/googleapis.git", - "sha": "0be7105dc52590fa9a24e784052298ae37ce53aa", - "internalRef": "302154871", - "log": "0be7105dc52590fa9a24e784052298ae37ce53aa\nAdd BUILD.bazel file to asset/v1p1beta1\n\nPiperOrigin-RevId: 302154871\n\n6c248fd13e8543f8d22cbf118d978301a9fbe2a8\nAdd missing resource annotations and additional_bindings to dialogflow v2 API.\n\nPiperOrigin-RevId: 302063117\n\n9a3a7f33be9eeacf7b3e98435816b7022d206bd7\nChange the service name from \"chromeos-moblab.googleapis.com\" to \"chromeosmoblab.googleapis.com\"\n\nPiperOrigin-RevId: 302060989\n\n98a339237577e3de26cb4921f75fb5c57cc7a19f\nfeat: devtools/build/v1 publish client library config annotations\n\n* add details field to some of the BuildEvents\n* add final_invocation_id and build_tool_exit_code fields to BuildStatus\n\nPiperOrigin-RevId: 302044087\n\ncfabc98c6bbbb22d1aeaf7612179c0be193b3a13\nfeat: home/graph/v1 publish client library config annotations & comment updates\n\nThis change includes adding the client library configuration annotations, updated proto comments, and some client library configuration files.\n\nPiperOrigin-RevId: 302042647\n\nc8c8c0bd15d082db9546253dbaad1087c7a9782c\nchore: use latest gapic-generator in bazel WORKSPACE.\nincluding the following commits from gapic-generator:\n- feat: take source protos in all sub-packages (#3144)\n\nPiperOrigin-RevId: 301843591\n\ne4daf5202ea31cb2cb6916fdbfa9d6bd771aeb4c\nAdd bazel file for v1 client lib generation\n\nPiperOrigin-RevId: 301802926\n\n275fbcce2c900278d487c33293a3c7e1fbcd3a34\nfeat: pubsub/v1 add an experimental filter field to Subscription\n\nPiperOrigin-RevId: 301661567\n\nf2b18cec51d27c999ad30011dba17f3965677e9c\nFix: UpdateBackupRequest.backup is a resource, not a resource reference - remove annotation.\n\nPiperOrigin-RevId: 301636171\n\n800384063ac93a0cac3a510d41726fa4b2cd4a83\nCloud Billing Budget API v1beta1\nModified api documentation to include warnings about the new filter field.\n\nPiperOrigin-RevId: 301634389\n\n0cc6c146b660db21f04056c3d58a4b752ee445e3\nCloud Billing Budget API v1alpha1\nModified api documentation to include warnings about the new filter field.\n\nPiperOrigin-RevId: 301630018\n\nff2ea00f69065585c3ac0993c8b582af3b6fc215\nFix: Add resource definition for a parent of InspectTemplate which was otherwise missing.\n\nPiperOrigin-RevId: 301623052\n\n55fa441c9daf03173910760191646399338f2b7c\nAdd proto definition for AccessLevel, AccessPolicy, and ServicePerimeter.\n\nPiperOrigin-RevId: 301620844\n\ne7b10591c5408a67cf14ffafa267556f3290e262\nCloud Bigtable Managed Backup service and message proto files.\n\nPiperOrigin-RevId: 301585144\n\nd8e226f702f8ddf92915128c9f4693b63fb8685d\nfeat: Add time-to-live in a queue for builds\n\nPiperOrigin-RevId: 301579876\n\n430375af011f8c7a5174884f0d0e539c6ffa7675\ndocs: add missing closing backtick\n\nPiperOrigin-RevId: 301538851\n\n0e9f1f60ded9ad1c2e725e37719112f5b487ab65\nbazel: Use latest release of gax_java\n\nPiperOrigin-RevId: 301480457\n\n5058c1c96d0ece7f5301a154cf5a07b2ad03a571\nUpdate GAPIC v2 with batching parameters for Logging API\n\nPiperOrigin-RevId: 301443847\n\n64ab9744073de81fec1b3a6a931befc8a90edf90\nFix: Introduce location-based organization/folder/billing-account resources\nChore: Update copyright years\n\nPiperOrigin-RevId: 301373760\n\n23d5f09e670ebb0c1b36214acf78704e2ecfc2ac\nUpdate field_behavior annotations in V1 and V2.\n\nPiperOrigin-RevId: 301337970\n\nb2cf37e7fd62383a811aa4d54d013ecae638851d\nData Catalog V1 API\n\nPiperOrigin-RevId: 301282503\n\n1976b9981e2900c8172b7d34b4220bdb18c5db42\nCloud DLP api update. Adds missing fields to Finding and adds support for hybrid jobs.\n\nPiperOrigin-RevId: 301205325\n\nae78682c05e864d71223ce22532219813b0245ac\nfix: several sample code blocks in comments are now properly indented for markdown\n\nPiperOrigin-RevId: 301185150\n\ndcd171d04bda5b67db13049320f97eca3ace3731\nPublish Media Translation API V1Beta1\n\nPiperOrigin-RevId: 301180096\n\nff1713453b0fbc5a7544a1ef6828c26ad21a370e\nAdd protos and BUILD rules for v1 API.\n\nPiperOrigin-RevId: 301179394\n\n8386761d09819b665b6a6e1e6d6ff884bc8ff781\nfeat: chromeos/modlab publish protos and config for Chrome OS Moblab API.\n\nPiperOrigin-RevId: 300843960\n\nb2e2bc62fab90e6829e62d3d189906d9b79899e4\nUpdates to GCS gRPC API spec:\n\n1. Changed GetIamPolicy and TestBucketIamPermissions to use wrapper messages around google.iam.v1 IAM requests messages, and added CommonRequestParams. This lets us support RequesterPays buckets.\n2. Added a metadata field to GetObjectMediaResponse, to support resuming an object media read safely (by extracting the generation of the object being read, and using it in the resumed read request).\n\nPiperOrigin-RevId: 300817706\n\n7fd916ce12335cc9e784bb9452a8602d00b2516c\nAdd deprecated_collections field for backward-compatiblity in PHP and monolith-generated Python and Ruby clients.\n\nGenerate TopicName class in Java which covers the functionality of both ProjectTopicName and DeletedTopicName. Introduce breaking changes to be fixed by synth.py.\n\nDelete default retry parameters.\n\nRetry codes defs can be deleted once # https://github.com/googleapis/gapic-generator/issues/3137 is fixed.\n\nPiperOrigin-RevId: 300813135\n\n047d3a8ac7f75383855df0166144f891d7af08d9\nfix!: google/rpc refactor ErrorInfo.type to ErrorInfo.reason and comment updates.\n\nPiperOrigin-RevId: 300773211\n\nfae4bb6d5aac52aabe5f0bb4396466c2304ea6f6\nAdding RetryPolicy to pubsub.proto\n\nPiperOrigin-RevId: 300769420\n\n7d569be2928dbd72b4e261bf9e468f23afd2b950\nAdding additional protocol buffer annotations to v3.\n\nPiperOrigin-RevId: 300718800\n\n13942d1a85a337515040a03c5108993087dc0e4f\nAdd logging protos for Recommender v1.\n\nPiperOrigin-RevId: 300689896\n\na1a573c3eecfe2c404892bfa61a32dd0c9fb22b6\nfix: change go package to use cloud.google.com/go/maps\n\nPiperOrigin-RevId: 300661825\n\nc6fbac11afa0c7ab2972d9df181493875c566f77\nfeat: publish documentai/v1beta2 protos\n\nPiperOrigin-RevId: 300656808\n\n5202a9e0d9903f49e900f20fe5c7f4e42dd6588f\nProtos for v1beta1 release of Cloud Security Center Settings API\n\nPiperOrigin-RevId: 300580858\n\n83518e18655d9d4ac044acbda063cc6ecdb63ef8\nAdds gapic.yaml file and BUILD.bazel file.\n\nPiperOrigin-RevId: 300554200\n\n836c196dc8ef8354bbfb5f30696bd3477e8db5e2\nRegenerate recommender v1beta1 gRPC ServiceConfig file for Insights methods.\n\nPiperOrigin-RevId: 300549302\n\n34a5450c591b6be3d6566f25ac31caa5211b2f3f\nIncreases the default timeout from 20s to 30s for MetricService\n\nPiperOrigin-RevId: 300474272\n\n5d8bffe87cd01ba390c32f1714230e5a95d5991d\nfeat: use the latest gapic-generator in WORKSPACE for bazel build.\n\nPiperOrigin-RevId: 300461878\n\nd631c651e3bcfac5d371e8560c27648f7b3e2364\nUpdated the GAPIC configs to include parameters for Backups APIs.\n\nPiperOrigin-RevId: 300443402\n\n678afc7055c1adea9b7b54519f3bdb228013f918\nAdding Game Servers v1beta API.\n\nPiperOrigin-RevId: 300433218\n\n80d2bd2c652a5e213302041b0620aff423132589\nEnable proto annotation and gapic v2 for talent API.\n\nPiperOrigin-RevId: 300393997\n\n85e454be7a353f7fe1bf2b0affb753305785b872\ndocs(google/maps/roads): remove mention of nonexported api\n\nPiperOrigin-RevId: 300367734\n\nbf839ae632e0f263a729569e44be4b38b1c85f9c\nAdding protocol buffer annotations and updated config info for v1 and v2.\n\nPiperOrigin-RevId: 300276913\n\n309b899ca18a4c604bce63882a161d44854da549\nPublish `Backup` APIs and protos.\n\nPiperOrigin-RevId: 300246038\n\neced64c3f122421350b4aca68a28e89121d20db8\nadd PHP client libraries\n\nPiperOrigin-RevId: 300193634\n\n7727af0e39df1ae9ad715895c8576d7b65cf6c6d\nfeat: use the latest gapic-generator and protoc-java-resource-name-plugin in googleapis/WORKSPACE.\n\nPiperOrigin-RevId: 300188410\n\n2a25aa351dd5b5fe14895266aff5824d90ce757b\nBreaking change: remove the ProjectOrTenant resource and its references.\n\nPiperOrigin-RevId: 300182152\n\na499dbb28546379415f51803505cfb6123477e71\nUpdate web risk v1 gapic config and BUILD file.\n\nPiperOrigin-RevId: 300152177\n\n52701da10fec2a5f9796e8d12518c0fe574488fe\nFix: apply appropriate namespace/package options for C#, PHP and Ruby.\n\nPiperOrigin-RevId: 300123508\n\n365c029b8cdb63f7751b92ab490f1976e616105c\nAdd CC targets to the kms protos.\n\nThese are needed by go/tink.\n\nPiperOrigin-RevId: 300038469\n\n4ba9aa8a4a1413b88dca5a8fa931824ee9c284e6\nExpose logo recognition API proto for GA.\n\nPiperOrigin-RevId: 299971671\n\n1c9fc2c9e03dadf15f16b1c4f570955bdcebe00e\nAdding ruby_package option to accessapproval.proto for the Ruby client libraries generation.\n\nPiperOrigin-RevId: 299955924\n\n1cc6f0a7bfb147e6f2ede911d9b01e7a9923b719\nbuild(google/maps/routes): generate api clients\n\nPiperOrigin-RevId: 299955905\n\n29a47c965aac79e3fe8e3314482ca0b5967680f0\nIncrease timeout to 1hr for method `dropRange` in bigtable/admin/v2, which is\nsynced with the timeout setting in gapic_yaml.\n\nPiperOrigin-RevId: 299917154\n\n8f631c4c70a60a9c7da3749511ee4ad432b62898\nbuild(google/maps/roads/v1op): move go to monorepo pattern\n\nPiperOrigin-RevId: 299885195\n\nd66816518844ebbf63504c9e8dfc7133921dd2cd\nbuild(google/maps/roads/v1op): Add bazel build files to generate clients.\n\nPiperOrigin-RevId: 299851148\n\naf7dff701fabe029672168649c62356cf1bb43d0\nAdd LogPlayerReports and LogImpressions to Playable Locations service\n\nPiperOrigin-RevId: 299724050\n\nb6927fca808f38df32a642c560082f5bf6538ced\nUpdate BigQuery Connection API v1beta1 proto: added credential to CloudSqlProperties.\n\nPiperOrigin-RevId: 299503150\n\n91e1fb5ef9829c0c7a64bfa5bde330e6ed594378\nchore: update protobuf (protoc) version to 3.11.2\n\nPiperOrigin-RevId: 299404145\n\n30e36b4bee6749c4799f4fc1a51cc8f058ba167d\nUpdate cloud asset api v1p4beta1.\n\nPiperOrigin-RevId: 299399890\n\nffbb493674099f265693872ae250711b2238090c\nfeat: cloudbuild/v1 add new fields and annotate OUTPUT_OUT fields.\n\nPiperOrigin-RevId: 299397780\n\nbc973a15818e00c19e121959832676e9b7607456\nbazel: Fix broken common dependency\n\nPiperOrigin-RevId: 299397431\n\n71094a343e3b962e744aa49eb9338219537474e4\nchore: bigtable/admin/v2 publish retry config\n\nPiperOrigin-RevId: 299391875\n\n8f488efd7bda33885cb674ddd023b3678c40bd82\nfeat: Migrate logging to GAPIC v2; release new features.\n\nIMPORTANT: This is a breaking change for client libraries\nin all languages.\n\nCommitter: @lukesneeringer, @jskeet\nPiperOrigin-RevId: 299370279\n\n007605bf9ad3a1fd775014ebefbf7f1e6b31ee71\nUpdate API for bigqueryreservation v1beta1.\n- Adds flex capacity commitment plan to CapacityCommitment.\n- Adds methods for getting and updating BiReservations.\n- Adds methods for updating/splitting/merging CapacityCommitments.\n\nPiperOrigin-RevId: 299368059\n\n" - } - }, - { - "git": { - "name": "synthtool", - "remote": "https://github.com/googleapis/synthtool.git", - "sha": "7e98e1609c91082f4eeb63b530c6468aefd18cfd" - } - } - ], + "updateTime": "2020-03-31T20:07:07.412385Z", "destinations": [ { "client": { diff --git a/packages/google-cloud-node/synth.py b/packages/google-cloud-node/synth.py index 7eda5ff85825..d64ad36aa590 100644 --- a/packages/google-cloud-node/synth.py +++ b/packages/google-cloud-node/synth.py @@ -50,10 +50,11 @@ # accepts streamingConfig when calling streamingRecognize. # Rename the generated methods to avoid confusion. s.replace(f'src/{version}/{name}_client.ts', r'( +)streamingRecognize\(', '\\1_streamingRecognize(') - s.replace(f'test/gapic-{name}-{version}.ts', r'client\.streamingRecognize\(', 'client._streamingRecognize(') + s.replace(f'test/gapic_{name}_{version}.ts', r'client\.streamingRecognize\(', 'client._streamingRecognize(') s.replace(f'src/{version}/{name}_client.ts', r'\Z', '\n' + "import {ImprovedStreamingClient} from '../helpers';\n" + + '// eslint-disable-next-line @typescript-eslint/no-empty-interface\n' + 'export interface SpeechClient extends ImprovedStreamingClient {}\n' ) diff --git a/packages/google-cloud-node/system-test/fixtures/sample/src/index.js b/packages/google-cloud-node/system-test/fixtures/sample/src/index.js index d281f72c707e..1f389ecd8a06 100644 --- a/packages/google-cloud-node/system-test/fixtures/sample/src/index.js +++ b/packages/google-cloud-node/system-test/fixtures/sample/src/index.js @@ -16,7 +16,6 @@ // ** https://github.com/googleapis/gapic-generator-typescript ** // ** All changes to this file may be overwritten. ** - /* eslint-disable node/no-missing-require, no-unused-vars */ const speech = require('@google-cloud/speech'); diff --git a/packages/google-cloud-node/system-test/fixtures/sample/src/index.ts b/packages/google-cloud-node/system-test/fixtures/sample/src/index.ts index ce7e711d8ad7..7e593209fb0c 100644 --- a/packages/google-cloud-node/system-test/fixtures/sample/src/index.ts +++ b/packages/google-cloud-node/system-test/fixtures/sample/src/index.ts @@ -19,7 +19,7 @@ import {SpeechClient} from '@google-cloud/speech'; function main() { - const speechClient = new SpeechClient(); + new SpeechClient(); } main(); diff --git a/packages/google-cloud-node/test/gapic-speech-v1.ts b/packages/google-cloud-node/test/gapic-speech-v1.ts deleted file mode 100644 index 83e2b68cd89d..000000000000 --- a/packages/google-cloud-node/test/gapic-speech-v1.ts +++ /dev/null @@ -1,317 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ** This file is automatically generated by gapic-generator-typescript. ** -// ** https://github.com/googleapis/gapic-generator-typescript ** -// ** All changes to this file may be overwritten. ** - -import * as protosTypes from '../protos/protos'; -import * as assert from 'assert'; -import {describe, it} from 'mocha'; -const speechModule = require('../src'); - -import {PassThrough} from 'stream'; - -const FAKE_STATUS_CODE = 1; -class FakeError { - name: string; - message: string; - code: number; - constructor(n: number) { - this.name = 'fakeName'; - this.message = 'fake message'; - this.code = n; - } -} -const error = new FakeError(FAKE_STATUS_CODE); -export interface Callback { - (err: FakeError | null, response?: {} | null): void; -} - -export class Operation { - constructor() {} - promise() {} -} -function mockSimpleGrpcMethod( - expectedRequest: {}, - response: {} | null, - error: FakeError | null -) { - return (actualRequest: {}, options: {}, callback: Callback) => { - assert.deepStrictEqual(actualRequest, expectedRequest); - if (error) { - callback(error); - } else if (response) { - callback(null, response); - } else { - callback(null); - } - }; -} -function mockBidiStreamingGrpcMethod( - expectedRequest: {}, - response: {} | null, - error: FakeError | null -) { - return () => { - const mockStream = new PassThrough({ - objectMode: true, - transform: (chunk: {}, enc: {}, callback: Callback) => { - assert.deepStrictEqual(chunk, expectedRequest); - if (error) { - callback(error); - } else { - callback(null, response); - } - }, - }); - return mockStream; - }; -} -function mockLongRunningGrpcMethod( - expectedRequest: {}, - response: {} | null, - error?: {} | null -) { - return (request: {}) => { - assert.deepStrictEqual(request, expectedRequest); - const mockOperation = { - promise() { - return new Promise((resolve, reject) => { - if (error) { - reject(error); - } else { - resolve([response]); - } - }); - }, - }; - return Promise.resolve([mockOperation]); - }; -} -describe('v1.SpeechClient', () => { - it('has servicePath', () => { - const servicePath = speechModule.v1.SpeechClient.servicePath; - assert(servicePath); - }); - it('has apiEndpoint', () => { - const apiEndpoint = speechModule.v1.SpeechClient.apiEndpoint; - assert(apiEndpoint); - }); - it('has port', () => { - const port = speechModule.v1.SpeechClient.port; - assert(port); - assert(typeof port === 'number'); - }); - it('should create a client with no option', () => { - const client = new speechModule.v1.SpeechClient(); - assert(client); - }); - it('should create a client with gRPC fallback', () => { - const client = new speechModule.v1.SpeechClient({ - fallback: true, - }); - assert(client); - }); - it('has initialize method and supports deferred initialization', async () => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - assert.strictEqual(client.speechStub, undefined); - await client.initialize(); - assert(client.speechStub); - }); - it('has close method', () => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - client.close(); - }); - describe('recognize', () => { - it('invokes recognize without error', done => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1.IRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.recognize = mockSimpleGrpcMethod( - request, - expectedResponse, - null - ); - client.recognize(request, (err: {}, response: {}) => { - assert.ifError(err); - assert.deepStrictEqual(response, expectedResponse); - done(); - }); - }); - - it('invokes recognize with error', done => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1.IRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.recognize = mockSimpleGrpcMethod( - request, - null, - error - ); - client.recognize(request, (err: FakeError, response: {}) => { - assert(err instanceof FakeError); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - assert(typeof response === 'undefined'); - done(); - }); - }); - }); - describe('longRunningRecognize', () => { - it('invokes longRunningRecognize without error', done => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1.ILongRunningRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.longRunningRecognize = mockLongRunningGrpcMethod( - request, - expectedResponse - ); - client - .longRunningRecognize(request) - .then((responses: [Operation]) => { - const operation = responses[0]; - return operation ? operation.promise() : {}; - }) - .then((responses: [Operation]) => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch((err: {}) => { - done(err); - }); - }); - - it('invokes longRunningRecognize with error', done => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1.ILongRunningRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.longRunningRecognize = mockLongRunningGrpcMethod( - request, - null, - error - ); - client - .longRunningRecognize(request) - .then((responses: [Operation]) => { - const operation = responses[0]; - return operation ? operation.promise() : {}; - }) - .then(() => { - assert.fail(); - }) - .catch((err: FakeError) => { - assert(err instanceof FakeError); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); - }); - }); - describe('streamingRecognize', () => { - it('invokes streamingRecognize without error', done => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1.IStreamingRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod( - request, - expectedResponse, - null - ); - const stream = client - ._streamingRecognize() - .on('data', (response: {}) => { - assert.deepStrictEqual(response, expectedResponse); - done(); - }) - .on('error', (err: FakeError) => { - done(err); - }); - stream.write(request); - }); - it('invokes streamingRecognize with error', done => { - const client = new speechModule.v1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1.IStreamingRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod( - request, - null, - error - ); - const stream = client - ._streamingRecognize() - .on('data', () => { - assert.fail(); - }) - .on('error', (err: FakeError) => { - assert(err instanceof FakeError); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); - stream.write(request); - }); - }); -}); diff --git a/packages/google-cloud-node/test/gapic-speech-v1p1beta1.ts b/packages/google-cloud-node/test/gapic-speech-v1p1beta1.ts deleted file mode 100644 index 7c82bc267125..000000000000 --- a/packages/google-cloud-node/test/gapic-speech-v1p1beta1.ts +++ /dev/null @@ -1,317 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ** This file is automatically generated by gapic-generator-typescript. ** -// ** https://github.com/googleapis/gapic-generator-typescript ** -// ** All changes to this file may be overwritten. ** - -import * as protosTypes from '../protos/protos'; -import * as assert from 'assert'; -import {describe, it} from 'mocha'; -const speechModule = require('../src'); - -import {PassThrough} from 'stream'; - -const FAKE_STATUS_CODE = 1; -class FakeError { - name: string; - message: string; - code: number; - constructor(n: number) { - this.name = 'fakeName'; - this.message = 'fake message'; - this.code = n; - } -} -const error = new FakeError(FAKE_STATUS_CODE); -export interface Callback { - (err: FakeError | null, response?: {} | null): void; -} - -export class Operation { - constructor() {} - promise() {} -} -function mockSimpleGrpcMethod( - expectedRequest: {}, - response: {} | null, - error: FakeError | null -) { - return (actualRequest: {}, options: {}, callback: Callback) => { - assert.deepStrictEqual(actualRequest, expectedRequest); - if (error) { - callback(error); - } else if (response) { - callback(null, response); - } else { - callback(null); - } - }; -} -function mockBidiStreamingGrpcMethod( - expectedRequest: {}, - response: {} | null, - error: FakeError | null -) { - return () => { - const mockStream = new PassThrough({ - objectMode: true, - transform: (chunk: {}, enc: {}, callback: Callback) => { - assert.deepStrictEqual(chunk, expectedRequest); - if (error) { - callback(error); - } else { - callback(null, response); - } - }, - }); - return mockStream; - }; -} -function mockLongRunningGrpcMethod( - expectedRequest: {}, - response: {} | null, - error?: {} | null -) { - return (request: {}) => { - assert.deepStrictEqual(request, expectedRequest); - const mockOperation = { - promise() { - return new Promise((resolve, reject) => { - if (error) { - reject(error); - } else { - resolve([response]); - } - }); - }, - }; - return Promise.resolve([mockOperation]); - }; -} -describe('v1p1beta1.SpeechClient', () => { - it('has servicePath', () => { - const servicePath = speechModule.v1p1beta1.SpeechClient.servicePath; - assert(servicePath); - }); - it('has apiEndpoint', () => { - const apiEndpoint = speechModule.v1p1beta1.SpeechClient.apiEndpoint; - assert(apiEndpoint); - }); - it('has port', () => { - const port = speechModule.v1p1beta1.SpeechClient.port; - assert(port); - assert(typeof port === 'number'); - }); - it('should create a client with no option', () => { - const client = new speechModule.v1p1beta1.SpeechClient(); - assert(client); - }); - it('should create a client with gRPC fallback', () => { - const client = new speechModule.v1p1beta1.SpeechClient({ - fallback: true, - }); - assert(client); - }); - it('has initialize method and supports deferred initialization', async () => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - assert.strictEqual(client.speechStub, undefined); - await client.initialize(); - assert(client.speechStub); - }); - it('has close method', () => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - client.close(); - }); - describe('recognize', () => { - it('invokes recognize without error', done => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.recognize = mockSimpleGrpcMethod( - request, - expectedResponse, - null - ); - client.recognize(request, (err: {}, response: {}) => { - assert.ifError(err); - assert.deepStrictEqual(response, expectedResponse); - done(); - }); - }); - - it('invokes recognize with error', done => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1p1beta1.IRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.recognize = mockSimpleGrpcMethod( - request, - null, - error - ); - client.recognize(request, (err: FakeError, response: {}) => { - assert(err instanceof FakeError); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - assert(typeof response === 'undefined'); - done(); - }); - }); - }); - describe('longRunningRecognize', () => { - it('invokes longRunningRecognize without error', done => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.longRunningRecognize = mockLongRunningGrpcMethod( - request, - expectedResponse - ); - client - .longRunningRecognize(request) - .then((responses: [Operation]) => { - const operation = responses[0]; - return operation ? operation.promise() : {}; - }) - .then((responses: [Operation]) => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch((err: {}) => { - done(err); - }); - }); - - it('invokes longRunningRecognize with error', done => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1p1beta1.ILongRunningRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.longRunningRecognize = mockLongRunningGrpcMethod( - request, - null, - error - ); - client - .longRunningRecognize(request) - .then((responses: [Operation]) => { - const operation = responses[0]; - return operation ? operation.promise() : {}; - }) - .then(() => { - assert.fail(); - }) - .catch((err: FakeError) => { - assert(err instanceof FakeError); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); - }); - }); - describe('streamingRecognize', () => { - it('invokes streamingRecognize without error', done => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1p1beta1.IStreamingRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod( - request, - expectedResponse, - null - ); - const stream = client - ._streamingRecognize() - .on('data', (response: {}) => { - assert.deepStrictEqual(response, expectedResponse); - done(); - }) - .on('error', (err: FakeError) => { - done(err); - }); - stream.write(request); - }); - it('invokes streamingRecognize with error', done => { - const client = new speechModule.v1p1beta1.SpeechClient({ - credentials: {client_email: 'bogus', private_key: 'bogus'}, - projectId: 'bogus', - }); - // Initialize client before mocking - client.initialize(); - // Mock request - const request: protosTypes.google.cloud.speech.v1p1beta1.IStreamingRecognizeRequest = {}; - // Mock response - const expectedResponse = {}; - // Mock gRPC layer - client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod( - request, - null, - error - ); - const stream = client - ._streamingRecognize() - .on('data', () => { - assert.fail(); - }) - .on('error', (err: FakeError) => { - assert(err instanceof FakeError); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); - stream.write(request); - }); - }); -}); diff --git a/packages/google-cloud-node/test/gapic_speech_v1.ts b/packages/google-cloud-node/test/gapic_speech_v1.ts new file mode 100644 index 000000000000..ba84336736ca --- /dev/null +++ b/packages/google-cloud-node/test/gapic_speech_v1.ts @@ -0,0 +1,489 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ** This file is automatically generated by gapic-generator-typescript. ** +// ** https://github.com/googleapis/gapic-generator-typescript ** +// ** All changes to this file may be overwritten. ** + +import * as protos from '../protos/protos'; +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import {SinonStub} from 'sinon'; +import {describe, it} from 'mocha'; +import * as speechModule from '../src'; + +import {PassThrough} from 'stream'; + +import {protobuf, LROperation} from 'google-gax'; + +function generateSampleMessage(instance: T) { + const filledObject = (instance.constructor as typeof protobuf.Message).toObject( + instance as protobuf.Message, + {defaults: true} + ); + return (instance.constructor as typeof protobuf.Message).fromObject( + filledObject + ) as T; +} + +function stubSimpleCall(response?: ResponseType, error?: Error) { + return error + ? sinon.stub().rejects(error) + : sinon.stub().resolves([response]); +} + +function stubSimpleCallWithCallback( + response?: ResponseType, + error?: Error +) { + return error + ? sinon.stub().callsArgWith(2, error) + : sinon.stub().callsArgWith(2, null, response); +} + +function stubBidiStreamingCall( + response?: ResponseType, + error?: Error +) { + const transformStub = error + ? sinon.stub().callsArgWith(2, error) + : sinon.stub().callsArgWith(2, null, response); + const mockStream = new PassThrough({ + objectMode: true, + transform: transformStub, + }); + return sinon.stub().returns(mockStream); +} + +function stubLongRunningCall( + response?: ResponseType, + callError?: Error, + lroError?: Error +) { + const innerStub = lroError + ? sinon.stub().rejects(lroError) + : sinon.stub().resolves([response]); + const mockOperation = { + promise: innerStub, + }; + return callError + ? sinon.stub().rejects(callError) + : sinon.stub().resolves([mockOperation]); +} + +function stubLongRunningCallWithCallback( + response?: ResponseType, + callError?: Error, + lroError?: Error +) { + const innerStub = lroError + ? sinon.stub().rejects(lroError) + : sinon.stub().resolves([response]); + const mockOperation = { + promise: innerStub, + }; + return callError + ? sinon.stub().callsArgWith(2, callError) + : sinon.stub().callsArgWith(2, null, mockOperation); +} + +describe('v1.SpeechClient', () => { + it('has servicePath', () => { + const servicePath = speechModule.v1.SpeechClient.servicePath; + assert(servicePath); + }); + + it('has apiEndpoint', () => { + const apiEndpoint = speechModule.v1.SpeechClient.apiEndpoint; + assert(apiEndpoint); + }); + + it('has port', () => { + const port = speechModule.v1.SpeechClient.port; + assert(port); + assert(typeof port === 'number'); + }); + + it('should create a client with no option', () => { + const client = new speechModule.v1.SpeechClient(); + assert(client); + }); + + it('should create a client with gRPC fallback', () => { + const client = new speechModule.v1.SpeechClient({ + fallback: true, + }); + assert(client); + }); + + it('has initialize method and supports deferred initialization', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + assert.strictEqual(client.speechStub, undefined); + await client.initialize(); + assert(client.speechStub); + }); + + it('has close method', () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.close(); + }); + + it('has getProjectId method', async () => { + const fakeProjectId = 'fake-project-id'; + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.auth.getProjectId = sinon.stub().resolves(fakeProjectId); + const result = await client.getProjectId(); + assert.strictEqual(result, fakeProjectId); + assert((client.auth.getProjectId as SinonStub).calledWithExactly()); + }); + + it('has getProjectId method with callback', async () => { + const fakeProjectId = 'fake-project-id'; + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.auth.getProjectId = sinon + .stub() + .callsArgWith(0, null, fakeProjectId); + const promise = new Promise((resolve, reject) => { + client.getProjectId((err?: Error | null, projectId?: string | null) => { + if (err) { + reject(err); + } else { + resolve(projectId); + } + }); + }); + const result = await promise; + assert.strictEqual(result, fakeProjectId); + }); + + describe('recognize', () => { + it('invokes recognize without error', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.RecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.cloud.speech.v1.RecognizeResponse() + ); + client.innerApiCalls.recognize = stubSimpleCall(expectedResponse); + const [response] = await client.recognize(request); + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.recognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + + it('invokes recognize without error using callback', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.RecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.cloud.speech.v1.RecognizeResponse() + ); + client.innerApiCalls.recognize = stubSimpleCallWithCallback( + expectedResponse + ); + const promise = new Promise((resolve, reject) => { + client.recognize( + request, + ( + err?: Error | null, + result?: protos.google.cloud.speech.v1.IRecognizeResponse | null + ) => { + if (err) { + reject(err); + } else { + resolve(result); + } + } + ); + }); + const response = await promise; + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.recognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions /*, callback defined above */) + ); + }); + + it('invokes recognize with error', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.RecognizeRequest() + ); + const expectedOptions = {}; + const expectedError = new Error('expected'); + client.innerApiCalls.recognize = stubSimpleCall(undefined, expectedError); + assert.rejects(async () => { + await client.recognize(request); + }, expectedError); + assert( + (client.innerApiCalls.recognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + }); + + describe('longRunningRecognize', () => { + it('invokes longRunningRecognize without error', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.longrunning.Operation() + ); + client.innerApiCalls.longRunningRecognize = stubLongRunningCall( + expectedResponse + ); + const [operation] = await client.longRunningRecognize(request); + const [response] = await operation.promise(); + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + + it('invokes longRunningRecognize without error using callback', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.longrunning.Operation() + ); + client.innerApiCalls.longRunningRecognize = stubLongRunningCallWithCallback( + expectedResponse + ); + const promise = new Promise((resolve, reject) => { + client.longRunningRecognize( + request, + ( + err?: Error | null, + result?: LROperation< + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata + > | null + ) => { + if (err) { + reject(err); + } else { + resolve(result); + } + } + ); + }); + const operation = (await promise) as LROperation< + protos.google.cloud.speech.v1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1.ILongRunningRecognizeMetadata + >; + const [response] = await operation.promise(); + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions /*, callback defined above */) + ); + }); + + it('invokes longRunningRecognize with call error', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedError = new Error('expected'); + client.innerApiCalls.longRunningRecognize = stubLongRunningCall( + undefined, + expectedError + ); + assert.rejects(async () => { + await client.longRunningRecognize(request); + }, expectedError); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + + it('invokes longRunningRecognize with LRO error', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedError = new Error('expected'); + client.innerApiCalls.longRunningRecognize = stubLongRunningCall( + undefined, + undefined, + expectedError + ); + const [operation] = await client.longRunningRecognize(request); + assert.rejects(async () => { + await operation.promise(); + }, expectedError); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + }); + + describe('streamingRecognize', () => { + it('invokes streamingRecognize without error', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.StreamingRecognizeRequest() + ); + const expectedResponse = generateSampleMessage( + new protos.google.cloud.speech.v1.StreamingRecognizeResponse() + ); + client.innerApiCalls.streamingRecognize = stubBidiStreamingCall( + expectedResponse + ); + const stream = client._streamingRecognize(); + const promise = new Promise((resolve, reject) => { + stream.on( + 'data', + ( + response: protos.google.cloud.speech.v1.StreamingRecognizeResponse + ) => { + resolve(response); + } + ); + stream.on('error', (err: Error) => { + reject(err); + }); + stream.write(request); + stream.end(); + }); + const response = await promise; + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.streamingRecognize as SinonStub) + .getCall(0) + .calledWithExactly(undefined) + ); + assert.deepStrictEqual( + (((stream as unknown) as PassThrough)._transform as SinonStub).getCall( + 0 + ).args[0], + request + ); + }); + + it('invokes streamingRecognize with error', async () => { + const client = new speechModule.v1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1.StreamingRecognizeRequest() + ); + const expectedError = new Error('expected'); + client.innerApiCalls.streamingRecognize = stubBidiStreamingCall( + undefined, + expectedError + ); + const stream = client._streamingRecognize(); + const promise = new Promise((resolve, reject) => { + stream.on( + 'data', + ( + response: protos.google.cloud.speech.v1.StreamingRecognizeResponse + ) => { + resolve(response); + } + ); + stream.on('error', (err: Error) => { + reject(err); + }); + stream.write(request); + stream.end(); + }); + assert.rejects(async () => { + await promise; + }, expectedError); + assert( + (client.innerApiCalls.streamingRecognize as SinonStub) + .getCall(0) + .calledWithExactly(undefined) + ); + assert.deepStrictEqual( + (((stream as unknown) as PassThrough)._transform as SinonStub).getCall( + 0 + ).args[0], + request + ); + }); + }); +}); diff --git a/packages/google-cloud-node/test/gapic_speech_v1p1beta1.ts b/packages/google-cloud-node/test/gapic_speech_v1p1beta1.ts new file mode 100644 index 000000000000..6b0d1d1fdc7e --- /dev/null +++ b/packages/google-cloud-node/test/gapic_speech_v1p1beta1.ts @@ -0,0 +1,489 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ** This file is automatically generated by gapic-generator-typescript. ** +// ** https://github.com/googleapis/gapic-generator-typescript ** +// ** All changes to this file may be overwritten. ** + +import * as protos from '../protos/protos'; +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import {SinonStub} from 'sinon'; +import {describe, it} from 'mocha'; +import * as speechModule from '../src'; + +import {PassThrough} from 'stream'; + +import {protobuf, LROperation} from 'google-gax'; + +function generateSampleMessage(instance: T) { + const filledObject = (instance.constructor as typeof protobuf.Message).toObject( + instance as protobuf.Message, + {defaults: true} + ); + return (instance.constructor as typeof protobuf.Message).fromObject( + filledObject + ) as T; +} + +function stubSimpleCall(response?: ResponseType, error?: Error) { + return error + ? sinon.stub().rejects(error) + : sinon.stub().resolves([response]); +} + +function stubSimpleCallWithCallback( + response?: ResponseType, + error?: Error +) { + return error + ? sinon.stub().callsArgWith(2, error) + : sinon.stub().callsArgWith(2, null, response); +} + +function stubBidiStreamingCall( + response?: ResponseType, + error?: Error +) { + const transformStub = error + ? sinon.stub().callsArgWith(2, error) + : sinon.stub().callsArgWith(2, null, response); + const mockStream = new PassThrough({ + objectMode: true, + transform: transformStub, + }); + return sinon.stub().returns(mockStream); +} + +function stubLongRunningCall( + response?: ResponseType, + callError?: Error, + lroError?: Error +) { + const innerStub = lroError + ? sinon.stub().rejects(lroError) + : sinon.stub().resolves([response]); + const mockOperation = { + promise: innerStub, + }; + return callError + ? sinon.stub().rejects(callError) + : sinon.stub().resolves([mockOperation]); +} + +function stubLongRunningCallWithCallback( + response?: ResponseType, + callError?: Error, + lroError?: Error +) { + const innerStub = lroError + ? sinon.stub().rejects(lroError) + : sinon.stub().resolves([response]); + const mockOperation = { + promise: innerStub, + }; + return callError + ? sinon.stub().callsArgWith(2, callError) + : sinon.stub().callsArgWith(2, null, mockOperation); +} + +describe('v1p1beta1.SpeechClient', () => { + it('has servicePath', () => { + const servicePath = speechModule.v1p1beta1.SpeechClient.servicePath; + assert(servicePath); + }); + + it('has apiEndpoint', () => { + const apiEndpoint = speechModule.v1p1beta1.SpeechClient.apiEndpoint; + assert(apiEndpoint); + }); + + it('has port', () => { + const port = speechModule.v1p1beta1.SpeechClient.port; + assert(port); + assert(typeof port === 'number'); + }); + + it('should create a client with no option', () => { + const client = new speechModule.v1p1beta1.SpeechClient(); + assert(client); + }); + + it('should create a client with gRPC fallback', () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + fallback: true, + }); + assert(client); + }); + + it('has initialize method and supports deferred initialization', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + assert.strictEqual(client.speechStub, undefined); + await client.initialize(); + assert(client.speechStub); + }); + + it('has close method', () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.close(); + }); + + it('has getProjectId method', async () => { + const fakeProjectId = 'fake-project-id'; + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.auth.getProjectId = sinon.stub().resolves(fakeProjectId); + const result = await client.getProjectId(); + assert.strictEqual(result, fakeProjectId); + assert((client.auth.getProjectId as SinonStub).calledWithExactly()); + }); + + it('has getProjectId method with callback', async () => { + const fakeProjectId = 'fake-project-id'; + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.auth.getProjectId = sinon + .stub() + .callsArgWith(0, null, fakeProjectId); + const promise = new Promise((resolve, reject) => { + client.getProjectId((err?: Error | null, projectId?: string | null) => { + if (err) { + reject(err); + } else { + resolve(projectId); + } + }); + }); + const result = await promise; + assert.strictEqual(result, fakeProjectId); + }); + + describe('recognize', () => { + it('invokes recognize without error', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.RecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.RecognizeResponse() + ); + client.innerApiCalls.recognize = stubSimpleCall(expectedResponse); + const [response] = await client.recognize(request); + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.recognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + + it('invokes recognize without error using callback', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.RecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.RecognizeResponse() + ); + client.innerApiCalls.recognize = stubSimpleCallWithCallback( + expectedResponse + ); + const promise = new Promise((resolve, reject) => { + client.recognize( + request, + ( + err?: Error | null, + result?: protos.google.cloud.speech.v1p1beta1.IRecognizeResponse | null + ) => { + if (err) { + reject(err); + } else { + resolve(result); + } + } + ); + }); + const response = await promise; + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.recognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions /*, callback defined above */) + ); + }); + + it('invokes recognize with error', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.RecognizeRequest() + ); + const expectedOptions = {}; + const expectedError = new Error('expected'); + client.innerApiCalls.recognize = stubSimpleCall(undefined, expectedError); + assert.rejects(async () => { + await client.recognize(request); + }, expectedError); + assert( + (client.innerApiCalls.recognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + }); + + describe('longRunningRecognize', () => { + it('invokes longRunningRecognize without error', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.longrunning.Operation() + ); + client.innerApiCalls.longRunningRecognize = stubLongRunningCall( + expectedResponse + ); + const [operation] = await client.longRunningRecognize(request); + const [response] = await operation.promise(); + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + + it('invokes longRunningRecognize without error using callback', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedResponse = generateSampleMessage( + new protos.google.longrunning.Operation() + ); + client.innerApiCalls.longRunningRecognize = stubLongRunningCallWithCallback( + expectedResponse + ); + const promise = new Promise((resolve, reject) => { + client.longRunningRecognize( + request, + ( + err?: Error | null, + result?: LROperation< + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + > | null + ) => { + if (err) { + reject(err); + } else { + resolve(result); + } + } + ); + }); + const operation = (await promise) as LROperation< + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeResponse, + protos.google.cloud.speech.v1p1beta1.ILongRunningRecognizeMetadata + >; + const [response] = await operation.promise(); + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions /*, callback defined above */) + ); + }); + + it('invokes longRunningRecognize with call error', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedError = new Error('expected'); + client.innerApiCalls.longRunningRecognize = stubLongRunningCall( + undefined, + expectedError + ); + assert.rejects(async () => { + await client.longRunningRecognize(request); + }, expectedError); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + + it('invokes longRunningRecognize with LRO error', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest() + ); + const expectedOptions = {}; + const expectedError = new Error('expected'); + client.innerApiCalls.longRunningRecognize = stubLongRunningCall( + undefined, + undefined, + expectedError + ); + const [operation] = await client.longRunningRecognize(request); + assert.rejects(async () => { + await operation.promise(); + }, expectedError); + assert( + (client.innerApiCalls.longRunningRecognize as SinonStub) + .getCall(0) + .calledWith(request, expectedOptions, undefined) + ); + }); + }); + + describe('streamingRecognize', () => { + it('invokes streamingRecognize without error', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest() + ); + const expectedResponse = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse() + ); + client.innerApiCalls.streamingRecognize = stubBidiStreamingCall( + expectedResponse + ); + const stream = client._streamingRecognize(); + const promise = new Promise((resolve, reject) => { + stream.on( + 'data', + ( + response: protos.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse + ) => { + resolve(response); + } + ); + stream.on('error', (err: Error) => { + reject(err); + }); + stream.write(request); + stream.end(); + }); + const response = await promise; + assert.deepStrictEqual(response, expectedResponse); + assert( + (client.innerApiCalls.streamingRecognize as SinonStub) + .getCall(0) + .calledWithExactly(undefined) + ); + assert.deepStrictEqual( + (((stream as unknown) as PassThrough)._transform as SinonStub).getCall( + 0 + ).args[0], + request + ); + }); + + it('invokes streamingRecognize with error', async () => { + const client = new speechModule.v1p1beta1.SpeechClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.initialize(); + const request = generateSampleMessage( + new protos.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest() + ); + const expectedError = new Error('expected'); + client.innerApiCalls.streamingRecognize = stubBidiStreamingCall( + undefined, + expectedError + ); + const stream = client._streamingRecognize(); + const promise = new Promise((resolve, reject) => { + stream.on( + 'data', + ( + response: protos.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse + ) => { + resolve(response); + } + ); + stream.on('error', (err: Error) => { + reject(err); + }); + stream.write(request); + stream.end(); + }); + assert.rejects(async () => { + await promise; + }, expectedError); + assert( + (client.innerApiCalls.streamingRecognize as SinonStub) + .getCall(0) + .calledWithExactly(undefined) + ); + assert.deepStrictEqual( + (((stream as unknown) as PassThrough)._transform as SinonStub).getCall( + 0 + ).args[0], + request + ); + }); + }); +}); diff --git a/packages/google-cloud-node/test/helpers.test.ts b/packages/google-cloud-node/test/helpers.test.ts index 343c51832c25..51752dcdd1ae 100644 --- a/packages/google-cloud-node/test/helpers.test.ts +++ b/packages/google-cloud-node/test/helpers.test.ts @@ -17,11 +17,10 @@ 'use strict'; import * as assert from 'assert'; -import {describe, it} from 'mocha'; +import {afterEach, describe, it} from 'mocha'; import * as sinon from 'sinon'; import * as stream from 'stream'; - -const speech = require('../src'); +import * as speech from '../src'; describe('Speech helper methods', () => { const sandbox = sinon.createSandbox(); @@ -31,8 +30,8 @@ describe('Speech helper methods', () => { }); describe('streamingRecognize', () => { - const CONFIG = { - config: {encoding: 'LINEAR16', languageCode: 'en-us', sampleRate: 16000}, + const CONFIG: speech.protos.google.cloud.speech.v1.IStreamingRecognitionConfig = { + config: {encoding: 'LINEAR16', languageCode: 'en-us'}, }; const OPTIONS = {timeout: Infinity}; @@ -47,7 +46,7 @@ describe('Speech helper methods', () => { // a bogus stream. const requestStream = new stream.PassThrough({objectMode: true}); const sr = sandbox - .stub(client._innerApiCalls, 'streamingRecognize') + .stub(client.innerApiCalls, 'streamingRecognize') .returns(requestStream); // Call the new helper method and establish that the config was @@ -81,7 +80,7 @@ describe('Speech helper methods', () => { // a bogus stream. const requestStream = new stream.PassThrough({objectMode: true}); const sr = sandbox - .stub(client._innerApiCalls, 'streamingRecognize') + .stub(client.innerApiCalls, 'streamingRecognize') .returns(requestStream); const userStream = client.streamingRecognize(CONFIG); @@ -103,7 +102,7 @@ describe('Speech helper methods', () => { // a bogus stream. const requestStream = new stream.PassThrough({objectMode: true}); sandbox - .stub(client._innerApiCalls, 'streamingRecognize') + .stub(client.innerApiCalls, 'streamingRecognize') .returns(requestStream); const userStream = client.streamingRecognize(CONFIG, OPTIONS); @@ -129,7 +128,7 @@ describe('Speech helper methods', () => { // a bogus stream. const requestStream = new stream.PassThrough({objectMode: true}); sandbox - .stub(client._innerApiCalls, 'streamingRecognize') + .stub(client.innerApiCalls, 'streamingRecognize') .returns(requestStream); const userStream = client.streamingRecognize(CONFIG, OPTIONS); @@ -157,7 +156,7 @@ describe('Speech helper methods', () => { const requestStream = new stream.PassThrough({objectMode: true}); sandbox - .stub(client._innerApiCalls, 'streamingRecognize') + .stub(client.innerApiCalls, 'streamingRecognize') .returns(requestStream); const userStream = client.streamingRecognize(CONFIG, OPTIONS); diff --git a/packages/google-cloud-node/webpack.config.js b/packages/google-cloud-node/webpack.config.js index f159d07440d7..de0be12e3f49 100644 --- a/packages/google-cloud-node/webpack.config.js +++ b/packages/google-cloud-node/webpack.config.js @@ -36,27 +36,27 @@ module.exports = { { test: /\.tsx?$/, use: 'ts-loader', - exclude: /node_modules/ + exclude: /node_modules/, }, { test: /node_modules[\\/]@grpc[\\/]grpc-js/, - use: 'null-loader' + use: 'null-loader', }, { test: /node_modules[\\/]grpc/, - use: 'null-loader' + use: 'null-loader', }, { test: /node_modules[\\/]retry-request/, - use: 'null-loader' + use: 'null-loader', }, { test: /node_modules[\\/]https?-proxy-agent/, - use: 'null-loader' + use: 'null-loader', }, { test: /node_modules[\\/]gtoken/, - use: 'null-loader' + use: 'null-loader', }, ], },