Skip to content

Commit

Permalink
feat: support groups in a slice's primary section (#338)
Browse files Browse the repository at this point in the history
* feat: support groups in a slice's primary section

* chore: use `@prismicio/types-internal` alpha

* refactor: use `type` import

* chore: release alpha as minor

* fix: update `@prismicio/types-internal`

* chore(release): 7.5.0-alpha.0

* fix: export `CustomTypeModelFieldForSlicePrimary`

* chore(release): 7.5.0-alpha.1

* fix: update

* chore(release): 7.5.0-alpha.2

* fix: update @prismicio/types-internal

* chore(release): 7.5.0-alpha.3

---------

Co-authored-by: Xavier Rutayisire <xavier.rutayisire@gmail.com>
  • Loading branch information
angeloashmore and xrutayisire authored May 8, 2024
1 parent f141026 commit 0672bcb
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 35 deletions.
56 changes: 56 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,62 @@

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.5.0-alpha.3](https://github.com/prismicio/prismic-client/compare/v7.5.0-alpha.2...v7.5.0-alpha.3) (2024-05-07)


### Bug Fixes

* update @prismicio/types-internal ([e75b4c2](https://github.com/prismicio/prismic-client/commit/e75b4c2fb6a6495543d66556d42e787e2ab226c9))

### [7.4.1](https://github.com/prismicio/prismic-client/compare/v7.4.0...v7.4.1) (2024-04-13)


### Bug Fixes

* type error in `mapSliceZone` ([#339](https://github.com/prismicio/prismic-client/issues/339)) ([638e87a](https://github.com/prismicio/prismic-client/commit/638e87aeb97a04c0b8fbb160bd65a1d26028335e))


### Chore

* **release:** 7.4.1 ([f141026](https://github.com/prismicio/prismic-client/commit/f141026309e21be8f7e2fbdcc9dea9c5dc4594b2))

## [7.5.0-alpha.2](https://github.com/prismicio/prismic-client/compare/v7.5.0-alpha.1...v7.5.0-alpha.2) (2024-04-26)


### Bug Fixes

* update ([c28ae3a](https://github.com/prismicio/prismic-client/commit/c28ae3aa53a9c82573677d5ceaadf51b051b729d))

## [7.5.0-alpha.1](https://github.com/prismicio/prismic-client/compare/v7.5.0-alpha.0...v7.5.0-alpha.1) (2024-04-18)


### Bug Fixes

* export `CustomTypeModelFieldForSlicePrimary` ([16739f6](https://github.com/prismicio/prismic-client/commit/16739f6ec00a244e51b008f98d243be739376c50))

## [7.5.0-alpha.0](https://github.com/prismicio/prismic-client/compare/v7.4.0...v7.5.0-alpha.0) (2024-04-17)


### Features

* support groups in a slice's primary section ([98a44fa](https://github.com/prismicio/prismic-client/commit/98a44fa7a9da314f24d8b2ba518c921625391db2))


### Bug Fixes

* update `@prismicio/types-internal` ([3ee1b06](https://github.com/prismicio/prismic-client/commit/3ee1b064f2ae5de7ddbbd2e69f93776ff0d9a0ad))


### Refactor

* use `type` import ([46c4343](https://github.com/prismicio/prismic-client/commit/46c43439cce0ccf6626102a77918cea1e04736b7))


### Chore

* release alpha as minor ([9bd6355](https://github.com/prismicio/prismic-client/commit/9bd6355d7098f45f221821e3485edc8bad05f29e))
* use `@prismicio/types-internal` alpha ([df8f63d](https://github.com/prismicio/prismic-client/commit/df8f63d858227a246913dac33f15529294fc0515))

### [7.4.1](https://github.com/prismicio/prismic-client/compare/v7.4.0...v7.4.1) (2024-04-13)


Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismicio/client",
"version": "7.4.1",
"version": "7.5.0-alpha.3",
"description": "The official JavaScript + TypeScript client library for Prismic",
"keywords": [
"typescript",
Expand Down Expand Up @@ -69,8 +69,8 @@
"prepare": "npm run build",
"release": "npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
"release:dry": "standard-version --dry-run",
"release:alpha": "npm run test && standard-version --release-as major --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
"release:alpha:dry": "standard-version --release-as major --prerelease alpha --dry-run",
"release:alpha": "npm run test && standard-version --release-as minor --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
"release:alpha:dry": "standard-version --release-as minor --prerelease alpha --dry-run",
"lint": "eslint --ext .js,.ts .",
"types": "tsc --noEmit",
"unit": "vitest run --coverage",
Expand All @@ -83,7 +83,7 @@
},
"devDependencies": {
"@prismicio/mock": "^0.3.1",
"@prismicio/types-internal": "2.3.0",
"@prismicio/types-internal": "^2.4.0",
"@size-limit/preset-small-lib": "^9.0.0",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@typescript-eslint/eslint-plugin": "^6.7.4",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ export type { Slice } from "./types/value/slice";
export type { SharedSlice } from "./types/value/sharedSlice";
export type { SharedSliceVariation } from "./types/value/sharedSliceVariation";

export type { FieldState, AnyRegularField } from "./types/value/types";
export type {
FieldState,
AnyRegularField,
AnySlicePrimaryField,
} from "./types/value/types";

// Models - Types representing Prismic content models.
export { CustomTypeModelFieldType } from "./types/model/types";
Expand Down Expand Up @@ -300,6 +304,7 @@ export type { CustomTypeModelSeparatorField } from "./types/model/separator";
export type {
CustomTypeModelField,
CustomTypeModelFieldForGroup,
CustomTypeModelFieldForSlicePrimary,
} from "./types/model/types";

// API - Types representing Prismic Rest API V2 responses.
Expand Down
11 changes: 7 additions & 4 deletions src/types/model/sharedSliceVariation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { CustomTypeModelFieldForGroup } from "./types";
import type {
CustomTypeModelFieldForGroup,
CustomTypeModelFieldForSlicePrimary,
} from "./types";

/**
* A shared Slice variation.
Expand All @@ -10,10 +13,10 @@ import type { CustomTypeModelFieldForGroup } from "./types";
*/
export interface SharedSliceModelVariation<
ID extends string = string,
PrimaryFields extends Record<string, CustomTypeModelFieldForGroup> = Record<
PrimaryFields extends Record<
string,
CustomTypeModelFieldForGroup
>,
CustomTypeModelFieldForSlicePrimary
> = Record<string, CustomTypeModelFieldForSlicePrimary>,
ItemFields extends Record<string, CustomTypeModelFieldForGroup> = Record<
string,
CustomTypeModelFieldForGroup
Expand Down
7 changes: 7 additions & 0 deletions src/types/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export type CustomTypeModelField =
| CustomTypeModelSliceZoneField
| CustomTypeModelFieldForGroup;

/**
* Any custom type field that is valid for a slice's primary section.
*/
export type CustomTypeModelFieldForSlicePrimary =
| CustomTypeModelGroupField
| CustomTypeModelFieldForGroup;

/**
* Any custom type field that is valid for a group field.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/types/value/sharedSliceVariation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { AnyRegularField } from "./types";
import type { AnyRegularField, AnySlicePrimaryField } from "./types";

/**
* A shared Slice variation.
*/
export interface SharedSliceVariation<
Variation = string,
PrimaryFields extends Record<string, AnyRegularField> = Record<
PrimaryFields extends Record<string, AnySlicePrimaryField> = Record<
string,
AnyRegularField
AnySlicePrimaryField
>,
ItemsFields extends Record<string, AnyRegularField> = Record<
string,
Expand Down
6 changes: 6 additions & 0 deletions src/types/value/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ContentRelationshipField } from "./contentRelationship";
import type { DateField } from "./date";
import type { EmbedField } from "./embed";
import type { GeoPointField } from "./geoPoint";
import type { GroupField } from "./group";
import type { ImageField } from "./image";
import type { IntegrationField } from "./integration";
import type { KeyTextField } from "./keyText";
Expand Down Expand Up @@ -46,6 +47,11 @@ export type AnyRegularField =
| GeoPointField
| IntegrationField;

/**
* Any field that can be used in a slice's primary section.
*/
export type AnySlicePrimaryField = GroupField | AnyRegularField;

/**
* 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
45 changes: 43 additions & 2 deletions test/types/customType-sharedSliceModelVariation.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ expectType<prismic.SharedSliceModelVariation<"foo">>({
expectType<
prismic.SharedSliceModelVariation<
string,
{ foo: prismic.CustomTypeModelBooleanField }
{
foo: prismic.CustomTypeModelBooleanField;
bar: prismic.CustomTypeModelGroupField<{
baz: prismic.CustomTypeModelBooleanField;
}>;
}
>
>({
id: "foo",
Expand All @@ -91,8 +96,22 @@ expectType<
label: "string",
},
},
// @ts-expect-error - Only given fields are valid.
bar: {
type: prismic.CustomTypeModelFieldType.Group,
config: {
label: "string",
fields: {
baz: {
type: "Boolean",
config: {
label: "string",
},
},
},
},
},
// @ts-expect-error - Only given fields are valid.
qux: {
type: prismic.CustomTypeModelFieldType.Boolean,
config: {
label: "string",
Expand Down Expand Up @@ -137,6 +156,28 @@ expectType<
imageUrl: "string",
});

/**
* Does not support groups in items section.
*/
expectType<
prismic.SharedSliceModelVariation<
string,
Record<string, never>,
// @ts-expect-error - Group fields are not supported.
{ foo: prismic.CustomTypeModelGroupField }
>
>({
id: "foo",
name: "string",
docURL: "string",
version: "string",
description: "string",
primary: {},
// @ts-expect-error - We don't care about the actual value.
items: {},
imageUrl: "string",
});

/**
* `@prismicio/types` extends `@prismicio/types-internal`
*/
Expand Down
49 changes: 34 additions & 15 deletions test/types/fields-sharedSliceVariation.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,36 @@ expectType<prismic.SharedSliceVariation<"foo">>({
/**
* Supports custom primary fields type.
*/
expectType<prismic.SharedSliceVariation<string, { foo: prismic.BooleanField }>>(
{
variation: "string",
version: "string",
primary: {
foo: true,
// @ts-expect-error - Only given fields are valid.
bar: false,
},
items: [],
expectType<
prismic.SharedSliceVariation<
string,
{ foo: prismic.BooleanField; bar: prismic.GroupField }
>
>({
variation: "string",
version: "string",
primary: {
foo: true,
bar: [],
// @ts-expect-error - Only given fields are valid.
baz: false,
},
);
items: [],
});

/**
* Supports custom items fields type.
*/
expectType<
prismic.SharedSliceVariation<
string,
{ foo: prismic.BooleanField },
Record<never, never>,
{ bar: prismic.KeyTextField }
>
>({
variation: "string",
version: "string",
primary: {
foo: true,
},
primary: {},
items: [
{
bar: "string",
Expand All @@ -81,3 +83,20 @@ expectType<
},
],
});

/**
* Does not support groups in items section.
*/
expectType<
prismic.SharedSliceVariation<
string,
Record<never, never>,
// @ts-expect-error - Group fields are not supported.
{ foo: prismic.GroupField }
>
>({
variation: "string",
version: "string",
primary: {},
items: [],
});
13 changes: 13 additions & 0 deletions test/types/fields.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ expectType<TypeOf<prismic.AnyRegularField, prismic.TitleField>>(true);
*/
expectType<TypeOf<prismic.AnyRegularField, prismic.SliceZone>>(false);
expectType<TypeOf<prismic.AnyRegularField, prismic.GroupField>>(false);

/**
* AnySlicePrimaryField supports any field compatible with a slice's primary
* section.
*/
expectType<TypeOf<prismic.AnySlicePrimaryField, prismic.GroupField>>(true);
expectType<TypeOf<prismic.AnySlicePrimaryField, prismic.AnyRegularField>>(true);

/**
* AnySlicePrimaryField excludes any fields not compatible with a slice's
* primary section.
*/
expectType<TypeOf<prismic.AnySlicePrimaryField, prismic.SliceZone>>(false);

0 comments on commit 0672bcb

Please sign in to comment.