Skip to content

Commit da82b12

Browse files
language: add note about the version & code style
1 parent fa8b52c commit da82b12

2 files changed

Lines changed: 63 additions & 26 deletions

File tree

packages/language/src/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var is = require('is');
2626
var v1 = require('./v1');
2727
var v1beta2 = require('./v1beta2');
2828

29-
var _GAPICS = {
29+
var GAPIC_APIS = {
3030
v1: v1,
3131
v1beta2: v1beta2
3232
};
@@ -56,8 +56,10 @@ var Document = require('./document.js');
5656
* @resource [Cloud Natural Language API Documentation]{@link https://cloud.google.com/natural-language/docs}
5757
*
5858
* @param {object} options - [Configuration object](#/docs).
59-
* @param {string} options.apiVersion - API version. Defaults to "v1";
60-
* the only other valid value is "v1beta2".
59+
* @param {string} options.apiVersion - Either `v1` or `v1beta2`. `v1beta2` is a
60+
* *newer* release than `v1`, and still in beta. By choosing it, you are
61+
* opting into new, back-end behavior which is not officially supported by
62+
* the design of this API. (Default: `v1`)
6163
*/
6264
function Language(options) {
6365
if (!(this instanceof Language)) {
@@ -70,14 +72,12 @@ function Language(options) {
7072
libVersion: require('../package.json').version
7173
});
7274

73-
// Determine the version to be used.
74-
// We default to "v1", but use "v1beta2" if explicitly requested. Both
75-
// GAPICs are packaged with the library.
76-
var apiVersion = options.apiVersion || 'v1';
77-
var gapic = _GAPICS[apiVersion];
78-
if (is.undefined(gapic)) {
79-
throw new Error('Invalid api_version: use "v1" or "v1beta2".');
75+
var gapic = GAPIC_APIS[options.apiVersion || 'v1'];
76+
77+
if (!gapic) {
78+
throw new Error('Invalid `apiVersion` specified. Use "v1" or "v1beta2".');
8079
}
80+
8181
delete options.apiVersion;
8282

8383
this.api = {

packages/language/test/index.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ function fakeV1() {
4848
};
4949
}
5050

51+
var fakeV1Beta2Override;
52+
function fakeV1Beta2() {
53+
if (fakeV1Beta2Override) {
54+
return fakeV1Beta2Override.apply(null, arguments);
55+
}
56+
57+
return {
58+
languageServiceClient: util.noop
59+
};
60+
}
61+
5162
describe('Language', function() {
5263
var Language;
5364
var language;
@@ -60,12 +71,14 @@ describe('Language', function() {
6071
util: fakeUtil
6172
},
6273
'./document.js': FakeDocument,
63-
'./v1': fakeV1
74+
'./v1': fakeV1,
75+
'./v1beta2': fakeV1Beta2
6476
});
6577
});
6678

6779
beforeEach(function() {
6880
fakeV1Override = null;
81+
fakeV1Beta2Override = null;
6982
language = new Language(OPTIONS);
7083
});
7184

@@ -74,21 +87,6 @@ describe('Language', function() {
7487
assert(promisified);
7588
});
7689

77-
it('should accept an apiVersion "v1beta2"', function() {
78-
var RealLanguage = require('../src/index.js');
79-
80-
// The v1beta2 GAPIC adds a new `analyzeEntitySentiment` function.
81-
var langBeta = new RealLanguage({apiVersion: 'v1beta2'});
82-
assert.strictEqual(typeof langBeta.api.Language.analyzeEntitySentiment,
83-
'function');
84-
});
85-
86-
it('should not accept an unrecognized version', function() {
87-
assert.throws(function() {
88-
new Language({apiVersion: 'v1beta42'});
89-
});
90-
});
91-
9290
it('should normalize the arguments', function() {
9391
var options = {
9492
projectId: 'project-id',
@@ -138,6 +136,45 @@ describe('Language', function() {
138136
Language: expectedLanguageService
139137
});
140138
});
139+
140+
it('should create a gax api client for v1beta2', function() {
141+
var expectedLanguageService = {};
142+
143+
fakeV1Override = function() {
144+
throw new Error('v1 should not be initialized.');
145+
};
146+
147+
fakeV1Beta2Override = function(options) {
148+
var expected = {
149+
libName: 'gccl',
150+
libVersion: require('../package.json').version
151+
};
152+
assert.deepStrictEqual(options, expected);
153+
154+
return {
155+
languageServiceClient: function(options) {
156+
assert.deepStrictEqual(options, expected);
157+
return expectedLanguageService;
158+
}
159+
};
160+
};
161+
162+
var language = new Language({
163+
apiVersion: 'v1beta2'
164+
});
165+
166+
assert.deepEqual(language.api, {
167+
Language: expectedLanguageService
168+
});
169+
});
170+
171+
it('should not accept an unrecognized version', function() {
172+
assert.throws(function() {
173+
new Language({
174+
apiVersion: 'v1beta42'
175+
});
176+
}, new RegExp('Invalid `apiVersion` specified. Use "v1" or "v1beta2".'));
177+
});
141178
});
142179

143180
describe('annotate', function() {

0 commit comments

Comments
 (0)