@vitalets/google-translate-api
Advanced tools
Comparing version 9.0.0 to 9.1.0
@@ -0,1 +1,3 @@ | ||
import { Response } from 'node-fetch'; | ||
import createHttpError from 'http-errors'; | ||
import { RawResponse, TranslateOptions } from './types.js'; | ||
@@ -19,4 +21,5 @@ declare const defaults: Required<Pick<TranslateOptions, 'from' | 'to' | 'host'>>; | ||
protected buildResText({ sentences }: RawResponse): string; | ||
protected buildError(res: Response): Promise<createHttpError.HttpError<number>>; | ||
} | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -9,2 +9,3 @@ "use strict"; | ||
const http_errors_1 = __importDefault(require("http-errors")); | ||
const helpers_js_1 = require("./helpers.js"); | ||
const defaults = { | ||
@@ -29,3 +30,3 @@ from: 'auto', | ||
if (!res.ok) | ||
throw (0, http_errors_1.default)(res.status, res.statusText); | ||
throw await this.buildError(res); | ||
const raw = await res.json(); | ||
@@ -70,4 +71,15 @@ const text = this.buildResText(raw); | ||
} | ||
async buildError(res) { | ||
if (res.status === 429) { | ||
const text = await res.text(); | ||
const { ip, time, url } = (0, helpers_js_1.extractTooManyRequestsInfo)(text); | ||
const message = `${res.statusText} IP: ${ip}, Time: ${time}, Url: ${url}`; | ||
return (0, http_errors_1.default)(res.status, message); | ||
} | ||
else { | ||
return (0, http_errors_1.default)(res.status, res.statusText); | ||
} | ||
} | ||
} | ||
exports.Translator = Translator; | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,3 @@ | ||
import { Response } from 'node-fetch'; | ||
import createHttpError from 'http-errors'; | ||
import { RawResponse, TranslateOptions } from './types.js'; | ||
@@ -19,4 +21,5 @@ declare const defaults: Required<Pick<TranslateOptions, 'from' | 'to' | 'host'>>; | ||
protected buildResText({ sentences }: RawResponse): string; | ||
protected buildError(res: Response): Promise<createHttpError.HttpError<number>>; | ||
} | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
import fetch from 'node-fetch'; | ||
import createHttpError from 'http-errors'; | ||
import { extractTooManyRequestsInfo } from './helpers.js'; | ||
const defaults = { | ||
@@ -21,3 +22,3 @@ from: 'auto', | ||
if (!res.ok) | ||
throw createHttpError(res.status, res.statusText); | ||
throw await this.buildError(res); | ||
const raw = await res.json(); | ||
@@ -62,3 +63,14 @@ const text = this.buildResText(raw); | ||
} | ||
async buildError(res) { | ||
if (res.status === 429) { | ||
const text = await res.text(); | ||
const { ip, time, url } = extractTooManyRequestsInfo(text); | ||
const message = `${res.statusText} IP: ${ip}, Time: ${time}, Url: ${url}`; | ||
return createHttpError(res.status, message); | ||
} | ||
else { | ||
return createHttpError(res.status, res.statusText); | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@vitalets/google-translate-api", | ||
"description": "A free and unlimited API for Google Translate", | ||
"version": "9.0.0", | ||
"version": "9.1.0", | ||
"type": "module", | ||
@@ -20,3 +20,3 @@ "main": "./dist/cjs/index.js", | ||
"test:esm": "node test/esm/prepare.mjs && node test/esm/test.cjs && node test/esm/test.mjs", | ||
"toc": "markdown-toc README.md -i", | ||
"toc": "markdown-toc README.md -i --maxdepth 4", | ||
"build:esm": "rm -rf dist/esm && tsc -p tsconfig.build.json", | ||
@@ -37,3 +37,3 @@ "build:cjs": "rm -rf dist/cjs && tsc -p tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json", | ||
"@types/mocha": "^10.0.0", | ||
"@types/node": "^18.8.3", | ||
"@types/node": "^18.11.18", | ||
"@types/node-fetch": "^2.6.2", | ||
@@ -40,0 +40,0 @@ "@typescript-eslint/eslint-plugin": "^5.39.0", |
@@ -24,2 +24,4 @@ # google-translate-api | ||
- [API](#api) | ||
+ [Parameters](#parameters) | ||
+ [Response](#response) | ||
- [Related projects](#related-projects) | ||
@@ -104,4 +106,6 @@ - [License](#license) | ||
``` | ||
> Available proxy list you can find [here](https://free-proxy-list.net/). | ||
See [examples/with-proxy.ts] for more details. | ||
> Available proxy list you can find [here](https://free-proxy-list.net/) (with `yes` in Google column). | ||
Common pattern for selecting proxy is following: | ||
@@ -122,4 +126,19 @@ ```ts | ||
## API | ||
tbd | ||
```ts | ||
translate(text: string, options?: Options): Promise<Response> | ||
``` | ||
#### Parameters | ||
* `text` *(string)* - The text to be translated | ||
* `options` *(object)* | ||
- `from` *(string)* - The language of `text`. Must be `auto` or one of the [supported languages](https://cloud.google.com/translate/docs/languages). Default: `auto` | ||
- `to` *(string)* - The language in which the text should be translated. Must be one of the [supported languages](https://cloud.google.com/translate/docs/languages). Default: `auto` | ||
- `host` *(string)* - Google translate host to be used in API calls. Default: `translate.google.com` | ||
- `fetchOptions` *(object)* - Additional [fetch options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters) passed into request. | ||
#### Response | ||
* `text` *(string)* – The translated text. | ||
* `raw` *(object)* - Raw responspe from the API. Contains sentences, detected original language and transliteration. [Example response](https://github.com/vitalets/google-translate-api/blob/master/response-sample.json). | ||
## Related projects | ||
@@ -126,0 +145,0 @@ * [matheuss/google-translate-api](https://github.com/matheuss/google-translate-api) - original repo |
@@ -1,4 +0,5 @@ | ||
import fetch from 'node-fetch'; | ||
import fetch, { Response } from 'node-fetch'; | ||
import createHttpError from 'http-errors'; | ||
import { RawResponse, Sentence, TranslateOptions } from './types.js'; | ||
import { extractTooManyRequestsInfo } from './helpers.js'; | ||
@@ -26,3 +27,3 @@ const defaults: Required<Pick<TranslateOptions, 'from' | 'to' | 'host'>> = { | ||
const res = await fetch(url, fetchOptions); | ||
if (!res.ok) throw createHttpError(res.status, res.statusText); | ||
if (!res.ok) throw await this.buildError(res); | ||
const raw = await res.json() as RawResponse; | ||
@@ -71,2 +72,13 @@ const text = this.buildResText(raw); | ||
} | ||
protected async buildError(res: Response) { | ||
if (res.status === 429) { | ||
const text = await res.text(); | ||
const { ip, time, url } = extractTooManyRequestsInfo(text); | ||
const message = `${res.statusText} IP: ${ip}, Time: ${time}, Url: ${url}`; | ||
return createHttpError(res.status, message); | ||
} else { | ||
return createHttpError(res.status, res.statusText); | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31112
31
391
151