This repository was archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathindex.ts
More file actions
603 lines (562 loc) · 19.3 KB
/
index.ts
File metadata and controls
603 lines (562 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Service, util} from '@google-cloud/common';
import {promisifyAll} from '@google-cloud/promisify';
import * as arrify from 'arrify';
import * as extend from 'extend';
import {GoogleAuthOptions} from 'google-auth-library';
import * as is from 'is';
const isHtml = require('is-html');
import {DecorateRequestOptions, BodyResponseCallback} from '@google-cloud/common/build/src/util';
import * as r from 'request';
import {teenyRequest} from 'teeny-request';
const PKG = require('../../package.json');
/**
* Translate request options.
*
* @typedef {object} TranslateRequest
* @property {string} [format] Set the text's format as `html` or `text`.
* If not provided, we will try to auto-detect if the text given is HTML.
* If not, we set the format as `text`.
* @property {string} [from] The ISO 639-1 language code the source input
* is written in.
* @property {string} [model] Set the model type requested for this
* translation. Please refer to the upstream documentation for possible
* values.
* @property {string} to The ISO 639-1 language code to translate the
* input to.
*/
export interface TranslateRequest {
format?: string;
from?: string;
model?: string;
to?: string;
}
/**
* @callback TranslateCallback
* @param {?Error} err Request error, if any.
* @param {object|object[]} translations If a single string input was given, a
* single translation is given. Otherwise, it is an array of translations.
* @param {object} apiResponse The full API response.
*/
export interface TranslateCallback<T extends string|string[]> {
(err: Error|null, translations?: T|null, apiResponse?: r.Response): void;
}
/**
* @typedef {object} DetectResult
* @property {string} 0.language The language code matched from the input.
* @property {number} [0.confidence] A float 0 - 1. The higher the number, the
* higher the confidence in language detection. Note, this is not always
* returned from the API.
* @property {object} 1 The full API response.
*/
export interface DetectResult {
language: string;
confidence: number;
input: string;
}
/**
* @callback DetectCallback
* @param {?Error} err Request error, if any.
* @param {object|object[]} results The detection results.
* @param {string} results.language The language code matched from the input.
* @param {number} [results.confidence] A float 0 - 1. The higher the number, the
* higher the confidence in language detection. Note, this is not always
* returned from the API.
* @param {object} apiResponse The full API response.
*/
export interface DetectCallback<T extends DetectResult|DetectResult[]> {
(err: Error|null, results?: T|null, apiResponse?: r.Response): void;
}
/**
* @typedef {object} LanguageResult
* @property {string} code The [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1)
* language code.
* @property {string} name The language name. This can be translated into your
* preferred language with the `target` option.
*/
export interface LanguageResult {
code: string;
name: string;
}
/**
* @callback GetLanguagesCallback
* @param {?Error} err Request error, if any.
* @param {object[]} results The languages supported by the API.
* @param {string} results.code The [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1)
* language code.
* @param {string} results.name The language name. This can be translated into your
* preferred language with the `target` option.
* @param {object} apiResponse The full API response.
*/
export interface GetLanguagesCallback {
(err: Error|null, results?: LanguageResult[]|null,
apiResponse?: r.Response): void;
}
/**
* @typedef {object} ClientConfig
* @property {string} [projectId] The project ID from the Google Developer's
* Console, e.g. 'grape-spaceship-123'. We will also check the environment
* variable `GCLOUD_PROJECT` for your project ID. If your app is running in
* an environment which supports {@link
* https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
* Application Default Credentials}, your project ID will be detected
* automatically.
* @property {string} [key] An API key. You should prefer using a Service
* Account key file instead of an API key.
* @property {string} [keyFilename] Full path to the a .json, .pem, or .p12 key
* downloaded from the Google Developers Console. If you provide a path to a
* JSON file, the `projectId` option above is not necessary. NOTE: .pem and
* .p12 require you to specify the `email` option as well.
* @property {string} [email] Account email address. Required when using a .pem
* or .p12 keyFilename.
* @property {object} [credentials] Credentials object.
* @property {string} [credentials.client_email]
* @property {string} [credentials.private_key]
* @property {boolean} [autoRetry=true] Automatically retry requests if the
* response is related to rate limits or certain intermittent server errors.
* We will exponentially backoff subsequent requests by default.
* @property {number} [maxRetries=3] Maximum number of automatic retries
* attempted before returning the error.
* @property {Constructor} [promise] Custom promise module to use instead of
* native Promises.
*/
export interface TranslateConfig extends GoogleAuthOptions {
key?: string;
autoRetry?: boolean;
maxRetries?: number;
request?: typeof r;
}
/**
* With [Google Translate](https://cloud.google.com/translate), you can
* dynamically translate text between thousands of language pairs.
*
* The Google Cloud Translation API lets websites and programs integrate with Google
* Translate programmatically.
*
* @class
*
* @see [Getting Started]{@link https://cloud.google.com/translate/v2/getting_started}
* @see [Identifying your application to Google]{@link https://cloud.google.com/translate/v2/using_rest#auth}
*
* @param {ClientConfig} [options] Configuration options.
*
* @example
* //-
* // <h3>Custom Translation API</h3>
* //
* // The environment variable, `GOOGLE_CLOUD_TRANSLATE_ENDPOINT`, is honored as
* // a custom backend which our library will send requests to.
* //-
*
* @example <caption>include:samples/quickstart.js</caption>
* region_tag:translate_quickstart
* Full quickstart example:
*/
class Translate extends Service {
options: TranslateConfig;
key?: string;
constructor(options?: TranslateConfig) {
let baseUrl = 'https://translation.googleapis.com/language/translate/v2';
if (process.env.GOOGLE_CLOUD_TRANSLATE_ENDPOINT) {
baseUrl = process.env.GOOGLE_CLOUD_TRANSLATE_ENDPOINT.replace(/\/+$/, '');
}
const config = {
baseUrl,
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
packageJson: require('../../package.json'),
projectIdRequired: false,
requestModule: teenyRequest as typeof r,
};
super(config, options);
this.options = options || {};
this.options.request = config.requestModule;
if (this.options.key) {
this.key = this.options.key;
}
}
detect(input: string): Promise<[DetectResult, r.Response]>;
detect(input: string[]): Promise<[DetectResult[], r.Response]>;
detect(input: string, callback: DetectCallback<DetectResult>): void;
detect(input: string[], callback: DetectCallback<DetectResult[]>): void;
/**
* Detect the language used in a string or multiple strings.
*
* @see [Detect Language]{@link https://cloud.google.com/translate/v2/using_rest#detect-language}
*
* @param {string|string[]} input - The source string input.
* @param {DetectCallback} [callback] Callback function.
* @returns {Promise<DetectResponse>}
*
* @example
* const {Translate} = require('@google-cloud/translate');
*
* const translate = new Translate();
*
* //-
* // Detect the language from a single string input.
* //-
* translate.detect('Hello', (err, results) => {
* if (!err) {
* // results = {
* // language: 'en',
* // confidence: 1,
* // input: 'Hello'
* // }
* }
* });
*
* //-
* // Detect the languages used in multiple strings. Note that the results are
* // now provided as an array.
* //-
* translate.detect([
* 'Hello',
* 'Hola'
* ], (err, results) => {
* if (!err) {
* // results = [
* // {
* // language: 'en',
* // confidence: 1,
* // input: 'Hello'
* // },
* // {
* // language: 'es',
* // confidence: 1,
* // input: 'Hola'
* // }
* // ]
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* translate.detect('Hello').then((data) => {
* const results = data[0];
* const apiResponse = data[2];
* });
*
* @example <caption>include:samples/translate.js</caption>
* region_tag:translate_detect_language
* Here's a full example:
*/
detect(
input: string|string[],
callback?: DetectCallback<DetectResult>|
DetectCallback<DetectResult[]>): void|Promise<[DetectResult, r.Response]>|
Promise<[DetectResult[], r.Response]> {
const inputIsArray = Array.isArray(input);
input = arrify(input);
this.request(
{
method: 'POST',
uri: '/detect',
json: {
q: input,
},
},
(err, resp) => {
if (err) {
(callback as Function)(err, null, resp);
return;
}
let results = resp.data.detections.map(
(detection: Array<{}>, index: number) => {
const result = extend({}, detection[0], {
input: input[index],
});
// Deprecated.
// tslint:disable-next-line no-any
delete (result as any).isReliable;
return result;
});
if (input.length === 1 && !inputIsArray) {
results = results[0];
}
(callback as Function)(null, results, resp);
});
}
getLanguages(target?: string): Promise<[LanguageResult[], r.Response]>;
getLanguages(target: string, callback: GetLanguagesCallback): void;
getLanguages(callback: GetLanguagesCallback): void;
/**
* Get an array of all supported languages.
*
* @see [Discovering Supported Languages]{@link https://cloud.google.com/translate/v2/discovering-supported-languages-with-rest}
*
* @param {string} [target] Get the language names in a language other than
* English.
* @param {GetLanguagesCallback} [callback] Callback function.
* @returns {Promise<GetLanguagesResponse>}
*
* @example <caption>include:samples/translate.js</caption>
* region_tag:translate_list_codes
* Gets the language names in English:
*
* @example <caption>include:samples/translate.js</caption>
* region_tag:translate_list_language_names
* Gets the language names in a language other than English:
*/
getLanguages(
targetOrCallback?: string|GetLanguagesCallback,
callback?: GetLanguagesCallback):
void|Promise<[LanguageResult[], r.Response]> {
let target: string;
if (is.fn(targetOrCallback)) {
callback = targetOrCallback as GetLanguagesCallback;
target = 'en';
} else {
target = targetOrCallback as string;
}
const reqOpts = {
uri: '/languages',
useQuerystring: true,
qs: {},
} as DecorateRequestOptions;
if (target && is.string(target)) {
reqOpts.qs.target = target;
}
this.request(reqOpts, (err, resp) => {
if (err) {
callback!(err, null, resp);
return;
}
const languages = resp.data.languages.map(
(language: {language: string, name: string}) => {
return {
code: language.language,
name: language.name,
};
});
callback!(null, languages, resp);
});
}
translate(input: string, options: TranslateRequest):
Promise<[string, r.Response]>;
translate(input: string[], options: TranslateRequest):
Promise<[string[], r.Response]>;
translate(input: string, to: string): Promise<[string, r.Response]>;
translate(input: string[], to: string): Promise<[string[], r.Response]>;
translate(
input: string, options: TranslateRequest,
callback: TranslateCallback<string>): void;
translate(input: string, to: string, callback: TranslateCallback<string>):
void;
translate(
input: string[], options: TranslateRequest,
callback: TranslateCallback<string[]>): void;
translate(input: string[], to: string, callback: TranslateCallback<string[]>):
void;
/**
* Translate a string or multiple strings into another language.
*
* @see [Translate Text](https://cloud.google.com/translate/v2/using_rest#Translate)
*
* @throws {Error} If `options` is provided as an object without a `to`
* property.
*
* @param {string|string[]} input The source string input.
* @param {string|TranslateRequest} [options] If a string, it is interpreted as the
* target ISO 639-1 language code to translate the source input to. (e.g.
* `en` for English). If an object, you may also specify the source
* language.
* @param {TranslateCallback} [callback] Callback function.
* @returns {Promise<TranslateResponse>}
*
* @example
* //-
* // Pass a string and a language code to get the translation.
* //-
* translate.translate('Hello', 'es', (err, translation) => {
* if (!err) {
* // translation = 'Hola'
* }
* });
*
* //-
* // The source language is auto-detected by default. To manually set it,
* // provide an object.
* //-
* const options = {
* from: 'en',
* to: 'es'
* };
*
* translate.translate('Hello', options, (err, translation) => {
* if (!err) {
* // translation = 'Hola'
* }
* });
*
* //-
* // Translate multiple strings of input. Note that the results are
* // now provided as an array.
* //-
* const input = [
* 'Hello',
* 'How are you today?'
* ];
*
* translate.translate(input, 'es', (err, translations) => {
* if (!err) {
* // translations = [
* // 'Hola',
* // 'Como estas hoy?'
* // ]
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* translate.translate('Hello', 'es').then((data) => {
* const translation = data[0];
* const apiResponse = data[1];
* });
*
* @example <caption>include:samples/translate.js</caption>
* region_tag:translate_translate_text
* Full translation example:
*
* @example <caption>include:samples/translate.js</caption>
* region_tag:translate_text_with_model
* Translation using the premium model:
*/
translate(
inputs: string|string[], optionsOrTo: string|TranslateRequest,
callback?: TranslateCallback<string>|TranslateCallback<string[]>):
void|Promise<[string, r.Response]>|Promise<[string[], r.Response]> {
const inputIsArray = Array.isArray(inputs);
const input = arrify(inputs);
let options: TranslateRequest = {};
if (typeof optionsOrTo === 'object') {
options = optionsOrTo as TranslateRequest;
} else if (typeof optionsOrTo === 'string') {
options = {to: optionsOrTo};
}
// tslint:disable-next-line no-any
const body: any = {
q: input,
format: options.format || (isHtml(input[0]) ? 'html' : 'text'),
};
if (is.string(options)) {
body.target = options;
} else {
if (options.from) {
body.source = options.from;
}
if (options.to) {
body.target = options.to;
}
if (options.model) {
body.model = options.model;
}
}
if (!body.target) {
throw new Error(
'A target language is required to perform a translation.');
}
this.request(
{
method: 'POST',
uri: '',
json: body,
},
(err, resp) => {
if (err) {
(callback as Function)(err, null, resp);
return;
}
let translations = resp.data.translations.map(
(x: {translatedText: string}) => x.translatedText);
if (body.q.length === 1 && !inputIsArray) {
translations = translations[0];
}
(callback as Function)(err, translations, resp);
});
}
request(reqOpts: DecorateRequestOptions): Promise<r.Response>;
request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback):
void;
/**
* A custom request implementation. Requests to this API may optionally use an
* API key for an application, not a bearer token from a service account. This
* means it is possible to skip the `makeAuthenticatedRequest` portion of the
* typical request lifecycle, and manually authenticate the request here.
*
* @private
*
* @param {object} reqOpts - Request options that are passed to `request`.
* @param {function} callback - The callback function passed to `request`.
*/
request(reqOpts: DecorateRequestOptions, callback?: BodyResponseCallback):
void|Promise<r.Response> {
if (!this.key) {
super.request(reqOpts, callback!);
return;
}
reqOpts.uri = this.baseUrl + reqOpts.uri;
reqOpts = extend(true, {}, reqOpts, {
qs: {
key: this.key,
},
headers: {
'User-Agent': util.getUserAgentFromPackageJson(PKG),
},
});
util.makeRequest(reqOpts, this.options, callback!);
}
}
/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
promisifyAll(Translate, {exclude: ['request']});
/**
* The `@google-cloud/translate` package has a single default export, the
* {@link Translate} class.
*
* See {@link Translate} and {@link ClientConfig} for client methods and
* configuration options.
*
* @module {constructor} @google-cloud/translate
* @alias nodejs-translate
*
* @example <caption>Install the client library with <a
* href="https://www.npmjs.com/">npm</a>:</caption> npm install --save
* @google-cloud/translate
*
* @example <caption>Import the client library:</caption>
* const {Translate} = require('@google-cloud/translate');
*
* @example <caption>Create a client that uses <a
* href="https://goo.gl/64dyYX">Application Default Credentials
* (ADC)</a>:</caption> const client = new Translate();
*
* @example <caption>Create a client with <a
* href="https://goo.gl/RXp6VL">explicit credentials</a>:</caption> const client
* = new Translate({ projectId: 'your-project-id', keyFilename:
* '/path/to/keyfile.json',
* });
*
* @example <caption>include:samples/quickstart.js</caption>
* region_tag:translate_quickstart
* Full quickstart example:
*/
export {Translate};