diff --git a/package-lock.json b/package-lock.json index bc219c1..d52852c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "prismic-ts-codegen": "bin/prismic-ts-codegen.js" }, "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", @@ -913,9 +913,9 @@ } }, "node_modules/@prismicio/client": { - "version": "7.5.0", - "resolved": "https://registry.npmjs.org/@prismicio/client/-/client-7.5.0.tgz", - "integrity": "sha512-4gcW59r2/JF9hngd7HRTJ3dmOJ3+befegb7FQi3s5e5Eo4UBSDgq9ufh9d/pJ0yqsRpuPrjbib82uJBlFQf+rQ==", + "version": "7.6.0-alpha.0", + "resolved": "https://registry.npmjs.org/@prismicio/client/-/client-7.6.0-alpha.0.tgz", + "integrity": "sha512-3bztcXISdis8PUxOGpEGW8Owc1OC0i+MB19PeRY7T/VJJx9EqrqmDoQXr65itIl7noQ2pLFx2RvicM993/Uf0g==", "dev": true, "dependencies": { "imgix-url-builder": "^0.0.4" @@ -9330,9 +9330,9 @@ } }, "@prismicio/client": { - "version": "7.5.0", - "resolved": "https://registry.npmjs.org/@prismicio/client/-/client-7.5.0.tgz", - "integrity": "sha512-4gcW59r2/JF9hngd7HRTJ3dmOJ3+befegb7FQi3s5e5Eo4UBSDgq9ufh9d/pJ0yqsRpuPrjbib82uJBlFQf+rQ==", + "version": "7.6.0-alpha.0", + "resolved": "https://registry.npmjs.org/@prismicio/client/-/client-7.6.0-alpha.0.tgz", + "integrity": "sha512-3bztcXISdis8PUxOGpEGW8Owc1OC0i+MB19PeRY7T/VJJx9EqrqmDoQXr65itIl7noQ2pLFx2RvicM993/Uf0g==", "dev": true, "requires": { "imgix-url-builder": "^0.0.4" diff --git a/package.json b/package.json index 16f8339..67ff019 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/buildFieldProperties.ts b/src/lib/buildFieldProperties.ts index 290b764..762ba93 100644 --- a/src/lib/buildFieldProperties.ts +++ b/src/lib/buildFieldProperties.ts @@ -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"; @@ -291,10 +294,25 @@ function buildFieldProperty( }); contentTypeNames.push(itemName); - code = addLine( - `${name}: prismic.GroupField>;`, - 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>;`, + code, + ); + } else { + code = addLine( + `${name}: prismic.GroupField>;`, + code, + ); + } break; } diff --git a/test/generateTypes-group.test.ts b/test/generateTypes-group.test.ts index 165d11b..3cbe6ad 100644 --- a/test/generateTypes-group.test.ts +++ b/test/generateTypes-group.test.ts @@ -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>", + ); + + 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",