Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support groups in groups #63

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

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.

### [0.1.21-alpha.0](https://github.com/prismicio/prismic-ts-codegen/compare/v0.1.20...v0.1.21-alpha.0) (2024-06-05)


### Features

* support groups in groups ([e93146c](https://github.com/prismicio/prismic-ts-codegen/commit/e93146c2dead44caeb869b309f4a0e20fb75ebd1))


### Bug Fixes

* update `@prismicio/mock` to prevent a type error ([bb2a846](https://github.com/prismicio/prismic-ts-codegen/commit/bb2a846326c99068348ebd7d30e8d89919c48b29))


### Chore

* add temporary `overrides` entry to prevent `@prismicio/mock` installation error ([33cb3d7](https://github.com/prismicio/prismic-ts-codegen/commit/33cb3d765634e595258c9765fd1e49ec78a79aad))

### [0.1.20](https://github.com/prismicio/prismic-ts-codegen/compare/v0.1.19...v0.1.20) (2024-05-08)


Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

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

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prismic-ts-codegen",
"version": "0.1.20",
"version": "0.1.21-alpha.0",
"description": "An experimental Prismic model-to-TypeScript-type generator",
"keywords": [
"typescript",
Expand Down Expand Up @@ -37,8 +37,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 patch --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
"release:alpha:dry": "standard-version --release-as patch --prerelease alpha --dry-run",
"lint": "eslint --ext .js,.ts .",
"types": "tsc --noEmit",
"unit": "vitest run --coverage",
Expand All @@ -60,8 +60,8 @@
"quick-lru": "^6.1.1"
},
"devDependencies": {
"@prismicio/client": "^7.5.0",
"@prismicio/mock": "^0.3.6",
"@prismicio/client": "^7.6.0-alpha.0",
"@prismicio/mock": "^0.3.7-alpha.1",
"@size-limit/preset-small-lib": "^8.2.6",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@types/common-tags": "^1.8.1",
Expand Down Expand Up @@ -99,5 +99,8 @@
},
"publishConfig": {
"access": "public"
},
"overrides": {
"@prismicio/client": "^7.6.0-alpha.0"
}
}
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>>",
);
lihbr marked this conversation as resolved.
Show resolved Hide resolved

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
Loading