-
Notifications
You must be signed in to change notification settings - Fork 61
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
refactor: link types #367
Open
angeloashmore
wants to merge
6
commits into
master
Choose a base branch
from
aa/refactor-link-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
refactor: link types #367
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df81d1d
refactor: link types
angeloashmore 0405e7c
refactor: clean up after self-review
angeloashmore 8b08803
refactor: fix name of `getOptionalLinkProperties` file
angeloashmore 4073174
fix(migration): only include optional link properties in links
angeloashmore 8217ae6
docs: add `MaybeLink` description
angeloashmore c73a552
fix: correct types
angeloashmore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { OptionalLinkProperties } from "../types/value/link" | ||
|
||
/** | ||
* Returns optional properties only available to link fields. Link fields can | ||
* have the same shape as content relationship and link to media fields, | ||
* requiring special treatment to extract link-specific properties. | ||
* | ||
* @param input - The content relationship or link to media field from which the | ||
* link properties will be extracted. | ||
* | ||
* @returns Optional link properties that `input` might have. | ||
*/ | ||
export const getOptionalLinkProperties = ( | ||
input: OptionalLinkProperties, | ||
): OptionalLinkProperties => { | ||
const res: OptionalLinkProperties = {} | ||
|
||
if ("text" in input) { | ||
res.text = input.text | ||
} | ||
|
||
return res | ||
} |
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 type { LinkType, OptionalLinkProperties } from "../value/link" | ||
|
||
/** | ||
* Adds `OptionalLinkProperties` to any type that looks like a link object (one | ||
* that includes a valid `link_type` property). | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* type Example = MaybeLink<PrismicDocument | LinkField> | ||
* // PrismicDocument | (LinkField & OptionalLinkProperties) | ||
* ``` | ||
* | ||
* @typeParam T - The type to augment. | ||
*/ | ||
export type MaybeLink<T> = | ||
| Exclude<T, { link_type: (typeof LinkType)[keyof typeof LinkType] }> | ||
| (Extract<T, { link_type: (typeof LinkType)[keyof typeof LinkType] }> & | ||
OptionalLinkProperties) |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Why did you remove text here? It's used in
resolveMigrationContentRelationship
and from what I understood with Lucie will still be used while handling link to document that can be from a generic link.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
text
was removed because this type defines how users specify a content relationship value.text
is invalid for content relationship fields.Internally, we needed to widen the types after verifying the shape, not before. You can see the
& OptionalLinkProperties
insrc/lib/isValue.ts
andsrc/lib/isMigrationValue.ts
.These changes support the following behavior when migrating a document:
text
orvariant
.text
only because we explicitly added it to the field type. We can easily remove it for this field type only if we decide to do so.text
andvariant
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, I'm not sure to get everything.
That I understand, but what's the difference with link to document? What I understood from Lucie is that the type is used also for migrating link to document implying that text and variant are supported.
There is no difference between link to document and content relationship behind the scene. Right?
For me, it's either both text + variant or nothing. We are waiting for the product but meanwhile, I would go for both. (details we can take care of course)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Content relationship functions are used for both content relationship fields and link fields. From the public API, we want to separate these field types so they only accept what is allowed. From the internal API, we need to treat both field types as overlapping.
Before this PR, we made the overlap public, which meant devs could assign
text
to a content relationship field, which should not be allowed.Here is what the autocomplete looks like after this PR for each field type:
text
andvariant
, along with the other migration-specific properties.text
.text
orvariants
.I'm happy to remove
text
from link to media if we want to do that. I left it in since that's how the product works today. Are you good with removing it from@prismicio/client
now?