lexisync 0.2.1 → 1.0.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.
@@ -0,0 +1,138 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import LexiSyncBackend from './backend.js';
3
+
4
+ function jsonRes(body, status = 200) {
5
+ return {
6
+ ok: status >= 200 && status < 300,
7
+ status,
8
+ json: async () => body,
9
+ text: async () => (typeof body === 'string' ? body : JSON.stringify(body)),
10
+ };
11
+ }
12
+
13
+ describe('LexiSyncBackend', () => {
14
+ beforeEach(() => {
15
+ vi.stubGlobal('fetch', vi.fn());
16
+ });
17
+ afterEach(() => {
18
+ vi.unstubAllGlobals();
19
+ });
20
+
21
+ it('exposes type and defaults options', () => {
22
+ expect(LexiSyncBackend.type).toBe('backend');
23
+ const b = new LexiSyncBackend({}, { projectId: 'p', lng: 'nl' });
24
+ expect(b.type).toBe('backend');
25
+ expect(b.options.version).toBe('latest');
26
+ expect(b.options.referenceLng).toBe('nl');
27
+ expect(b.options.sourceLng).toBe('nl'); // deprecated mirror
28
+ expect(b.options.referenceLng).toBe('nl');
29
+ });
30
+
31
+ it('init merges options and defaults version', () => {
32
+ const b = new LexiSyncBackend({}, { projectId: 'p', version: 'production' });
33
+ b.init({}, { apiKey: 'sk' });
34
+ expect(b.options.apiKey).toBe('sk');
35
+ expect(b.options.version).toBe('production');
36
+ b.options.version = undefined;
37
+ b.init({}, {});
38
+ expect(b.options.version).toBe('latest');
39
+ });
40
+
41
+ it('read requires projectId', () => {
42
+ const b = new LexiSyncBackend({}, {});
43
+ const cb = vi.fn();
44
+ b.read('en', 'common', cb);
45
+ expect(cb).toHaveBeenCalledWith(expect.any(Error), null);
46
+ expect(fetch).not.toHaveBeenCalled();
47
+ });
48
+
49
+ it('read uses latest when version is empty', async () => {
50
+ fetch.mockResolvedValueOnce(jsonRes({ ok: 1 }));
51
+ const b = new LexiSyncBackend(
52
+ {},
53
+ { projectId: 'p', version: '', cdnUrl: 'https://cdn.example.com' },
54
+ );
55
+ const cb = vi.fn();
56
+ b.read('en', 'common', cb);
57
+ await vi.waitFor(() => expect(cb).toHaveBeenCalled());
58
+ expect(String(fetch.mock.calls[0][0])).toContain('/latest/');
59
+ });
60
+
61
+ it('read success and failure', async () => {
62
+ fetch.mockResolvedValueOnce(jsonRes({ a: '1' }));
63
+ const b = new LexiSyncBackend(
64
+ {},
65
+ { projectId: 'p', version: 'production', cdnUrl: 'https://cdn.example.com' },
66
+ );
67
+ const cb = vi.fn();
68
+ b.read('en', 'common', cb);
69
+ await vi.waitFor(() => expect(cb).toHaveBeenCalled());
70
+ expect(cb).toHaveBeenCalledWith(null, { a: '1' });
71
+
72
+ fetch.mockResolvedValueOnce(jsonRes('missing', 404));
73
+ const cb2 = vi.fn();
74
+ b.read('en', 'common', cb2);
75
+ await vi.waitFor(() => expect(cb2).toHaveBeenCalled());
76
+ expect(cb2.mock.calls[0][0]).toBeInstanceOf(Error);
77
+ expect(cb2.mock.calls[0][1]).toBeNull();
78
+ });
79
+
80
+ it('create no-ops without apiKey/projectId', () => {
81
+ const b = new LexiSyncBackend({}, { projectId: 'p' });
82
+ b.create('en', 'common', 'k', 'v');
83
+ expect(fetch).not.toHaveBeenCalled();
84
+ });
85
+
86
+ it('create writes to sourceLng with fallback value coercion', async () => {
87
+ fetch.mockResolvedValue(jsonRes({ ok: true }));
88
+ const b = new LexiSyncBackend(
89
+ {},
90
+ { projectId: 'p', apiKey: 'sk', referenceLng: 'nl', apiUrl: 'https://x.example' },
91
+ );
92
+ b.create(undefined, 'common', 'k', undefined);
93
+ await vi.waitFor(() => expect(fetch).toHaveBeenCalled());
94
+ const [url, init] = fetch.mock.calls[0];
95
+ expect(String(url)).toContain('/api/translations');
96
+ const body = JSON.parse(init.body);
97
+ expect(body).toMatchObject({
98
+ projectId: 'p',
99
+ namespace: 'common',
100
+ identifier: 'k',
101
+ languageCode: 'nl',
102
+ value: 'k',
103
+ });
104
+ expect(init.headers['x-api-key']).toBe('sk');
105
+
106
+ b.create(['de'], undefined, 'k2', 42);
107
+ await vi.waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
108
+ const body2 = JSON.parse(fetch.mock.calls[1][1].body);
109
+ expect(body2).toMatchObject({
110
+ languageCode: 'de',
111
+ namespace: 'default',
112
+ value: '42',
113
+ });
114
+ });
115
+
116
+ it('create logs warn on upsert failure', async () => {
117
+ fetch.mockResolvedValueOnce(jsonRes({ error: 'nope' }, 500));
118
+ const warn = vi.fn();
119
+ const b = new LexiSyncBackend(
120
+ { logger: { warn } },
121
+ { projectId: 'p', apiKey: 'sk', referenceLng: 'en', apiUrl: 'https://x.example' },
122
+ );
123
+ b.create('en', 'common', 'k', 'v');
124
+ await vi.waitFor(() => expect(warn).toHaveBeenCalled());
125
+ expect(warn.mock.calls[0][0]).toMatch(/saveMissing failed/);
126
+ });
127
+
128
+ it('create swallows errors without logger', async () => {
129
+ fetch.mockResolvedValueOnce(jsonRes({ error: 'nope' }, 500));
130
+ const b = new LexiSyncBackend(
131
+ {},
132
+ { projectId: 'p', apiKey: 'sk', apiUrl: 'https://x.example' },
133
+ );
134
+ b.create('en', 'common', 'k', 'v');
135
+ await vi.waitFor(() => expect(fetch).toHaveBeenCalled());
136
+ // no throw
137
+ });
138
+ });
package/lib/client.js CHANGED
@@ -1,13 +1,17 @@
1
1
  'use strict';
2
2
 
3
- const DEFAULT_API_URL = 'https://lexisync.app';
4
- const DEFAULT_CDN_URL = 'https://cdn.lexisync.app';
3
+ /**
4
+ * LexiSync is self-hosted: there is no default server. Pass `apiUrl` (the base URL of
5
+ * your instance, e.g. https://lexisync.example.com) or set LEXISYNC_API_URL.
6
+ */
7
+ const MISSING_API_URL =
8
+ 'LexiSync: apiUrl is required. Pass the base URL of your self-hosted instance ' +
9
+ '(e.g. apiUrl: "https://lexisync.example.com") or set LEXISYNC_API_URL.';
5
10
 
6
11
  function resolveApiUrl(override) {
7
12
  const raw =
8
- override ||
9
- (typeof process !== 'undefined' && process.env && process.env.LEXISYNC_API_URL) ||
10
- DEFAULT_API_URL;
13
+ override || (typeof process !== 'undefined' && process.env && process.env.LEXISYNC_API_URL);
14
+ if (!raw) throw new Error(MISSING_API_URL);
11
15
  return String(raw).replace(/\/$/, '');
12
16
  }
13
17
 
@@ -26,8 +30,8 @@ function rowsToResource(rows) {
26
30
 
27
31
  /**
28
32
  * CDN URLs:
29
- * https://cdn.lexisync.app/{projectId}/{version}/{lng}/{ns}.json
30
- * https://lexisync.app/cdn/{projectId}/{version}/{lng}/{ns}
33
+ * {cdnUrl}/{projectId}/{version}/{lng}/{ns}.json (dedicated CDN host, cdnUrl set)
34
+ * {apiUrl}/cdn/{projectId}/{version}/{lng}/{ns} (default: served by the app)
31
35
  * version "latest" = working copy; other versions = published catalogs (immediate after publish).
32
36
  */
33
37
  function resolveCdnBase(apiUrl, cdnUrl) {
@@ -35,12 +39,7 @@ function resolveCdnBase(apiUrl, cdnUrl) {
35
39
  if (typeof process !== 'undefined' && process.env && process.env.LEXISYNC_CDN_URL) {
36
40
  return String(process.env.LEXISYNC_CDN_URL).replace(/\/$/, '');
37
41
  }
38
- // Prefer dedicated CDN host when using the default production API
39
- const base = resolveApiUrl(apiUrl);
40
- if (base === DEFAULT_API_URL || base.endsWith('lexisync.app')) {
41
- return DEFAULT_CDN_URL;
42
- }
43
- return `${base}/cdn`;
42
+ return `${resolveApiUrl(apiUrl)}/cdn`;
44
43
  }
45
44
 
46
45
  function cdnUrl({ apiUrl, cdnUrl: cdnBase, projectId, version, language, namespace }) {
@@ -51,11 +50,18 @@ function cdnUrl({ apiUrl, cdnUrl: cdnBase, projectId, version, language, namespa
51
50
  if (root.endsWith('/cdn')) {
52
51
  return `${root}/${encodeURIComponent(projectId)}/${v}/${lng}/${ns}`;
53
52
  }
54
- // Pure CDN domain base (cdn.lexisync.app)
53
+ // Dedicated CDN host (cdnUrl / LEXISYNC_CDN_URL)
55
54
  return `${root}/${encodeURIComponent(projectId)}/${v}/${lng}/${ns}.json`;
56
55
  }
57
56
 
58
- async function getTranslations({ apiUrl, cdnUrl: cdnBase, projectId, namespace, language, version }) {
57
+ async function getTranslations({
58
+ apiUrl,
59
+ cdnUrl: cdnBase,
60
+ projectId,
61
+ namespace,
62
+ language,
63
+ version,
64
+ }) {
59
65
  const ver = version || 'latest';
60
66
  const url = cdnUrl({ apiUrl, cdnUrl: cdnBase, projectId, version: ver, language, namespace });
61
67
  const res = await fetch(url);
@@ -206,18 +212,31 @@ async function addLanguage({ apiUrl, apiKey, projectId, code, name }) {
206
212
  });
207
213
  }
208
214
 
209
- async function downloadCatalog({ apiUrl, apiKey, projectId, version = 'latest', format = 'nested', language, namespace }) {
215
+ async function downloadCatalog({
216
+ apiUrl,
217
+ apiKey,
218
+ projectId,
219
+ version = 'latest',
220
+ format = 'nested',
221
+ language,
222
+ namespace,
223
+ }) {
210
224
  const q = new URLSearchParams({ version, format });
211
225
  if (language) q.set('language', language);
212
226
  if (namespace) q.set('namespace', namespace);
213
- return v1Fetch(
214
- apiUrl,
215
- `/api/v1/projects/${encodeURIComponent(projectId)}/download?${q}`,
216
- { apiKey }
217
- );
227
+ return v1Fetch(apiUrl, `/api/v1/projects/${encodeURIComponent(projectId)}/download?${q}`, {
228
+ apiKey,
229
+ });
218
230
  }
219
231
 
220
- async function reportMissing({ apiUrl, apiKey, projectId, namespace = 'common', languageCode = 'en', keys }) {
232
+ async function reportMissing({
233
+ apiUrl,
234
+ apiKey,
235
+ projectId,
236
+ namespace = 'common',
237
+ languageCode = 'en',
238
+ keys,
239
+ }) {
221
240
  return v1Fetch(apiUrl, `/api/v1/projects/${encodeURIComponent(projectId)}/missing`, {
222
241
  apiKey,
223
242
  method: 'POST',
@@ -226,8 +245,7 @@ async function reportMissing({ apiUrl, apiKey, projectId, namespace = 'common',
226
245
  }
227
246
 
228
247
  module.exports = {
229
- DEFAULT_API_URL,
230
- DEFAULT_CDN_URL,
248
+ MISSING_API_URL,
231
249
  resolveApiUrl,
232
250
  resolveCdnBase,
233
251
  rowsToResource,
@@ -243,4 +261,4 @@ module.exports = {
243
261
  addLanguage,
244
262
  downloadCatalog,
245
263
  reportMissing,
246
- };
264
+ };