Skip to content
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

Compatibility with the Preview API #388

Open
TimBenjamin opened this issue Nov 27, 2024 · 0 comments
Open

Compatibility with the Preview API #388

TimBenjamin opened this issue Nov 27, 2024 · 0 comments
Assignees

Comments

@TimBenjamin
Copy link

We're using this tool as part of our TypeScript project. We have a prod site using the Content Delivery API, and we have a preview build that content editors use as part of Contentful's Live Preview, and that uses the Content Preview API.

The issue is that the Content Preview API will return content that does not conform to the types that this tool creates.

To replicate:

  • create a content model with a mandatory "title" field.
  • create a new (Draft) entry using this model, but do not populate the "title" field.
  • wait a short while, and this will get auto-saved in Contentful (even though the mandatory field is not yet populated). NB it is still "Draft" at this point.
  • the Preview API wil pull through this entry, as expected, because Live Preview needs to see Draft entries.
  • supposing that your code does something with this "title" field, you'll get type errors because the "title" field is undefined, yet the cf-content-types-generator does not allow that field to be undefined.

For example suppose that you use the .includes() string function on title:
if (entry.fields.title.includes("foo")) {

You would get the error:
Cannot read properties of undefined (reading 'includes') TypeError: Cannot read properties of undefined

To fix, I think every field should include undefined in the type definition, just like optional fields do, even if that field is mandatory. This would then ensure that the developer must do:
if (entry.fields.title?.includes("foo")) {

@veu veu self-assigned this Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants