@cutticat/libretranslate 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,9 @@ Library for working with the LibreTranslate API.
13
13
 
14
14
  ## Overview
15
15
 
16
- The library provides the `translate` function for translating text via the LibreTranslate API. Supports language auto-detection, alternative translations, custom baseUrl and API key. Uses Zod for response validation and types from `@cutticat/types`.
16
+ The library provides the `translate` function for translating text via the LibreTranslate API.
17
+ Supports language auto-detection, alternative translations, custom baseUrl and API key. Uses Zod for
18
+ response validation and types from `@cutticat/types`.
17
19
 
18
20
  ## Installation
19
21
 
@@ -46,15 +48,31 @@ console.log(result.translatedText) // "Hello"
46
48
  console.log(result.detectedLanguage) // { code: "fr", confidence: 0.95 }
47
49
  ```
48
50
 
51
+ With cancellation: `signal` goes to `fetch`, and an aborted request rejects with `signal.reason`.
52
+ For a timeout pass `AbortSignal.timeout(ms)`; combine it with your own signal through
53
+ `AbortSignal.any`:
54
+
55
+ ```typescript
56
+ const result = await translate({
57
+ text: "Hello",
58
+ source: "en",
59
+ target: "ru",
60
+ signal: AbortSignal.any([request.signal, AbortSignal.timeout(10_000)]),
61
+ })
62
+ ```
63
+
49
64
  ## API
50
65
 
51
66
  ### Functions
52
67
 
53
- - `translate(options)` — translates text via the LibreTranslate API; returns `Promise<TranslateResponse>`; throws `Error` on request failure
68
+ - `translate(options)` — translates text via the LibreTranslate API; returns
69
+ `Promise<TranslateResponse>`; throws `Error` on request failure and rejects with
70
+ `signal.reason` when `signal` aborts
54
71
 
55
72
  ### Types
56
73
 
57
- - `TranslationOptions` — translation parameters (`text`, `source`, `target`, `format?`, `alternatives?`, `baseUrl?`, `apiKey?`, `fetch?`)
74
+ - `TranslationOptions` — translation parameters (`text`, `source`, `target`, `format?`,
75
+ `alternatives?`, `baseUrl?`, `apiKey?`, `fetch?`, `signal?`)
58
76
  - `TranslationFormat` — `"text"` | `"html"`
59
77
  - `TranslateResponse` — response (`translatedText`, `detectedLanguage?`, `alternatives?`)
60
78
  - `DetectedLanguage` — detected language info (`code`, `confidence`)
@@ -67,15 +85,15 @@ console.log(result.detectedLanguage) // { code: "fr", confidence: 0.95 }
67
85
 
68
86
  Detailed documentation:
69
87
 
70
- - [Project structure](documents/STRUCTURE.md)
71
- - [Scripts](documents/SCRIPTS.md)
72
- - [Style guide](documents/STYLE_GUIDE.md)
73
- - [Contributing](documents/CONTRIBUTING.md)
88
+ - [Project structure](<https://gitflic.ru/project/cutticat-npm/libretranslate/blob?branch=main&file=documents/STRUCTURE.md>)
89
+ - [Scripts](<https://gitflic.ru/project/cutticat-npm/libretranslate/blob?branch=main&file=documents/SCRIPTS.md>)
90
+ - [Style guide](<https://gitflic.ru/project/cutticat-npm/libretranslate/blob?branch=main&file=documents/STYLE_GUIDE.md>)
91
+ - [Contributing](<https://gitflic.ru/project/cutticat-npm/libretranslate/blob?branch=main&file=documents/CONTRIBUTING.md>)
74
92
 
75
93
  Full API documentation is available in JSDoc comments. Use IDE autocomplete to browse it.
76
94
 
77
95
  ## Requirements
78
96
 
79
- - Node.js >= 22.0.0
80
- - pnpm >= 10.17.0
97
+ - Node.js >= 24.14.0
98
+ - pnpm >= 12.21.0
81
99
  - TypeScript >= 5.9.0
package/dist/index.d.ts CHANGED
@@ -1,18 +1,14 @@
1
1
  import { z } from "zod";
2
2
  import { DeepReadonly, FullyUndefinable } from "@cutticat/types";
3
-
4
- //#region lib/schemas/DetectedLanguage.d.ts
5
3
  /** Detected language info schema. */
6
- declare const DetectedLanguageSchema: z.ZodObject<{
4
+ export declare const DetectedLanguageSchema: z.ZodObject<{
7
5
  code: z.ZodString;
8
6
  confidence: z.ZodNumber;
9
7
  }, z.core.$strip>;
10
8
  /** Detected language info (when using auto-detection). */
11
- type DetectedLanguage = z.infer<typeof DetectedLanguageSchema>;
12
- //#endregion
13
- //#region lib/schemas/TranslateResponse.d.ts
9
+ export type DetectedLanguage = z.infer<typeof DetectedLanguageSchema>;
14
10
  /** Translation response schema. */
15
- declare const TranslateResponseSchema: z.ZodObject<{
11
+ export declare const TranslateResponseSchema: z.ZodObject<{
16
12
  translatedText: z.ZodString;
17
13
  detectedLanguage: z.ZodOptional<z.ZodObject<{
18
14
  code: z.ZodString;
@@ -21,15 +17,11 @@ declare const TranslateResponseSchema: z.ZodObject<{
21
17
  alternatives: z.ZodOptional<z.ZodArray<z.ZodString>>;
22
18
  }, z.core.$strip>;
23
19
  /** Translation response from the LibreTranslate API. */
24
- type TranslateResponse = z.infer<typeof TranslateResponseSchema>;
25
- //#endregion
26
- //#region lib/types/TranslationFormat.d.ts
20
+ export type TranslateResponse = z.infer<typeof TranslateResponseSchema>;
27
21
  /** Text format: "text" or "html" for markup. */
28
- type TranslationFormat = "text" | "html";
29
- //#endregion
30
- //#region lib/types/TranslationOptions.d.ts
22
+ export type TranslationFormat = "text" | "html";
31
23
  /** Translation request parameters. */
32
- type TranslationOptions = {
24
+ export type TranslationOptions = {
33
25
  /** Text to translate. */
34
26
  text: string;
35
27
  /** Source language code (e.g. "en", "ru") or "auto" for auto-detection. */
@@ -47,19 +39,23 @@ type TranslationOptions = {
47
39
  apiKey: string;
48
40
  /** fetch function for HTTP requests (defaults to globalThis.fetch; pass a mock for tests). */
49
41
  fetch: typeof globalThis.fetch;
42
+ /**
43
+ * Cancels the request. Passed to `fetch`; once aborted, `translate` rejects with
44
+ * `signal.reason`. For a timeout pass `AbortSignal.timeout(ms)`, combined with other signals
45
+ * through `AbortSignal.any`.
46
+ */
47
+ signal: AbortSignal;
50
48
  }>;
51
- //#endregion
52
- //#region lib/constants.d.ts
53
49
  /** Default URL for the LibreTranslate API. */
54
- declare const defaultBaseUrl = "https://libretranslate.com";
55
- //#endregion
56
- //#region lib/functions.d.ts
50
+ export declare const defaultBaseUrl = "https://libretranslate.com";
57
51
  /**
58
52
  * Translates text via the LibreTranslate API.
59
53
  *
60
54
  * @param options — translation parameters
61
55
  * @returns Promise with translation response
62
56
  * @throws {Error} On request failure (HTTP status included in message)
57
+ * @throws {DOMException} When `signal` is aborted before or during the request: rejects with
58
+ * `signal.reason`, which is an `AbortError` or `TimeoutError` unless the caller set its own reason
63
59
  *
64
60
  * @example
65
61
  * ```typescript
@@ -108,6 +104,17 @@ declare const defaultBaseUrl = "https://libretranslate.com";
108
104
  *
109
105
  * @example
110
106
  * ```typescript
107
+ * // Cancelled after 10 seconds
108
+ * const result = await translate({
109
+ * text: "Hello",
110
+ * source: "en",
111
+ * target: "ru",
112
+ * signal: AbortSignal.timeout(10_000)
113
+ * })
114
+ * ```
115
+ *
116
+ * @example
117
+ * ```typescript
111
118
  * // With alternative translations
112
119
  * const result = await translate({
113
120
  * text: "Hello",
@@ -118,16 +125,5 @@ declare const defaultBaseUrl = "https://libretranslate.com";
118
125
  * console.log(result.alternatives) // ["Здравствуй", "Приветствую"]
119
126
  * ```
120
127
  */
121
- declare function translate({
122
- text,
123
- source,
124
- target,
125
- format,
126
- alternatives,
127
- apiKey,
128
- baseUrl,
129
- fetch
130
- }: DeepReadonly<TranslationOptions>): Promise<TranslateResponse>;
131
- //#endregion
132
- export { DetectedLanguage, DetectedLanguageSchema, TranslateResponse, TranslateResponseSchema, TranslationFormat, TranslationOptions, defaultBaseUrl, translate };
128
+ export declare function translate({ text, source, target, format, alternatives, apiKey, baseUrl, fetch, signal }: DeepReadonly<TranslationOptions>): Promise<TranslateResponse>;
133
129
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../lib/schemas/DetectedLanguage.ts","../lib/schemas/TranslateResponse.ts","../lib/types/TranslationFormat.ts","../lib/types/TranslationOptions.ts","../lib/constants.ts","../lib/functions.ts"],"sourcesContent":[],"mappings":";;;;;cAGa,wBAAsB,CAAA,CAAA;;EAAtB,UAAA,aAAA;;;KAMD,gBAAA,GAAmB,CAAA,CAAE,aAAa;;;;cCLjC,yBAAuB,CAAA,CAAA;;EDDvB,gBAAA,eAGX,YAAA,CAAA;;;;EAHiC,YAAA,eAAA,WAAA,YAAA,CAAA,CAAA;CAAA,eAAA,CAAA;AAMnC;KCEY,iBAAA,GAAoB,CAAA,CAAE,aAAa;;;;KCVnC,iBAAA;;;;KCGA,kBAAA;EHDC;;;;EAAsB;EAAA,MAAA,EAAA,MAAA;AAMnC,CAAA,GGEI,gBHFQ,CAAA;;UGIF;;EFTG,YAAA,EAAA,MAAA;;;;;;gBEiBG,UAAA,CAAW;;;;;cCpBd,cAAA;;;;AJEb;;;;;;AAMA;;;;ACLA;;;;;;;;;;;;;AAOA;;;;ACVA;;;;ACGA;;;;;;;;ACHA;;;;ACoEA;;;;;;;;;;;;;;;;;;;;iBAAsB,SAAA;;;;;;;;;GASnB,aAAa,sBAAsB,QAAQ"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../lib/schemas/DetectedLanguage.ts","../lib/schemas/TranslateResponse.ts","../lib/types/TranslationFormat.ts","../lib/types/TranslationOptions.ts","../lib/constants.ts","../lib/functions.ts"],"mappings":";;;qBAGa,wBAAsB,EAAA;;;GAGjC,EAAA,KAAA;;YAGU,mBAAmB,EAAE,aAAa;;qBCLjC,yBAAuB,EAAA;;;;;;;GAIlC,EAAA,KAAA;;YAGU,oBAAoB,EAAE,aAAa;;YCVnC;;YCGA;;EAEV;;EAEA;;EAEA;IACE;;EAEF,QAAQ;;EAER;;EAEA;;EAEA;;EAEA,cAAc,WAAW;;;;;;EAMzB,QAAQ;;;qBC1BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBCiFS,YACpB,MACA,QACA,QACA,QACA,cACA,QACA,SACA,OACA,UACC,aAAa,sBAAsB,QAAQ"}
package/dist/index.js CHANGED
@@ -1,34 +1,29 @@
1
1
  import { z } from "zod";
2
-
3
- //#region lib/schemas/DetectedLanguage.ts
2
+ // lib/schemas/DetectedLanguage.ts
4
3
  /** Detected language info schema. */
5
4
  const DetectedLanguageSchema = z.object({
6
5
  code: z.string(),
7
6
  confidence: z.number()
8
7
  });
9
-
10
- //#endregion
11
- //#region lib/schemas/TranslateResponse.ts
8
+ // lib/schemas/TranslateResponse.ts
12
9
  /** Translation response schema. */
13
10
  const TranslateResponseSchema = z.object({
14
11
  translatedText: z.string(),
15
12
  detectedLanguage: DetectedLanguageSchema.optional(),
16
13
  alternatives: z.array(z.string()).optional()
17
14
  });
18
-
19
- //#endregion
20
- //#region lib/constants.ts
15
+ // lib/constants.ts
21
16
  /** Default URL for the LibreTranslate API. */
22
17
  const defaultBaseUrl = "https://libretranslate.com";
23
-
24
- //#endregion
25
- //#region lib/functions.ts
18
+ // lib/functions.ts
26
19
  /**
27
20
  * Translates text via the LibreTranslate API.
28
21
  *
29
22
  * @param options — translation parameters
30
23
  * @returns Promise with translation response
31
24
  * @throws {Error} On request failure (HTTP status included in message)
25
+ * @throws {DOMException} When `signal` is aborted before or during the request: rejects with
26
+ * `signal.reason`, which is an `AbortError` or `TimeoutError` unless the caller set its own reason
32
27
  *
33
28
  * @example
34
29
  * ```typescript
@@ -77,6 +72,17 @@ const defaultBaseUrl = "https://libretranslate.com";
77
72
  *
78
73
  * @example
79
74
  * ```typescript
75
+ * // Cancelled after 10 seconds
76
+ * const result = await translate({
77
+ * text: "Hello",
78
+ * source: "en",
79
+ * target: "ru",
80
+ * signal: AbortSignal.timeout(10_000)
81
+ * })
82
+ * ```
83
+ *
84
+ * @example
85
+ * ```typescript
80
86
  * // With alternative translations
81
87
  * const result = await translate({
82
88
  * text: "Hello",
@@ -87,7 +93,7 @@ const defaultBaseUrl = "https://libretranslate.com";
87
93
  * console.log(result.alternatives) // ["Здравствуй", "Приветствую"]
88
94
  * ```
89
95
  */
90
- async function translate({ text, source, target, format = "text", alternatives = 0, apiKey, baseUrl = defaultBaseUrl, fetch = globalThis.fetch }) {
96
+ async function translate({ text, source, target, format = "text", alternatives = 0, apiKey, baseUrl = defaultBaseUrl, fetch = globalThis.fetch, signal }) {
91
97
  if (alternatives < 0) throw new Error("Number of alternatives must be zero or positive");
92
98
  if (!Number.isInteger(alternatives)) throw new Error("Number of alternatives must be an integer");
93
99
  const body = {
@@ -98,16 +104,17 @@ async function translate({ text, source, target, format = "text", alternatives =
98
104
  if (format != null) body.format = format;
99
105
  if (alternatives > 0) body.alternatives = alternatives;
100
106
  if (apiKey != null) body.api_key = apiKey;
107
+ signal?.throwIfAborted();
101
108
  const response = await fetch(new URL("/translate", baseUrl), {
102
109
  method: "POST",
103
110
  headers: { "Content-Type": "application/json" },
104
- body: JSON.stringify(body)
111
+ body: JSON.stringify(body),
112
+ signal: signal ?? null
105
113
  });
106
114
  if (!response.ok) throw new Error(`Failed to translate text: ${response.status} ${response.statusText}`);
107
115
  const json = await response.json();
108
116
  return TranslateResponseSchema.parse(json);
109
117
  }
110
-
111
- //#endregion
112
118
  export { DetectedLanguageSchema, TranslateResponseSchema, defaultBaseUrl, translate };
119
+
113
120
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../lib/schemas/DetectedLanguage.ts","../lib/schemas/TranslateResponse.ts","../lib/constants.ts","../lib/functions.ts"],"sourcesContent":["import { z } from \"zod\"\n\n/** Detected language info schema. */\nexport const DetectedLanguageSchema = z.object({\n code: z.string(),\n confidence: z.number(),\n})\n\n/** Detected language info (when using auto-detection). */\nexport type DetectedLanguage = z.infer<typeof DetectedLanguageSchema>\n","import { z } from \"zod\"\nimport { DetectedLanguageSchema } from \"./DetectedLanguage.js\"\n\n/** Translation response schema. */\nexport const TranslateResponseSchema = z.object({\n translatedText: z.string(),\n detectedLanguage: DetectedLanguageSchema.optional(),\n alternatives: z.array(z.string()).optional(),\n})\n\n/** Translation response from the LibreTranslate API. */\nexport type TranslateResponse = z.infer<typeof TranslateResponseSchema>\n","/** Default URL for the LibreTranslate API. */\nexport const defaultBaseUrl = \"https://libretranslate.com\"\n","import type { DeepReadonly } from \"@cutticat/types\"\nimport type { TranslationOptions } from \"./types/TranslationOptions.js\"\nimport { TranslateResponseSchema, type TranslateResponse } from \"./schemas/TranslateResponse.js\"\nimport { defaultBaseUrl } from \"./constants.js\"\n\n/**\n * Translates text via the LibreTranslate API.\n *\n * @param options — translation parameters\n * @returns Promise with translation response\n * @throws {Error} On request failure (HTTP status included in message)\n *\n * @example\n * ```typescript\n * const result = await translate({\n * text: \"Hello, world!\",\n * source: \"en\",\n * target: \"ru\"\n * })\n * console.log(result.translatedText) // \"Привет, мир!\"\n * ```\n *\n * @example\n * ```typescript\n * // With language auto-detection\n * const result = await translate({\n * text: \"Bonjour\",\n * source: \"auto\",\n * target: \"en\"\n * })\n * console.log(result.translatedText) // \"Hello\"\n * console.log(result.detectedLanguage) // { code: \"fr\", confidence: 0.95 }\n * ```\n *\n * @example\n * ```typescript\n * // With custom API and key\n * const result = await translate({\n * text: \"Hello\",\n * source: \"en\",\n * target: \"es\",\n * baseUrl: \"https://translate.example.com\",\n * apiKey: \"your-api-key\"\n * })\n * ```\n *\n * @example\n * ```typescript\n * // With custom fetch (e.g. for tests or proxy)\n * const result = await translate({\n * text: \"Hello\",\n * source: \"en\",\n * target: \"ru\",\n * fetch: myCustomFetch\n * })\n * ```\n *\n * @example\n * ```typescript\n * // With alternative translations\n * const result = await translate({\n * text: \"Hello\",\n * source: \"en\",\n * target: \"ru\",\n * alternatives: 2\n * })\n * console.log(result.alternatives) // [\"Здравствуй\", \"Приветствую\"]\n * ```\n */\nexport async function translate({\n text,\n source,\n target,\n format = \"text\",\n alternatives = 0,\n apiKey,\n baseUrl = defaultBaseUrl,\n fetch = globalThis.fetch,\n}: DeepReadonly<TranslationOptions>): Promise<TranslateResponse> {\n // Validation\n\n if (alternatives < 0) throw new Error(\"Number of alternatives must be zero or positive\")\n if (!Number.isInteger(alternatives)) throw new Error(\"Number of alternatives must be an integer\")\n\n // Body creation\n\n const body: Record<string, unknown> = {\n q: text,\n source: source,\n target: target,\n }\n\n if (format != null) body.format = format\n if (alternatives > 0) body.alternatives = alternatives\n if (apiKey != null) body.api_key = apiKey\n\n // Request sending\n\n const url = new URL(\"/translate\", baseUrl)\n const response = await fetch(url, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(body),\n })\n\n // Response processing\n\n if (!response.ok)\n throw new Error(`Failed to translate text: ${response.status} ${response.statusText}`)\n\n const json = await response.json()\n const data = TranslateResponseSchema.parse(json)\n\n return data\n}\n"],"mappings":";;;;AAGA,MAAa,yBAAyB,EAAE,OAAO;CAC7C,MAAM,EAAE,QAAQ;CAChB,YAAY,EAAE,QAAQ;CACvB,CAAC;;;;;ACFF,MAAa,0BAA0B,EAAE,OAAO;CAC9C,gBAAgB,EAAE,QAAQ;CAC1B,kBAAkB,uBAAuB,UAAU;CACnD,cAAc,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,UAAU;CAC7C,CAAC;;;;;ACPF,MAAa,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACoE9B,eAAsB,UAAU,EAC9B,MACA,QACA,QACA,SAAS,QACT,eAAe,GACf,QACA,UAAU,gBACV,QAAQ,WAAW,SAC4C;AAG/D,KAAI,eAAe,EAAG,OAAM,IAAI,MAAM,kDAAkD;AACxF,KAAI,CAAC,OAAO,UAAU,aAAa,CAAE,OAAM,IAAI,MAAM,4CAA4C;CAIjG,MAAM,OAAgC;EACpC,GAAG;EACK;EACA;EACT;AAED,KAAI,UAAU,KAAM,MAAK,SAAS;AAClC,KAAI,eAAe,EAAG,MAAK,eAAe;AAC1C,KAAI,UAAU,KAAM,MAAK,UAAU;CAKnC,MAAM,WAAW,MAAM,MADX,IAAI,IAAI,cAAc,QAAQ,EACR;EAChC,QAAQ;EACR,SAAS,EAAE,gBAAgB,oBAAoB;EAC/C,MAAM,KAAK,UAAU,KAAK;EAC3B,CAAC;AAIF,KAAI,CAAC,SAAS,GACZ,OAAM,IAAI,MAAM,6BAA6B,SAAS,OAAO,GAAG,SAAS,aAAa;CAExF,MAAM,OAAO,MAAM,SAAS,MAAM;AAGlC,QAFa,wBAAwB,MAAM,KAAK"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../lib/schemas/DetectedLanguage.ts","../lib/schemas/TranslateResponse.ts","../lib/constants.ts","../lib/functions.ts"],"sourcesContent":["import { z } from \"zod\"\n\n/** Detected language info schema. */\nexport const DetectedLanguageSchema = z.object({\n code: z.string(),\n confidence: z.number(),\n})\n\n/** Detected language info (when using auto-detection). */\nexport type DetectedLanguage = z.infer<typeof DetectedLanguageSchema>\n","import { z } from \"zod\"\nimport { DetectedLanguageSchema } from \"./DetectedLanguage.ts\"\n\n/** Translation response schema. */\nexport const TranslateResponseSchema = z.object({\n translatedText: z.string(),\n detectedLanguage: DetectedLanguageSchema.optional(),\n alternatives: z.array(z.string()).optional(),\n})\n\n/** Translation response from the LibreTranslate API. */\nexport type TranslateResponse = z.infer<typeof TranslateResponseSchema>\n","/** Default URL for the LibreTranslate API. */\nexport const defaultBaseUrl = \"https://libretranslate.com\"\n","import type { DeepReadonly } from \"@cutticat/types\"\nimport type { TranslationOptions } from \"./types/TranslationOptions.ts\"\nimport { TranslateResponseSchema, type TranslateResponse } from \"./schemas/TranslateResponse.ts\"\nimport { defaultBaseUrl } from \"./constants.ts\"\n\n/**\n * Translates text via the LibreTranslate API.\n *\n * @param options — translation parameters\n * @returns Promise with translation response\n * @throws {Error} On request failure (HTTP status included in message)\n * @throws {DOMException} When `signal` is aborted before or during the request: rejects with\n * `signal.reason`, which is an `AbortError` or `TimeoutError` unless the caller set its own reason\n *\n * @example\n * ```typescript\n * const result = await translate({\n * text: \"Hello, world!\",\n * source: \"en\",\n * target: \"ru\"\n * })\n * console.log(result.translatedText) // \"Привет, мир!\"\n * ```\n *\n * @example\n * ```typescript\n * // With language auto-detection\n * const result = await translate({\n * text: \"Bonjour\",\n * source: \"auto\",\n * target: \"en\"\n * })\n * console.log(result.translatedText) // \"Hello\"\n * console.log(result.detectedLanguage) // { code: \"fr\", confidence: 0.95 }\n * ```\n *\n * @example\n * ```typescript\n * // With custom API and key\n * const result = await translate({\n * text: \"Hello\",\n * source: \"en\",\n * target: \"es\",\n * baseUrl: \"https://translate.example.com\",\n * apiKey: \"your-api-key\"\n * })\n * ```\n *\n * @example\n * ```typescript\n * // With custom fetch (e.g. for tests or proxy)\n * const result = await translate({\n * text: \"Hello\",\n * source: \"en\",\n * target: \"ru\",\n * fetch: myCustomFetch\n * })\n * ```\n *\n * @example\n * ```typescript\n * // Cancelled after 10 seconds\n * const result = await translate({\n * text: \"Hello\",\n * source: \"en\",\n * target: \"ru\",\n * signal: AbortSignal.timeout(10_000)\n * })\n * ```\n *\n * @example\n * ```typescript\n * // With alternative translations\n * const result = await translate({\n * text: \"Hello\",\n * source: \"en\",\n * target: \"ru\",\n * alternatives: 2\n * })\n * console.log(result.alternatives) // [\"Здравствуй\", \"Приветствую\"]\n * ```\n */\nexport async function translate({\n text,\n source,\n target,\n format = \"text\",\n alternatives = 0,\n apiKey,\n baseUrl = defaultBaseUrl,\n fetch = globalThis.fetch,\n signal,\n}: DeepReadonly<TranslationOptions>): Promise<TranslateResponse> {\n // Validation\n\n if (alternatives < 0) throw new Error(\"Number of alternatives must be zero or positive\")\n if (!Number.isInteger(alternatives)) throw new Error(\"Number of alternatives must be an integer\")\n\n // Body creation\n\n const body: Record<string, unknown> = {\n q: text,\n source: source,\n target: target,\n }\n\n if (format != null) body.format = format\n if (alternatives > 0) body.alternatives = alternatives\n if (apiKey != null) body.api_key = apiKey\n\n // Request sending\n\n signal?.throwIfAborted()\n\n const url = new URL(\"/translate\", baseUrl)\n const response = await fetch(url, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(body),\n signal: signal ?? null,\n })\n\n // Response processing\n\n if (!response.ok)\n throw new Error(`Failed to translate text: ${response.status} ${response.statusText}`)\n\n const json = await response.json()\n const data = TranslateResponseSchema.parse(json)\n\n return data\n}\n"],"mappings":";;;AAGA,MAAa,yBAAyB,EAAE,OAAO;CAC7C,MAAM,EAAE,OAAO;CACf,YAAY,EAAE,OAAO;AACvB,CAAC;;;ACFD,MAAa,0BAA0B,EAAE,OAAO;CAC9C,gBAAgB,EAAE,OAAO;CACzB,kBAAkB,uBAAuB,SAAS;CAClD,cAAc,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS;AAC7C,CAAC;;;ACPD,MAAa,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACiF9B,eAAsB,UAAU,EAC9B,MACA,QACA,QACA,SAAS,QACT,eAAe,GACf,QACA,UAAU,gBACV,QAAQ,WAAW,OACnB,UAC+D;CAG/D,IAAI,eAAe,GAAG,MAAM,IAAI,MAAM,iDAAiD;CACvF,IAAI,CAAC,OAAO,UAAU,YAAY,GAAG,MAAM,IAAI,MAAM,2CAA2C;CAIhG,MAAM,OAAgC;EACpC,GAAG;EACK;EACA;CACV;CAEA,IAAI,UAAU,MAAM,KAAK,SAAS;CAClC,IAAI,eAAe,GAAG,KAAK,eAAe;CAC1C,IAAI,UAAU,MAAM,KAAK,UAAU;CAInC,QAAQ,eAAe;CAGvB,MAAM,WAAW,MAAM,MAAM,IADb,IAAI,cAAc,OACH,GAAG;EAChC,QAAQ;EACR,SAAS,EAAE,gBAAgB,mBAAmB;EAC9C,MAAM,KAAK,UAAU,IAAI;EACzB,QAAQ,UAAU;CACpB,CAAC;CAID,IAAI,CAAC,SAAS,IACZ,MAAM,IAAI,MAAM,6BAA6B,SAAS,OAAO,GAAG,SAAS,YAAY;CAEvF,MAAM,OAAO,MAAM,SAAS,KAAK;CAGjC,OAFa,wBAAwB,MAAM,IAEjC;AACZ"}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.0.3",
6
+ "version": "1.1.0",
7
7
  "description": "CuttiCat library for working with LibreTranslate",
8
8
  "author": "Maksim Victorovich Fomin",
9
9
  "repository": {
@@ -20,20 +20,22 @@
20
20
  "cutticat"
21
21
  ],
22
22
  "type": "module",
23
+ "imports": {
24
+ "#/*": "./lib/*"
25
+ },
23
26
  "engines": {
24
- "node": ">=22.0.0"
27
+ "node": ">=24.14.0"
25
28
  },
26
29
  "dependencies": {
27
30
  "@cutticat/types": "^1.7.2",
28
31
  "zod": "^4.4.3"
29
32
  },
30
33
  "devDependencies": {
31
- "@biomejs/biome": "^2.4.16",
32
- "@types/node": "^25.9.1",
33
- "rimraf": "^6.1.3",
34
- "tsdown": "0.19.0-beta.2",
35
- "tsx": "^4.22.4",
36
- "typescript": "^5.9.3"
34
+ "@biomejs/biome": "^2.6.7",
35
+ "@cutticat/md-url-map": "^2.1.0",
36
+ "@types/node": "^24.19.2",
37
+ "tsdown": "0.23.7",
38
+ "typescript": "^7.0.2"
37
39
  },
38
40
  "exports": {
39
41
  ".": {
@@ -52,10 +54,9 @@
52
54
  "lint:fix:unsafe": "biome check --write --unsafe .",
53
55
  "format": "biome format --write .",
54
56
  "format:check": "biome format .",
55
- "clean": "rimraf ./dist",
56
- "build": "tsc --noEmit -p ./tsconfig.build.json && tsdown",
57
- "test": "tsx --test \"./tests/**/*.test.ts\"",
58
- "test:watch": "tsx --test --watch \"./tests/**/*.test.ts\"",
59
- "test:coverage": "tsx --test --experimental-test-coverage \"./tests/**/*.test.ts\""
57
+ "build": "tsc -p ./tsconfig.build.json && tsdown",
58
+ "test": "tsc && node --test \"./tests/**/*.test.ts\"",
59
+ "test:watch": "node --test --watch \"./tests/**/*.test.ts\"",
60
+ "test:coverage": "node --test --experimental-test-coverage \"./tests/**/*.test.ts\""
60
61
  }
61
62
  }