Skip to content

Commit

Permalink
feat: support groups in a slice's primary section
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Apr 10, 2024
1 parent 8b20c5a commit 98a44fa
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 25 deletions.
6 changes: 5 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
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 { 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 98a44fa

Please sign in to comment.