lexisync 0.2.0 → 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.
- package/README.md +29 -76
- package/bin/lexisync.js +397 -9
- package/lib/backend.js +35 -15
- package/lib/backend.test.js +138 -0
- package/lib/client.js +60 -22
- package/lib/client.test.js +490 -0
- package/lib/config-store.js +12 -10
- package/lib/config-store.test.js +54 -0
- package/package.json +11 -9
package/lib/backend.js
CHANGED
|
@@ -9,14 +9,19 @@ const { getTranslations, resolveApiUrl, upsertTranslation } = require('./client'
|
|
|
9
9
|
* - version "latest" (default): working copy — good for development + saveMissing
|
|
10
10
|
* - version "production" | "1.0.0" | …: published CDN catalog (immutable, cacheable)
|
|
11
11
|
*
|
|
12
|
+
* Reference language (Locize term):
|
|
13
|
+
* backend.referenceLng — where saveMissing pushes defaultValues.
|
|
14
|
+
* Must match Project.referenceLng on the platform.
|
|
15
|
+
* Deprecated alias: backend.sourceLng (same meaning).
|
|
16
|
+
*
|
|
12
17
|
* @example
|
|
13
18
|
* import LexiSyncBackend from 'lexisync';
|
|
14
19
|
* i18next.use(LexiSyncBackend).init({
|
|
15
20
|
* backend: {
|
|
16
21
|
* projectId: '…',
|
|
17
|
-
* apiKey: 'sk_…',
|
|
18
|
-
* version: '
|
|
19
|
-
*
|
|
22
|
+
* apiKey: 'sk_…',
|
|
23
|
+
* version: 'production',
|
|
24
|
+
* referenceLng: 'nl',
|
|
20
25
|
* },
|
|
21
26
|
* saveMissing: true,
|
|
22
27
|
* });
|
|
@@ -24,27 +29,42 @@ const { getTranslations, resolveApiUrl, upsertTranslation } = require('./client'
|
|
|
24
29
|
class LexiSyncBackend {
|
|
25
30
|
constructor(services, backendOptions = {}, i18nextOptions = {}) {
|
|
26
31
|
this.services = services;
|
|
27
|
-
this.options =
|
|
32
|
+
this.options = this.#normalizeOptions(backendOptions);
|
|
33
|
+
this.i18nextOptions = i18nextOptions;
|
|
34
|
+
this.type = 'backend';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#normalizeOptions(backendOptions = {}) {
|
|
38
|
+
const referenceLng =
|
|
39
|
+
backendOptions.referenceLng ||
|
|
40
|
+
backendOptions.sourceLng || // deprecated alias
|
|
41
|
+
backendOptions.lng ||
|
|
42
|
+
'en';
|
|
43
|
+
return {
|
|
28
44
|
apiUrl: backendOptions.apiUrl,
|
|
45
|
+
cdnUrl: backendOptions.cdnUrl,
|
|
29
46
|
projectId: backendOptions.projectId,
|
|
30
47
|
apiKey: backendOptions.apiKey,
|
|
31
48
|
version: backendOptions.version || 'latest',
|
|
32
|
-
referenceLng
|
|
49
|
+
referenceLng,
|
|
50
|
+
// Keep sourceLng as a mirrored deprecated property for older call sites.
|
|
51
|
+
sourceLng: referenceLng,
|
|
33
52
|
...backendOptions,
|
|
53
|
+
// Re-assert after spread so aliases cannot drift.
|
|
54
|
+
referenceLng,
|
|
55
|
+
sourceLng: referenceLng,
|
|
34
56
|
};
|
|
35
|
-
this.i18nextOptions = i18nextOptions;
|
|
36
|
-
this.type = 'backend';
|
|
37
57
|
}
|
|
38
58
|
|
|
39
59
|
init(services, backendOptions = {}, i18nextOptions = {}) {
|
|
40
60
|
this.services = services;
|
|
41
|
-
this.options = { ...this.options, ...backendOptions };
|
|
61
|
+
this.options = this.#normalizeOptions({ ...this.options, ...backendOptions });
|
|
42
62
|
if (!this.options.version) this.options.version = 'latest';
|
|
43
63
|
this.i18nextOptions = i18nextOptions;
|
|
44
64
|
}
|
|
45
65
|
|
|
46
66
|
read(language, namespace, callback) {
|
|
47
|
-
const { projectId, apiUrl, version } = this.options;
|
|
67
|
+
const { projectId, apiUrl, cdnUrl, version } = this.options;
|
|
48
68
|
if (!projectId) {
|
|
49
69
|
callback(new Error('LexiSync backend: projectId is required'), null);
|
|
50
70
|
return;
|
|
@@ -52,6 +72,7 @@ class LexiSyncBackend {
|
|
|
52
72
|
|
|
53
73
|
getTranslations({
|
|
54
74
|
apiUrl,
|
|
75
|
+
cdnUrl,
|
|
55
76
|
projectId,
|
|
56
77
|
namespace,
|
|
57
78
|
language,
|
|
@@ -61,15 +82,14 @@ class LexiSyncBackend {
|
|
|
61
82
|
.catch((err) => callback(err, null));
|
|
62
83
|
}
|
|
63
84
|
|
|
64
|
-
/** i18next saveMissing →
|
|
85
|
+
/** i18next saveMissing → working copy in the project reference language. */
|
|
65
86
|
create(languages, namespace, key, fallbackValue) {
|
|
66
|
-
const { projectId, apiKey, apiUrl, referenceLng
|
|
87
|
+
const { projectId, apiKey, apiUrl, referenceLng } = this.options;
|
|
67
88
|
if (!apiKey || !projectId) return;
|
|
68
|
-
// Never write into a published version via CDN path — always latest working copy
|
|
69
|
-
if (version && version !== 'latest') return;
|
|
70
89
|
|
|
71
90
|
const lng = Array.isArray(languages) ? languages[0] : languages;
|
|
72
|
-
|
|
91
|
+
// Prefer explicit language from i18next when provided; otherwise referenceLng.
|
|
92
|
+
const languageCode = lng || referenceLng;
|
|
73
93
|
const value = typeof fallbackValue === 'string' ? fallbackValue : String(fallbackValue ?? key);
|
|
74
94
|
|
|
75
95
|
upsertTranslation({
|
|
@@ -91,4 +111,4 @@ class LexiSyncBackend {
|
|
|
91
111
|
LexiSyncBackend.type = 'backend';
|
|
92
112
|
|
|
93
113
|
module.exports = LexiSyncBackend;
|
|
94
|
-
module.exports.default = LexiSyncBackend;
|
|
114
|
+
module.exports.default = LexiSyncBackend;
|
|
@@ -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,12 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
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.';
|
|
4
10
|
|
|
5
11
|
function resolveApiUrl(override) {
|
|
6
12
|
const raw =
|
|
7
|
-
override ||
|
|
8
|
-
|
|
9
|
-
DEFAULT_API_URL;
|
|
13
|
+
override || (typeof process !== 'undefined' && process.env && process.env.LEXISYNC_API_URL);
|
|
14
|
+
if (!raw) throw new Error(MISSING_API_URL);
|
|
10
15
|
return String(raw).replace(/\/$/, '');
|
|
11
16
|
}
|
|
12
17
|
|
|
@@ -24,22 +29,41 @@ function rowsToResource(rows) {
|
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
32
|
+
* CDN URLs:
|
|
33
|
+
* {cdnUrl}/{projectId}/{version}/{lng}/{ns}.json (dedicated CDN host, cdnUrl set)
|
|
34
|
+
* {apiUrl}/cdn/{projectId}/{version}/{lng}/{ns} (default: served by the app)
|
|
35
|
+
* version "latest" = working copy; other versions = published catalogs (immediate after publish).
|
|
30
36
|
*/
|
|
31
|
-
function
|
|
32
|
-
|
|
37
|
+
function resolveCdnBase(apiUrl, cdnUrl) {
|
|
38
|
+
if (cdnUrl) return String(cdnUrl).replace(/\/$/, '');
|
|
39
|
+
if (typeof process !== 'undefined' && process.env && process.env.LEXISYNC_CDN_URL) {
|
|
40
|
+
return String(process.env.LEXISYNC_CDN_URL).replace(/\/$/, '');
|
|
41
|
+
}
|
|
42
|
+
return `${resolveApiUrl(apiUrl)}/cdn`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function cdnUrl({ apiUrl, cdnUrl: cdnBase, projectId, version, language, namespace }) {
|
|
46
|
+
const root = resolveCdnBase(apiUrl, cdnBase);
|
|
33
47
|
const v = encodeURIComponent(version || 'latest');
|
|
34
48
|
const lng = encodeURIComponent(language || 'en');
|
|
35
49
|
const ns = encodeURIComponent(namespace || 'default');
|
|
36
|
-
|
|
50
|
+
if (root.endsWith('/cdn')) {
|
|
51
|
+
return `${root}/${encodeURIComponent(projectId)}/${v}/${lng}/${ns}`;
|
|
52
|
+
}
|
|
53
|
+
// Dedicated CDN host (cdnUrl / LEXISYNC_CDN_URL)
|
|
54
|
+
return `${root}/${encodeURIComponent(projectId)}/${v}/${lng}/${ns}.json`;
|
|
37
55
|
}
|
|
38
56
|
|
|
39
|
-
async function getTranslations({
|
|
57
|
+
async function getTranslations({
|
|
58
|
+
apiUrl,
|
|
59
|
+
cdnUrl: cdnBase,
|
|
60
|
+
projectId,
|
|
61
|
+
namespace,
|
|
62
|
+
language,
|
|
63
|
+
version,
|
|
64
|
+
}) {
|
|
40
65
|
const ver = version || 'latest';
|
|
41
|
-
|
|
42
|
-
const url = cdnUrl({ apiUrl, projectId, version: ver, language, namespace });
|
|
66
|
+
const url = cdnUrl({ apiUrl, cdnUrl: cdnBase, projectId, version: ver, language, namespace });
|
|
43
67
|
const res = await fetch(url);
|
|
44
68
|
if (res.ok) {
|
|
45
69
|
const data = await res.json();
|
|
@@ -188,18 +212,31 @@ async function addLanguage({ apiUrl, apiKey, projectId, code, name }) {
|
|
|
188
212
|
});
|
|
189
213
|
}
|
|
190
214
|
|
|
191
|
-
async function downloadCatalog({
|
|
215
|
+
async function downloadCatalog({
|
|
216
|
+
apiUrl,
|
|
217
|
+
apiKey,
|
|
218
|
+
projectId,
|
|
219
|
+
version = 'latest',
|
|
220
|
+
format = 'nested',
|
|
221
|
+
language,
|
|
222
|
+
namespace,
|
|
223
|
+
}) {
|
|
192
224
|
const q = new URLSearchParams({ version, format });
|
|
193
225
|
if (language) q.set('language', language);
|
|
194
226
|
if (namespace) q.set('namespace', namespace);
|
|
195
|
-
return v1Fetch(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
{ apiKey }
|
|
199
|
-
);
|
|
227
|
+
return v1Fetch(apiUrl, `/api/v1/projects/${encodeURIComponent(projectId)}/download?${q}`, {
|
|
228
|
+
apiKey,
|
|
229
|
+
});
|
|
200
230
|
}
|
|
201
231
|
|
|
202
|
-
async function reportMissing({
|
|
232
|
+
async function reportMissing({
|
|
233
|
+
apiUrl,
|
|
234
|
+
apiKey,
|
|
235
|
+
projectId,
|
|
236
|
+
namespace = 'common',
|
|
237
|
+
languageCode = 'en',
|
|
238
|
+
keys,
|
|
239
|
+
}) {
|
|
203
240
|
return v1Fetch(apiUrl, `/api/v1/projects/${encodeURIComponent(projectId)}/missing`, {
|
|
204
241
|
apiKey,
|
|
205
242
|
method: 'POST',
|
|
@@ -208,8 +245,9 @@ async function reportMissing({ apiUrl, apiKey, projectId, namespace = 'common',
|
|
|
208
245
|
}
|
|
209
246
|
|
|
210
247
|
module.exports = {
|
|
211
|
-
|
|
248
|
+
MISSING_API_URL,
|
|
212
249
|
resolveApiUrl,
|
|
250
|
+
resolveCdnBase,
|
|
213
251
|
rowsToResource,
|
|
214
252
|
cdnUrl,
|
|
215
253
|
getTranslations,
|
|
@@ -223,4 +261,4 @@ module.exports = {
|
|
|
223
261
|
addLanguage,
|
|
224
262
|
downloadCatalog,
|
|
225
263
|
reportMissing,
|
|
226
|
-
};
|
|
264
|
+
};
|