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: add support for repeatable link values #358

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions src/types/value/contentRelationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import type { EmptyLinkField, LinkType } from "./link"
import type { SliceZone } from "./sliceZone"

/**
* Field for related documents
* Single field value for related documents
*
* @typeParam TypeEnum - Type API ID of the document.
* @typeParam LangEnum - Language API ID of the document.
* @typeParam DataInterface - Data fields for the document (filled in via
* GraphQuery of `fetchLinks`).
* @typeParam State - State of the field which determines its shape.
*/
export type ContentRelationshipField<
export type SingleContentRelationshipField<
TypeEnum = string,
LangEnum = string,
DataInterface extends
Expand All @@ -24,6 +24,49 @@ export type ContentRelationshipField<
? EmptyLinkField<typeof LinkType.Document>
: FilledContentRelationshipField<TypeEnum, LangEnum, DataInterface>

/**
* Repeatable field value for related documents
*
* @typeParam TypeEnum - Type API ID of the document.
* @typeParam LangEnum - Language API ID of the document.
* @typeParam DataInterface - Data fields for the document (filled in via
* GraphQuery of `fetchLinks`).
* @typeParam State - State of the field which determines its shape.
*/
export type RepeatableContentRelationshipField<
TypeEnum = string,
LangEnum = string,
DataInterface extends
| Record<string, AnyRegularField | GroupField | SliceZone>
| unknown = unknown,
State extends FieldState = FieldState,
> = State extends "empty"
? []
: [
SingleContentRelationshipField<TypeEnum, LangEnum, DataInterface>,
...SingleContentRelationshipField<TypeEnum, LangEnum, DataInterface>[],
]

/**
* Field for related documents
*
* @typeParam TypeEnum - Type API ID of the document.
* @typeParam LangEnum - Language API ID of the document.
* @typeParam DataInterface - Data fields for the document (filled in via
* GraphQuery of `fetchLinks`).
* @typeParam State - State of the field which determines its shape.
*/
export type ContentRelationshipField<
TypeEnum = string,
LangEnum = string,
DataInterface extends
| Record<string, AnyRegularField | GroupField | SliceZone>
| unknown = unknown,
State extends FieldState = FieldState,
> =
| SingleContentRelationshipField<TypeEnum, LangEnum, DataInterface, State>
| RepeatableContentRelationshipField<TypeEnum, LangEnum, DataInterface, State>

/**
* Links that refer to documents
*/
Expand Down
55 changes: 49 additions & 6 deletions src/types/value/link.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AnyRegularField, FieldState } from "./types"

import type { ContentRelationshipField } from "./contentRelationship"
import type { SingleContentRelationshipField } from "./contentRelationship"
import type { GroupField } from "./group"
import type { LinkToMediaField } from "./linkToMedia"
import type { SingleLinkToMediaField } from "./linkToMedia"
import type { SliceZone } from "./sliceZone"

/**
Expand Down Expand Up @@ -38,15 +38,15 @@ export interface FilledLinkToWebField {
}

/**
* A link field.
* A single link field value.
*
* @typeParam TypeEnum - Type API ID of the document.
* @typeParam LangEnum - Language API ID of the document.
* @typeParam DataInterface - Data fields for the document (filled in via
* GraphQuery of `fetchLinks`).
* @typeParam State - State of the field which determines its shape.
*/
export type LinkField<
export type SingleLinkField<
TypeEnum = string,
LangEnum = string,
DataInterface extends
Expand All @@ -56,6 +56,49 @@ export type LinkField<
> = State extends "empty"
? EmptyLinkField<typeof LinkType.Any>
:
| ContentRelationshipField<TypeEnum, LangEnum, DataInterface, State>
| SingleContentRelationshipField<TypeEnum, LangEnum, DataInterface, State>
| FilledLinkToWebField
| LinkToMediaField<State>
| SingleLinkToMediaField<State>

/**
* A repeatable link field value.
*
* @typeParam TypeEnum - Type API ID of the document.
* @typeParam LangEnum - Language API ID of the document.
* @typeParam DataInterface - Data fields for the document (filled in via
* GraphQuery of `fetchLinks`).
* @typeParam State - State of the field which determines its shape.
*/
export type RepeatableLinkFieldValue<
TypeEnum = string,
LangEnum = string,
DataInterface extends
| Record<string, AnyRegularField | GroupField | SliceZone>
| unknown = unknown,
State extends FieldState = FieldState,
> = State extends "empty"
? []
: [
SingleLinkField<TypeEnum, LangEnum, DataInterface>,
...SingleLinkField<TypeEnum, LangEnum, DataInterface>[],
]

/**
* A link field.
*
* @typeParam TypeEnum - Type API ID of the document.
* @typeParam LangEnum - Language API ID of the document.
* @typeParam DataInterface - Data fields for the document (filled in via
* GraphQuery of `fetchLinks`).
* @typeParam State - State of the field which determines its shape.
*/
export type LinkField<
TypeEnum = string,
LangEnum = string,
DataInterface extends
| Record<string, AnyRegularField | GroupField | SliceZone>
| unknown = unknown,
State extends FieldState = FieldState,
> =
| SingleLinkField<TypeEnum, LangEnum, DataInterface, State>
| RepeatableLinkFieldValue<TypeEnum, LangEnum, DataInterface, State>
23 changes: 21 additions & 2 deletions src/types/value/linkToMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@ import type { FieldState } from "./types"
import type { EmptyLinkField, LinkType } from "./link"

/**
* A link field that points to media.
* A single link field value that points to media.
*
* @typeParam State - State of the field which determines its shape.
*/
export type LinkToMediaField<State extends FieldState = FieldState> =
export type SingleLinkToMediaField<State extends FieldState = FieldState> =
State extends "empty"
? EmptyLinkField<typeof LinkType.Media>
: FilledLinkToMediaField

/**
* Repeatable link field values that point to media.
*
* @typeParam State - State of the field which determines its shape.
*/
export type RepeatableLinkToMediaField<State extends FieldState = FieldState> =
State extends "empty"
? []
: [SingleLinkToMediaField, ...SingleLinkToMediaField[]]

/**
* A link field that points to media.
*
* @typeParam State - State of the field which determines its shape.
*/
export type LinkToMediaField<State extends FieldState = FieldState> =
| SingleLinkToMediaField<State>
| RepeatableLinkToMediaField<State>

/**
* A link that points to media.
*/
Expand Down
Loading
Loading