Skip to content

Commit

Permalink
feat(plugin-contentful): allow fetching all kind of unknown models fr…
Browse files Browse the repository at this point in the history
…om contentful, not just the ones named 'custom'
  • Loading branch information
asastre committed Sep 30, 2021
1 parent 781c701 commit 2faf3b4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/botonic-plugin-contentful/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"scripts": {
"build": "rm -rf lib && ../../node_modules/.bin/tsc",
"build:watch": "../../node_modules/.bin/tsc --watch",
"build_unit_tests": "tsc -b tests/tsconfig.json",
"test": "../../node_modules/.bin/jest --coverage",
"ts-node": "../../node_modules/.bin/ts-node -O '{ \"noUnusedLocals\":false}'",
Expand Down
11 changes: 11 additions & 0 deletions packages/botonic-plugin-contentful/src/cms/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ export const BOTONIC_CONTENT_TYPES: BotonicContentType[] = [
...Object.values(SubContentType),
]

export function isCustomModel(
cmsModelType: ContentType,
localModelType: ContentType
): boolean {
return (
localModelType === ContentType.CUSTOM &&
(isSameModel(cmsModelType, ContentType.CUSTOM) ||
!CONTENT_TYPES.includes(cmsModelType))
)
}

export function isSameModel(model1: ContentType, model2: ContentType): boolean {
switch (model1) {
case ContentType.TEXT:
Expand Down
4 changes: 3 additions & 1 deletion packages/botonic-plugin-contentful/src/cms/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,13 @@ export class Button extends Content {
}
}

export type CustomFields = Record<string, unknown>

export class Custom extends Content {
constructor(
readonly id: string,
readonly name: string,
readonly fields: Record<string, unknown> | {}
readonly fields: CustomFields = {}
) {
super(ContentType.CUSTOM)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AssetDelivery } from './contents/asset'
import { ButtonDelivery } from './contents/button'
import { CarouselDelivery } from './contents/carousel'
import { ContentsDelivery } from './contents/contents'
import { CustomDelivery } from './contents/custom'
import { DateRangeDelivery } from './contents/date-range'
import { DocumentDelivery } from './contents/document'
import { FollowUpDelivery } from './contents/follow-up'
Expand All @@ -40,7 +41,6 @@ import {
} from './delivery-utils'
import { IgnoreFallbackDecorator } from './ignore-fallback-decorator'
import { KeywordsDelivery } from './search/keywords'
import { CustomDelivery } from './contents/custom'

export class Contentful implements cms.CMS {
private readonly _delivery: DeliveryApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ContentId,
ContentType,
Context,
isCustomModel,
isSameModel,
ResourceId,
} from '../cms'
Expand Down Expand Up @@ -113,7 +114,10 @@ export abstract class ContentDelivery extends ResourceDelivery {
): Promise<contentful.Entry<T>> {
const entry = await this.delivery.getEntry<T>(id, context, query)
const gotType = ContentfulEntryUtils.getContentModel(entry)
if (!isSameModel(gotType, this.modelType)) {
if (
!isCustomModel(gotType, this.modelType) &&
!isSameModel(gotType, this.modelType)
) {
throw new Error(
`Requested model with id '${id}' of type '${this.modelType}' but got '${gotType}'`
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as contentful from 'contentful'
import { ContentWithNameFields } from '../delivery-utils'

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 {
Expand All @@ -12,26 +12,24 @@ export class CustomDelivery extends ContentDelivery {
}

public async custom(id: string, context: cms.Context): Promise<cms.Custom> {
const entry = await this.getEntry<CustomFields>(id, context)
const entry = await this.getEntry<ContentWithCustomFields>(id, context)
return this.fromEntry(entry)
}

public fromEntry(customEntry: contentful.Entry<CustomFields>): cms.Custom {
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: CustomFields
): Record<string, unknown> | {} {
private getCustomFields(entryFields: ContentWithCustomFields): CustomFields {
const { name, ...fields } = entryFields
return fields ? fields : {}
}
}

export interface CustomFields extends ContentWithNameFields {
fields: Record<string, unknown> | undefined
}
export type ContentWithCustomFields = ContentWithNameFields & CustomFields

0 comments on commit 2faf3b4

Please sign in to comment.