@belocal/js-sdk 0.1.0 → 0.1.2

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.
Binary file
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@belocal/js-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "BeLocal runtime JS SDK for on-demand translation (browser + node)",
5
- "main": "index.js",
6
5
  "scripts": {
7
6
  "test": "echo \"Error: no test specified\" && exit 1",
8
7
  "build": "tsup",
@@ -27,15 +26,16 @@
27
26
  "tsup": "^8.5.0",
28
27
  "typescript": "^5.9.2"
29
28
  },
29
+ "main": "./dist/index.cjs",
30
+ "module": "./dist/index.mjs",
31
+ "types": "./dist/index.d.ts",
30
32
  "exports": {
31
33
  ".": {
32
- "browser": "./dist/browser.mjs",
33
- "node": "./dist/node.mjs",
34
+ "types": "./dist/index.d.ts",
34
35
  "import": "./dist/index.mjs",
35
36
  "require": "./dist/index.cjs",
36
37
  "default": "./dist/index.mjs"
37
38
  }
38
39
  },
39
- "types": "./dist/index.d.ts",
40
40
  "sideEffects": false
41
41
  }
@@ -4,6 +4,7 @@ import { createNodeTransport } from '../transports/node';
4
4
 
5
5
  export class BelocalEngine {
6
6
  private transport: Transport;
7
+ private debug: boolean;
7
8
 
8
9
  constructor(options: BelocalEngineOptions) {
9
10
  const {
@@ -14,9 +15,12 @@ export class BelocalEngine {
14
15
  retries = 3,
15
16
  credentials,
16
17
  headers = {},
17
- agent
18
+ agent,
19
+ debug = false
18
20
  } = options;
19
21
 
22
+ this.debug = debug;
23
+
20
24
  const authHeaders = {
21
25
  'Authorization': `Bearer ${apiKey}`,
22
26
  ...headers
@@ -28,7 +32,8 @@ export class BelocalEngine {
28
32
  retries,
29
33
  credentials,
30
34
  headers: authHeaders,
31
- agent
35
+ agent,
36
+ debug: this.debug
32
37
  });
33
38
  }
34
39
 
@@ -45,12 +50,22 @@ export class BelocalEngine {
45
50
  credentials?: RequestCredentials;
46
51
  headers: Record<string, string>;
47
52
  agent?: unknown;
53
+ debug: boolean;
48
54
  }
49
55
  ): Transport {
50
56
  const actualTransport = transportType === 'auto' ? this.detectEnvironment() : transportType;
51
-
52
57
  const path = '/v1/translate';
53
58
 
59
+ if (config.debug) {
60
+ console.log(`[BeLocal Engine] Creating ${actualTransport} transport with config:`, {
61
+ baseUrl: config.baseUrl,
62
+ path,
63
+ timeoutMs: config.timeoutMs,
64
+ ...(actualTransport === 'browser' && { credentials: config.credentials }),
65
+ ...(actualTransport === 'node' && { retries: config.retries, hasAgent: !!config.agent })
66
+ });
67
+ }
68
+
54
69
  if (actualTransport === 'browser') {
55
70
  return createBrowserTransport({
56
71
  baseUrl: config.baseUrl,
@@ -58,6 +73,7 @@ export class BelocalEngine {
58
73
  credentials: config.credentials,
59
74
  headers: config.headers,
60
75
  timeoutMs: config.timeoutMs,
76
+ debug: config.debug,
61
77
  });
62
78
  } else {
63
79
  return createNodeTransport({
@@ -67,6 +83,7 @@ export class BelocalEngine {
67
83
  timeoutMs: config.timeoutMs,
68
84
  agent: config.agent,
69
85
  retries: config.retries,
86
+ debug: config.debug,
70
87
  });
71
88
  }
72
89
  }
package/src/core/types.ts CHANGED
@@ -16,4 +16,5 @@ export interface BelocalEngineOptions {
16
16
  credentials?: RequestCredentials;
17
17
  headers?: Record<string, string>;
18
18
  agent?: unknown;
19
+ debug?: boolean; // default: false - включает логирование
19
20
  }
@@ -6,6 +6,7 @@ export interface BrowserTransportConfig {
6
6
  credentials?: RequestCredentials;
7
7
  headers?: Record<string, string>;
8
8
  timeoutMs?: number;
9
+ debug?: boolean;
9
10
  }
10
11
 
11
12
  export function createBrowserTransport(config: BrowserTransportConfig): Transport {
@@ -14,6 +15,10 @@ export function createBrowserTransport(config: BrowserTransportConfig): Transpor
14
15
  const controller = new AbortController();
15
16
  const timeoutId = config.timeoutMs ? setTimeout(() => controller.abort(), config.timeoutMs) : null;
16
17
 
18
+ if (config.debug) {
19
+ console.log(`[BeLocal Browser Transport] Translating "${text}" to ${lang} with url ${url}`);
20
+ }
21
+
17
22
  try {
18
23
  const response = await fetch(url, {
19
24
  method: 'POST',
@@ -27,10 +32,17 @@ export function createBrowserTransport(config: BrowserTransportConfig): Transpor
27
32
  });
28
33
 
29
34
  if (!response.ok) {
30
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
35
+ const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
36
+ if (config.debug) {
37
+ console.error(`[BeLocal Browser Transport] Request failed:`, errorMsg);
38
+ }
39
+ throw new Error(errorMsg);
31
40
  }
32
41
 
33
42
  const result = await response.json();
43
+ if (config.debug) {
44
+ console.log(`[BeLocal Browser Transport] Translation successful: "${result.text || text}"`);
45
+ }
34
46
  return result.text || text;
35
47
  } finally {
36
48
  if (timeoutId) {
@@ -7,6 +7,7 @@ export interface NodeTransportConfig {
7
7
  timeoutMs?: number;
8
8
  agent?: unknown;
9
9
  retries?: number;
10
+ debug?: boolean;
10
11
  }
11
12
 
12
13
  export function createNodeTransport(config: NodeTransportConfig): Transport {
@@ -15,6 +16,10 @@ export function createNodeTransport(config: NodeTransportConfig): Transport {
15
16
  const maxRetries = config.retries || 0;
16
17
  let attempt = 0;
17
18
 
19
+ if (config.debug) {
20
+ console.log(`[BeLocal Node Transport] Translating "${text}" to ${lang} with url ${url}`);
21
+ }
22
+
18
23
  while (attempt <= maxRetries) {
19
24
  try {
20
25
  const controller = new AbortController();
@@ -36,6 +41,9 @@ export function createNodeTransport(config: NodeTransportConfig): Transport {
36
41
  }
37
42
 
38
43
  const result = await response.json();
44
+ if (config.debug) {
45
+ console.log(`[BeLocal Node Transport] Translation successful: "${result.text || text}"`);
46
+ }
39
47
  return result.text || text;
40
48
  } finally {
41
49
  if (timeoutId) {
@@ -44,6 +52,9 @@ export function createNodeTransport(config: NodeTransportConfig): Transport {
44
52
  }
45
53
  } catch (error) {
46
54
  attempt++;
55
+ if (config.debug) {
56
+ console.error(`[BeLocal Node Transport] Attempt ${attempt} failed:`, error instanceof Error ? error.message : String(error));
57
+ }
47
58
  if (attempt > maxRetries) {
48
59
  throw error;
49
60
  }
Binary file