-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtransform-space.ts
More file actions
35 lines (29 loc) · 1.39 KB
/
transform-space.ts
File metadata and controls
35 lines (29 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { defaults, omit } from 'lodash'
import * as defaultTransformers from './transformers'
import sortEntries from '../utils/sort-entries'
import sortLocales from '../utils/sort-locales'
import { DestinationData, OriginalSourceData, TransformedSourceData } from '../types'
const spaceEntities = [
'contentTypes', 'entries', 'assets', 'locales', 'webhooks', 'tags'
]
/**
* Run transformer methods on each item for each kind of entity, in case there
* is a need to transform data when copying it to the destination space
*/
export default function (
sourceData: OriginalSourceData, destinationData: DestinationData, customTransformers?: any, entities = spaceEntities
): TransformedSourceData {
const transformers = defaults(customTransformers, defaultTransformers)
const baseSpaceData = omit(sourceData, ...entities)
sourceData.locales = sortLocales(sourceData.locales)
const tagsEnabled = !!destinationData.tags
return <TransformedSourceData>entities.reduce((transformedSpaceData, type) => {
// tags don't contain links to other entities, don't need to be sorted
const sortedEntities = (type === 'tags') ? sourceData[type] : sortEntries(sourceData[type])
transformedSpaceData[type] = sortedEntities?.map((entity) => ({
original: entity,
transformed: transformers[type](entity, destinationData[type], tagsEnabled)
}))
return transformedSpaceData
}, baseSpaceData)
}