🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@kabeep/node-translate

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kabeep/node-translate - npm Package Compare versions

Comparing version
1.1.6
to
1.2.0
+3
-1
dist/index.d.ts
import { LanguageCode as LanguageCode$1 } from 'iso-639-1';
import { OptionsInit } from 'got';

@@ -349,2 +350,3 @@ type AdditionalCodeOptions = typeof additionalCodes;

retry?: number;
requestOptions?: Partial<OptionsInit>;
}

@@ -364,4 +366,4 @@

*/
declare function translate(text: string, { from, to, timeout, retry, raw: rawEnabled }?: TranslateOptions): Promise<Mutable<TranslationOption>>;
declare function translate(text: string, { from, to, timeout, retry, raw: rawEnabled, requestOptions, }?: TranslateOptions): Promise<Mutable<TranslationOption>>;
export { type LanguageCode, type LanguageOption, type TranslateErrorCodes, type TranslateOptions, type ResponseBody as TranslateRawBody, type TranslationOption, type TranslationOptionFrom, type TranslationOptionFromLanguage, type TranslationOptionFromText, type TranslationOptionTo, type TranslationOptionToText, translate as default, iso6391X };
import iso6391 from 'iso-639-1';
import has from 'lodash.has';
import { osLocaleSync } from 'os-locale';
import querystring from 'node:querystring';
import process from 'process';
import querystring from 'querystring';
import got from 'got';
import { HttpsProxyAgent } from 'hpagent';

@@ -105,3 +107,6 @@ // src/core/iso-639-1-x.ts

getAllNames() {
return [...iso6391.getAllNames(), ...Object.values(additional_codes_constant_default).map((item) => item.name)];
return [
...iso6391.getAllNames(),
...Object.values(additional_codes_constant_default).map((item) => item.name)
];
}

@@ -121,3 +126,8 @@ /**

getAllNativeNames() {
return [...iso6391.getAllNativeNames(), ...Object.values(additional_codes_constant_default).map((option) => option.nativeName)];
return [
...iso6391.getAllNativeNames(),
...Object.values(additional_codes_constant_default).map(
(option) => option.nativeName
)
];
}

@@ -130,4 +140,8 @@ /**

getCode(name) {
const additionalNames = Object.values(additional_codes_constant_default).map((option) => option.name.toLowerCase());
const matchIndex = additionalNames.indexOf(name.toLowerCase());
const additionalNames = Object.values(additional_codes_constant_default).map(
(option) => option.name.toLowerCase()
);
const matchIndex = additionalNames.indexOf(
name.toLowerCase()
);
return Object.keys(additional_codes_constant_default)[matchIndex] ?? iso6391.getCode(name);

@@ -140,3 +154,6 @@ }

getAllCodes() {
return [...iso6391.getAllCodes(), ...Object.keys(additional_codes_constant_default)];
return [
...iso6391.getAllCodes(),
...Object.keys(additional_codes_constant_default)
];
}

@@ -160,3 +177,6 @@ /**

}
return [...iso6391.getLanguages(isoCodes), ...googleLanguages];
return [
...iso6391.getLanguages(isoCodes),
...googleLanguages
];
}

@@ -202,3 +222,4 @@ /**

function getCode(codeOrLang, adaptive = false) {
if (!codeOrLang || codeOrLang === "auto") return adaptive ? get_locale_default() : "auto";
if (!codeOrLang || codeOrLang === "auto")
return adaptive ? get_locale_default() : "auto";
codeOrLang = codeOrLang.toLowerCase();

@@ -300,3 +321,4 @@ const isValidated = validate_default(codeOrLang);

timeout = 3e4,
retry = 0
retry = 0,
requestOptions = {}
}) {

@@ -318,7 +340,7 @@ const baseUrl = "https://translate.google.com/translate_a/single";

});
const getUrl = (url2, data2) => `${baseUrl}?${querystring.stringify(data2)}`;
const getUrl = (url2, data2) => `${url2}?${querystring.stringify(data2)}`;
const isOverflow = getUrl(baseUrl, data).length > 2048;
isOverflow && delete data.q;
const url = getUrl(baseUrl, data);
const requestOptions = mutable_default({
const options = mutable_default({
method: "get",

@@ -337,8 +359,20 @@ // HTTP request method

if (isOverflow) {
requestOptions.method = "post";
requestOptions.body = new URLSearchParams({ q: text }).toString();
options.method = "post";
options.body = new URLSearchParams({ q: text }).toString();
}
const proxyUrl = process.env.https_proxy ?? process.env.http_proxy ?? process.env.all_proxy;
if (proxyUrl) {
options.agent = {
https: new HttpsProxyAgent({
keepAlive: false,
proxy: proxyUrl
})
};
}
let response;
try {
response = await got(url, requestOptions);
response = await got(url, {
...options,
...requestOptions
});
} catch (error) {

@@ -352,7 +386,21 @@ throw new Error(error.code);

// src/core/translate.ts
async function translate(text, { from = "auto", to = "auto", timeout, retry, raw: rawEnabled = false } = {}) {
async function translate(text, {
from = "auto",
to = "auto",
timeout,
retry,
raw: rawEnabled = false,
requestOptions
} = {}) {
from = get_code_default(from);
to = get_code_default(to, true);
text = String(text);
const response = await request_default({ from, to, text, timeout, retry });
const response = await request_default({
from,
to,
text,
timeout,
retry,
requestOptions
});
const data = parse_default(response);

@@ -359,0 +407,0 @@ if (!rawEnabled) {

{
"name": "@kabeep/node-translate",
"version": "1.1.6",
"version": "1.2.0",
"type": "module",

@@ -8,8 +8,11 @@ "description": "A powerful, secure and feature-rich api via Google Translation.",

"types": "./dist/index.d.ts",
"files": [
"dist"
],
"source": "./src/index.ts",
"files": ["dist"],
"scripts": {
"docs": "jsdoc -c jsdoc.config.json",
"lint": "xo src/**/*.ts --fix",
"prepare": "husky",
"lint": "biome check src",
"lint:write": "biome check src --write",
"lint:staged": "biome check src --write --staged",
"lint:changed": "biome check src --write --changed",
"lint-staged": "lint-staged",
"pretest": "npm run lint",

@@ -21,19 +24,28 @@ "test": "vitest run",

"build": "tsup --dts --treeshake smallest",
"prepare": "husky"
"build:docs": "npm run build && typedoc"
},
"devDependencies": {
"@biomejs/biome": "^2.4.8",
"@types/lodash.has": "^4.5.9",
"@types/node": "^20.12.7",
"@vitest/coverage-istanbul": "^1.5.1",
"docdash": "^2.0.2",
"@types/node": "^22.2.0",
"@vitest/coverage-istanbul": "^3.2.4",
"husky": "^9.0.11",
"jsdoc": "^4.0.2",
"lint-staged": "^16.4.0",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"typedoc": "~0.26.11",
"typedoc-material-theme": "^1.2.0",
"typedoc-plugin-include-example": "^2.0.2",
"typedoc-plugin-inline-sources": "~1.1.0",
"typedoc-plugin-mdn-links": "^4.0.7",
"typedoc-plugin-missing-exports": "^3.1.0",
"typedoc-plugin-rename-documents": "^1.0.0",
"typedoc-plugin-replace-text": "~4.0.0",
"typedoc-plugin-version-header": "^1.0.0",
"typescript": "^5.4.5",
"vitest": "^1.5.1",
"xo": "^0.58.0"
"vitest": "^3.2.4"
},
"dependencies": {
"got": "^13.0.0",
"hpagent": "^1.2.0",
"iso-639-1": "^3.1.2",

@@ -43,6 +55,16 @@ "lodash.has": "^4.5.2",

},
"author": "Zhang Zixin (kabeep)",
"homepage": "https://github.com/kabeep/node-translate#readme",
"repository": "git@github.com:kabeep/node-translate.git",
"bugs": "https://github.com/kabeep/node-translate/issue",
"author": {
"name": "Jason Zhang",
"email": "zzx2067@outlook.com"
},
"homepage": "https://kabeep.github.io/node-translate",
"repository": {
"type": "git",
"url": "git+https://github.com/kabeep/node-translate.git"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"bugs": "https://github.com/kabeep/node-translate/issues",
"keywords": [

@@ -58,5 +80,5 @@ "nodejs",

"engines": {
"node": ">=16"
"node": "18.18.x || >=18.20.0"
},
"license": "MIT"
}
<div align="center">
<img width="814" src="docs/images/node-translate-logo.png" alt="logo">
<img width="630" src="assets/node-translate-logo.png" alt="logo">
A powerful, secure and feature-rich api via Google Translation.
---
[![NodeJS](https://img.shields.io/node/v/%40kabeep%2Fnode-translate?color=lightseagreen)](https://nodejs.org/docs/latest/api/)

@@ -461,2 +459,2 @@ [![License](https://img.shields.io/github/license/kabeep/node-translate?color=slateblue)](LICENSE)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
<div align="center">
<img width="814" src="docs/images/node-translate-logo.png" alt="logo">
<img width="630" src="assets/node-translate-logo.png" alt="logo">

@@ -458,2 +458,2 @@ 一个强大、安全且功能丰富的 API,通过 Google 翻译。

本项目采用 MIT 许可证。详情请见 [LICENSE](LICENSE) 文件。
本项目采用 MIT 许可证。详情请见 [LICENSE](LICENSE) 文件。