Skip to content

Commit be1f6e0

Browse files
committed
feat(migrate): more types
1 parent 8bebbbd commit be1f6e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+851
-840
lines changed

packages/sync-actions/src/assets-actions.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/sync-actions/src/attribute-groups-actions.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ import createBuildArrayActions, {
44
CHANGE_ACTIONS,
55
REMOVE_ACTIONS,
66
} from './utils/create-build-array-actions'
7+
import { UpdateAction } from '@commercetools/sdk-client-v2'
8+
import { ActionMapBase } from './utils/create-map-action-group'
9+
import { AttributeReference } from '@commercetools/platform-sdk/src'
710

8-
const hasAttribute = (attributes, newValue) =>
9-
attributes.some((attribute) => attribute.key === newValue.key)
11+
const hasAttribute = (
12+
attributes: Array<AttributeReference>,
13+
newValue: AttributeReference
14+
) => attributes.some((attribute) => attribute.key === newValue.key)
1015

11-
export const baseActionsList = [
16+
export const baseActionsList: Array<UpdateAction> = [
1217
{ action: 'changeName', key: 'name' },
1318
{ action: 'setKey', key: 'key' },
1419
{ action: 'setDescription', key: 'description' },
1520
]
1621

17-
export function actionsMapBase(
18-
diff,
19-
oldObj,
20-
newObj,
21-
config: { shouldOmitEmptyString?: boolean } = {}
22-
) {
22+
export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
2323
return buildBaseAttributesActions({
2424
actions: baseActionsList,
2525
diff,
2626
oldObj,
2727
newObj,
28-
shouldOmitEmptyString: config.shouldOmitEmptyString,
28+
shouldOmitEmptyString: config?.shouldOmitEmptyString,
2929
})
3030
}
3131

32-
export function actionsMapAttributes(diff, oldObj, newObj) {
32+
export function actionsMapAttributes(diff: any, oldObj: any, newObj: any) {
3333
const handler = createBuildArrayActions('attributes', {
3434
[ADD_ACTIONS]: (newAttribute) => ({
3535
action: 'addAttribute',

packages/sync-actions/src/attribute-groups.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,26 @@ import {
1313
} from './attribute-groups-actions'
1414
import { SyncAction } from './types/update-actions'
1515
import createBuildActions from './utils/create-build-actions'
16-
import createMapActionGroup from './utils/create-map-action-group'
16+
import createMapActionGroup, {
17+
MapActionGroup,
18+
MapActionResult,
19+
} from './utils/create-map-action-group'
1720
import { diff } from './utils/diffpatcher'
1821

1922
function createAttributeGroupsMapActions(
20-
mapActionGroup: (
21-
type: string,
22-
fn: () => Array<UpdateAction>
23-
) => Array<UpdateAction>,
23+
mapActionGroup: MapActionGroup,
2424
syncActionConfig?: SyncActionConfig
25-
): (diff: any, newObj: any, oldObj: any) => Array<UpdateAction> {
26-
return function doMapActions(
27-
diff: any,
28-
newObj: any,
29-
oldObj: any
30-
): Array<UpdateAction> {
31-
const allActions = []
25+
): MapActionResult {
26+
return function doMapActions(diff, newObj, oldObj) {
27+
const allActions: Array<Array<UpdateAction>> = []
3228
allActions.push(
33-
mapActionGroup(
34-
'base',
35-
(): Array<UpdateAction> =>
36-
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
29+
mapActionGroup('base', () =>
30+
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
3731
)
3832
)
3933
allActions.push(
40-
mapActionGroup(
41-
'attributes',
42-
(): Array<UpdateAction> => actionsMapAttributes(diff, oldObj, newObj)
34+
mapActionGroup('attributes', () =>
35+
actionsMapAttributes(diff, oldObj, newObj)
4336
).flat()
4437
)
4538
return allActions.flat()
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { buildBaseAttributesActions } from './utils/common-actions'
2+
import { ActionMapBase } from './utils/create-map-action-group'
3+
import { UpdateAction } from '@commercetools/sdk-client-v2'
24

3-
export const baseActionsList = [
5+
export const baseActionsList: Array<UpdateAction> = [
46
{ action: 'changeIsActive', key: 'isActive' },
57
{ action: 'changeName', key: 'name' },
68
{ action: 'changeCartPredicate', key: 'cartPredicate' },
@@ -15,17 +17,12 @@ export const baseActionsList = [
1517
{ action: 'setKey', key: 'key' },
1618
]
1719

18-
export function actionsMapBase(
19-
diff,
20-
oldObj,
21-
newObj,
22-
config: { shouldOmitEmptyString?: boolean } = {}
23-
) {
20+
export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
2421
return buildBaseAttributesActions({
2522
actions: baseActionsList,
2623
diff,
2724
oldObj,
2825
newObj,
29-
shouldOmitEmptyString: config.shouldOmitEmptyString,
26+
shouldOmitEmptyString: config?.shouldOmitEmptyString,
3027
})
3128
}

packages/sync-actions/src/cart-discounts.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@ import {
22
CartDiscount,
33
CartDiscountUpdateAction,
44
} from '@commercetools/platform-sdk'
5-
import { ActionGroup, SyncActionConfig } from '@commercetools/sdk-client-v2'
5+
import {
6+
ActionGroup,
7+
SyncActionConfig,
8+
UpdateAction,
9+
} from '@commercetools/sdk-client-v2'
610
import { actionsMapBase } from './cart-discounts-actions'
711
import { SyncAction } from './types/update-actions'
812
import actionsMapCustom from './utils/action-map-custom'
913
import combineValidityActions from './utils/combine-validity-actions'
1014
import createBuildActions from './utils/create-build-actions'
11-
import createMapActionGroup from './utils/create-map-action-group'
15+
import createMapActionGroup, {
16+
MapActionGroup,
17+
MapActionResult,
18+
} from './utils/create-map-action-group'
1219
import { diff } from './utils/diffpatcher'
1320

1421
export const actionGroups = ['base', 'custom']
1522

16-
function createCartDiscountsMapActions(mapActionGroup, syncActionConfig) {
23+
function createCartDiscountsMapActions(
24+
mapActionGroup: MapActionGroup,
25+
syncActionConfig?: SyncActionConfig
26+
): MapActionResult {
1727
return function doMapActions(diff, newObj, oldObj) {
18-
const allActions = []
28+
const allActions: Array<Array<UpdateAction>> = []
1929

2030
allActions.push(
2131
mapActionGroup('base', () =>

packages/sync-actions/src/categories.ts

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
SyncActionConfig,
55
UpdateAction,
66
} from '@commercetools/sdk-client-v2'
7-
import actionsMapAssets from './assets-actions'
7+
import actionsMapAssets from './category-assets-actions'
88
import {
99
actionsMapBase,
1010
actionsMapMeta,
@@ -14,56 +14,43 @@ import { SyncAction } from './types/update-actions'
1414
import actionsMapCustom from './utils/action-map-custom'
1515
import copyEmptyArrayProps from './utils/copy-empty-array-props'
1616
import createBuildActions from './utils/create-build-actions'
17-
import createMapActionGroup from './utils/create-map-action-group'
17+
import createMapActionGroup, {
18+
MapActionGroup,
19+
MapActionResult,
20+
} from './utils/create-map-action-group'
1821
import { diff } from './utils/diffpatcher'
1922

2023
export const actionGroups = ['base', 'references', 'meta', 'custom', 'assets']
2124

2225
function createCategoryMapActions(
23-
mapActionGroup: Function,
26+
mapActionGroup: MapActionGroup,
2427
syncActionConfig?: SyncActionConfig
25-
): (diff: any, newObj: any, oldObj: any) => Array<UpdateAction> {
26-
return function doMapActions(
27-
diff: any,
28-
newObj: any,
29-
oldObj: any /* , options */
30-
): Array<UpdateAction> {
31-
const allActions = []
28+
): MapActionResult {
29+
return function doMapActions(diff, newObj, oldObj) {
30+
const allActions: Array<Array<UpdateAction>> = []
3231

3332
allActions.push(
34-
mapActionGroup(
35-
'base',
36-
(): Array<UpdateAction> =>
37-
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
33+
mapActionGroup('base', () =>
34+
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
3835
)
3936
)
4037

4138
allActions.push(
42-
mapActionGroup(
43-
'references',
44-
(): Array<UpdateAction> => actionsMapReferences(diff, oldObj, newObj)
39+
mapActionGroup('references', () =>
40+
actionsMapReferences(diff, oldObj, newObj)
4541
)
4642
)
4743

4844
allActions.push(
49-
mapActionGroup(
50-
'meta',
51-
(): Array<UpdateAction> => actionsMapMeta(diff, oldObj, newObj)
52-
)
45+
mapActionGroup('meta', () => actionsMapMeta(diff, oldObj, newObj))
5346
)
5447

5548
allActions.push(
56-
mapActionGroup(
57-
'custom',
58-
(): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj)
59-
)
49+
mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj))
6050
)
6151

6252
allActions.push(
63-
mapActionGroup(
64-
'assets',
65-
(): Array<UpdateAction> => actionsMapAssets(diff, oldObj, newObj)
66-
)
53+
mapActionGroup('assets', () => actionsMapAssets(diff, oldObj, newObj))
6754
)
6855

6956
return allActions.flat()

packages/sync-actions/src/category-actions.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {
22
buildBaseAttributesActions,
33
buildReferenceActions,
44
} from './utils/common-actions'
5+
import { ActionMapBase } from './utils/create-map-action-group'
6+
import { UpdateAction } from '@commercetools/sdk-client-v2'
57

6-
export const baseActionsList = [
8+
export const baseActionsList: Array<UpdateAction> = [
79
{ action: 'changeName', key: 'name' },
810
{ action: 'changeSlug', key: 'slug' },
911
{ action: 'setDescription', key: 'description' },
@@ -12,7 +14,7 @@ export const baseActionsList = [
1214
{ action: 'setKey', key: 'key' },
1315
]
1416

15-
export const metaActionsList = [
17+
export const metaActionsList: Array<UpdateAction> = [
1618
{ action: 'setMetaTitle', key: 'metaTitle' },
1719
{ action: 'setMetaKeywords', key: 'metaKeywords' },
1820
{ action: 'setMetaDescription', key: 'metaDescription' },
@@ -24,22 +26,17 @@ export const referenceActionsList = [{ action: 'changeParent', key: 'parent' }]
2426
* SYNC FUNCTIONS
2527
*/
2628

27-
export function actionsMapBase(
28-
diff,
29-
oldObj,
30-
newObj,
31-
config: { shouldOmitEmptyString?: boolean } = {}
32-
) {
29+
export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
3330
return buildBaseAttributesActions({
3431
actions: baseActionsList,
3532
diff,
3633
oldObj,
3734
newObj,
38-
shouldOmitEmptyString: config.shouldOmitEmptyString,
35+
shouldOmitEmptyString: config?.shouldOmitEmptyString,
3936
})
4037
}
4138

42-
export function actionsMapReferences(diff, oldObj, newObj) {
39+
export function actionsMapReferences(diff: any, oldObj: any, newObj: any) {
4340
return buildReferenceActions({
4441
actions: referenceActionsList,
4542
diff,
@@ -48,7 +45,7 @@ export function actionsMapReferences(diff, oldObj, newObj) {
4845
})
4946
}
5047

51-
export function actionsMapMeta(diff, oldObj, newObj) {
48+
export function actionsMapMeta(diff: any, oldObj: any, newObj: any) {
5249
return buildBaseAttributesActions({
5350
actions: metaActionsList,
5451
diff,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import createBuildArrayActions, {
2+
ADD_ACTIONS,
3+
CHANGE_ACTIONS,
4+
REMOVE_ACTIONS,
5+
} from './utils/create-build-array-actions'
6+
import {
7+
CategoryAddAssetAction,
8+
CategoryRemoveAssetAction,
9+
} from '@commercetools/platform-sdk'
10+
import { UpdateAction } from '@commercetools/sdk-client-v2'
11+
12+
function toAssetIdentifier(asset: { id?: string; key?: string }) {
13+
return asset.id ? { assetId: asset.id } : { assetKey: asset.key }
14+
}
15+
16+
export default function actionsMapAssets(diff: any, oldObj: any, newObj: any) {
17+
const handler = createBuildArrayActions('assets', {
18+
[ADD_ACTIONS]: (newAsset): CategoryAddAssetAction => ({
19+
action: 'addAsset',
20+
asset: newAsset,
21+
}),
22+
[REMOVE_ACTIONS]: (oldAsset): CategoryRemoveAssetAction => ({
23+
action: 'removeAsset',
24+
...toAssetIdentifier(oldAsset),
25+
}),
26+
[CHANGE_ACTIONS]: (oldAsset, newAsset): Array<UpdateAction> =>
27+
// here we could use more atomic update actions (e.g. changeAssetName)
28+
// but for now we use the simpler approach to first remove and then
29+
// re-add the asset - which reduces the code complexity
30+
{
31+
return [
32+
{
33+
action: 'removeAsset',
34+
...toAssetIdentifier(oldAsset),
35+
},
36+
{
37+
action: 'addAsset',
38+
asset: newAsset,
39+
},
40+
]
41+
},
42+
})
43+
44+
return handler(diff, oldObj, newObj)
45+
}

0 commit comments

Comments
 (0)