Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
fix(browser): check for fetch on window
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation authored Nov 23, 2020
1 parent ca6bb7d commit a0925b3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 36 deletions.
50 changes: 46 additions & 4 deletions protos/protos.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 22 additions & 14 deletions src/v1/text_to_speech_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
// ** https://github.com/googleapis/gapic-generator-typescript **
// ** All changes to this file may be overwritten. **

/* global window */
import * as gax from 'google-gax';
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
import * as path from 'path';

import * as protos from '../../protos/protos';
/**
* Client JSON configuration object, loaded from
* `src/v1/text_to_speech_client_config.json`.
* This file defines retry strategy and timeouts for all API methods in this library.
*/
import * as gapicConfig from './text_to_speech_client_config.json';

const version = require('../../../package.json').version;
Expand Down Expand Up @@ -73,9 +79,9 @@ export class TextToSpeechClient {
* your project ID will be detected automatically.
* @param {string} [options.apiEndpoint] - The domain name of the
* API remote host.
* @param {gax.ClientConfig} [options.clientConfig] - client configuration override.
* TODO(@alexander-fenster): link to gax documentation.
* @param {boolean} fallback - Use HTTP fallback mode.
* @param {gax.ClientConfig} [options.clientConfig] - Client configuration override.
* Follows the structure of {@link gapicConfig}.
* @param {boolean} [options.fallback] - Use HTTP fallback mode.
* In fallback mode, a special browser-compatible transport implementation is used
* instead of gRPC transport. In browser context (if the `window` object is defined)
* the fallback mode is enabled automatically; set `options.fallback` to `false`
Expand All @@ -88,7 +94,9 @@ export class TextToSpeechClient {
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
const port = opts?.port || staticMembers.port;
const clientConfig = opts?.clientConfig ?? {};
const fallback = opts?.fallback ?? typeof window !== 'undefined';
const fallback =
opts?.fallback ??
(typeof window !== 'undefined' && typeof window?.fetch === 'function');
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);

// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
Expand Down Expand Up @@ -272,7 +280,7 @@ export class TextToSpeechClient {
// -------------------
listVoices(
request: protos.google.cloud.texttospeech.v1.IListVoicesRequest,
options?: gax.CallOptions
options?: CallOptions
): Promise<
[
protos.google.cloud.texttospeech.v1.IListVoicesResponse,
Expand All @@ -282,7 +290,7 @@ export class TextToSpeechClient {
>;
listVoices(
request: protos.google.cloud.texttospeech.v1.IListVoicesRequest,
options: gax.CallOptions,
options: CallOptions,
callback: Callback<
protos.google.cloud.texttospeech.v1.IListVoicesResponse,
protos.google.cloud.texttospeech.v1.IListVoicesRequest | null | undefined,
Expand Down Expand Up @@ -324,7 +332,7 @@ export class TextToSpeechClient {
listVoices(
request: protos.google.cloud.texttospeech.v1.IListVoicesRequest,
optionsOrCallback?:
| gax.CallOptions
| CallOptions
| Callback<
protos.google.cloud.texttospeech.v1.IListVoicesResponse,
| protos.google.cloud.texttospeech.v1.IListVoicesRequest
Expand All @@ -345,20 +353,20 @@ export class TextToSpeechClient {
]
> | void {
request = request || {};
let options: gax.CallOptions;
let options: CallOptions;
if (typeof optionsOrCallback === 'function' && callback === undefined) {
callback = optionsOrCallback;
options = {};
} else {
options = optionsOrCallback as gax.CallOptions;
options = optionsOrCallback as CallOptions;
}
options = options || {};
this.initialize();
return this.innerApiCalls.listVoices(request, options, callback);
}
synthesizeSpeech(
request: protos.google.cloud.texttospeech.v1.ISynthesizeSpeechRequest,
options?: gax.CallOptions
options?: CallOptions
): Promise<
[
protos.google.cloud.texttospeech.v1.ISynthesizeSpeechResponse,
Expand All @@ -368,7 +376,7 @@ export class TextToSpeechClient {
>;
synthesizeSpeech(
request: protos.google.cloud.texttospeech.v1.ISynthesizeSpeechRequest,
options: gax.CallOptions,
options: CallOptions,
callback: Callback<
protos.google.cloud.texttospeech.v1.ISynthesizeSpeechResponse,
| protos.google.cloud.texttospeech.v1.ISynthesizeSpeechRequest
Expand Down Expand Up @@ -412,7 +420,7 @@ export class TextToSpeechClient {
synthesizeSpeech(
request: protos.google.cloud.texttospeech.v1.ISynthesizeSpeechRequest,
optionsOrCallback?:
| gax.CallOptions
| CallOptions
| Callback<
protos.google.cloud.texttospeech.v1.ISynthesizeSpeechResponse,
| protos.google.cloud.texttospeech.v1.ISynthesizeSpeechRequest
Expand All @@ -435,12 +443,12 @@ export class TextToSpeechClient {
]
> | void {
request = request || {};
let options: gax.CallOptions;
let options: CallOptions;
if (typeof optionsOrCallback === 'function' && callback === undefined) {
callback = optionsOrCallback;
options = {};
} else {
options = optionsOrCallback as gax.CallOptions;
options = optionsOrCallback as CallOptions;
}
options = options || {};
this.initialize();
Expand Down
36 changes: 22 additions & 14 deletions src/v1beta1/text_to_speech_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
// ** https://github.com/googleapis/gapic-generator-typescript **
// ** All changes to this file may be overwritten. **

/* global window */
import * as gax from 'google-gax';
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
import * as path from 'path';

import * as protos from '../../protos/protos';
/**
* Client JSON configuration object, loaded from
* `src/v1beta1/text_to_speech_client_config.json`.
* This file defines retry strategy and timeouts for all API methods in this library.
*/
import * as gapicConfig from './text_to_speech_client_config.json';

const version = require('../../../package.json').version;
Expand Down Expand Up @@ -73,9 +79,9 @@ export class TextToSpeechClient {
* your project ID will be detected automatically.
* @param {string} [options.apiEndpoint] - The domain name of the
* API remote host.
* @param {gax.ClientConfig} [options.clientConfig] - client configuration override.
* TODO(@alexander-fenster): link to gax documentation.
* @param {boolean} fallback - Use HTTP fallback mode.
* @param {gax.ClientConfig} [options.clientConfig] - Client configuration override.
* Follows the structure of {@link gapicConfig}.
* @param {boolean} [options.fallback] - Use HTTP fallback mode.
* In fallback mode, a special browser-compatible transport implementation is used
* instead of gRPC transport. In browser context (if the `window` object is defined)
* the fallback mode is enabled automatically; set `options.fallback` to `false`
Expand All @@ -88,7 +94,9 @@ export class TextToSpeechClient {
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
const port = opts?.port || staticMembers.port;
const clientConfig = opts?.clientConfig ?? {};
const fallback = opts?.fallback ?? typeof window !== 'undefined';
const fallback =
opts?.fallback ??
(typeof window !== 'undefined' && typeof window?.fetch === 'function');
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);

// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
Expand Down Expand Up @@ -272,7 +280,7 @@ export class TextToSpeechClient {
// -------------------
listVoices(
request: protos.google.cloud.texttospeech.v1beta1.IListVoicesRequest,
options?: gax.CallOptions
options?: CallOptions
): Promise<
[
protos.google.cloud.texttospeech.v1beta1.IListVoicesResponse,
Expand All @@ -282,7 +290,7 @@ export class TextToSpeechClient {
>;
listVoices(
request: protos.google.cloud.texttospeech.v1beta1.IListVoicesRequest,
options: gax.CallOptions,
options: CallOptions,
callback: Callback<
protos.google.cloud.texttospeech.v1beta1.IListVoicesResponse,
| protos.google.cloud.texttospeech.v1beta1.IListVoicesRequest
Expand Down Expand Up @@ -328,7 +336,7 @@ export class TextToSpeechClient {
listVoices(
request: protos.google.cloud.texttospeech.v1beta1.IListVoicesRequest,
optionsOrCallback?:
| gax.CallOptions
| CallOptions
| Callback<
protos.google.cloud.texttospeech.v1beta1.IListVoicesResponse,
| protos.google.cloud.texttospeech.v1beta1.IListVoicesRequest
Expand All @@ -351,20 +359,20 @@ export class TextToSpeechClient {
]
> | void {
request = request || {};
let options: gax.CallOptions;
let options: CallOptions;
if (typeof optionsOrCallback === 'function' && callback === undefined) {
callback = optionsOrCallback;
options = {};
} else {
options = optionsOrCallback as gax.CallOptions;
options = optionsOrCallback as CallOptions;
}
options = options || {};
this.initialize();
return this.innerApiCalls.listVoices(request, options, callback);
}
synthesizeSpeech(
request: protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechRequest,
options?: gax.CallOptions
options?: CallOptions
): Promise<
[
protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechResponse,
Expand All @@ -377,7 +385,7 @@ export class TextToSpeechClient {
>;
synthesizeSpeech(
request: protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechRequest,
options: gax.CallOptions,
options: CallOptions,
callback: Callback<
protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechResponse,
| protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechRequest
Expand Down Expand Up @@ -423,7 +431,7 @@ export class TextToSpeechClient {
synthesizeSpeech(
request: protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechRequest,
optionsOrCallback?:
| gax.CallOptions
| CallOptions
| Callback<
protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechResponse,
| protos.google.cloud.texttospeech.v1beta1.ISynthesizeSpeechRequest
Expand All @@ -449,12 +457,12 @@ export class TextToSpeechClient {
]
> | void {
request = request || {};
let options: gax.CallOptions;
let options: CallOptions;
if (typeof optionsOrCallback === 'function' && callback === undefined) {
callback = optionsOrCallback;
options = {};
} else {
options = optionsOrCallback as gax.CallOptions;
options = optionsOrCallback as CallOptions;
}
options = options || {};
this.initialize();
Expand Down
14 changes: 10 additions & 4 deletions synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
{
"git": {
"name": ".",
"remote": "git@github.com:googleapis/nodejs-text-to-speech.git",
"sha": "152ef92cbedac010a0642c24da54a9b4850829ac"
"remote": "https://github.com/googleapis/nodejs-text-to-speech.git",
"sha": "ca6bb7d05cc451c896d1df7c6397d34286a17082"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "2f019bf70bfe06f1e2af1b04011b0a2405190e43",
"internalRef": "343202295"
}
},
{
Expand Down Expand Up @@ -88,15 +96,13 @@
"README.md",
"api-extractor.json",
"linkinator.config.json",
"package-lock.json.2478872801",
"protos/google/cloud/texttospeech/v1/cloud_tts.proto",
"protos/google/cloud/texttospeech/v1beta1/cloud_tts.proto",
"protos/protos.d.ts",
"protos/protos.js",
"protos/protos.json",
"renovate.json",
"samples/README.md",
"samples/package-lock.json.2811680083",
"src/index.ts",
"src/v1/index.ts",
"src/v1/text_to_speech_client.ts",
Expand Down

0 comments on commit a0925b3

Please sign in to comment.