Skip to content

Commit

Permalink
fix: do not modify options object, use defaultScopes (#935)
Browse files Browse the repository at this point in the history
Regenerated the library using
[gapic-generator-typescript](https://github.com/googleapis/gapic-generator-typescript)
v1.2.1.
  • Loading branch information
alexander-fenster committed Nov 6, 2020
1 parent e594c9b commit fd1e63a
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 337 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -57,7 +57,7 @@
"extend": "^3.0.2",
"gcp-metadata": "^4.0.0",
"google-auth-library": "^6.0.0",
"google-gax": "^2.7.0",
"google-gax": "^2.9.2",
"is": "^3.3.0",
"on-finished": "^2.3.0",
"pumpify": "^2.0.1",
Expand Down
313 changes: 183 additions & 130 deletions src/v2/config_service_v2_client.ts

Large diffs are not rendered by default.

253 changes: 133 additions & 120 deletions src/v2/logging_service_v2_client.ts

Large diffs are not rendered by default.

151 changes: 85 additions & 66 deletions src/v2/metrics_service_v2_client.ts
Expand Up @@ -60,8 +60,10 @@ export class MetricsServiceV2Client {
/**
* Construct an instance of MetricsServiceV2Client.
*
* @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 @@ -81,42 +83,33 @@ export class MetricsServiceV2Client {
* 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 MetricsServiceV2Client;
const servicePath =
opts && opts.servicePath
? opts.servicePath
: opts && opts.apiEndpoint
? opts.apiEndpoint
: staticMembers.servicePath;
const port = opts && opts.port ? opts.port : staticMembers.port;

if (!opts) {
opts = {servicePath, 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 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 MetricsServiceV2Client 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 MetricsServiceV2Client).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 @@ -125,6 +118,11 @@ export class MetricsServiceV2Client {
// 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 @@ -320,6 +318,7 @@ export class MetricsServiceV2Client {

/**
* The DNS address for this API service.
* @returns {string} The DNS address for this service.
*/
static get servicePath() {
return 'logging.googleapis.com';
Expand All @@ -328,13 +327,15 @@ export class MetricsServiceV2Client {
/**
* 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 'logging.googleapis.com';
}

/**
* The port for this API service.
* @returns {number} The default port for this service.
*/
static get port() {
return 443;
Expand All @@ -343,6 +344,7 @@ export class MetricsServiceV2Client {
/**
* The scopes needed to make gRPC calls for every method defined
* in this service.
* @returns {string[]} List of default scopes.
*/
static get scopes() {
return [
Expand All @@ -358,8 +360,7 @@ export class MetricsServiceV2Client {
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 Down Expand Up @@ -414,7 +415,11 @@ export class MetricsServiceV2Client {
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
* The first element of the array is an object representing [LogMetric]{@link google.logging.v2.LogMetric}.
* The promise has a method named "cancel" which cancels the ongoing API call.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
* for more details and examples.
* @example
* const [response] = await client.getLogMetric(request);
*/
getLogMetric(
request: protos.google.logging.v2.IGetLogMetricRequest,
Expand Down Expand Up @@ -501,7 +506,11 @@ export class MetricsServiceV2Client {
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
* The first element of the array is an object representing [LogMetric]{@link google.logging.v2.LogMetric}.
* The promise has a method named "cancel" which cancels the ongoing API call.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
* for more details and examples.
* @example
* const [response] = await client.createLogMetric(request);
*/
createLogMetric(
request: protos.google.logging.v2.ICreateLogMetricRequest,
Expand Down Expand Up @@ -589,7 +598,11 @@ export class MetricsServiceV2Client {
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
* The first element of the array is an object representing [LogMetric]{@link google.logging.v2.LogMetric}.
* The promise has a method named "cancel" which cancels the ongoing API call.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
* for more details and examples.
* @example
* const [response] = await client.updateLogMetric(request);
*/
updateLogMetric(
request: protos.google.logging.v2.IUpdateLogMetricRequest,
Expand Down Expand Up @@ -671,7 +684,11 @@ export class MetricsServiceV2Client {
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
* The first element of the array is an object representing [Empty]{@link google.protobuf.Empty}.
* The promise has a method named "cancel" which cancels the ongoing API call.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
* for more details and examples.
* @example
* const [response] = await client.deleteLogMetric(request);
*/
deleteLogMetric(
request: protos.google.logging.v2.IDeleteLogMetricRequest,
Expand Down Expand Up @@ -763,19 +780,14 @@ export class MetricsServiceV2Client {
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
* The first element of the array is Array of [LogMetric]{@link google.logging.v2.LogMetric}.
* The client library support auto-pagination by default: it will call the API as many
* The client library will perform auto-pagination by default: it will call the API as many
* times as needed and will merge results from all the pages into this array.
*
* When autoPaginate: false is specified through options, the array has three elements.
* The first element is Array of [LogMetric]{@link google.logging.v2.LogMetric} that corresponds to
* the one page received from the API server.
* If the second element is not null it contains the request object of type [ListLogMetricsRequest]{@link google.logging.v2.ListLogMetricsRequest}
* that can be used to obtain the next page of the results.
* If it is null, the next page does not exist.
* The third element contains the raw response received from the API server. Its type is
* [ListLogMetricsResponse]{@link google.logging.v2.ListLogMetricsResponse}.
*
* The promise has a method named "cancel" which cancels the ongoing API call.
* Note that it can affect your quota.
* We recommend using `listLogMetricsAsync()`
* method described below for async iteration which you can stop as needed.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#auto-pagination)
* for more details and examples.
*/
listLogMetrics(
request: protos.google.logging.v2.IListLogMetricsRequest,
Expand Down Expand Up @@ -819,18 +831,7 @@ export class MetricsServiceV2Client {
}

/**
* Equivalent to {@link listLogMetrics}, but returns a NodeJS Stream object.
*
* This fetches the paged responses for {@link listLogMetrics} continuously
* and invokes the callback registered for 'data' event for each element in the
* responses.
*
* The returned object has 'end' method when no more elements are required.
*
* autoPaginate option will be ignored.
*
* @see {@link https://nodejs.org/api/stream.html}
*
* Equivalent to `method.name.toCamelCase()`, but returns a NodeJS Stream object.
* @param {Object} request
* The request object that will be sent.
* @param {string} request.parent
Expand All @@ -850,6 +851,13 @@ export class MetricsServiceV2Client {
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Stream}
* An object stream which emits an object representing [LogMetric]{@link google.logging.v2.LogMetric} on 'data' event.
* The client library will perform auto-pagination by default: it will call the API as many
* times as needed. Note that it can affect your quota.
* We recommend using `listLogMetricsAsync()`
* method described below for async iteration which you can stop as needed.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#auto-pagination)
* for more details and examples.
*/
listLogMetricsStream(
request?: protos.google.logging.v2.IListLogMetricsRequest,
Expand All @@ -874,10 +882,9 @@ export class MetricsServiceV2Client {
}

/**
* Equivalent to {@link listLogMetrics}, but returns an iterable object.
*
* for-await-of syntax is used with the iterable to recursively get response element on-demand.
* Equivalent to `listLogMetrics`, but returns an iterable object.
*
* `for`-`await`-`of` syntax is used with the iterable to get response elements on-demand.
* @param {Object} request
* The request object that will be sent.
* @param {string} request.parent
Expand All @@ -896,7 +903,18 @@ export class MetricsServiceV2Client {
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Object}
* An iterable Object that conforms to @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols.
* An iterable Object that allows [async iteration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols).
* When you iterate the returned iterable, each element will be an object representing
* [LogMetric]{@link google.logging.v2.LogMetric}. The API will be called under the hood as needed, once per the page,
* so you can stop the iteration when you don't need more results.
* Please see the
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#auto-pagination)
* for more details and examples.
* @example
* const iterable = client.listLogMetricsAsync(request);
* for await (const response of iterable) {
* // process response
* }
*/
listLogMetricsAsync(
request?: protos.google.logging.v2.IListLogMetricsRequest,
Expand Down Expand Up @@ -1809,9 +1827,10 @@ export class MetricsServiceV2Client {
}

/**
* 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
Expand Up @@ -3,23 +3,15 @@
{
"git": {
"name": ".",
"remote": "https://github.com/googleapis/nodejs-logging.git",
"sha": "fd8110f1a05ca097fa34986e9ed951cfbda5fb2c"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "3a54e988edcbdef1e47c6ac19d3074a87214f667",
"internalRef": "326582222"
"remote": "git@github.com:googleapis/nodejs-logging.git",
"sha": "e594c9bbd4966ede5ba0872ea715b11c1f3ca967"
}
},
{
"git": {
"name": "synthtool",
"remote": "https://github.com/googleapis/synthtool.git",
"sha": "ba9918cd22874245b55734f57470c719b577e591"
"sha": "1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b"
}
}
],
Expand Down Expand Up @@ -87,6 +79,7 @@
"README.md",
"api-extractor.json",
"linkinator.config.json",
"package-lock.json.1634671361",
"protos/google/logging/v2/log_entry.proto",
"protos/google/logging/v2/logging.proto",
"protos/google/logging/v2/logging_config.proto",
Expand All @@ -96,6 +89,7 @@
"protos/protos.json",
"renovate.json",
"samples/README.md",
"samples/package-lock.json.2353518511",
"src/v2/config_service_v2_client.ts",
"src/v2/config_service_v2_client_config.json",
"src/v2/config_service_v2_proto_list.json",
Expand Down
18 changes: 9 additions & 9 deletions system-test/install.ts
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);
});
});

0 comments on commit fd1e63a

Please sign in to comment.