Skip to content

Commit 927cc54

Browse files
committed
🐛 fix(api-export): Fix handling of nested object keys in API export
## Features - Handle nested object keys by building nested objects based on key segments - Preserve flat structure for single-segment keys that contain dots ## Bug fixes - Fix issue where `key` variable was not properly updated in the loop ## Refactoring - Simplify logic for handling flat vs. nested keys - Remove unnecessary variable assignments
1 parent bf908c3 commit 927cc54

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/api/methods/api-export.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,32 @@ export class ApiExport extends ApiBase {
3030

3131
protected static mapResult(keysList: Key[]): Json {
3232
return keysList.reduce((acc: Json, cur: Key): Json => {
33-
const keys: string[] = [...cur.key];
33+
const parts: string[] = cur.key;
3434

35-
let key: string | undefined = keys.shift();
36-
let path: string = key || '';
37-
38-
while (key) {
39-
if (keys.length === 0) {
40-
acc[path] = cur.value;
41-
} else if (!acc[path]) {
42-
acc[path] = {};
35+
// Flat: single-item array key stays as-is, even if it contains dots
36+
if (parts.length <= 1) {
37+
const only = parts[0];
38+
if (only !== undefined) {
39+
acc[only] = cur.value;
4340
}
41+
return acc;
42+
}
43+
44+
// Nested: array key → build nested objects by segments
45+
let node: any = acc;
46+
for (let i = 0; i < parts.length; i++) {
47+
const seg = parts[i];
48+
const isLast = i === parts.length - 1;
4449

45-
key = keys.shift();
46-
if (key) {
47-
path = `${path}.${key}`;
50+
if (seg !== undefined) {
51+
if (isLast) {
52+
node[seg] = cur.value;
53+
} else {
54+
if (node[seg] === undefined) {
55+
node[seg] = {};
56+
}
57+
node = node[seg];
58+
}
4859
}
4960
}
5061

tests/specs/export.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ describe('Export', (): void => {
2323
};
2424
const jsonExport: I18nJson = await api.export.json(request);
2525

26+
// simple keys remain direct properties
2627
expect(jsonExport.en.app_title).toBe('My Application');
28+
expect(jsonExport.en.welcome_message).toBe('Welcome to My Application!');
29+
expect(jsonExport.en.login_button).toBe('Log In');
30+
31+
// nested keys (array of segments) become nested objects
32+
expect(jsonExport.en.headers.role).toBe('Project role');
33+
expect(jsonExport.en.headers.email).toBe('User email');
34+
35+
// single-segment keys that contain dots remain flat (dot-notation key)
36+
expect(jsonExport.en['table.actions.invite']).toBe('Invite user');
37+
expect(jsonExport.en['table.actions.refresh']).toBe('Refresh data');
2738
});
2839
});

0 commit comments

Comments
 (0)