@belocal/js-sdk 1.0.0 → 1.0.3
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 +78 -40
- package/dist/browser.cjs +2 -2
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.ts +31 -177
- package/dist/browser.mjs +2 -2
- package/dist/browser.mjs.map +1 -1
- package/dist/node.cjs +97 -170
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.ts +32 -177
- package/dist/node.mjs +96 -171
- package/dist/node.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,80 +1,118 @@
|
|
|
1
1
|
# @belocal/js-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
On-demand translation SDK for browser and Node.js.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
|
+
|
|
6
7
|
```bash
|
|
7
8
|
npm i @belocal/js-sdk
|
|
8
9
|
```
|
|
9
10
|
|
|
10
11
|
## Usage
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
### Initialization
|
|
13
14
|
|
|
14
15
|
```javascript
|
|
15
16
|
import { BelocalEngine } from '@belocal/js-sdk';
|
|
16
17
|
|
|
17
|
-
const engine = new BelocalEngine({
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const engine = new BelocalEngine({ apiKey: 'your-api-key' });
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### translate
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
```javascript
|
|
24
|
+
const text = await engine.translate('Hello world', 'es');
|
|
23
25
|
```
|
|
24
26
|
|
|
25
|
-
###
|
|
27
|
+
### translateMany
|
|
26
28
|
|
|
27
29
|
```javascript
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
const texts = await engine.translateMany(['Hello', 'World'], 'es');
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### All options
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
const text = await engine.translate('crane', 'ru', {
|
|
37
|
+
source_lang: 'en',
|
|
38
|
+
context: 'website dedicated to animals',
|
|
39
|
+
managed: true,
|
|
40
|
+
ctx: {
|
|
41
|
+
user_type: 'product',
|
|
42
|
+
entity_key: 'birds',
|
|
43
|
+
entity_id: 'crane-001'
|
|
44
|
+
}
|
|
31
45
|
});
|
|
32
46
|
```
|
|
33
47
|
|
|
34
|
-
##
|
|
48
|
+
## API Reference
|
|
35
49
|
|
|
36
|
-
|
|
50
|
+
### translate(text, lang, options?)
|
|
37
51
|
|
|
38
|
-
|
|
39
|
-
- **Node.js environments**: Uses HTTP/2 with connection pooling and automatic retries (~4.3KB)
|
|
40
|
-
- **CommonJS**: Supported in both environments
|
|
52
|
+
Translates a single text string.
|
|
41
53
|
|
|
42
|
-
|
|
54
|
+
| Param | Type | Description |
|
|
55
|
+
|-------|------|-------------|
|
|
56
|
+
| text | string | Text to translate |
|
|
57
|
+
| lang | string | Target language code |
|
|
58
|
+
| options | [TranslateOptions](#translateoptions) | Optional translation options |
|
|
43
59
|
|
|
44
|
-
|
|
45
|
-
|------|-------------|--------|------|----------|
|
|
46
|
-
| `browser.mjs` | Browser | ESM | ~1.3KB | Fetch API, minified |
|
|
47
|
-
| `browser.cjs` | Browser | CJS | ~1.3KB | Fetch API, minified |
|
|
48
|
-
| `node.mjs` | Node.js | ESM | ~4.3KB | HTTP/2, retries |
|
|
49
|
-
| `node.cjs` | Node.js | CJS | ~4.8KB | HTTP/2, retries |
|
|
60
|
+
### translateMany(texts, lang, options?)
|
|
50
61
|
|
|
51
|
-
|
|
62
|
+
Translates multiple texts in a single batch.
|
|
52
63
|
|
|
53
|
-
|
|
64
|
+
| Param | Type | Description |
|
|
65
|
+
|-------|------|-------------|
|
|
66
|
+
| texts | string[] | Texts to translate |
|
|
67
|
+
| lang | string | Target language code |
|
|
68
|
+
| options | [TranslateOptions](#translateoptions) | Optional translation options |
|
|
54
69
|
|
|
55
|
-
|
|
56
|
-
|
|
70
|
+
### TranslateOptions
|
|
71
|
+
|
|
72
|
+
| Property | Type | Description |
|
|
73
|
+
|----------|------|-------------|
|
|
74
|
+
| source_lang | string | Source language of the text |
|
|
75
|
+
| context | string | Context for better understanding of what is being translated |
|
|
76
|
+
| managed | boolean | Use managed translations cache (available on the website in Managed Translations section) |
|
|
77
|
+
| ctx | object | User context, see [ctx properties](#ctx-properties) below |
|
|
78
|
+
|
|
79
|
+
#### ctx properties
|
|
80
|
+
|
|
81
|
+
| Property | Description |
|
|
82
|
+
|----------|-------------|
|
|
83
|
+
| user_type | `product` or `chat`. Correct type improves translation quality |
|
|
84
|
+
| entity_key | Used together with entity_id. When passed with the same values, a shared context is always used |
|
|
85
|
+
| entity_id | Used together with entity_key. When passed with the same values, a shared context is always used |
|
|
57
86
|
|
|
58
|
-
|
|
59
|
-
apiKey: 'your-api-key',
|
|
60
|
-
debug: true
|
|
61
|
-
};
|
|
87
|
+
## Options
|
|
62
88
|
|
|
63
|
-
|
|
89
|
+
```javascript
|
|
90
|
+
const engine = new BelocalEngine({
|
|
91
|
+
apiKey: 'your-api-key', // required
|
|
92
|
+
batchWindowMs: 50, // batch window in ms (default: 50)
|
|
93
|
+
timeoutMs: 10000, // request timeout in ms (default: 10000)
|
|
94
|
+
debug: false // enable debug logging (default: false)
|
|
95
|
+
});
|
|
64
96
|
```
|
|
65
97
|
|
|
66
|
-
|
|
98
|
+
### batchWindowMs
|
|
67
99
|
|
|
68
|
-
|
|
100
|
+
The SDK batches multiple `translate()` calls made within a time window into a single HTTP request. When you call `translate()`, the request is not sent immediately — instead it is queued. After `batchWindowMs` milliseconds of inactivity, all queued texts are grouped by target language and sent as one batch request to `/v1/translate/multi`. This reduces the number of HTTP round-trips when translating many strings at once (e.g. rendering a page). Default is `50`ms.
|
|
69
101
|
|
|
70
|
-
|
|
71
|
-
# Run all tests
|
|
72
|
-
npm test
|
|
102
|
+
## TypeScript
|
|
73
103
|
|
|
74
|
-
|
|
75
|
-
|
|
104
|
+
```typescript
|
|
105
|
+
import {
|
|
106
|
+
BelocalEngine,
|
|
107
|
+
type BelocalEngineOptions,
|
|
108
|
+
type Lang,
|
|
109
|
+
type TranslateOptions,
|
|
110
|
+
type UserCtx,
|
|
111
|
+
USER_TYPE_PRODUCT,
|
|
112
|
+
USER_TYPE_CHAT
|
|
113
|
+
} from '@belocal/js-sdk';
|
|
76
114
|
```
|
|
77
115
|
|
|
78
116
|
## License
|
|
79
117
|
|
|
80
|
-
MIT
|
|
118
|
+
MIT
|
package/dist/browser.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var jsMd5=require('js-md5');var
|
|
2
|
-
exports.BaseBrowserTransport=
|
|
1
|
+
'use strict';var jsMd5=require('js-md5');var M="1.0.3";function E(a,t){let e=s=>{if(s==null)return s;if(Array.isArray(s))return s.map(e);if(typeof s=="object"){let i={};for(let u of Object.keys(s).sort())i[u]=e(s[u]);return i}return s},r=JSON.stringify(e(t));return `${a}:${r}`}var m=class{constructor(t,e){this.wrappedTransport=t;this.debug=e;this.inFlightRequests=new Map;}async post(t,e){let r=E(e,t),s=this.inFlightRequests.get(r);if(s)return this.debug&&console.log(`[DedupeTransport] Deduplicating request to ${e}`),s;let i=this.wrappedTransport.post(t,e).finally(()=>{this.inFlightRequests.delete(r);});return this.inFlightRequests.set(r,i),this.debug&&console.log(`[DedupeTransport] New request to ${e} (${this.inFlightRequests.size} in-flight)`),i}};var R="https://dynamic.belocal.dev",d=class{constructor(t){this.config=t;}async post(t,e){let r=`${R}${e}`,s=new AbortController,i=this.config.timeoutMs??1e4,u=setTimeout(()=>s.abort(),i);this.config.debug&&console.log(`[Base Browser Transport] POST request to ${r}`,t);try{let g=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json","x-sdk":"js","x-sdk-version":M,...this.config.headers},body:JSON.stringify(t),signal:s.signal});if(!g.ok){let o=`HTTP ${g.status}: ${g.statusText}`;throw this.config.debug&&console.error("[Base Browser Transport] Request failed:",o),new Error(o)}let n=await g.json();return this.config.debug&&console.log("[Base Browser Transport] Request successful:",n),n}finally{u&&clearTimeout(u);}}};function h(a){let t=new d(a);return new m(t,a.debug)}function q(a,t,e,r){let s=[...a].sort(),i=r&&Object.keys(r).length>0?Object.fromEntries(Object.entries(r).sort(([g],[n])=>g.localeCompare(n))):null;return jsMd5.md5(JSON.stringify([s,t,e||null,i]))}async function S(a,t,e){a.debug&&console.log(`[BeLocal Multi Transport] Sending multi request with ${t.length} texts`);try{let r=new Map;t.forEach(n=>{let o=JSON.stringify({lang:n.lang,sourceLang:n.sourceLang||null,context:n.context||null});r.has(o)||r.set(o,[]),r.get(o).push(n);});let s=new Map,i=Array.from(r.entries()).map(([n,o])=>{let c=o[0],l=o.map(f=>f.text),p=q(l,c.lang,c.sourceLang,c.context);return s.set(p,o),{request_id:p,texts:l,lang:c.lang,source_lang:c.sourceLang,ctx:c.context}}),u=await a.baseTransport.post({requests:i},"/v1/translate/multi");a.debug&&console.log(`[BeLocal Multi Transport] Multi response received with ${u.results.length} groups`);let g=new Map;u.results.forEach(n=>{g.set(n.request_id,{texts:n.data.texts,status:n.data.status});}),s.forEach((n,o)=>{let c=g.get(o);if(!c){a.debug&&console.error(`[BeLocal Multi Transport] No result found for request_id: ${o}`),n.forEach(l=>{l.reject(new Error(`No result found for request ${o}`));});return}if(c.texts.length!==n.length){let l=new Error(`Mismatch: expected ${n.length} texts, got ${c.texts.length} for request_id ${o}`);a.debug&&console.error("[BeLocal Multi Transport]",l.message),n.forEach(p=>p.reject(l));return}n.forEach((l,p)=>{let f=c.texts[p],_=c.status||"success";a.debug&&console.log(`[BeLocal Multi Transport] Success for request_id ${o}[${p}]: "${f}"`),l.resolve({text:f,status:_});});});}catch(r){a.debug&&console.error("[BeLocal Multi Transport] Multi request error:",r);let s=r instanceof Error?r:new Error(String(r));t.forEach(i=>i.reject(s));}}function B(a,t){if(t.currentMulti.length===0||t.isRequestInFlight)return;let e=[...t.currentMulti];t.currentMulti=[],t.multiTimer=null,t.isRequestInFlight=true,S(a,e).finally(()=>{if(t.isRequestInFlight=false,t.currentMulti.length>0){let r=a.batchWindowMs??50;t.multiTimer=setTimeout(()=>B(a,t),r);}});}function x(a){let t=a.batchWindowMs??50,e={currentMulti:[],multiTimer:null,isRequestInFlight:false},r=(({text:s,lang:i,source_lang:u,ctx:g})=>new Promise((n,o)=>{if(a.debug){let l=jsMd5.md5(JSON.stringify([s,i,u||null,g||null]));console.log(`[BeLocal Multi Transport] Queuing request ${l}: "${s}" to ${i}`);}let c={text:s,lang:i,sourceLang:u,context:g,resolve:n,reject:o};e.currentMulti.push(c),e.multiTimer===null&&!e.isRequestInFlight&&(e.multiTimer=setTimeout(()=>B(a,e),t));}));return r.destroy=()=>{e.multiTimer&&(clearTimeout(e.multiTimer),e.multiTimer=null),e.currentMulti.forEach(s=>s.reject(new Error("Transport destroyed"))),e.currentMulti=[];},r}var T=class{constructor(t={}){this.storage=new Map;this.maxSize=t.maxSize??1e4,this.ttlMs=t.ttlMs??36e5;}get(t){let e=this.storage.get(t);return e===void 0?null:Date.now()>e.expiresAt?(this.storage.delete(t),null):(this.storage.delete(t),this.storage.set(t,e),e.value)}set(t,e){if(this.storage.delete(t),this.storage.size>=this.maxSize){let r=this.storage.keys().next().value;r!==void 0&&this.storage.delete(r);}this.storage.set(t,{value:e,expiresAt:Date.now()+this.ttlMs});}clear(){this.storage.clear();}};var y=class{constructor(t,e){let{apiKey:r,batchWindowMs:s=50,timeoutMs:i=1e4,debug:u=false,cacheTtlMs:g=36e5,cacheMaxSize:n=1e4}=t;if(!r||typeof r!="string")throw new Error("[BeLocal] apiKey is required and must be a non-empty string");if(typeof i!="number"||!Number.isFinite(i)||i<=0)throw new Error("[BeLocal] timeoutMs must be a positive finite number");let o=Math.max(0,s);this.debug=u,this.cache=new T({ttlMs:g,maxSize:n});let c={Authorization:`Bearer ${r}`},l=e({headers:c,timeoutMs:i,debug:this.debug});this.transport=x({baseTransport:l,debug:this.debug,batchWindowMs:o}),this.debug&&console.log("[BeLocal Engine] Multi transport created with config:",{baseUrl:"https://dynamic.belocal.dev",timeoutMs:i,batchWindowMs:o});}async translate(t,e,r){return (await this.translateMany([t],e,r))[0]}async translateMany(t,e,r){let{source_lang:s,ctx:i}=this.resolveOptions(r),u=new Array(t.length),g=[];for(let n=0;n<t.length;n++){let o=t[n],c=this.generateCacheKey(o,e,s,i),l=this.cache.get(c);if(l!==null){u[n]=l,this.debug&&console.log("[BeLocal Engine] Translation from cache:",o);continue}u[n]=null,g.push({index:n,text:o});}return g.length>0&&(await Promise.allSettled(g.map(async({index:o,text:c})=>{let l=await this.transport({text:c,lang:e,source_lang:s,ctx:i});if(l.status!=="error"){let p=this.generateCacheKey(c,e,s,i);this.cache.set(p,l.text),this.debug&&console.log("[BeLocal Engine] Translation from API, cached:",c);}else this.debug&&console.log("[BeLocal Engine] Translation from API (not cached due to error status):",c);return {index:o,translation:l.text}}))).forEach((o,c)=>{let{index:l,text:p}=g[c];if(o.status==="fulfilled")u[l]=o.value.translation;else if(u[l]=p,this.debug){let f=o.reason;console.error("[BeLocal Engine] Translation failed, using original text:",p,f instanceof Error?f.message:String(f));}}),u}resolveOptions(t){if(!t)return {};let e={...t.ctx,...t.context&&{user_ctx:t.context},...t.managed&&{cache_type:"managed"}},r=Object.values(e).some(Boolean)?e:void 0;return {source_lang:t.source_lang,ctx:r}}destroy(){this.transport.destroy?.(),this.cache.clear();}generateCacheKey(t,e,r,s){let i=s?Object.fromEntries(Object.entries(s).sort(([g],[n])=>g.localeCompare(n))):null;return jsMd5.md5(JSON.stringify({text:t,lang:e,source_lang:r||null,ctx:i}))}};var v="product",L="chat";var w=class extends y{constructor(t){super(t,h);}};
|
|
2
|
+
exports.BaseBrowserTransport=d;exports.BelocalEngine=w;exports.USER_TYPE_CHAT=L;exports.USER_TYPE_PRODUCT=v;exports.createBaseBrowserTransport=h;exports.createMultiTransport=x;//# sourceMappingURL=browser.cjs.map
|
|
3
3
|
//# sourceMappingURL=browser.cjs.map
|
package/dist/browser.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/version.ts","../src/transports/base/dedupe.ts","../src/transports/base/browser.ts","../src/transports/multi.ts","../src/cache/local.ts","../src/core/engine/engine.ts","../src/core/engine/browser.ts"],"names":["SDK_VERSION","SDK_NAME","generateRequestKey","endpointPath","data","normalize","value","sorted","key","normalizedData","DedupeTransport","wrappedTransport","debug","requestKey","existingRequest","requestPromise","BASE_URL","BaseBrowserTransport","config","url","controller","timeoutMs","timeoutId","response","errorMsg","result","createBaseBrowserTransport","transport","generateRequestId","texts","lang","sourceLang","context","sortedTexts","sortedContext","sortedKeys","json","md5","sendMulti","items","state","groups","item","groupKey","requestIdToGroupItems","requests","groupItems","firstItem","requestId","multiResponse","resultMap","error","index","translatedText","status","errorToReject","processMulti","itemsToSend","windowMs","createMultiTransport","text","source_lang","ctx","resolve","reject","tempRequestId","requestItem","LocalCache","BelocalEngine","options","baseTransportFactory","apiKey","batchWindowMs","authHeaders","baseTransport","results","cacheMisses","i","cacheKey","cachedResult","translation","sortedCtx","acc"],"mappings":"yCAIO,IAAMA,GACV,IAAM,CAAE,GAAI,CAAE,OAAgD,OAA+B,CAAA,KAAQ,CAAE,OAAO,WAAa,CAAE,CAAA,GAAG,CAEtHC,CAAAA,CAAW,KCDxB,SAASC,CAAAA,CAAmBC,CAAAA,CAAsBC,CAAAA,CAAmB,CAEnE,IAAMC,CAAAA,CAAaC,CAAAA,EAAoB,CACrC,GAAIA,CAAAA,EAAU,IAAA,CACZ,OAAOA,CAAAA,CAET,GAAI,KAAA,CAAM,OAAA,CAAQA,CAAK,CAAA,CACrB,OAAOA,CAAAA,CAAM,GAAA,CAAID,CAAS,CAAA,CAE5B,GAAI,OAAOC,CAAAA,EAAU,QAAA,CAAU,CAC7B,IAAMC,CAAAA,CAA8B,EAAC,CACrC,IAAA,IAAWC,KAAO,MAAA,CAAO,IAAA,CAAKF,CAAK,CAAA,CAAE,MAAK,C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MA,CAAAA,CAAS3C,CAA0B,EAC3C,CACF","file":"browser.cjs","sourcesContent":["// SDK version - will be replaced during build with tsup define\ndeclare const __SDK_VERSION__: string | undefined;\n\n// Safely check if __SDK_VERSION__ is defined (replaced during build)\nexport const SDK_VERSION: string = \n (() => { try { return typeof __SDK_VERSION__ !== 'undefined' ? __SDK_VERSION__ : 'undefined'; } catch { return 'undefined'; } })();\n\nexport const SDK_NAME = 'js';\n\n","import type { BaseTransport } from '../../core/types';\n\n/**\n * Generates a deterministic key for request deduplication.\n * Uses sorted keys to ensure consistent key generation regardless of object property order.\n */\nfunction generateRequestKey(endpointPath: string, data: any): string {\n // Normalize data by sorting object keys for consistent serialization\n const normalize = (value: any): any => {\n if (value === null || value === undefined) {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(normalize);\n }\n if (typeof value === 'object') {\n const sorted: Record<string, any> = {};\n for (const key of Object.keys(value).sort()) {\n sorted[key] = normalize(value[key]);\n }\n return sorted;\n }\n return value;\n };\n \n const normalizedData = JSON.stringify(normalize(data));\n return `${endpointPath}:${normalizedData}`;\n}\n\n/**\n * DedupeTransport wraps a BaseTransport instance and deduplicates in-flight requests.\n * If multiple identical requests (same endpointPath + same request body) are made\n * concurrently, they will share the same underlying HTTP request and promise.\n */\nexport class DedupeTransport implements BaseTransport {\n private inFlightRequests = new Map<string, Promise<any>>();\n\n constructor(\n private wrappedTransport: BaseTransport,\n private debug?: boolean\n ) {}\n\n async post(data: any, endpointPath: string): Promise<any> {\n const requestKey = generateRequestKey(endpointPath, data);\n\n // Check if an identical request is already in-flight\n const existingRequest = this.inFlightRequests.get(requestKey);\n if (existingRequest) {\n if (this.debug) {\n console.log(`[DedupeTransport] Deduplicating request to ${endpointPath}`);\n }\n return existingRequest;\n }\n\n // Create new request and store it\n const requestPromise = this.wrappedTransport.post(data, endpointPath)\n .finally(() => {\n // Clean up when request completes (success or failure)\n this.inFlightRequests.delete(requestKey);\n });\n\n this.inFlightRequests.set(requestKey, requestPromise);\n\n if (this.debug) {\n console.log(`[DedupeTransport] New request to ${endpointPath} (${this.inFlightRequests.size} in-flight)`);\n }\n\n return requestPromise;\n }\n}\n","import type { BaseTransport } from '../../core/types';\nimport { SDK_NAME, SDK_VERSION } from '../../version';\nimport { DedupeTransport } from './dedupe';\n\nconst BASE_URL = 'https://dynamic.belocal.dev';\n\nexport interface BaseBrowserTransportConfig {\n headers?: Record<string, string>;\n timeoutMs?: number;\n debug?: boolean;\n}\n\nexport class BaseBrowserTransport implements BaseTransport {\n constructor(private config: BaseBrowserTransportConfig) {}\n\n async post(data: any, endpointPath: string): Promise<any> {\n const url = `${BASE_URL}${endpointPath}`;\n const controller = new AbortController();\n const timeoutMs = this.config.timeoutMs ?? 10000;\n const timeoutId = setTimeout(() => controller.abort(), timeoutMs);\n\n if (this.config.debug) {\n console.log(`[Base Browser Transport] POST request to ${url}`, data);\n }\n\n try {\n const response = await fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'x-sdk': SDK_NAME,\n 'x-sdk-version': SDK_VERSION,\n ...this.config.headers,\n },\n body: JSON.stringify(data),\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const errorMsg = `HTTP ${response.status}: ${response.statusText}`;\n if (this.config.debug) {\n console.error(`[Base Browser Transport] Request failed:`, errorMsg);\n }\n throw new Error(errorMsg);\n }\n\n const result = await response.json();\n if (this.config.debug) {\n console.log(`[Base Browser Transport] Request successful:`, result);\n }\n \n return result;\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n }\n}\n\nexport function createBaseBrowserTransport(config: BaseBrowserTransportConfig): BaseTransport {\n const transport = new BaseBrowserTransport(config);\n return new DedupeTransport(transport, config.debug);\n}\n","import type { Transport, BaseTransport } from '../core/types';\nimport { md5 } from 'js-md5';\n\nexport interface MultiTransportConfig {\n baseTransport: BaseTransport;\n debug?: boolean;\n batchWindowMs?: number;\n}\n\ninterface MultiRequest {\n request_id: string;\n texts: string[];\n lang: string;\n source_lang?: string;\n ctx?: Record<string, string>;\n}\n\ninterface MultiRequestItem {\n text: string;\n lang: string;\n sourceLang?: string;\n context?: Record<string, string>;\n resolve: (value: { text: string; status: string }) => void;\n reject: (error: Error) => void;\n}\n\ninterface MultiResponse {\n results: Array<{\n request_id: string;\n data: { texts: string[]; status: string };\n }>;\n}\n\ninterface MultiState {\n currentMulti: MultiRequestItem[];\n multiTimer: ReturnType<typeof setTimeout> | null;\n isRequestInFlight: boolean;\n}\n\nfunction generateRequestId(texts: string[], lang: string, sourceLang?: string, context?: Record<string, string>): string {\n const sortedTexts = [...texts].sort();\n \n let sortedContext: Record<string, string> | null = null;\n if (context && Object.keys(context).length > 0) {\n sortedContext = {};\n const sortedKeys = Object.keys(context).sort();\n for (const key of sortedKeys) {\n sortedContext[key] = context[key];\n }\n }\n \n // Include sourceLang for proper grouping of requests with different source languages\n const data = [sortedTexts, lang, sourceLang || null, sortedContext];\n const json = JSON.stringify(data);\n \n return md5(json);\n}\n\nasync function sendMulti(\n config: MultiTransportConfig,\n items: MultiRequestItem[],\n state: MultiState\n): Promise<void> {\n if (config.debug) {\n console.log(`[BeLocal Multi Transport] Sending multi request with ${items.length} texts`);\n }\n\n try {\n // Group texts by translation parameters\n const groups = new Map<string, MultiRequestItem[]>();\n \n items.forEach(item => {\n const groupKey = JSON.stringify({\n lang: item.lang,\n sourceLang: item.sourceLang || null,\n context: item.context || null\n });\n \n if (!groups.has(groupKey)) {\n groups.set(groupKey, []);\n }\n groups.get(groupKey)!.push(item);\n });\n\n const requestIdToGroupItems = new Map<string, MultiRequestItem[]>();\n const requests: MultiRequest[] = Array.from(groups.entries()).map(([groupKey, groupItems]) => {\n const firstItem = groupItems[0];\n const texts = groupItems.map(item => item.text);\n \n const requestId = generateRequestId(\n texts,\n firstItem.lang,\n firstItem.sourceLang,\n firstItem.context\n );\n \n requestIdToGroupItems.set(requestId, groupItems);\n \n return {\n request_id: requestId,\n texts,\n lang: firstItem.lang,\n source_lang: firstItem.sourceLang,\n ctx: firstItem.context\n };\n });\n\n const multiResponse: MultiResponse = await config.baseTransport.post({ requests }, '/v1/translate/multi');\n \n if (config.debug) {\n console.log(`[BeLocal Multi Transport] Multi response received with ${multiResponse.results.length} groups`);\n }\n\n const resultMap = new Map<string, { texts: string[]; status: string }>();\n multiResponse.results.forEach(result => {\n resultMap.set(result.request_id, { texts: result.data.texts, status: result.data.status });\n });\n\n requestIdToGroupItems.forEach((groupItems, requestId) => {\n const result = resultMap.get(requestId);\n \n if (!result) {\n if (config.debug) {\n console.error(`[BeLocal Multi Transport] No result found for request_id: ${requestId}`);\n }\n groupItems.forEach(item => {\n item.reject(new Error(`No result found for request ${requestId}`));\n });\n return;\n }\n\n if (result.texts.length !== groupItems.length) {\n const error = new Error(`Mismatch: expected ${groupItems.length} texts, got ${result.texts.length} for request_id ${requestId}`);\n if (config.debug) {\n console.error(`[BeLocal Multi Transport]`, error.message);\n }\n groupItems.forEach(item => item.reject(error));\n return;\n }\n\n groupItems.forEach((item, index) => {\n const translatedText = result.texts[index];\n const status = result.status || 'success';\n \n if (config.debug) {\n console.log(`[BeLocal Multi Transport] Success for request_id ${requestId}[${index}]: \"${translatedText}\"`);\n }\n \n item.resolve({ text: translatedText, status });\n });\n });\n\n } catch (error) {\n if (config.debug) {\n console.error(`[BeLocal Multi Transport] Multi request error:`, error);\n }\n \n const errorToReject = error instanceof Error ? error : new Error(String(error));\n items.forEach(item => item.reject(errorToReject));\n } finally {\n // Cleanup handled by base transport\n }\n}\n\nfunction processMulti(config: MultiTransportConfig, state: MultiState): void {\n if (state.currentMulti.length === 0 || state.isRequestInFlight) {\n return;\n }\n\n const itemsToSend = [...state.currentMulti];\n state.currentMulti = [];\n state.multiTimer = null;\n state.isRequestInFlight = true;\n\n sendMulti(config, itemsToSend, state).finally(() => {\n state.isRequestInFlight = false;\n \n if (state.currentMulti.length > 0) {\n const windowMs = config.batchWindowMs ?? 50;\n state.multiTimer = setTimeout(() => processMulti(config, state), windowMs);\n }\n });\n}\n\nexport function createMultiTransport(config: MultiTransportConfig): Transport {\n const windowMs = config.batchWindowMs ?? 50;\n\n const state: MultiState = {\n currentMulti: [],\n multiTimer: null,\n isRequestInFlight: false,\n };\n \n return ({ text, lang, source_lang, ctx }) => {\n return new Promise<{ text: string; status: string }>((resolve, reject) => {\n if (config.debug) {\n const tempRequestId = md5(JSON.stringify([text, lang, source_lang || null, ctx || null]));\n console.log(`[BeLocal Multi Transport] Queuing request ${tempRequestId}: \"${text}\" to ${lang}`);\n }\n\n const requestItem: MultiRequestItem = {\n text,\n lang,\n sourceLang: source_lang,\n context: ctx,\n resolve,\n reject,\n };\n\n state.currentMulti.push(requestItem);\n\n if (state.multiTimer === null && !state.isRequestInFlight) {\n state.multiTimer = setTimeout(() => processMulti(config, state), windowMs);\n }\n });\n };\n}\n\n","import type { Cache } from './types';\n\nexport class LocalCache implements Cache {\n private storage = new Map<string, string>();\n\n get(key: string): string | null {\n return this.storage.get(key) || null;\n }\n\n set(key: string, value: string): void {\n this.storage.set(key, value);\n }\n\n isAvailable(): boolean {\n return true;\n }\n}\n","import type { BelocalEngineOptions, KV, Lang, Transport, BaseTransport } from '../types';\nimport { createMultiTransport } from '../../transports/multi';\nimport { LocalCache } from '../../cache/local';\nimport type { Cache } from '../../cache/types';\nimport { md5 } from 'js-md5';\n\n/**\n * Base engine implementation shared between browser and Node.js environments.\n */\nexport class BelocalEngine {\n private transport: Transport;\n private debug: boolean;\n private cache: Cache;\n\n constructor(options: BelocalEngineOptions, baseTransportFactory: (opts: any) => BaseTransport) {\n const {\n apiKey,\n batchWindowMs = 50,\n timeoutMs = 10000,\n debug = false\n } = options;\n\n this.debug = debug;\n this.cache = new LocalCache();\n\n const authHeaders = {\n 'Authorization': `Bearer ${apiKey}`\n };\n\n const baseTransport = baseTransportFactory({\n headers: authHeaders,\n timeoutMs,\n debug: this.debug\n });\n\n this.transport = createMultiTransport({\n baseTransport,\n debug: this.debug,\n batchWindowMs\n });\n \n if (this.debug) {\n console.log('[BeLocal Engine] Multi transport created with config:', {\n baseUrl: 'https://dynamic.belocal.dev',\n timeoutMs,\n batchWindowMs\n });\n }\n }\n\n /**\n * Translates a single text string to the target language.\n * \n * Uses in-memory cache to avoid redundant API calls. Results are automatically cached\n * for subsequent requests with the same parameters.\n * \n * @param text - The text to translate\n * @param lang - Target language code (e.g., 'es', 'fr', 'ru')\n * @param source_lang - Optional source language code. If not provided, auto-detection is used\n * @param ctx - Optional key-value pairs for translation context. Supported keys: `user_ctx` (string - descriptive context), `cache_type` ('managed')\n * @returns Promise resolving to the translated text\n * @throws {Error} If the translation request fails (network error, API error, timeout)\n * \n * @example\n * ```typescript\n * // Simple translation\n * const result = await engine.translate('Hello world', 'es');\n * \n * // With source language\n * const result = await engine.translate('Hello world', 'es', 'en');\n * \n * // With context (user_ctx) - descriptive context helps improve translation quality\n * const result = await engine.translate('Hello world', 'es', undefined, { \n * user_ctx: 'greeting message on the homepage' \n * });\n * \n * // With cache_type\n * const result = await engine.translate('Hello world', 'es', undefined, { cache_type: 'managed' });\n * \n * // With source language and context\n * const result = await engine.translate('Hello world', 'es', 'en', { \n * user_ctx: 'greeting message on the homepage' \n * });\n * ```\n */\n async translate(text: string, lang: Lang, source_lang?: string, ctx?: KV): Promise<string> {\n const results = await this.translateMany([text], lang, source_lang, ctx);\n return results[0];\n }\n\n /**\n * Translates multiple text strings to the target language in a single batch.\n * \n * This method is more efficient than calling `translate()` multiple times as it:\n * - Batches requests together to reduce API calls\n * - Checks cache for each text individually\n * - Only requests translations for cache misses\n * - Maintains the order of input texts in the result array\n * \n * @param texts - Array of texts to translate\n * @param lang - Target language code (e.g., 'es', 'fr', 'ru')\n * @param source_lang - Optional source language code. If not provided, auto-detection is used\n * @param ctx - Optional key-value pairs for translation context. Supported keys: `user_ctx` (string - descriptive context in English), `cache_type` ('managed' | string)\n * @returns Promise resolving to an array of translated texts in the same order as input\n * @throws {Error} If the translation request fails (network error, API error, timeout)\n * \n * @example\n * ```typescript\n * // Translate multiple texts\n * const results = await engine.translateMany(['Hello', 'World', 'Test'], 'es');\n * // Returns: ['Hola', 'Mundo', 'Prueba']\n * \n * // With source language\n * const results = await engine.translateMany(['Hello', 'World'], 'fr', 'en');\n * \n * // With context (user_ctx) - descriptive context helps improve translation quality\n * const results = await engine.translateMany(\n * ['Hello', 'World'], \n * 'es', \n * undefined, \n * { user_ctx: 'greeting message on the homepage' }\n * );\n * \n * // With cache_type\n * const results = await engine.translateMany(\n * ['Hello', 'World'], \n * 'es', \n * undefined, \n * { cache_type: 'managed' }\n * );\n * \n * // Empty array returns empty array\n * const results = await engine.translateMany([], 'es');\n * // Returns: []\n * ```\n */\n async translateMany(texts: string[], lang: Lang, source_lang?: string, ctx?: KV): Promise<string[]> {\n const results: (string | null)[] = new Array(texts.length);\n const cacheMisses: Array<{ index: number; text: string }> = [];\n\n for (let i = 0; i < texts.length; i++) {\n const text = texts[i];\n const cacheKey = this.generateCacheKey(text, lang, source_lang, ctx);\n \n const cachedResult = this.cache.get(cacheKey);\n if (cachedResult) {\n results[i] = cachedResult;\n if (this.debug) {\n console.log('[BeLocal Engine] Translation from cache:', text);\n }\n continue;\n }\n \n results[i] = null;\n cacheMisses.push({ index: i, text });\n }\n\n if (cacheMisses.length > 0) {\n const translations = await Promise.all(\n cacheMisses.map(async ({ index, text }) => {\n const result = await this.transport({ text, lang, source_lang, ctx });\n \n if (result.status !== 'error') {\n const cacheKey = this.generateCacheKey(text, lang, source_lang, ctx);\n this.cache.set(cacheKey, result.text);\n if (this.debug) {\n console.log('[BeLocal Engine] Translation from API, cached:', text);\n }\n } else {\n if (this.debug) {\n console.log('[BeLocal Engine] Translation from API (not cached due to error status):', text);\n }\n }\n \n return { index, translation: result.text };\n })\n );\n\n translations.forEach(({ index, translation }) => {\n results[index] = translation;\n });\n }\n\n return results as string[];\n }\n\n /**\n * Shortcut method for translation with simplified API.\n * \n * This is a convenience method that wraps `translate()`. When `context` is provided as a string,\n * it is automatically wrapped in `{user_ctx: context}` object.\n * \n * @param text - The text to translate\n * @param lang - Target language code (e.g., 'es', 'fr', 'ru')\n * @param source_lang - Optional source language code\n * @param context - Optional descriptive context string in English (will be wrapped as {user_ctx: context})\n * @returns Promise resolving to the translated text\n * @throws {Error} If the translation request fails (network error, API error, timeout)\n * \n * @example\n * ```typescript\n * // Simple translation\n * const result = await engine.t('Hello world', 'es');\n * \n * // With source language\n * const result = await engine.t('Hello world', 'fr', 'en');\n * \n * // With context string (automatically wrapped as {user_ctx: 'greeting message on the homepage'})\n * const result = await engine.t('Hello world', 'es', undefined, 'greeting message on the homepage');\n * \n * // With source language and context\n * const result = await engine.t('Hello world', 'es', 'en', 'greeting message on the homepage');\n * ```\n */\n async t(text: string, lang: Lang, source_lang?: string, context?: string): Promise<string> {\n if (context) {\n return this.translate(text, lang, source_lang, {user_ctx: context});\n }\n return this.translate(text, lang, source_lang);\n }\n\n private generateCacheKey(text: string, lang: Lang, source_lang?: string, ctx?: KV): string {\n const sortedCtx = ctx ? Object.keys(ctx)\n .sort()\n .reduce((acc, key) => {\n acc[key] = ctx[key];\n return acc;\n }, {} as KV) : null;\n\n const data = {\n text,\n lang,\n source_lang: source_lang || null,\n ctx: sortedCtx\n };\n return md5(JSON.stringify(data));\n }\n}\n","import type { BelocalEngineOptions } from '../types';\nimport { createBaseBrowserTransport } from '../../transports/base/browser';\nimport { BelocalEngine as BaseEngine } from './engine';\n\n/**\n * BeLocal translation engine for browser environments.\n * \n * Provides on-demand translation with automatic request batching, caching, and deduplication.\n * Optimized for browser environments using the Fetch API.\n * \n * @example\n * ```typescript\n * const engine = new BelocalEngine({\n * apiKey: 'your-api-key',\n * debug: true\n * });\n * \n * const translated = await engine.translate('Hello world', 'es');\n * ```\n */\nexport class BelocalEngine extends BaseEngine {\n /**\n * Creates a new BelocalEngine instance for browser environments.\n * \n * @param options - Configuration options for the engine\n * @throws {Error} If apiKey is not provided or invalid\n * \n * @example\n * ```typescript\n * const engine = new BelocalEngine({\n * apiKey: 'your-api-key',\n * batchWindowMs: 100,\n * timeoutMs: 10000,\n * debug: false\n * });\n * ```\n */\n constructor(options: BelocalEngineOptions) {\n super(options, createBaseBrowserTransport);\n }\n}\n\n// Re-export types and transports\nexport type { BelocalEngineOptions, Lang, KV, BaseTransport } from '../types';\nexport { createMultiTransport } from '../../transports/multi';\nexport { BaseBrowserTransport, createBaseBrowserTransport } from '../../transports/base';\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/version.ts","../src/transports/base/dedupe.ts","../src/transports/base/browser.ts","../src/transports/multi.ts","../src/cache/local.ts","../src/core/engine/engine.ts","../src/core/types.ts","../src/core/engine/browser.ts"],"names":["SDK_VERSION","generateRequestKey","endpointPath","data","normalize","value","sorted","key","normalizedData","DedupeTransport","wrappedTransport","debug","requestKey","existingRequest","requestPromise","BASE_URL","BaseBrowserTransport","config","url","controller","timeoutMs","timeoutId","response","errorMsg","result","createBaseBrowserTransport","transport","generateRequestId","texts","lang","sourceLang","context","sortedTexts","sortedContext","a","b","md5","sendMulti","items","state","groups","item","groupKey","requestIdToGroupItems","requests","groupItems","firstItem","requestId","multiResponse","resultMap","error","index","translatedText","status","errorToReject","processMulti","itemsToSend","windowMs","createMultiTransport","text","source_lang","ctx","resolve","reject","tempRequestId","requestItem","LocalCache","options","entry","oldestKey","BelocalEngine","baseTransportFactory","apiKey","batchWindowMs","cacheTtlMs","cacheMaxSize","safeBatchWindowMs","authHeaders","baseTransport","results","cacheMisses","i","cacheKey","cachedResult","settlement","reason","serverCtx","sortedCtx","USER_TYPE_PRODUCT","USER_TYPE_CHAT"],"mappings":"yCACO,IAAMA,EAAuD,OAAA,CCCpE,SAASC,EAAmBC,CAAAA,CAAsBC,CAAAA,CAAmB,CACnE,IAAMC,CAAAA,CAAaC,GAAoB,CACrC,GAAIA,GAAU,IAAA,CACZ,OAAOA,EAET,GAAI,KAAA,CAAM,QAAQA,CAAK,CAAA,CACrB,OAAOA,CAAAA,CAAM,GAAA,CAAID,CAAS,CAAA,CAE5B,GAAI,OAAOC,C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CAAAA,CAA8B,CACrG,IAAM0B,CAAAA,CAAY1B,EACd,MAAA,CAAO,WAAA,CAAY,OAAO,OAAA,CAAQA,CAAG,EAAE,IAAA,CAAK,CAAC,CAAC3B,CAAC,CAAA,CAAG,CAACC,CAAC,CAAA,GAAMD,EAAE,aAAA,CAAcC,CAAC,CAAC,CAAC,CAAA,CAC7E,KAQJ,OAAOC,SAAAA,CAAI,KAAK,SAAA,CANH,CACX,KAAAuB,CAAAA,CACA,IAAA,CAAA9B,EACA,WAAA,CAAa+B,CAAAA,EAAe,KAC5B,GAAA,CAAK2B,CACP,CAC8B,CAAC,CACjC,CACF,CAAA,CChJO,IAAMC,EAAoB,SAAA,CACpBC,CAAAA,CAAiB,OCbvB,IAAMnB,CAAAA,CAAN,cAA4BA,CAAW,CAC5C,YAAYH,CAAAA,CAA+B,CACzC,MAAMA,CAAAA,CAAS1C,CAA0B,EAC3C,CACF","file":"browser.cjs","sourcesContent":["declare const __SDK_VERSION__: string;\nexport const SDK_VERSION = typeof __SDK_VERSION__ !== 'undefined' ? __SDK_VERSION__ : 'undefined';\nexport const SDK_NAME = 'js';\n","import type { BaseTransport } from '../../core/types';\n\nfunction generateRequestKey(endpointPath: string, data: any): string {\n const normalize = (value: any): any => {\n if (value === null || value === undefined) {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(normalize);\n }\n if (typeof value === 'object') {\n const sorted: Record<string, any> = {};\n for (const key of Object.keys(value).sort()) {\n sorted[key] = normalize(value[key]);\n }\n return sorted;\n }\n return value;\n };\n\n const normalizedData = JSON.stringify(normalize(data));\n return `${endpointPath}:${normalizedData}`;\n}\n\n/** Deduplicates identical in-flight requests so they share a single HTTP call. */\nexport class DedupeTransport implements BaseTransport {\n private inFlightRequests = new Map<string, Promise<any>>();\n\n constructor(\n private wrappedTransport: BaseTransport,\n private debug?: boolean\n ) {}\n\n async post(data: any, endpointPath: string): Promise<any> {\n const requestKey = generateRequestKey(endpointPath, data);\n\n const existingRequest = this.inFlightRequests.get(requestKey);\n if (existingRequest) {\n if (this.debug) {\n console.log(`[DedupeTransport] Deduplicating request to ${endpointPath}`);\n }\n return existingRequest;\n }\n\n const requestPromise = this.wrappedTransport.post(data, endpointPath)\n .finally(() => {\n this.inFlightRequests.delete(requestKey);\n });\n\n this.inFlightRequests.set(requestKey, requestPromise);\n\n if (this.debug) {\n console.log(`[DedupeTransport] New request to ${endpointPath} (${this.inFlightRequests.size} in-flight)`);\n }\n\n return requestPromise;\n }\n}\n","import type { BaseTransport } from '../../core/types';\nimport { SDK_NAME, SDK_VERSION } from '../../version';\nimport { DedupeTransport } from './dedupe';\n\nconst BASE_URL = 'https://dynamic.belocal.dev';\n\nexport interface BaseBrowserTransportConfig {\n headers?: Record<string, string>;\n timeoutMs?: number;\n debug?: boolean;\n}\n\nexport class BaseBrowserTransport implements BaseTransport {\n constructor(private config: BaseBrowserTransportConfig) {}\n\n async post(data: any, endpointPath: string): Promise<any> {\n const url = `${BASE_URL}${endpointPath}`;\n const controller = new AbortController();\n const timeoutMs = this.config.timeoutMs ?? 10000;\n const timeoutId = setTimeout(() => controller.abort(), timeoutMs);\n\n if (this.config.debug) {\n console.log(`[Base Browser Transport] POST request to ${url}`, data);\n }\n\n try {\n const response = await fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'x-sdk': SDK_NAME,\n 'x-sdk-version': SDK_VERSION,\n ...this.config.headers,\n },\n body: JSON.stringify(data),\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const errorMsg = `HTTP ${response.status}: ${response.statusText}`;\n if (this.config.debug) {\n console.error(`[Base Browser Transport] Request failed:`, errorMsg);\n }\n throw new Error(errorMsg);\n }\n\n const result = await response.json();\n if (this.config.debug) {\n console.log(`[Base Browser Transport] Request successful:`, result);\n }\n \n return result;\n } finally {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n }\n }\n}\n\nexport function createBaseBrowserTransport(config: BaseBrowserTransportConfig): BaseTransport {\n const transport = new BaseBrowserTransport(config);\n return new DedupeTransport(transport, config.debug);\n}\n","import type { Transport, BaseTransport, TranslationCtx } from '../core/types';\nimport { md5 } from 'js-md5';\n\nexport interface MultiTransportConfig {\n baseTransport: BaseTransport;\n debug?: boolean;\n batchWindowMs?: number;\n}\n\ninterface MultiRequest {\n request_id: string;\n texts: string[];\n lang: string;\n source_lang?: string;\n ctx?: TranslationCtx;\n}\n\ninterface MultiRequestItem {\n text: string;\n lang: string;\n sourceLang?: string;\n context?: TranslationCtx;\n resolve: (value: { text: string; status: string }) => void;\n reject: (error: Error) => void;\n}\n\ninterface MultiResponse {\n results: Array<{\n request_id: string;\n data: { texts: string[]; status: string };\n }>;\n}\n\ninterface MultiState {\n currentMulti: MultiRequestItem[];\n multiTimer: ReturnType<typeof setTimeout> | null;\n isRequestInFlight: boolean;\n}\n\nfunction generateRequestId(texts: string[], lang: string, sourceLang?: string, context?: TranslationCtx): string {\n const sortedTexts = [...texts].sort();\n\n const sortedContext = context && Object.keys(context).length > 0\n ? Object.fromEntries(Object.entries(context).sort(([a], [b]) => a.localeCompare(b)))\n : null;\n\n const data = [sortedTexts, lang, sourceLang || null, sortedContext];\n return md5(JSON.stringify(data));\n}\n\nasync function sendMulti(\n config: MultiTransportConfig,\n items: MultiRequestItem[],\n state: MultiState\n): Promise<void> {\n if (config.debug) {\n console.log(`[BeLocal Multi Transport] Sending multi request with ${items.length} texts`);\n }\n\n try {\n const groups = new Map<string, MultiRequestItem[]>();\n\n items.forEach(item => {\n const groupKey = JSON.stringify({\n lang: item.lang,\n sourceLang: item.sourceLang || null,\n context: item.context || null\n });\n\n if (!groups.has(groupKey)) {\n groups.set(groupKey, []);\n }\n groups.get(groupKey)!.push(item);\n });\n\n const requestIdToGroupItems = new Map<string, MultiRequestItem[]>();\n const requests: MultiRequest[] = Array.from(groups.entries()).map(([groupKey, groupItems]) => {\n const firstItem = groupItems[0];\n const texts = groupItems.map(item => item.text);\n\n const requestId = generateRequestId(\n texts,\n firstItem.lang,\n firstItem.sourceLang,\n firstItem.context\n );\n\n requestIdToGroupItems.set(requestId, groupItems);\n\n return {\n request_id: requestId,\n texts,\n lang: firstItem.lang,\n source_lang: firstItem.sourceLang,\n ctx: firstItem.context\n };\n });\n\n const multiResponse: MultiResponse = await config.baseTransport.post({ requests }, '/v1/translate/multi');\n\n if (config.debug) {\n console.log(`[BeLocal Multi Transport] Multi response received with ${multiResponse.results.length} groups`);\n }\n\n const resultMap = new Map<string, { texts: string[]; status: string }>();\n multiResponse.results.forEach(result => {\n resultMap.set(result.request_id, { texts: result.data.texts, status: result.data.status });\n });\n\n requestIdToGroupItems.forEach((groupItems, requestId) => {\n const result = resultMap.get(requestId);\n\n if (!result) {\n if (config.debug) {\n console.error(`[BeLocal Multi Transport] No result found for request_id: ${requestId}`);\n }\n groupItems.forEach(item => {\n item.reject(new Error(`No result found for request ${requestId}`));\n });\n return;\n }\n\n if (result.texts.length !== groupItems.length) {\n const error = new Error(`Mismatch: expected ${groupItems.length} texts, got ${result.texts.length} for request_id ${requestId}`);\n if (config.debug) {\n console.error(`[BeLocal Multi Transport]`, error.message);\n }\n groupItems.forEach(item => item.reject(error));\n return;\n }\n\n groupItems.forEach((item, index) => {\n const translatedText = result.texts[index];\n const status = result.status || 'success';\n\n if (config.debug) {\n console.log(`[BeLocal Multi Transport] Success for request_id ${requestId}[${index}]: \"${translatedText}\"`);\n }\n\n item.resolve({ text: translatedText, status });\n });\n });\n } catch (error) {\n if (config.debug) {\n console.error(`[BeLocal Multi Transport] Multi request error:`, error);\n }\n\n const errorToReject = error instanceof Error ? error : new Error(String(error));\n items.forEach(item => item.reject(errorToReject));\n }\n}\n\nfunction processMulti(config: MultiTransportConfig, state: MultiState): void {\n if (state.currentMulti.length === 0 || state.isRequestInFlight) {\n return;\n }\n\n const itemsToSend = [...state.currentMulti];\n state.currentMulti = [];\n state.multiTimer = null;\n state.isRequestInFlight = true;\n\n sendMulti(config, itemsToSend, state).finally(() => {\n state.isRequestInFlight = false;\n\n if (state.currentMulti.length > 0) {\n const windowMs = config.batchWindowMs ?? 50;\n state.multiTimer = setTimeout(() => processMulti(config, state), windowMs);\n }\n });\n}\n\nexport function createMultiTransport(config: MultiTransportConfig): Transport {\n const windowMs = config.batchWindowMs ?? 50;\n\n const state: MultiState = {\n currentMulti: [],\n multiTimer: null,\n isRequestInFlight: false,\n };\n\n const transport = (({ text, lang, source_lang, ctx }) => {\n return new Promise<{ text: string; status: string }>((resolve, reject) => {\n if (config.debug) {\n const tempRequestId = md5(JSON.stringify([text, lang, source_lang || null, ctx || null]));\n console.log(`[BeLocal Multi Transport] Queuing request ${tempRequestId}: \"${text}\" to ${lang}`);\n }\n\n const requestItem: MultiRequestItem = {\n text,\n lang,\n sourceLang: source_lang,\n context: ctx,\n resolve,\n reject,\n };\n\n state.currentMulti.push(requestItem);\n\n if (state.multiTimer === null && !state.isRequestInFlight) {\n state.multiTimer = setTimeout(() => processMulti(config, state), windowMs);\n }\n });\n }) as Transport & { destroy: () => void };\n\n transport.destroy = () => {\n if (state.multiTimer) {\n clearTimeout(state.multiTimer);\n state.multiTimer = null;\n }\n state.currentMulti.forEach(item =>\n item.reject(new Error('Transport destroyed'))\n );\n state.currentMulti = [];\n };\n\n return transport;\n}\n","import type { Cache, CacheOptions } from './types';\n\ninterface CacheEntry {\n value: string;\n expiresAt: number;\n}\n\nexport class LocalCache implements Cache {\n private storage = new Map<string, CacheEntry>();\n private maxSize: number;\n private ttlMs: number;\n\n constructor(options: CacheOptions = {}) {\n this.maxSize = options.maxSize ?? 10_000;\n this.ttlMs = options.ttlMs ?? 3_600_000;\n }\n\n get(key: string): string | null {\n const entry = this.storage.get(key);\n if (entry === undefined) return null;\n\n if (Date.now() > entry.expiresAt) {\n this.storage.delete(key);\n return null;\n }\n\n this.storage.delete(key);\n this.storage.set(key, entry);\n\n return entry.value;\n }\n\n set(key: string, value: string): void {\n this.storage.delete(key);\n\n if (this.storage.size >= this.maxSize) {\n const oldestKey = this.storage.keys().next().value;\n if (oldestKey !== undefined) {\n this.storage.delete(oldestKey);\n }\n }\n\n this.storage.set(key, {\n value,\n expiresAt: Date.now() + this.ttlMs,\n });\n }\n\n clear(): void {\n this.storage.clear();\n }\n}\n","import type { BelocalEngineOptions, Lang, TranslateOptions, TranslationCtx, Transport, BaseTransport } from '../types';\nimport { createMultiTransport } from '../../transports/multi';\nimport { LocalCache } from '../../cache/local';\nimport type { Cache } from '../../cache/types';\nimport { md5 } from 'js-md5';\n\nexport class BelocalEngine {\n private transport: Transport;\n private debug: boolean;\n private cache: Cache;\n\n constructor(options: BelocalEngineOptions, baseTransportFactory: (opts: any) => BaseTransport) {\n const {\n apiKey,\n batchWindowMs = 50,\n timeoutMs = 10000,\n debug = false,\n cacheTtlMs = 3_600_000,\n cacheMaxSize = 10_000\n } = options;\n\n if (!apiKey || typeof apiKey !== 'string') {\n throw new Error('[BeLocal] apiKey is required and must be a non-empty string');\n }\n\n if (typeof timeoutMs !== 'number' || !Number.isFinite(timeoutMs) || timeoutMs <= 0) {\n throw new Error('[BeLocal] timeoutMs must be a positive finite number');\n }\n\n const safeBatchWindowMs = Math.max(0, batchWindowMs);\n\n this.debug = debug;\n this.cache = new LocalCache({ ttlMs: cacheTtlMs, maxSize: cacheMaxSize });\n\n const authHeaders = {\n 'Authorization': `Bearer ${apiKey}`\n };\n\n const baseTransport = baseTransportFactory({\n headers: authHeaders,\n timeoutMs,\n debug: this.debug\n });\n\n this.transport = createMultiTransport({\n baseTransport,\n debug: this.debug,\n batchWindowMs: safeBatchWindowMs\n });\n\n if (this.debug) {\n console.log('[BeLocal Engine] Multi transport created with config:', {\n baseUrl: 'https://dynamic.belocal.dev',\n timeoutMs,\n batchWindowMs: safeBatchWindowMs\n });\n }\n }\n\n /** Translates a single text string to the target language. */\n async translate(text: string, lang: Lang, options?: TranslateOptions): Promise<string> {\n const results = await this.translateMany([text], lang, options);\n return results[0];\n }\n\n /** Translates multiple text strings to the target language in a single batch. */\n async translateMany(texts: string[], lang: Lang, options?: TranslateOptions): Promise<string[]> {\n const { source_lang, ctx } = this.resolveOptions(options);\n const results: (string | null)[] = new Array(texts.length);\n const cacheMisses: Array<{ index: number; text: string }> = [];\n\n for (let i = 0; i < texts.length; i++) {\n const text = texts[i];\n const cacheKey = this.generateCacheKey(text, lang, source_lang, ctx);\n\n const cachedResult = this.cache.get(cacheKey);\n if (cachedResult !== null) {\n results[i] = cachedResult;\n if (this.debug) {\n console.log('[BeLocal Engine] Translation from cache:', text);\n }\n continue;\n }\n\n results[i] = null;\n cacheMisses.push({ index: i, text });\n }\n\n if (cacheMisses.length > 0) {\n const settlements = await Promise.allSettled(\n cacheMisses.map(async ({ index, text }) => {\n const result = await this.transport({ text, lang, source_lang, ctx });\n\n if (result.status !== 'error') {\n const cacheKey = this.generateCacheKey(text, lang, source_lang, ctx);\n this.cache.set(cacheKey, result.text);\n if (this.debug) {\n console.log('[BeLocal Engine] Translation from API, cached:', text);\n }\n } else {\n if (this.debug) {\n console.log('[BeLocal Engine] Translation from API (not cached due to error status):', text);\n }\n }\n\n return { index, translation: result.text };\n })\n );\n\n settlements.forEach((settlement, i) => {\n const { index, text } = cacheMisses[i];\n if (settlement.status === 'fulfilled') {\n results[index] = settlement.value.translation;\n } else {\n results[index] = text;\n if (this.debug) {\n const reason = settlement.reason;\n console.error(\n '[BeLocal Engine] Translation failed, using original text:',\n text,\n reason instanceof Error ? reason.message : String(reason)\n );\n }\n }\n });\n }\n\n return results as string[];\n }\n\n private resolveOptions(options?: TranslateOptions): { source_lang?: string; ctx?: TranslationCtx } {\n if (!options) return {};\n const serverCtx: TranslationCtx = {\n ...options.ctx,\n ...(options.context && { user_ctx: options.context }),\n ...(options.managed && { cache_type: 'managed' }),\n };\n const ctx = Object.values(serverCtx).some(Boolean) ? serverCtx : undefined;\n return { source_lang: options.source_lang, ctx };\n }\n\n /** Cleans up internal resources (timers, cache). Call when the engine is no longer needed. */\n destroy(): void {\n (this.transport as any).destroy?.();\n this.cache.clear();\n }\n\n private generateCacheKey(text: string, lang: Lang, source_lang?: string, ctx?: TranslationCtx): string {\n const sortedCtx = ctx\n ? Object.fromEntries(Object.entries(ctx).sort(([a], [b]) => a.localeCompare(b)))\n : null;\n\n const data = {\n text,\n lang,\n source_lang: source_lang || null,\n ctx: sortedCtx\n };\n return md5(JSON.stringify(data));\n }\n}\n","export type Lang = string;\n\nexport type UserCtx = {\n user_type?: string;\n entity_key?: string;\n entity_id?: string;\n};\n\nexport type TranslationCtx = {\n user_type?: string;\n user_ctx?: string;\n cache_type?: string;\n entity_key?: string;\n entity_id?: string;\n};\n\nexport const USER_TYPE_PRODUCT = 'product';\nexport const USER_TYPE_CHAT = 'chat';\n\nexport type TranslateOptions = {\n source_lang?: string;\n context?: string;\n managed?: boolean;\n ctx?: UserCtx;\n};\n\nexport interface BaseTransport {\n post(data: any, endpointPath: string): Promise<any>;\n}\n\nexport type Transport = (params: { text: string; lang: Lang; source_lang?: string; ctx?: TranslationCtx }) => Promise<{ text: string; status: string }>;\n\nexport interface BelocalEngineOptions {\n apiKey: string;\n batchWindowMs?: number;\n timeoutMs?: number;\n debug?: boolean;\n cacheTtlMs?: number;\n cacheMaxSize?: number;\n}\n","import type { BelocalEngineOptions } from '../types';\nimport { createBaseBrowserTransport } from '../../transports/base/browser';\nimport { BelocalEngine as BaseEngine } from './engine';\n\nexport class BelocalEngine extends BaseEngine {\n constructor(options: BelocalEngineOptions) {\n super(options, createBaseBrowserTransport);\n }\n}\n\nexport type { BelocalEngineOptions, Lang, TranslateOptions, BaseTransport, TranslationCtx, UserCtx } from '../types';\nexport { USER_TYPE_PRODUCT, USER_TYPE_CHAT } from '../types';\nexport { createMultiTransport } from '../../transports/multi';\nexport { BaseBrowserTransport, createBaseBrowserTransport } from '../../transports/base';\n"]}
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,171 +1,57 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Language code for translation (e.g., 'en', 'es', 'fr', 'ru').
|
|
3
|
-
* ISO 639-1 language codes are recommended.
|
|
4
|
-
*/
|
|
5
1
|
type Lang = string;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
2
|
+
type UserCtx = {
|
|
3
|
+
user_type?: string;
|
|
4
|
+
entity_key?: string;
|
|
5
|
+
entity_id?: string;
|
|
6
|
+
};
|
|
7
|
+
type TranslationCtx = {
|
|
8
|
+
user_type?: string;
|
|
9
|
+
user_ctx?: string;
|
|
10
|
+
cache_type?: string;
|
|
11
|
+
entity_key?: string;
|
|
12
|
+
entity_id?: string;
|
|
13
|
+
};
|
|
14
|
+
declare const USER_TYPE_PRODUCT = "product";
|
|
15
|
+
declare const USER_TYPE_CHAT = "chat";
|
|
16
|
+
type TranslateOptions = {
|
|
17
|
+
source_lang?: string;
|
|
18
|
+
context?: string;
|
|
19
|
+
managed?: boolean;
|
|
20
|
+
ctx?: UserCtx;
|
|
21
|
+
};
|
|
19
22
|
interface BaseTransport {
|
|
20
23
|
post(data: any, endpointPath: string): Promise<any>;
|
|
21
24
|
}
|
|
22
|
-
/**
|
|
23
|
-
* Transport function type for translation requests.
|
|
24
|
-
* @internal
|
|
25
|
-
*/
|
|
26
25
|
type Transport = (params: {
|
|
27
26
|
text: string;
|
|
28
27
|
lang: Lang;
|
|
29
28
|
source_lang?: string;
|
|
30
|
-
ctx?:
|
|
29
|
+
ctx?: TranslationCtx;
|
|
31
30
|
}) => Promise<{
|
|
32
31
|
text: string;
|
|
33
32
|
status: string;
|
|
34
33
|
}>;
|
|
35
|
-
/**
|
|
36
|
-
* Configuration options for BelocalEngine.
|
|
37
|
-
*/
|
|
38
34
|
interface BelocalEngineOptions {
|
|
39
|
-
/** Required: Your BeLocal API key for authentication */
|
|
40
35
|
apiKey: string;
|
|
41
|
-
/** Optional: Batch window in milliseconds for grouping multiple translation requests. Defaults to 50ms */
|
|
42
36
|
batchWindowMs?: number;
|
|
43
|
-
/** Optional: Request timeout in milliseconds. Defaults to 10000ms (10 seconds) */
|
|
44
37
|
timeoutMs?: number;
|
|
45
|
-
/** Optional: Enable debug logging to console. Defaults to false */
|
|
46
38
|
debug?: boolean;
|
|
39
|
+
cacheTtlMs?: number;
|
|
40
|
+
cacheMaxSize?: number;
|
|
47
41
|
}
|
|
48
42
|
|
|
49
|
-
/**
|
|
50
|
-
* Base engine implementation shared between browser and Node.js environments.
|
|
51
|
-
*/
|
|
52
43
|
declare class BelocalEngine$1 {
|
|
53
44
|
private transport;
|
|
54
45
|
private debug;
|
|
55
46
|
private cache;
|
|
56
47
|
constructor(options: BelocalEngineOptions, baseTransportFactory: (opts: any) => BaseTransport);
|
|
57
|
-
/**
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
* @param lang - Target language code (e.g., 'es', 'fr', 'ru')
|
|
65
|
-
* @param source_lang - Optional source language code. If not provided, auto-detection is used
|
|
66
|
-
* @param ctx - Optional key-value pairs for translation context. Supported keys: `user_ctx` (string - descriptive context), `cache_type` ('managed')
|
|
67
|
-
* @returns Promise resolving to the translated text
|
|
68
|
-
* @throws {Error} If the translation request fails (network error, API error, timeout)
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```typescript
|
|
72
|
-
* // Simple translation
|
|
73
|
-
* const result = await engine.translate('Hello world', 'es');
|
|
74
|
-
*
|
|
75
|
-
* // With source language
|
|
76
|
-
* const result = await engine.translate('Hello world', 'es', 'en');
|
|
77
|
-
*
|
|
78
|
-
* // With context (user_ctx) - descriptive context helps improve translation quality
|
|
79
|
-
* const result = await engine.translate('Hello world', 'es', undefined, {
|
|
80
|
-
* user_ctx: 'greeting message on the homepage'
|
|
81
|
-
* });
|
|
82
|
-
*
|
|
83
|
-
* // With cache_type
|
|
84
|
-
* const result = await engine.translate('Hello world', 'es', undefined, { cache_type: 'managed' });
|
|
85
|
-
*
|
|
86
|
-
* // With source language and context
|
|
87
|
-
* const result = await engine.translate('Hello world', 'es', 'en', {
|
|
88
|
-
* user_ctx: 'greeting message on the homepage'
|
|
89
|
-
* });
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
translate(text: string, lang: Lang, source_lang?: string, ctx?: KV): Promise<string>;
|
|
93
|
-
/**
|
|
94
|
-
* Translates multiple text strings to the target language in a single batch.
|
|
95
|
-
*
|
|
96
|
-
* This method is more efficient than calling `translate()` multiple times as it:
|
|
97
|
-
* - Batches requests together to reduce API calls
|
|
98
|
-
* - Checks cache for each text individually
|
|
99
|
-
* - Only requests translations for cache misses
|
|
100
|
-
* - Maintains the order of input texts in the result array
|
|
101
|
-
*
|
|
102
|
-
* @param texts - Array of texts to translate
|
|
103
|
-
* @param lang - Target language code (e.g., 'es', 'fr', 'ru')
|
|
104
|
-
* @param source_lang - Optional source language code. If not provided, auto-detection is used
|
|
105
|
-
* @param ctx - Optional key-value pairs for translation context. Supported keys: `user_ctx` (string - descriptive context in English), `cache_type` ('managed' | string)
|
|
106
|
-
* @returns Promise resolving to an array of translated texts in the same order as input
|
|
107
|
-
* @throws {Error} If the translation request fails (network error, API error, timeout)
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
|
-
* ```typescript
|
|
111
|
-
* // Translate multiple texts
|
|
112
|
-
* const results = await engine.translateMany(['Hello', 'World', 'Test'], 'es');
|
|
113
|
-
* // Returns: ['Hola', 'Mundo', 'Prueba']
|
|
114
|
-
*
|
|
115
|
-
* // With source language
|
|
116
|
-
* const results = await engine.translateMany(['Hello', 'World'], 'fr', 'en');
|
|
117
|
-
*
|
|
118
|
-
* // With context (user_ctx) - descriptive context helps improve translation quality
|
|
119
|
-
* const results = await engine.translateMany(
|
|
120
|
-
* ['Hello', 'World'],
|
|
121
|
-
* 'es',
|
|
122
|
-
* undefined,
|
|
123
|
-
* { user_ctx: 'greeting message on the homepage' }
|
|
124
|
-
* );
|
|
125
|
-
*
|
|
126
|
-
* // With cache_type
|
|
127
|
-
* const results = await engine.translateMany(
|
|
128
|
-
* ['Hello', 'World'],
|
|
129
|
-
* 'es',
|
|
130
|
-
* undefined,
|
|
131
|
-
* { cache_type: 'managed' }
|
|
132
|
-
* );
|
|
133
|
-
*
|
|
134
|
-
* // Empty array returns empty array
|
|
135
|
-
* const results = await engine.translateMany([], 'es');
|
|
136
|
-
* // Returns: []
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
translateMany(texts: string[], lang: Lang, source_lang?: string, ctx?: KV): Promise<string[]>;
|
|
140
|
-
/**
|
|
141
|
-
* Shortcut method for translation with simplified API.
|
|
142
|
-
*
|
|
143
|
-
* This is a convenience method that wraps `translate()`. When `context` is provided as a string,
|
|
144
|
-
* it is automatically wrapped in `{user_ctx: context}` object.
|
|
145
|
-
*
|
|
146
|
-
* @param text - The text to translate
|
|
147
|
-
* @param lang - Target language code (e.g., 'es', 'fr', 'ru')
|
|
148
|
-
* @param source_lang - Optional source language code
|
|
149
|
-
* @param context - Optional descriptive context string in English (will be wrapped as {user_ctx: context})
|
|
150
|
-
* @returns Promise resolving to the translated text
|
|
151
|
-
* @throws {Error} If the translation request fails (network error, API error, timeout)
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* ```typescript
|
|
155
|
-
* // Simple translation
|
|
156
|
-
* const result = await engine.t('Hello world', 'es');
|
|
157
|
-
*
|
|
158
|
-
* // With source language
|
|
159
|
-
* const result = await engine.t('Hello world', 'fr', 'en');
|
|
160
|
-
*
|
|
161
|
-
* // With context string (automatically wrapped as {user_ctx: 'greeting message on the homepage'})
|
|
162
|
-
* const result = await engine.t('Hello world', 'es', undefined, 'greeting message on the homepage');
|
|
163
|
-
*
|
|
164
|
-
* // With source language and context
|
|
165
|
-
* const result = await engine.t('Hello world', 'es', 'en', 'greeting message on the homepage');
|
|
166
|
-
* ```
|
|
167
|
-
*/
|
|
168
|
-
t(text: string, lang: Lang, source_lang?: string, context?: string): Promise<string>;
|
|
48
|
+
/** Translates a single text string to the target language. */
|
|
49
|
+
translate(text: string, lang: Lang, options?: TranslateOptions): Promise<string>;
|
|
50
|
+
/** Translates multiple text strings to the target language in a single batch. */
|
|
51
|
+
translateMany(texts: string[], lang: Lang, options?: TranslateOptions): Promise<string[]>;
|
|
52
|
+
private resolveOptions;
|
|
53
|
+
/** Cleans up internal resources (timers, cache). Call when the engine is no longer needed. */
|
|
54
|
+
destroy(): void;
|
|
169
55
|
private generateCacheKey;
|
|
170
56
|
}
|
|
171
57
|
|
|
@@ -188,40 +74,8 @@ declare class BaseBrowserTransport implements BaseTransport {
|
|
|
188
74
|
}
|
|
189
75
|
declare function createBaseBrowserTransport(config: BaseBrowserTransportConfig): BaseTransport;
|
|
190
76
|
|
|
191
|
-
/**
|
|
192
|
-
* BeLocal translation engine for browser environments.
|
|
193
|
-
*
|
|
194
|
-
* Provides on-demand translation with automatic request batching, caching, and deduplication.
|
|
195
|
-
* Optimized for browser environments using the Fetch API.
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```typescript
|
|
199
|
-
* const engine = new BelocalEngine({
|
|
200
|
-
* apiKey: 'your-api-key',
|
|
201
|
-
* debug: true
|
|
202
|
-
* });
|
|
203
|
-
*
|
|
204
|
-
* const translated = await engine.translate('Hello world', 'es');
|
|
205
|
-
* ```
|
|
206
|
-
*/
|
|
207
77
|
declare class BelocalEngine extends BelocalEngine$1 {
|
|
208
|
-
/**
|
|
209
|
-
* Creates a new BelocalEngine instance for browser environments.
|
|
210
|
-
*
|
|
211
|
-
* @param options - Configuration options for the engine
|
|
212
|
-
* @throws {Error} If apiKey is not provided or invalid
|
|
213
|
-
*
|
|
214
|
-
* @example
|
|
215
|
-
* ```typescript
|
|
216
|
-
* const engine = new BelocalEngine({
|
|
217
|
-
* apiKey: 'your-api-key',
|
|
218
|
-
* batchWindowMs: 100,
|
|
219
|
-
* timeoutMs: 10000,
|
|
220
|
-
* debug: false
|
|
221
|
-
* });
|
|
222
|
-
* ```
|
|
223
|
-
*/
|
|
224
78
|
constructor(options: BelocalEngineOptions);
|
|
225
79
|
}
|
|
226
80
|
|
|
227
|
-
export { BaseBrowserTransport, type BaseTransport, BelocalEngine, type BelocalEngineOptions, type
|
|
81
|
+
export { BaseBrowserTransport, type BaseTransport, BelocalEngine, type BelocalEngineOptions, type Lang, type TranslateOptions, type TranslationCtx, USER_TYPE_CHAT, USER_TYPE_PRODUCT, type UserCtx, createBaseBrowserTransport, createMultiTransport };
|