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

fix: support partial document in asLink() and documentToLinkField() #51

Merged
merged 1 commit into from
Jun 9, 2022

Conversation

angeloashmore
Copy link
Member

@angeloashmore angeloashmore commented Jun 9, 2022

Types of changes

  • Chore (a non-breaking change which is related to package maintenance)
  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This PR fixes a bug where asLink() would throw an error when given a Prismic document without a data property.

import * as prismicH from "@prismicio/helpers";

asLink({}); // Throws

The following error is thrown (the stacktrace has been truncated):

TypeError {
  message: 'Cannot convert undefined or null to object',
}

› Function.keys (<anonymous>)
› null.documentToLinkField (node_modules/@prismicio/helpers/src/documentToLinkField.ts:39:14)
› Object.asLink (node_modules/@prismicio/helpers/src/asLink.ts:62:6)

This happens because documentToLinkField() tries to call Object.keys() on document.data, which is undefined.

Although asLink() (and its underlying documentToLinkField()) is typed to only accept full Prismic documents, a partial document could be passed in untyped projects.

Context

The issue this PR solves was discovered while resolving this @prismicio/react issue: prismicio/prismic-react#150

Checklist:

  • My change requires an update to the official documentation.
  • All TSDoc comments are up-to-date and new ones have been added where necessary.
  • All new and existing tests are passing.

🐳

@angeloashmore angeloashmore changed the title fix: support partial document in documentToLinkField() fix: support partial document in asLink() and documentToLinkField() Jun 9, 2022
@github-actions
Copy link

github-actions bot commented Jun 9, 2022

size-limit report 📦

Path Size
dist/index.js 3.1 KB (+0.13% 🔺)
dist/index.cjs 3.64 KB (+0.09% 🔺)

Copy link
Member

@lihbr lihbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I'll release and let you proceed on the React side :)

@lihbr lihbr merged commit e3a3493 into master Jun 9, 2022
@lihbr lihbr deleted the aa/fix-documentToLink-empty-data branch June 9, 2022 09:47
@lihbr
Copy link
Member

lihbr commented Jun 9, 2022

Released in 2.3.1: https://github.com/prismicio/prismic-helpers/blob/master/CHANGELOG.md#231-2022-06-09 🎉

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

Successfully merging this pull request may close these issues.

None yet

2 participants