Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

fix: do not modify options object, use defaultScopes #94

Merged
merged 2 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"api-documenter": "api-documenter yaml --input-folder=temp"
},
"dependencies": {
"google-gax": "^2.1.0"
"google-gax": "^2.9.2"
},
"devDependencies": {
"@types/mocha": "^8.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import * as v1beta1 from './v1beta1';
const SpeechTranslationServiceClient = v1beta1.SpeechTranslationServiceClient;
type SpeechTranslationServiceClient = v1beta1.SpeechTranslationServiceClient;
export {v1beta1, SpeechTranslationServiceClient};
export default {v1beta1, SpeechTranslationServiceClient};
import * as protos from '../protos/protos';
Expand Down
76 changes: 43 additions & 33 deletions src/v1beta1/speech_translation_service_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export class SpeechTranslationServiceClient {
/**
* Construct an instance of SpeechTranslationServiceClient.
*
* @param {object} [options] - The configuration object. See the subsequent
* parameters for more details.
* @param {object} [options] - The configuration object.
* The options accepted by the constructor are described in detail
* in [this document](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#creating-the-client-instance).
* The common options are:
* @param {object} [options.credentials] - Credentials object.
* @param {string} [options.credentials.client_email]
* @param {string} [options.credentials.private_key]
Expand All @@ -71,44 +73,34 @@ export class SpeechTranslationServiceClient {
* your project ID will be detected automatically.
* @param {string} [options.apiEndpoint] - The domain name of the
* API remote host.
* @param {gax.ClientConfig} [options.clientConfig] - client configuration override.
* TODO(@alexander-fenster): link to gax documentation.
* @param {boolean} fallback - Use HTTP fallback mode.
* In fallback mode, a special browser-compatible transport implementation is used
* instead of gRPC transport. In browser context (if the `window` object is defined)
* the fallback mode is enabled automatically; set `options.fallback` to `false`
* if you need to override this behavior.
*/

constructor(opts?: ClientOptions) {
// Ensure that options include the service address and port.
// Ensure that options include all the required fields.
const staticMembers = this
.constructor as typeof SpeechTranslationServiceClient;
const servicePath =
opts && opts.servicePath
? opts.servicePath
: opts && opts.apiEndpoint
? opts.apiEndpoint
: staticMembers.servicePath;
const port = opts && opts.port ? opts.port : staticMembers.port;
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
const port = opts?.port || staticMembers.port;
const clientConfig = opts?.clientConfig ?? {};
const fallback = opts?.fallback ?? typeof window !== 'undefined';
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);

if (!opts) {
opts = {servicePath, port};
// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
if (servicePath !== staticMembers.servicePath && !('scopes' in opts)) {
opts['scopes'] = staticMembers.scopes;
}
opts.servicePath = opts.servicePath || servicePath;
opts.port = opts.port || port;

// users can override the config from client side, like retry codes name.
// The detailed structure of the clientConfig can be found here: https://github.com/googleapis/gax-nodejs/blob/master/src/gax.ts#L546
// The way to override client config for Showcase API:
//
// const customConfig = {"interfaces": {"google.showcase.v1beta1.Echo": {"methods": {"Echo": {"retry_codes_name": "idempotent", "retry_params_name": "default"}}}}}
// const showcaseClient = new showcaseClient({ projectId, customConfig });
opts.clientConfig = opts.clientConfig || {};

// If we're running in browser, it's OK to omit `fallback` since
// google-gax has `browser` field in its `package.json`.
// For Electron (which does not respect `browser` field),
// pass `{fallback: true}` to the SpeechTranslationServiceClient constructor.
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
this._gaxModule = opts.fallback ? gax.fallback : gax;

// Create a `gaxGrpc` object, with any grpc-specific options
// sent to the client.
opts.scopes = (this
.constructor as typeof SpeechTranslationServiceClient).scopes;
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);

// Save options to use in initialize() method.
Expand All @@ -117,6 +109,11 @@ export class SpeechTranslationServiceClient {
// Save the auth object to the client, for use by other methods.
this.auth = this._gaxGrpc.auth as gax.GoogleAuth;

// Set the default scopes in auth client if needed.
if (servicePath === staticMembers.servicePath) {
this.auth.defaultScopes = staticMembers.scopes;
}

// Determine the client header string.
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
if (typeof process !== 'undefined' && 'versions' in process) {
Expand Down Expand Up @@ -232,6 +229,7 @@ export class SpeechTranslationServiceClient {

/**
* The DNS address for this API service.
* @returns {string} The DNS address for this service.
*/
static get servicePath() {
return 'mediatranslation.googleapis.com';
Expand All @@ -240,13 +238,15 @@ export class SpeechTranslationServiceClient {
/**
* The DNS address for this API service - same as servicePath(),
* exists for compatibility reasons.
* @returns {string} The DNS address for this service.
*/
static get apiEndpoint() {
return 'mediatranslation.googleapis.com';
}

/**
* The port for this API service.
* @returns {number} The default port for this service.
*/
static get port() {
return 443;
Expand All @@ -255,6 +255,7 @@ export class SpeechTranslationServiceClient {
/**
* The scopes needed to make gRPC calls for every method defined
* in this service.
* @returns {string[]} List of default scopes.
*/
static get scopes() {
return ['https://www.googleapis.com/auth/cloud-platform'];
Expand All @@ -264,8 +265,7 @@ export class SpeechTranslationServiceClient {
getProjectId(callback: Callback<string, undefined, undefined>): void;
/**
* Return the project ID used by this class.
* @param {function(Error, string)} callback - the callback to
* be called with the current project Id.
* @returns {Promise} A promise that resolves to string containing the project ID.
*/
getProjectId(
callback?: Callback<string, undefined, undefined>
Expand All @@ -291,16 +291,26 @@ export class SpeechTranslationServiceClient {
* An object stream which is both readable and writable. It accepts objects
* representing [StreamingTranslateSpeechRequest]{@link google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechRequest} for write() method, and
* will emit objects representing [StreamingTranslateSpeechResponse]{@link google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResponse} on 'data' event asynchronously.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#bi-directional-streaming)
* for more details and examples.
* @example
* const stream = client.streamingTranslateSpeech();
* stream.on('data', (response) => { ... });
* stream.on('end', () => { ... });
* stream.write(request);
* stream.end();
*/
streamingTranslateSpeech(options?: gax.CallOptions): gax.CancellableStream {
this.initialize();
return this.innerApiCalls.streamingTranslateSpeech(options);
}

/**
* Terminate the GRPC channel and close the client.
* Terminate the gRPC channel and close the client.
*
* The client will no longer be usable and all future behavior is undefined.
* @returns {Promise} A promise that resolves when the client is closed.
*/
close(): Promise<void> {
this.initialize();
Expand Down
16 changes: 5 additions & 11 deletions synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@
{
"git": {
"name": ".",
"remote": "https://github.com/googleapis/nodejs-media-translation.git",
"sha": "c1a44314dd7b73faa69e3a7fe1edde54266c3a88"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "4c5071b615d96ef9dfd6a63d8429090f1f2872bb",
"internalRef": "327369997"
"remote": "git@github.com:googleapis/nodejs-media-translation.git",
"sha": "f132c7b2725509ef3d4d497733ba85f851b52789"
}
},
{
"git": {
"name": "synthtool",
"remote": "https://github.com/googleapis/synthtool.git",
"sha": "ba9918cd22874245b55734f57470c719b577e591"
"sha": "1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b"
}
}
],
Expand Down Expand Up @@ -86,12 +78,14 @@
"LICENSE",
"README.md",
"api-extractor.json",
"package-lock.json.3635725974",
"protos/google/cloud/mediatranslation/v1beta1/media_translation.proto",
"protos/protos.d.ts",
"protos/protos.js",
"protos/protos.json",
"renovate.json",
"samples/README.md",
"samples/package-lock.json.3114264012",
"src/index.ts",
"src/v1beta1/index.ts",
"src/v1beta1/speech_translation_service_client.ts",
Expand Down
11 changes: 10 additions & 1 deletion system-test/fixtures/sample/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@

import {SpeechTranslationServiceClient} from '@google-cloud/media-translation';

// check that the client class type name can be used
function doStuffWithSpeechTranslationServiceClient(
client: SpeechTranslationServiceClient
) {
client.close();
}

function main() {
new SpeechTranslationServiceClient();
// check that the client instance can be created
const speechTranslationServiceClient = new SpeechTranslationServiceClient();
doStuffWithSpeechTranslationServiceClient(speechTranslationServiceClient);
}

main();
18 changes: 9 additions & 9 deletions system-test/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ import {packNTest} from 'pack-n-play';
import {readFileSync} from 'fs';
import {describe, it} from 'mocha';

describe('typescript consumer tests', () => {
it('should have correct type signature for typescript users', async function () {
describe('📦 pack-n-play test', () => {
it('TypeScript code', async function () {
this.timeout(300000);
const options = {
packageDir: process.cwd(), // path to your module.
packageDir: process.cwd(),
sample: {
description: 'typescript based user can use the type definitions',
description: 'TypeScript user can use the type definitions',
ts: readFileSync(
'./system-test/fixtures/sample/src/index.ts'
).toString(),
},
};
await packNTest(options); // will throw upon error.
await packNTest(options);
});

it('should have correct type signature for javascript users', async function () {
it('JavaScript code', async function () {
this.timeout(300000);
const options = {
packageDir: process.cwd(), // path to your module.
packageDir: process.cwd(),
sample: {
description: 'typescript based user can use the type definitions',
description: 'JavaScript user can use the library',
ts: readFileSync(
'./system-test/fixtures/sample/src/index.js'
).toString(),
},
};
await packNTest(options); // will throw upon error.
await packNTest(options);
});
});