Skip to content

Commit

Permalink
feat: support groups in groups
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Jun 5, 2024
1 parent 71b05a8 commit e93146c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"quick-lru": "^6.1.1"
},
"devDependencies": {
"@prismicio/client": "^7.5.0",
"@prismicio/client": "^7.6.0-alpha.0",
"@prismicio/mock": "^0.3.6",
"@size-limit/preset-small-lib": "^8.2.6",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
Expand Down
26 changes: 22 additions & 4 deletions src/lib/buildFieldProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { CustomTypeModelField } from "@prismicio/client";
import {
type CustomTypeModelField,
CustomTypeModelFieldType,
} from "@prismicio/client";
import { source, stripIndent } from "common-tags";

import { AuxiliaryType, FieldConfigs, FieldPath } from "../types";
Expand Down Expand Up @@ -291,10 +294,25 @@ function buildFieldProperty(
});
contentTypeNames.push(itemName);

code = addLine(
`${name}: prismic.GroupField<Simplify<${itemName}>>;`,
code,
const indexOfFirstGroupInPath = path.findIndex(
(pathElement) =>
pathElement.model &&
"type" in pathElement.model &&
pathElement.model.type === CustomTypeModelFieldType.Group,
);
const isNestedGroup = indexOfFirstGroupInPath < path.length - 1;

if (isNestedGroup) {
code = addLine(
`${name}: prismic.NestedGroupField<Simplify<${itemName}>>;`,
code,
);
} else {
code = addLine(
`${name}: prismic.GroupField<Simplify<${itemName}>>;`,
code,
);
}

break;
}
Expand Down
45 changes: 45 additions & 0 deletions test/generateTypes-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,51 @@ it("creates an interface for a group item containing its fields", (ctx) => {
).toBe("prismic.SelectField");
});

it("supports nested groups", (ctx) => {
const model = ctx.mock.model.customType({
id: "foo",
fields: {
bar: ctx.mock.model.group({
fields: {
baz: ctx.mock.model.group({
fields: {
qux: ctx.mock.model.keyText(),
quux: ctx.mock.model.select(),
},
}),
},
}),
},
});

const types = lib.generateTypes({ customTypeModels: [model] });
const file = parseSourceFile(types);

const nestedGroupProperty = file
.getInterfaceOrThrow("FooDocumentDataBarItem")
.getPropertyOrThrow("baz");
expect(nestedGroupProperty.getTypeNodeOrThrow().getText()).toBe(
"prismic.NestedGroupField<Simplify<FooDocumentDataBazItem>>",
);

const nestedGroupItemInterface = file.getInterfaceOrThrow(
"FooDocumentDataBazItem",
);
expect(nestedGroupItemInterface.isExported()).toBe(true);
expect(
nestedGroupItemInterface
.getPropertyOrThrow("qux")
.getTypeNodeOrThrow()
.getText(),
).toBe("prismic.KeyTextField");
expect(
nestedGroupItemInterface
.getPropertyOrThrow("quux")
.getTypeNodeOrThrow()
.getText(),
).toBe("prismic.SelectField");
});

it("prefixes Group types starting with a number using an underscore prefix", (ctx) => {
const model = ctx.mock.model.customType({
id: "123",
Expand Down

0 comments on commit e93146c

Please sign in to comment.