diff --git a/package.json b/package.json index f3320290..0386b4a0 100644 --- a/package.json +++ b/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/src/v2/index.ts b/src/v2/index.ts index 1f90e951..169f9bc5 100644 --- a/src/v2/index.ts +++ b/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/test/index.ts b/test/index.ts index f8c1dd78..2b4141ac 100644 --- a/test/index.ts +++ b/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',