Skip to content

Commit 6b8f2ba

Browse files
authored
Merge pull request #82 from watson-developer-cloud/allow-user-url
feat: add option for user-provided URL in `synthesize`
2 parents c68af17 + 42d9f1b commit 6b8f2ba

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ where the max length is around 1000 characters after the token is accounted for.
7070

7171
Options:
7272
* text - the text to speak
73+
* url - the Watson Text to Speech API URL (defaults to https://stream.watsonplatform.net/text-to-speech/api)
7374
* voice - the desired playback voice's name - see .getVoices(). Note that the voices are language-specific.
7475
* customization_id - GUID of a custom voice model - omit to use the voice with no customization.
7576
* autoPlay - set to false to prevent the audio from automatically playing

Diff for: speech-to-text/get-models.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
3838
* @todo define format in @return statement
3939
* @param {Object} options
40+
* @param {String} options.url=https://stream.watsonplatform.net/speech-to-text/api URL for Watson Speech to Text API
4041
* @param {String} options.token auth token for CF services
4142
* @param {String} options.access_token IAM access token for RC services
4243
* @return {Promise.<T>}
@@ -51,11 +52,11 @@ module.exports = function getModels(options) {
5152
accept: 'application/json'
5253
}
5354
};
54-
var url;
55+
var url = options.url || 'https://stream.watsonplatform.net/speech-to-text/api';
5556
if (options.access_token) {
56-
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/models?access_token=' + options.access_token;
57+
url = `${url}/v1/models?access_token=${options.access_token}`;
5758
} else {
58-
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/models?watson-token=' + options.token;
59+
url = `${url}/v1/models?access_token=${options.token}`;
5960
}
6061
return fetch(url, reqOpts)
6162
.then(function(response) {

Diff for: text-to-speech/get-voices.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
3939
* @todo define format in @return statement
4040
* @param {Object} options
41+
* @param {String} options.url=https://stream.watsonplatform.net/text-to-speech/api URL for Watson Text to Speech API
4142
* @param {String} options.token auth token for CF services
4243
* @param {String} options.access_token IAM access token for RC services
4344
* @return {Promise.<T>}
@@ -52,11 +53,11 @@ module.exports = function getVoices(options) {
5253
accept: 'application/json'
5354
}
5455
};
55-
var url;
56+
var url = options.url || 'https://stream.watsonplatform.net/text-to-speech/api';
5657
if (options.access_token) {
57-
url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/voices?watson-token=' + options.access_token;
58+
url = `${url}'/v1/voices?watson-token='${options.access_token}`;
5859
} else {
59-
url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/voices?watson-token=' + options.token;
60+
url = `${url}'/v1/voices?watson-token='${options.token}`;
6061
}
6162
return fetch(url, reqOpts)
6263
.then(function(response) {

Diff for: text-to-speech/synthesize.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var QUERY_PARAMS_ALLOWED = ['voice', 'X-WDC-PL-OPT-OUT', 'X-Watson-Learning-Opt-
2929
* Creates and returns a HTML5 `<audio>` element
3030
*
3131
* @param {Object} options
32+
* @param {String} options.url=https://stream.watsonplatform.net/text-to-speech/api URL for Watson Text to Speech API
3233
* @param {String} [options.token] - Auth token for CF services
3334
* @param {String} options.access_token - IAM Access Token for RC services
3435
* @param {String} options.text text to speak
@@ -47,9 +48,10 @@ module.exports = function synthesize(options) {
4748
}
4849
options['watson-token'] = options.token;
4950
delete options.token;
51+
var url = options.url || 'https://stream.watsonplatform.net/text-to-speech/api';
5052
var audio = options.element || new Audio();
5153
audio.crossOrigin = 'anonymous';
52-
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(pick(options, QUERY_PARAMS_ALLOWED));
54+
audio.src = `${url}/v1/synthesize?${qs.stringify(pick(options, QUERY_PARAMS_ALLOWED))}`;
5355
if (options.autoPlay !== false) {
5456
audio.play();
5557
}

0 commit comments

Comments
 (0)