-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contentful: custom content model (#1900)
* feat(contentful): add custom content model * feat(contentful): change naming * feat(plugin-contentful): allow fetching all kind of unknown models from contentful, not just the ones named 'custom' * style(contentful): refactor types in order to not break tests and to remove custom type from TopContentType Co-authored-by: Adrià Sastre <adria.sastre@gmail.com>
- Loading branch information
Showing
15 changed files
with
148 additions
and
6,463 deletions.
There are no files selected for viewing
6,456 changes: 0 additions & 6,456 deletions
6,456
packages/botonic-plugin-contentful/package-lock.json
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
packages/botonic-plugin-contentful/src/contentful/contents/custom.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,35 @@ | ||
import * as contentful from 'contentful' | ||
|
||
import * as cms from '../../cms' | ||
import { CustomFields } from '../../cms' | ||
import { ContentDelivery } from '../content-delivery' | ||
import { ContentWithNameFields } from '../delivery-utils' | ||
import { DeliveryApi } from '../index' | ||
|
||
export class CustomDelivery extends ContentDelivery { | ||
constructor(delivery: DeliveryApi, resumeErrors: boolean) { | ||
super(cms.CustomContentType.CUSTOM, delivery, resumeErrors) | ||
} | ||
|
||
public async custom(id: string, context: cms.Context): Promise<cms.Custom> { | ||
const entry = await this.getEntry<ContentWithCustomFields>(id, context) | ||
return this.fromEntry(entry) | ||
} | ||
|
||
public fromEntry( | ||
customEntry: contentful.Entry<ContentWithCustomFields> | ||
): cms.Custom { | ||
return new cms.Custom( | ||
customEntry.sys.id, | ||
customEntry.fields.name, | ||
this.getCustomFields(customEntry.fields) | ||
) | ||
} | ||
|
||
private getCustomFields(entryFields: ContentWithCustomFields): CustomFields { | ||
const { name, ...fields } = entryFields | ||
return fields ? fields : {} | ||
} | ||
} | ||
|
||
export type ContentWithCustomFields = ContentWithNameFields & CustomFields |
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
13 changes: 13 additions & 0 deletions
13
packages/botonic-plugin-contentful/tests/contentful/contents/custom.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,13 @@ | ||
import { testContentful, testContext } from '../contentful.helper' | ||
|
||
const CUSTOM_TEST = '5kqsVkXbN7ZnXXbUIiaWso' | ||
|
||
test('TEST: contentful custom with custom fields', async () => { | ||
const sut = testContentful() | ||
const custom = await sut.custom(CUSTOM_TEST, testContext()) // actually returns the fallback language (es) | ||
expect(custom.fields).toEqual({ | ||
customJson: { width: '100' }, | ||
customText: "Hi, I'm a custom text!", | ||
customBoolean: true, | ||
}) | ||
}) |