-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add force flag to import omitted fields [#62] #919
Draft
marcolink
wants to merge
1
commit into
main
Choose a base branch
from
feat/62-add-force-flag-for-omitted-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ContentTypeProps } from 'contentful-management' | ||
|
||
export function forceDeleteOmittedFieldTransform (contentType: ContentTypeProps) { | ||
const omittedFields = contentType.fields.filter(field => field.omitted) | ||
omittedFields.forEach(field => { | ||
contentType.fields = contentType.fields.filter(f => f.id !== field.id) | ||
}) | ||
return contentType | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { ContentTypeProps, EntryProps, TagProps, WebhookProps } from 'contentful-management' | ||
import { find, omit, pick, reduce } from 'lodash' | ||
import { omit, pick, find, reduce } from 'lodash' | ||
import { AssetProps, ContentTypeProps, EntryProps, LocaleProps, TagProps, WebhookProps } from 'contentful-management' | ||
import { MetadataProps } from 'contentful-management/dist/typings/common-types' | ||
|
||
/** | ||
* Default transformer methods for each kind of entity. | ||
|
@@ -8,19 +9,19 @@ import { find, omit, pick, reduce } from 'lodash' | |
* as the whole upload process needs to be followed again. | ||
*/ | ||
|
||
export function contentTypes (contentType: ContentTypeProps) { | ||
function contentTypes (contentType: ContentTypeProps) { | ||
return contentType | ||
} | ||
|
||
export function tags (tag: TagProps) { | ||
function tags (tag: TagProps) { | ||
return tag | ||
} | ||
|
||
export function entries (entry: EntryProps, _, tagsEnabled = false) { | ||
function entries (entry: EntryProps, _, tagsEnabled = false) { | ||
return removeMetadataTags(entry, tagsEnabled) | ||
} | ||
|
||
export function webhooks (webhook: WebhookProps) { | ||
function webhooks (webhook: WebhookProps) { | ||
// Workaround for webhooks with credentials | ||
if (webhook.httpBasicUsername) { | ||
delete webhook.httpBasicUsername | ||
|
@@ -34,17 +35,21 @@ export function webhooks (webhook: WebhookProps) { | |
return webhook | ||
} | ||
|
||
export function assets (asset, _, tagsEnabled = false) { | ||
function assets (asset: AssetProps, _, tagsEnabled = false) { | ||
const transformedAsset = omit(asset, 'sys') | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
transformedAsset.sys = pick(asset.sys, 'id') | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
transformedAsset.fields = pick(asset.fields, 'title', 'description') | ||
transformedAsset.fields.file = reduce( | ||
asset.fields.file, | ||
(newFile, localizedFile, locale) => { | ||
newFile[locale] = pick(localizedFile, 'contentType', 'fileName') | ||
if (!localizedFile.uploadFrom) { | ||
const assetUrl = localizedFile.url || localizedFile.upload | ||
newFile[locale].upload = `${/^(http|https):\/\//i.test(assetUrl) ? '' : 'https:'}${assetUrl}` | ||
newFile[locale].upload = `${/^(http|https):\/\//i.test(assetUrl!) ? '' : 'https:'}${assetUrl}` | ||
} else { | ||
newFile[locale].uploadFrom = localizedFile.uploadFrom | ||
} | ||
|
@@ -55,7 +60,7 @@ export function assets (asset, _, tagsEnabled = false) { | |
return removeMetadataTags(transformedAsset, tagsEnabled) | ||
} | ||
|
||
export function locales (locale, destinationLocales) { | ||
function locales (locale: LocaleProps, destinationLocales: Array<LocaleProps>): LocaleProps { | ||
const transformedLocale = pick(locale, 'code', 'name', 'contentManagementApi', 'contentDeliveryApi', 'fallbackCode', 'optional') | ||
const destinationLocale = find(destinationLocales, { code: locale.code }) | ||
if (destinationLocale) { | ||
|
@@ -66,12 +71,23 @@ export function locales (locale, destinationLocales) { | |
transformedLocale.sys = pick(destinationLocale.sys, 'id') | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return transformedLocale | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this ignore here, what's the issue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The retuned shape only has a subset of data in the |
||
} | ||
|
||
function removeMetadataTags (entity, tagsEnabled = false) { | ||
function removeMetadataTags<T extends { metadata?: MetadataProps }> (entity: T, tagsEnabled = false): T { | ||
if (!tagsEnabled) { | ||
delete entity.metadata | ||
} | ||
return entity | ||
} | ||
|
||
export const transformers = { | ||
contentTypes, | ||
tags, | ||
entries, | ||
webhooks, | ||
assets, | ||
locales | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test/unit/transform/force-delete-omitted-field-transform.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { cloneMock } from 'contentful-batch-libs/test/mocks/' | ||
import { ContentTypeProps } from 'contentful-management' | ||
import { forceDeleteOmittedFieldTransform } from '../../../lib/transform/force-delete-omitted-field-transform' | ||
|
||
test('It should transform content types by dropping omitted fields', () => { | ||
const contentTypeMock = cloneMock('contentType') as ContentTypeProps | ||
contentTypeMock.fields[0].omitted = true | ||
expect(contentTypeMock.fields).toHaveLength(1) | ||
const transformedContentTypeMock = forceDeleteOmittedFieldTransform(contentTypeMock) | ||
expect(transformedContentTypeMock.fields).toHaveLength(0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally not the biggest fan of adding the pipe dependency "just" for this, not sure about the benefit, we also just can pass the params through all all transformers instead, no?