Skip to content

Commit

Permalink
feat: support apiEndpoint override (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and stephenplusplus committed May 31, 2019
1 parent c83fc48 commit 437cca6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/google-cloud-translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 9 additions & 3 deletions packages/google-cloud-translate/src/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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'),
Expand Down
10 changes: 10 additions & 0 deletions packages/google-cloud-translate/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 437cca6

Please sign in to comment.