Skip to content

Commit

Permalink
feat: add support for repeatable links in models (#355)
Browse files Browse the repository at this point in the history
* chore: bump types-internal dep

* feat: support repeatable links in models

* Bump types internal

* Remove repeat from content rel. and link to media

* Add Repeatable (#360)

* Add Repetable to index

* chore(release): 7.13.0-alpha.0

* Use stable types internal

---------

Co-authored-by: Daniel Martín <[email protected]>
  • Loading branch information
levimykel and dani-mp authored Dec 5, 2024
1 parent 0cf80dd commit ed120a8
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 23 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [7.13.0-alpha.0](https://github.com/prismicio/prismic-client/compare/v7.12.0...v7.13.0-alpha.0) (2024-11-28)


### Features

* support repeatable links in models ([00f8524](https://github.com/prismicio/prismic-client/commit/00f8524d3df5034e263f74c884a9d78627a37dce))


### Chore

* bump types-internal dep ([6a5eb26](https://github.com/prismicio/prismic-client/commit/6a5eb26b1bd3cb26eb68150ce43c081630bcfbfd))

## [7.12.0](https://github.com/prismicio/prismic-client/compare/v7.11.1...v7.12.0) (2024-10-30)


Expand Down
27 changes: 9 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismicio/client",
"version": "7.12.0",
"version": "7.13.0-alpha.0",
"description": "The official JavaScript + TypeScript client library for Prismic",
"keywords": [
"typescript",
Expand Down Expand Up @@ -83,7 +83,7 @@
},
"devDependencies": {
"@prismicio/mock": "^0.3.9",
"@prismicio/types-internal": "^2.8.0",
"@prismicio/types-internal": "3.1.0",
"@size-limit/preset-small-lib": "^11.1.6",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
Expand Down
16 changes: 15 additions & 1 deletion src/helpers/isFilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { Slice } from "../types/value/slice"
import type { SliceZone } from "../types/value/sliceZone"
import type { TimestampField } from "../types/value/timestamp"
import type { TitleField } from "../types/value/title"
import type { AnyRegularField } from "../types/value/types"
import type { AnyRegularField, Repeatable } from "../types/value/types"

/**
* Determines if a value is not nullish (i.e. not `null` or `undefined`). This
Expand Down Expand Up @@ -266,6 +266,20 @@ export const integrationField = isNonNullish as <
// TODO: Remove when we remove support for deprecated `integrationFields` export.
export const integrationFields = integrationField

/**
* Determines if a repeatable field has at least one item.
*
* @param repeatable - Repeatable to check.
*
* @returns `true` if `repeatable` contains at least one item, `false`
* otherwise.
*/
export const repeatable = <T extends LinkField>(
repeatable: Repeatable<T> | null | undefined,
): repeatable is Repeatable<T, "filled"> => {
return isNonNullish(repeatable) && isNonEmptyArray(repeatable)
}

/**
* Determines if a Group has at least one item.
*
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export type {
FieldState,
AnyRegularField,
AnySlicePrimaryField,
Repeatable,
} from "./types/value/types"

// Models - Types representing Prismic content models.
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 @@ -15,6 +15,7 @@ export interface CustomTypeModelLinkField {
| (typeof CustomTypeModelLinkSelectType)[keyof typeof CustomTypeModelLinkSelectType]
allowText?: boolean
allowTargetBlank?: boolean
repeat?: boolean
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/types/value/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type AnyRegularField =
| ImageField
| ContentRelationshipField
| LinkField
| Repeatable<LinkField>
| LinkToMediaField
| EmbedField
| DateField
Expand All @@ -52,6 +53,14 @@ export type AnyRegularField =
*/
export type AnySlicePrimaryField = GroupField | AnyRegularField

/**
* A list of repeatable fields.
*/
export type Repeatable<
Field extends LinkField,
State extends FieldState = FieldState,
> = State extends "empty" ? [] : [Field, ...Field[]]

/**
* Useful to flatten the type output to improve type hints shown in editors. And
* also to transform an interface into a type to aide with assignability.
Expand Down
12 changes: 12 additions & 0 deletions test/types/customType-contentRelationship.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ expectType<prismic.CustomTypeModelContentRelationshipField<string, "foo">>({
},
})

/**
* Supports optional `allowText` property.
*/
expectType<prismic.CustomTypeModelContentRelationshipField<string, "foo">>({
type: prismic.CustomTypeModelFieldType.Link,
config: {
label: "string",
select: prismic.CustomTypeModelLinkSelectType.Document,
allowText: true,
},
})

/**
* `@prismicio/types` extends `@prismicio/types-internal`
*/
Expand Down
13 changes: 12 additions & 1 deletion test/types/customType-link.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ expectType<prismic.CustomTypeModelLinkField>({
})

/**
* Supports optional `text` property.
* Supports optional `allowText` property.
*/
expectType<prismic.CustomTypeModelLinkField>({
type: prismic.CustomTypeModelFieldType.Link,
Expand All @@ -71,6 +71,17 @@ expectType<prismic.CustomTypeModelLinkField>({
},
})

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

/**
* `@prismicio/types` extends `@prismicio/types-internal`
*/
Expand Down
2 changes: 1 addition & 1 deletion test/types/customType-linkToMedia.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ expectType<prismic.CustomTypeModelLinkToMediaField>({
})

/**
* Supports optional `text` property.
* Supports optional `allowText` property.
*/
expectType<prismic.CustomTypeModelLinkToMediaField>({
type: prismic.CustomTypeModelFieldType.Link,
Expand Down

0 comments on commit ed120a8

Please sign in to comment.