Skip to content

Commit

Permalink
refactor: adapt parsing for changed configurations REST call (#307)
Browse files Browse the repository at this point in the history
REST call response changed in 7.10.20 from array structure to pure object tree

BREAKING CHANGES: required ICM version 7.10.20+ GA

Closes #294
  • Loading branch information
dhhyi authored Jun 30, 2020
1 parent 8282584 commit 3d82674
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
6 changes: 2 additions & 4 deletions src/app/core/models/server-config/server-config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export interface ServerConfigDataEntry {
id: string;
elements?: ServerConfigDataEntry[];
[key: string]: string | boolean | number | string[] | ServerConfigDataEntry[];
[key: string]: string | boolean | number | string[] | ServerConfigDataEntry;
}

export interface ServerConfigData {
data: ServerConfigDataEntry[];
data: ServerConfigDataEntry;
}
20 changes: 9 additions & 11 deletions src/app/core/models/server-config/server-config.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ describe('Server Config Mapper', () => {
describe('fromData', () => {
it(`should return the ServerConfig when getting ServerConfigData`, () => {
const config = ServerConfigMapper.fromData({
data: [
{ applicationType: 'intershop.B2CResponsive', id: 'application', urlIdentifier: '-' },
{ acceleration: true, id: 'basket' },
{ id: 'general', locales: ['en_US', 'de_DE'] },
{
data: {
application: { applicationType: 'intershop.B2CResponsive', id: 'application', urlIdentifier: '-' },
basket: { acceleration: true, id: 'basket' },
general: { id: 'general', locales: ['en_US', 'de_DE'] },
services: {
id: 'services',
elements: [
{ id: 'captcha', siteKey: 'ASDF' },
{ id: 'gtm', token: 'QWERTY', monitor: 'true' },
{ id: 'deeper', elements: [{ id: 'hidden', foo: 'bar', num: 123, alt: '123' }] },
],
captcha: { id: 'captcha', siteKey: 'ASDF' },
gtm: { id: 'gtm', token: 'QWERTY', monitor: 'true' },
deeper: { id: 'deeper', hidden: { id: 'hidden', foo: 'bar', num: 123, alt: '123' } },
},
],
},
});

expect(config).toMatchInlineSnapshot(`
Expand Down
24 changes: 12 additions & 12 deletions src/app/core/models/server-config/server-config.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mapValues, omit } from 'lodash-es';
import { omit } from 'lodash-es';

import { ServerConfigData, ServerConfigDataEntry } from './server-config.interface';
import { ServerConfig } from './server-config.model';
Expand All @@ -17,19 +17,19 @@ export class ServerConfigMapper {
return val;
}

private static mapEntries(entries: ServerConfigDataEntry[]) {
return entries.reduce(
private static mapEntries(entries: ServerConfigDataEntry): ServerConfig {
return Object.entries(entries).reduce(
(acc, entry) => ({
...acc,
[entry.id]: Array.isArray(entry.elements)
? // do recursion if elements array is set
ServerConfigMapper.mapEntries(entry.elements)
: mapValues(
// filter out unnecessary 'id' attribute
omit(entry, 'id'),
// transform string types to better values
ServerConfigMapper.transformType
),
[entry[0]]:
typeof entry[1] === 'object' && !Array.isArray(entry[1])
? // do recursion if we find an object
ServerConfigMapper.mapEntries(
// get rid of id
omit(entry[1], 'id')
)
: // improve data quality
ServerConfigMapper.transformType(entry[1]),
}),
{}
);
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
"from": "lodash.*"
},
{
"import": "^(?!(range|uniq|memoize|once|groupBy|countBy|flatten|isEqual|intersection|omit|pick|mapValues)$).*",
"import": "^(?!(range|uniq|memoize|once|groupBy|countBy|flatten|isEqual|intersection|omit|pick)$).*",
"from": "lodash.*"
},
{
Expand Down

0 comments on commit 3d82674

Please sign in to comment.