-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support resource link fields (#246)
- Loading branch information
Showing
19 changed files
with
213 additions
and
32 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ContentTypeField } from 'contentful'; | ||
import { renderTypeGeneric } from '../generic'; | ||
import { RenderContext } from '../type'; | ||
|
||
export const renderPropResourceLink = (field: ContentTypeField, context: RenderContext): string => { | ||
for (const resource of field.allowedResources!) { | ||
if (resource.type !== 'Contentful:Entry') { | ||
throw new Error(`Unknown type "${resource.type}"`); | ||
} | ||
} | ||
|
||
context.imports.add({ | ||
moduleSpecifier: 'contentful', | ||
namedImports: ['Entry'], | ||
isTypeOnly: true, | ||
}); | ||
|
||
return renderTypeGeneric('Entry', 'Record<string, any>'); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createDefaultContext, renderPropResourceLink } from '../../../src'; | ||
|
||
describe('A renderPropResourceLink function', () => { | ||
it('can evaluate a "ResourceLink" type', () => { | ||
const field = JSON.parse(` | ||
{ | ||
"id": "category", | ||
"name": "Category", | ||
"type": "ResourceLink", | ||
"localized": false, | ||
"required": true, | ||
"validations": [], | ||
"disabled": false, | ||
"omitted": false, | ||
"allowedResources": [ | ||
{ | ||
"type": "Contentful:Entry", | ||
"source": "crn:contentful:::content:spaces/spaceId", | ||
"contentTypes": [ | ||
"topicCategory" | ||
] | ||
} | ||
] | ||
} | ||
`); | ||
|
||
expect(renderPropResourceLink(field, createDefaultContext())).toEqual( | ||
'Entry<Record<string, any>>', | ||
); | ||
}); | ||
|
||
it('rejects a "ResourceLink" with an unknown resource type', () => { | ||
const field = JSON.parse(` | ||
{ | ||
"id": "category", | ||
"name": "Category", | ||
"type": "ResourceLink", | ||
"localized": false, | ||
"required": true, | ||
"validations": [], | ||
"disabled": false, | ||
"omitted": false, | ||
"allowedResources": [ | ||
{ | ||
"type": "Contentful:UnknownEntity", | ||
"source": "crn:contentful:::content:spaces/spaceId", | ||
"contentTypes": [ | ||
"topicCategory" | ||
] | ||
} | ||
] | ||
} | ||
`); | ||
|
||
expect(() => renderPropResourceLink(field, createDefaultContext())).toThrow( | ||
'Unknown type "Contentful:UnknownEntity"', | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.