From 3d82674914c5970457485d2ffd7966d82e47e8ce Mon Sep 17 00:00:00 2001 From: Danilo Hoffmann Date: Tue, 30 Jun 2020 13:20:31 +0200 Subject: [PATCH] refactor: adapt parsing for changed configurations REST call (#307) 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 --- .../server-config/server-config.interface.ts | 6 ++--- .../server-config.mapper.spec.ts | 20 +++++++--------- .../server-config/server-config.mapper.ts | 24 +++++++++---------- tslint.json | 2 +- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/app/core/models/server-config/server-config.interface.ts b/src/app/core/models/server-config/server-config.interface.ts index 46d4aa5dfc..11c0d671d0 100644 --- a/src/app/core/models/server-config/server-config.interface.ts +++ b/src/app/core/models/server-config/server-config.interface.ts @@ -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; } diff --git a/src/app/core/models/server-config/server-config.mapper.spec.ts b/src/app/core/models/server-config/server-config.mapper.spec.ts index 5e880632a2..5da110c55f 100644 --- a/src/app/core/models/server-config/server-config.mapper.spec.ts +++ b/src/app/core/models/server-config/server-config.mapper.spec.ts @@ -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(` diff --git a/src/app/core/models/server-config/server-config.mapper.ts b/src/app/core/models/server-config/server-config.mapper.ts index e56426b729..dab9fbb4aa 100644 --- a/src/app/core/models/server-config/server-config.mapper.ts +++ b/src/app/core/models/server-config/server-config.mapper.ts @@ -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'; @@ -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]), }), {} ); diff --git a/tslint.json b/tslint.json index 9e9525778f..de9d9ddd78 100644 --- a/tslint.json +++ b/tslint.json @@ -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.*" }, {