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

feat: support link variants #368

Open
wants to merge 9 commits into
base: aa/refactor-link-types
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/lib/getOptionalLinkProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export const getOptionalLinkProperties = (
): OptionalLinkProperties => {
const res: OptionalLinkProperties = {}

if ("variant" in input) {
res.variant = input.variant
}

if ("text" in input) {
res.text = input.text
}
Expand Down
1 change: 1 addition & 0 deletions src/types/model/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CustomTypeModelLinkField {
allowText?: boolean
allowTargetBlank?: boolean
repeat?: boolean
variants?: string[]
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types/value/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ export type FilledLinkToWebField = {
// Remember to update the `getOptionalLinkProperties()` function when updating
// this type. The function should check for every property.
export type OptionalLinkProperties = {
variant?: string
text?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,97 @@ exports[`patches content relationship fields (from Prismic) > withText > static
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > group 1`] = `
{
"group": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > shared slice 1`] = `
{
"slices": [
{
"id": "9b9dd0f2c3f",
"items": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
"group": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
},
"slice_label": null,
"slice_type": "nunc",
"variation": "ullamcorper",
"version": "8bfc905",
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > slice 1`] = `
{
"slices": [
{
"id": "5306297c5ed",
"items": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
"slice_label": "Vel",
"slice_type": "hac_habitasse",
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > static zone 1`] = `
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
}
`;

exports[`patches content relationship fields > existing > group 1`] = `
{
"group": [
Expand Down Expand Up @@ -525,6 +616,97 @@ exports[`patches content relationship fields > existingLongFormWithText > static
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > group 1`] = `
{
"group": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > shared slice 1`] = `
{
"slices": [
{
"id": "9b9dd0f2c3f",
"items": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
"group": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
},
"slice_label": null,
"slice_type": "nunc",
"variation": "ullamcorper",
"version": "8bfc905",
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > slice 1`] = `
{
"slices": [
{
"id": "5306297c5ed",
"items": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
"slice_label": "Vel",
"slice_type": "hac_habitasse",
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > static zone 1`] = `
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
}
`;

exports[`patches content relationship fields > lazyExisting > group 1`] = `
{
"group": [
Expand Down
10 changes: 10 additions & 0 deletions test/types/customType-link.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ expectType<prismic.CustomTypeModelLinkField>({
},
})

/**
* Supports optional `variants` property.
*/
expectType<prismic.CustomTypeModelLinkField>({
type: prismic.CustomTypeModelFieldType.Link,
config: {
variants: ["string"],
},
})

/**
* `@prismicio/types` extends `@prismicio/types-internal`
*/
Expand Down
17 changes: 17 additions & 0 deletions test/types/fields-link.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ expectType<prismic.LinkField>({
url: "string",
target: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkField>({
link_type: prismic.LinkType.Document,
Expand All @@ -54,6 +55,7 @@ expectType<prismic.LinkField>({
isBroken: true,
data: undefined,
text: "string",
variant: "string",
})
expectType<prismic.LinkField>({
link_type: prismic.LinkType.Media,
Expand All @@ -65,19 +67,22 @@ expectType<prismic.LinkField>({
height: "string",
width: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkField<string, string, never, "filled">>({
link_type: prismic.LinkType.Web,
url: "string",
target: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkField<string, string, never, "empty">>({
// @ts-expect-error - Empty fields cannot contain a filled link type.
link_type: prismic.LinkType.Web,
url: "string",
target: "string",
text: "string",
variant: "string",
})

/**
Expand Down Expand Up @@ -144,6 +149,18 @@ expectType<prismic.LinkField<string, string, never, "filled">>({
text: "string",
})

/**
* Empty state with variant.
*/
expectType<prismic.LinkField>({
link_type: prismic.LinkType.Any,
variant: "string",
})
expectType<prismic.LinkField<string, string, never, "empty">>({
link_type: prismic.LinkType.Any,
variant: "string",
})

/**
* Supports custom document type for document links.
*/
Expand Down
27 changes: 23 additions & 4 deletions test/writeClient-migrate-patch-contentRelationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ testMigrationFieldPatching<
text: "foo",
}
},
existingLongFormWithVariant: ({ existingDocuments }) => {
return {
link_type: LinkType.Document,
id: existingDocuments[0],
variant: "Secondary",
}
},
otherCreate: ({ otherCreateDocument }) => otherCreateDocument,
lazyExisting: ({ existingDocuments }) => {
return () => existingDocuments[0]
Expand Down Expand Up @@ -86,11 +93,23 @@ testMigrationFieldPatching<ContentRelationshipField>(
contentRelationship.id =
otherFromPrismicDocument.originalPrismicDocument!.id

// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
contentRelationship.text = "foo"
return {
...contentRelationship,
text: "foo",
}
},
withVariant: ({ ctx, otherFromPrismicDocument }) => {
const contentRelationship = ctx.mock.value.link({
type: LinkType.Document,
})
// `migrationDocuments` contains documents from "another repository"
contentRelationship.id =
otherFromPrismicDocument.originalPrismicDocument!.id

return contentRelationship
return {
...contentRelationship,
variant: "Secondary",
}
},
broken: () => {
return {
Expand Down
Loading