Skip to content

Commit bf48076

Browse files
fix: do not modify options object, use defaultScopes (#220)
1 parent 3269eaa commit bf48076

File tree

6 files changed

+69
-58
lines changed

6 files changed

+69
-58
lines changed

packages/google-cloud-phishingprotection/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"Phishing Protection API"
2424
],
2525
"dependencies": {
26-
"google-gax": "^2.1.0"
26+
"google-gax": "^2.9.2"
2727
},
2828
"devDependencies": {
2929
"@types/mocha": "^8.0.0",

packages/google-cloud-phishingprotection/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as v1beta1 from './v1beta1';
2020

2121
const PhishingProtectionServiceV1Beta1Client =
2222
v1beta1.PhishingProtectionServiceV1Beta1Client;
23+
type PhishingProtectionServiceV1Beta1Client = v1beta1.PhishingProtectionServiceV1Beta1Client;
2324

2425
export {v1beta1, PhishingProtectionServiceV1Beta1Client};
2526
export default {v1beta1, PhishingProtectionServiceV1Beta1Client};

packages/google-cloud-phishingprotection/src/v1beta1/phishing_protection_service_v1_beta1_client.ts

+41-36
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ export class PhishingProtectionServiceV1Beta1Client {
5151
/**
5252
* Construct an instance of PhishingProtectionServiceV1Beta1Client.
5353
*
54-
* @param {object} [options] - The configuration object. See the subsequent
55-
* parameters for more details.
54+
* @param {object} [options] - The configuration object.
55+
* The options accepted by the constructor are described in detail
56+
* in [this document](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#creating-the-client-instance).
57+
* The common options are:
5658
* @param {object} [options.credentials] - Credentials object.
5759
* @param {string} [options.credentials.client_email]
5860
* @param {string} [options.credentials.private_key]
@@ -72,44 +74,34 @@ export class PhishingProtectionServiceV1Beta1Client {
7274
* your project ID will be detected automatically.
7375
* @param {string} [options.apiEndpoint] - The domain name of the
7476
* API remote host.
77+
* @param {gax.ClientConfig} [options.clientConfig] - client configuration override.
78+
* TODO(@alexander-fenster): link to gax documentation.
79+
* @param {boolean} fallback - Use HTTP fallback mode.
80+
* In fallback mode, a special browser-compatible transport implementation is used
81+
* instead of gRPC transport. In browser context (if the `window` object is defined)
82+
* the fallback mode is enabled automatically; set `options.fallback` to `false`
83+
* if you need to override this behavior.
7584
*/
76-
7785
constructor(opts?: ClientOptions) {
78-
// Ensure that options include the service address and port.
86+
// Ensure that options include all the required fields.
7987
const staticMembers = this
8088
.constructor as typeof PhishingProtectionServiceV1Beta1Client;
8189
const servicePath =
82-
opts && opts.servicePath
83-
? opts.servicePath
84-
: opts && opts.apiEndpoint
85-
? opts.apiEndpoint
86-
: staticMembers.servicePath;
87-
const port = opts && opts.port ? opts.port : staticMembers.port;
88-
89-
if (!opts) {
90-
opts = {servicePath, port};
90+
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
91+
const port = opts?.port || staticMembers.port;
92+
const clientConfig = opts?.clientConfig ?? {};
93+
const fallback = opts?.fallback ?? typeof window !== 'undefined';
94+
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);
95+
96+
// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
97+
if (servicePath !== staticMembers.servicePath && !('scopes' in opts)) {
98+
opts['scopes'] = staticMembers.scopes;
9199
}
92-
opts.servicePath = opts.servicePath || servicePath;
93-
opts.port = opts.port || port;
94-
95-
// users can override the config from client side, like retry codes name.
96-
// The detailed structure of the clientConfig can be found here: https://github.com/googleapis/gax-nodejs/blob/master/src/gax.ts#L546
97-
// The way to override client config for Showcase API:
98-
//
99-
// const customConfig = {"interfaces": {"google.showcase.v1beta1.Echo": {"methods": {"Echo": {"retry_codes_name": "idempotent", "retry_params_name": "default"}}}}}
100-
// const showcaseClient = new showcaseClient({ projectId, customConfig });
101-
opts.clientConfig = opts.clientConfig || {};
102-
103-
// If we're running in browser, it's OK to omit `fallback` since
104-
// google-gax has `browser` field in its `package.json`.
105-
// For Electron (which does not respect `browser` field),
106-
// pass `{fallback: true}` to the PhishingProtectionServiceV1Beta1Client constructor.
100+
101+
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
107102
this._gaxModule = opts.fallback ? gax.fallback : gax;
108103

109-
// Create a `gaxGrpc` object, with any grpc-specific options
110-
// sent to the client.
111-
opts.scopes = (this
112-
.constructor as typeof PhishingProtectionServiceV1Beta1Client).scopes;
104+
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
113105
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
114106

115107
// Save options to use in initialize() method.
@@ -118,6 +110,11 @@ export class PhishingProtectionServiceV1Beta1Client {
118110
// Save the auth object to the client, for use by other methods.
119111
this.auth = this._gaxGrpc.auth as gax.GoogleAuth;
120112

113+
// Set the default scopes in auth client if needed.
114+
if (servicePath === staticMembers.servicePath) {
115+
this.auth.defaultScopes = staticMembers.scopes;
116+
}
117+
121118
// Determine the client header string.
122119
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
123120
if (typeof process !== 'undefined' && 'versions' in process) {
@@ -234,6 +231,7 @@ export class PhishingProtectionServiceV1Beta1Client {
234231

235232
/**
236233
* The DNS address for this API service.
234+
* @returns {string} The DNS address for this service.
237235
*/
238236
static get servicePath() {
239237
return 'phishingprotection.googleapis.com';
@@ -242,13 +240,15 @@ export class PhishingProtectionServiceV1Beta1Client {
242240
/**
243241
* The DNS address for this API service - same as servicePath(),
244242
* exists for compatibility reasons.
243+
* @returns {string} The DNS address for this service.
245244
*/
246245
static get apiEndpoint() {
247246
return 'phishingprotection.googleapis.com';
248247
}
249248

250249
/**
251250
* The port for this API service.
251+
* @returns {number} The default port for this service.
252252
*/
253253
static get port() {
254254
return 443;
@@ -257,6 +257,7 @@ export class PhishingProtectionServiceV1Beta1Client {
257257
/**
258258
* The scopes needed to make gRPC calls for every method defined
259259
* in this service.
260+
* @returns {string[]} List of default scopes.
260261
*/
261262
static get scopes() {
262263
return ['https://www.googleapis.com/auth/cloud-platform'];
@@ -266,8 +267,7 @@ export class PhishingProtectionServiceV1Beta1Client {
266267
getProjectId(callback: Callback<string, undefined, undefined>): void;
267268
/**
268269
* Return the project ID used by this class.
269-
* @param {function(Error, string)} callback - the callback to
270-
* be called with the current project Id.
270+
* @returns {Promise} A promise that resolves to string containing the project ID.
271271
*/
272272
getProjectId(
273273
callback?: Callback<string, undefined, undefined>
@@ -336,7 +336,11 @@ export class PhishingProtectionServiceV1Beta1Client {
336336
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
337337
* @returns {Promise} - The promise which resolves to an array.
338338
* The first element of the array is an object representing [ReportPhishingResponse]{@link google.cloud.phishingprotection.v1beta1.ReportPhishingResponse}.
339-
* The promise has a method named "cancel" which cancels the ongoing API call.
339+
* Please see the
340+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
341+
* for more details and examples.
342+
* @example
343+
* const [response] = await client.reportPhishing(request);
340344
*/
341345
reportPhishing(
342346
request: protos.google.cloud.phishingprotection.v1beta1.IReportPhishingRequest,
@@ -414,9 +418,10 @@ export class PhishingProtectionServiceV1Beta1Client {
414418
}
415419

416420
/**
417-
* Terminate the GRPC channel and close the client.
421+
* Terminate the gRPC channel and close the client.
418422
*
419423
* The client will no longer be usable and all future behavior is undefined.
424+
* @returns {Promise} A promise that resolves when the client is closed.
420425
*/
421426
close(): Promise<void> {
422427
this.initialize();

packages/google-cloud-phishingprotection/synth.metadata

+5-11
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33
{
44
"git": {
55
"name": ".",
6-
"remote": "https://github.com/googleapis/nodejs-phishing-protection.git",
7-
"sha": "831b9efca4ee05ee20ff040a96aaef6a42300eb0"
8-
}
9-
},
10-
{
11-
"git": {
12-
"name": "googleapis",
13-
"remote": "https://github.com/googleapis/googleapis.git",
14-
"sha": "20b11dfe4538cd5da7b4c3dd7d2bf5b9922ff3ed",
15-
"internalRef": "338646463"
6+
"remote": "[email protected]:googleapis/nodejs-phishing-protection.git",
7+
"sha": "bfdb4795cb70a251324d382948facf207fd53007"
168
}
179
},
1810
{
1911
"git": {
2012
"name": "synthtool",
2113
"remote": "https://github.com/googleapis/synthtool.git",
22-
"sha": "ba9918cd22874245b55734f57470c719b577e591"
14+
"sha": "1f1148d3c7a7a52f0c98077f976bd9b3c948ee2b"
2315
}
2416
}
2517
],
@@ -87,12 +79,14 @@
8779
"README.md",
8880
"api-extractor.json",
8981
"linkinator.config.json",
82+
"package-lock.json.167914005",
9083
"protos/google/cloud/phishingprotection/v1beta1/phishingprotection.proto",
9184
"protos/protos.d.ts",
9285
"protos/protos.js",
9386
"protos/protos.json",
9487
"renovate.json",
9588
"samples/README.md",
89+
"samples/package-lock.json.3958293626",
9690
"src/index.ts",
9791
"src/v1beta1/index.ts",
9892
"src/v1beta1/phishing_protection_service_v1_beta1_client.ts",

packages/google-cloud-phishingprotection/system-test/fixtures/sample/src/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@
1818

1919
import {PhishingProtectionServiceV1Beta1Client} from '@google-cloud/phishing-protection';
2020

21+
// check that the client class type name can be used
22+
function doStuffWithPhishingProtectionServiceV1Beta1Client(
23+
client: PhishingProtectionServiceV1Beta1Client
24+
) {
25+
client.close();
26+
}
27+
2128
function main() {
22-
new PhishingProtectionServiceV1Beta1Client();
29+
// check that the client instance can be created
30+
const phishingProtectionServiceV1Beta1Client = new PhishingProtectionServiceV1Beta1Client();
31+
doStuffWithPhishingProtectionServiceV1Beta1Client(
32+
phishingProtectionServiceV1Beta1Client
33+
);
2334
}
2435

2536
main();

packages/google-cloud-phishingprotection/system-test/install.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@ import {packNTest} from 'pack-n-play';
2020
import {readFileSync} from 'fs';
2121
import {describe, it} from 'mocha';
2222

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

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

0 commit comments

Comments
 (0)