From 437cca687e807b3283f52098ac0ae7811e4ca7ea Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Fri, 31 May 2019 13:06:41 +0000 Subject: [PATCH] feat: support apiEndpoint override (#285) --- packages/google-cloud-translate/package.json | 4 ++-- packages/google-cloud-translate/src/v2/index.ts | 12 +++++++++--- packages/google-cloud-translate/test/index.ts | 10 ++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/google-cloud-translate/package.json b/packages/google-cloud-translate/package.json index f3320290acc..0386b4a048d 100644 --- a/packages/google-cloud-translate/package.json +++ b/packages/google-cloud-translate/package.json @@ -46,7 +46,7 @@ "predocs-test": "npm run docs" }, "dependencies": { - "@google-cloud/common": "^1.0.0", + "@google-cloud/common": "^2.0.0", "@google-cloud/promisify": "^1.0.0", "arrify": "^2.0.0", "extend": "^3.0.1", @@ -74,7 +74,7 @@ "jsdoc": "^3.6.2", "jsdoc-baseline": "^0.1.0", "linkinator": "^1.1.2", - "mocha": "^6.0.0", + "mocha": "^6.1.4", "nyc": "^14.0.0", "power-assert": "^1.6.0", "prettier": "^1.13.5", diff --git a/packages/google-cloud-translate/src/v2/index.ts b/packages/google-cloud-translate/src/v2/index.ts index 1f90e951c81..169f9bc55cb 100644 --- a/packages/google-cloud-translate/src/v2/index.ts +++ b/packages/google-cloud-translate/src/v2/index.ts @@ -59,6 +59,11 @@ export interface TranslateConfig extends GoogleAuthOptions { key?: string; autoRetry?: boolean; maxRetries?: number; + /** + * The API endpoint of the service used to make requests. + * Defaults to `translation.googleapis.com`. + */ + apiEndpoint?: string; } /** @@ -120,14 +125,15 @@ export interface TranslateConfig extends GoogleAuthOptions { export class Translate extends Service { options: TranslateConfig; key?: string; - constructor(options?: TranslateConfig) { - let baseUrl = 'https://translation.googleapis.com/language/translate/v2'; - + constructor(options: TranslateConfig = {}) { + options.apiEndpoint = options.apiEndpoint || 'translation.googleapis.com'; + let baseUrl = `https://${options.apiEndpoint}/language/translate/v2`; if (process.env.GOOGLE_CLOUD_TRANSLATE_ENDPOINT) { baseUrl = process.env.GOOGLE_CLOUD_TRANSLATE_ENDPOINT.replace(/\/+$/, ''); } const config = { + apiEndpoint: options.apiEndpoint, baseUrl, scopes: ['https://www.googleapis.com/auth/cloud-platform'], packageJson: require('../../../package.json'), diff --git a/packages/google-cloud-translate/test/index.ts b/packages/google-cloud-translate/test/index.ts index f8c1dd78a06..2b4141ac0fd 100644 --- a/packages/google-cloud-translate/test/index.ts +++ b/packages/google-cloud-translate/test/index.ts @@ -110,6 +110,16 @@ describe('Translate v2', () => { assert.strictEqual(calledWith.projectIdRequired, false); }); + it('should allow apiEndpoint override', () => { + const apiEndpoint = 'fake.endpoint'; + translate = new Translate({ + projectId: 'test-project', + apiEndpoint, + }); + const calledWith = translate.calledWith_[0]; + assert.strictEqual(calledWith.apiEndpoint, apiEndpoint); + }); + describe('Using an API Key', () => { const KEY_OPTIONS = { key: 'api-key',