prismic-ts-codegen 0.1.10 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.cjs +19 -21
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.ts +2 -3
- package/dist/constants.js +19 -21
- package/dist/constants.js.map +1 -1
- package/dist/generateTypes.cjs +71 -90
- package/dist/generateTypes.cjs.map +1 -1
- package/dist/generateTypes.d.ts +2 -1
- package/dist/generateTypes.js +71 -90
- package/dist/generateTypes.js.map +1 -1
- package/dist/lib/addBlankLine.cjs +12 -0
- package/dist/lib/addBlankLine.cjs.map +1 -0
- package/dist/lib/addBlankLine.d.ts +5 -0
- package/dist/lib/addBlankLine.js +12 -0
- package/dist/lib/addBlankLine.js.map +1 -0
- package/dist/lib/addLine.cjs +8 -0
- package/dist/lib/addLine.cjs.map +1 -0
- package/dist/lib/addLine.d.ts +1 -0
- package/dist/lib/addLine.js +8 -0
- package/dist/lib/addLine.js.map +1 -0
- package/dist/lib/addSection.cjs +8 -0
- package/dist/lib/addSection.cjs.map +1 -0
- package/dist/lib/addSection.d.ts +1 -0
- package/dist/lib/addSection.js +8 -0
- package/dist/lib/addSection.js.map +1 -0
- package/dist/lib/addTrailingNewLine.cjs +11 -0
- package/dist/lib/addTrailingNewLine.cjs.map +1 -0
- package/dist/lib/addTrailingNewLine.d.ts +5 -0
- package/dist/lib/addTrailingNewLine.js +11 -0
- package/dist/lib/addTrailingNewLine.js.map +1 -0
- package/dist/lib/buildCustomTypeDataType.cjs +51 -0
- package/dist/lib/buildCustomTypeDataType.cjs.map +1 -0
- package/dist/lib/buildCustomTypeDataType.d.ts +13 -0
- package/dist/lib/buildCustomTypeDataType.js +51 -0
- package/dist/lib/buildCustomTypeDataType.js.map +1 -0
- package/dist/lib/buildCustomTypeType.cjs +60 -0
- package/dist/lib/buildCustomTypeType.cjs.map +1 -0
- package/dist/lib/buildCustomTypeType.d.ts +17 -0
- package/dist/lib/buildCustomTypeType.js +60 -0
- package/dist/lib/buildCustomTypeType.js.map +1 -0
- package/dist/lib/buildFieldDocs.cjs +67 -35
- package/dist/lib/buildFieldDocs.cjs.map +1 -1
- package/dist/lib/buildFieldDocs.d.ts +7 -11
- package/dist/lib/buildFieldDocs.js +67 -35
- package/dist/lib/buildFieldDocs.js.map +1 -1
- package/dist/lib/buildFieldProperties.cjs +300 -0
- package/dist/lib/buildFieldProperties.cjs.map +1 -0
- package/dist/lib/buildFieldProperties.d.ts +14 -0
- package/dist/lib/buildFieldProperties.js +300 -0
- package/dist/lib/buildFieldProperties.js.map +1 -0
- package/dist/lib/buildSharedSliceType.cjs +140 -0
- package/dist/lib/buildSharedSliceType.cjs.map +1 -0
- package/dist/lib/buildSharedSliceType.d.ts +16 -0
- package/dist/lib/buildSharedSliceType.js +140 -0
- package/dist/lib/buildSharedSliceType.js.map +1 -0
- package/dist/lib/buildTypeName.cjs +4 -2
- package/dist/lib/buildTypeName.cjs.map +1 -1
- package/dist/lib/buildTypeName.js +4 -2
- package/dist/lib/buildTypeName.js.map +1 -1
- package/dist/lib/buildUnion.cjs +7 -0
- package/dist/lib/buildUnion.cjs.map +1 -0
- package/dist/lib/buildUnion.d.ts +1 -0
- package/dist/lib/buildUnion.js +7 -0
- package/dist/lib/buildUnion.js.map +1 -0
- package/dist/lib/checkHasUIDFIeld.cjs +7 -0
- package/dist/lib/checkHasUIDFIeld.cjs.map +1 -0
- package/dist/lib/checkHasUIDFIeld.d.ts +2 -0
- package/dist/lib/checkHasUIDFIeld.js +7 -0
- package/dist/lib/checkHasUIDFIeld.js.map +1 -0
- package/dist/lib/getAPIIDPath.cjs +18 -14
- package/dist/lib/getAPIIDPath.cjs.map +1 -1
- package/dist/lib/getAPIIDPath.d.ts +4 -8
- package/dist/lib/getAPIIDPath.js +18 -14
- package/dist/lib/getAPIIDPath.js.map +1 -1
- package/dist/lib/getCacheKey.cjs +25 -0
- package/dist/lib/getCacheKey.cjs.map +1 -0
- package/dist/lib/getCacheKey.d.ts +9 -0
- package/dist/lib/getCacheKey.js +8 -0
- package/dist/lib/getCacheKey.js.map +1 -0
- package/dist/lib/getHumanReadableModelName.cjs +13 -17
- package/dist/lib/getHumanReadableModelName.cjs.map +1 -1
- package/dist/lib/getHumanReadableModelName.d.ts +3 -3
- package/dist/lib/getHumanReadableModelName.js +13 -17
- package/dist/lib/getHumanReadableModelName.js.map +1 -1
- package/dist/lib/{getHumanReadableFieldPath.cjs → getHumanReadablePath.cjs} +8 -8
- package/dist/lib/getHumanReadablePath.cjs.map +1 -0
- package/dist/lib/getHumanReadablePath.d.ts +6 -0
- package/dist/lib/{getHumanReadableFieldPath.js → getHumanReadablePath.js} +8 -8
- package/dist/lib/getHumanReadablePath.js.map +1 -0
- package/dist/types.d.ts +9 -1
- package/package.json +19 -18
- package/src/constants.ts +19 -22
- package/src/generateTypes.ts +109 -108
- package/src/lib/addBlankLine.ts +13 -0
- package/src/lib/addLine.ts +5 -0
- package/src/lib/addSection.ts +5 -0
- package/src/lib/addTrailingNewLine.ts +14 -0
- package/src/lib/buildCustomTypeDataType.ts +74 -0
- package/src/lib/buildCustomTypeType.ts +101 -0
- package/src/lib/buildFieldDocs.ts +128 -68
- package/src/lib/buildFieldProperties.ts +513 -0
- package/src/lib/buildSharedSliceType.ts +213 -0
- package/src/lib/buildTypeName.ts +4 -2
- package/src/lib/buildUnion.ts +3 -0
- package/src/lib/checkHasUIDFIeld.ts +5 -0
- package/src/lib/getAPIIDPath.ts +40 -41
- package/src/lib/getCacheKey.ts +13 -0
- package/src/lib/getHumanReadableModelName.ts +23 -20
- package/src/lib/getHumanReadablePath.ts +24 -0
- package/src/types.ts +11 -1
- package/dist/lib/addInterfacePropertiesForFields.cjs +0 -489
- package/dist/lib/addInterfacePropertiesForFields.cjs.map +0 -1
- package/dist/lib/addInterfacePropertiesForFields.d.ts +0 -21
- package/dist/lib/addInterfacePropertiesForFields.js +0 -489
- package/dist/lib/addInterfacePropertiesForFields.js.map +0 -1
- package/dist/lib/addTypeAliasForCustomType.cjs +0 -81
- package/dist/lib/addTypeAliasForCustomType.cjs.map +0 -1
- package/dist/lib/addTypeAliasForCustomType.d.ts +0 -11
- package/dist/lib/addTypeAliasForCustomType.js +0 -81
- package/dist/lib/addTypeAliasForCustomType.js.map +0 -1
- package/dist/lib/addTypeAliasForSharedSlice.cjs +0 -149
- package/dist/lib/addTypeAliasForSharedSlice.cjs.map +0 -1
- package/dist/lib/addTypeAliasForSharedSlice.d.ts +0 -10
- package/dist/lib/addTypeAliasForSharedSlice.js +0 -149
- package/dist/lib/addTypeAliasForSharedSlice.js.map +0 -1
- package/dist/lib/buildSharedSliceInterfaceNamePart.cjs +0 -8
- package/dist/lib/buildSharedSliceInterfaceNamePart.cjs.map +0 -1
- package/dist/lib/buildSharedSliceInterfaceNamePart.d.ts +0 -5
- package/dist/lib/buildSharedSliceInterfaceNamePart.js +0 -8
- package/dist/lib/buildSharedSliceInterfaceNamePart.js.map +0 -1
- package/dist/lib/getFieldDocumentationURL.cjs +0 -8
- package/dist/lib/getFieldDocumentationURL.cjs.map +0 -1
- package/dist/lib/getFieldDocumentationURL.d.ts +0 -6
- package/dist/lib/getFieldDocumentationURL.js +0 -8
- package/dist/lib/getFieldDocumentationURL.js.map +0 -1
- package/dist/lib/getHumanReadableFieldPath.cjs.map +0 -1
- package/dist/lib/getHumanReadableFieldPath.d.ts +0 -10
- package/dist/lib/getHumanReadableFieldPath.js.map +0 -1
- package/dist/lib/getHumanReadableFieldType.cjs +0 -36
- package/dist/lib/getHumanReadableFieldType.cjs.map +0 -1
- package/dist/lib/getHumanReadableFieldType.d.ts +0 -6
- package/dist/lib/getHumanReadableFieldType.js +0 -36
- package/dist/lib/getHumanReadableFieldType.js.map +0 -1
- package/dist/lib/getSourceFileText.cjs +0 -9
- package/dist/lib/getSourceFileText.cjs.map +0 -1
- package/dist/lib/getSourceFileText.d.ts +0 -2
- package/dist/lib/getSourceFileText.js +0 -9
- package/dist/lib/getSourceFileText.js.map +0 -1
- package/dist/lib/hasOwnProperty.cjs +0 -7
- package/dist/lib/hasOwnProperty.cjs.map +0 -1
- package/dist/lib/hasOwnProperty.d.ts +0 -10
- package/dist/lib/hasOwnProperty.js +0 -7
- package/dist/lib/hasOwnProperty.js.map +0 -1
- package/dist/lib/isCustomTypeModel.cjs +0 -7
- package/dist/lib/isCustomTypeModel.cjs.map +0 -1
- package/dist/lib/isCustomTypeModel.d.ts +0 -2
- package/dist/lib/isCustomTypeModel.js +0 -7
- package/dist/lib/isCustomTypeModel.js.map +0 -1
- package/dist/lib/isCustomTypeModelField.cjs +0 -7
- package/dist/lib/isCustomTypeModelField.cjs.map +0 -1
- package/dist/lib/isCustomTypeModelField.d.ts +0 -2
- package/dist/lib/isCustomTypeModelField.js +0 -7
- package/dist/lib/isCustomTypeModelField.js.map +0 -1
- package/dist/lib/isCustomTypeModelSlice.cjs +0 -8
- package/dist/lib/isCustomTypeModelSlice.cjs.map +0 -1
- package/dist/lib/isCustomTypeModelSlice.d.ts +0 -2
- package/dist/lib/isCustomTypeModelSlice.js +0 -8
- package/dist/lib/isCustomTypeModelSlice.js.map +0 -1
- package/dist/lib/isSharedSliceModel.cjs +0 -8
- package/dist/lib/isSharedSliceModel.cjs.map +0 -1
- package/dist/lib/isSharedSliceModel.d.ts +0 -2
- package/dist/lib/isSharedSliceModel.js +0 -8
- package/dist/lib/isSharedSliceModel.js.map +0 -1
- package/dist/lib/pascalCase.cjs +0 -8
- package/dist/lib/pascalCase.cjs.map +0 -1
- package/dist/lib/pascalCase.d.ts +0 -1
- package/dist/lib/pascalCase.js +0 -8
- package/dist/lib/pascalCase.js.map +0 -1
- package/dist/lib/writeFieldDocsDescription.cjs +0 -29
- package/dist/lib/writeFieldDocsDescription.cjs.map +0 -1
- package/dist/lib/writeFieldDocsDescription.d.ts +0 -13
- package/dist/lib/writeFieldDocsDescription.js +0 -29
- package/dist/lib/writeFieldDocsDescription.js.map +0 -1
- package/src/lib/addInterfacePropertiesForFields.ts +0 -665
- package/src/lib/addTypeAliasForCustomType.ts +0 -120
- package/src/lib/addTypeAliasForSharedSlice.ts +0 -201
- package/src/lib/buildSharedSliceInterfaceNamePart.ts +0 -11
- package/src/lib/getFieldDocumentationURL.ts +0 -15
- package/src/lib/getHumanReadableFieldPath.ts +0 -36
- package/src/lib/getHumanReadableFieldType.ts +0 -55
- package/src/lib/getSourceFileText.ts +0 -14
- package/src/lib/hasOwnProperty.ts +0 -15
- package/src/lib/isCustomTypeModel.ts +0 -5
- package/src/lib/isCustomTypeModelField.ts +0 -7
- package/src/lib/isCustomTypeModelSlice.ts +0 -14
- package/src/lib/isSharedSliceModel.ts +0 -14
- package/src/lib/pascalCase.ts +0 -9
- package/src/lib/writeFieldDocsDescription.ts +0 -51
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { CustomTypeModelFieldType, CustomTypeModelLinkSelectType } from "@prismicio/client";
|
|
2
|
+
import { stripIndent, source } from "common-tags";
|
|
3
|
+
import { addLine } from "./addLine.js";
|
|
4
|
+
import { addSection } from "./addSection.js";
|
|
5
|
+
import { buildFieldDocs } from "./buildFieldDocs.js";
|
|
6
|
+
import { buildTypeName } from "./buildTypeName.js";
|
|
7
|
+
import { buildUnion } from "./buildUnion.js";
|
|
8
|
+
import { getHumanReadablePath } from "./getHumanReadablePath.js";
|
|
9
|
+
function buildFieldProperty(args) {
|
|
10
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
11
|
+
let code = buildFieldDocs({
|
|
12
|
+
name: args.name,
|
|
13
|
+
field: args.field,
|
|
14
|
+
path: args.path,
|
|
15
|
+
tabName: args.tabName
|
|
16
|
+
});
|
|
17
|
+
const auxiliaryTypes = [];
|
|
18
|
+
const name = args.name.includes("-") || args.name.includes(":") || /^[0-9]/.test(args.name) ? `"${args.name}"` : args.name;
|
|
19
|
+
switch (args.field.type) {
|
|
20
|
+
case CustomTypeModelFieldType.UID: {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
case CustomTypeModelFieldType.Boolean: {
|
|
24
|
+
code = addLine(`${name}: prismic.BooleanField;`, code);
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case CustomTypeModelFieldType.Color: {
|
|
28
|
+
code = addLine(`${name}: prismic.ColorField;`, code);
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
case CustomTypeModelFieldType.Date: {
|
|
32
|
+
code = addLine(`${name}: prismic.DateField;`, code);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case CustomTypeModelFieldType.Embed: {
|
|
36
|
+
const providerTypes = [];
|
|
37
|
+
if ((_a = args.fieldConfigs.embed) == null ? void 0 : _a.providerTypes) {
|
|
38
|
+
for (const providerType in (_b = args.fieldConfigs.embed) == null ? void 0 : _b.providerTypes) {
|
|
39
|
+
const configuredProviderType = (_c = args.fieldConfigs.embed) == null ? void 0 : _c.providerTypes[providerType];
|
|
40
|
+
providerTypes.push(`({ provider_name: "${providerType}" } & ${configuredProviderType})`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const providerTypesUnion = buildUnion(providerTypes);
|
|
44
|
+
code = providerTypes.length > 0 ? addLine(`${name}: prismic.EmbedField<prismic.AnyOEmbed & prismic.OEmbedExtra & (${providerTypesUnion})>`, code) : addLine(`${name}: prismic.EmbedField`, code);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case CustomTypeModelFieldType.GeoPoint: {
|
|
48
|
+
code = addLine(`${name}: prismic.GeoPointField;`, code);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
case CustomTypeModelFieldType.Image: {
|
|
52
|
+
if (((_d = args.field.config) == null ? void 0 : _d.thumbnails) && args.field.config.thumbnails.length > 0) {
|
|
53
|
+
const thumbnailNames = buildUnion(args.field.config.thumbnails.map((thumb) => `"${thumb.name}"`));
|
|
54
|
+
code = addLine(`${name}: prismic.ImageField<${thumbnailNames}>;`, code);
|
|
55
|
+
} else {
|
|
56
|
+
code = addLine(`${name}: prismic.ImageField<never>;`, code);
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case CustomTypeModelFieldType.Integration: {
|
|
61
|
+
const catalogType = ((_e = args.field.config) == null ? void 0 : _e.catalog) ? (_g = (_f = args.fieldConfigs.integrationFields) == null ? void 0 : _f.catalogTypes) == null ? void 0 : _g[args.field.config.catalog] : void 0;
|
|
62
|
+
if (catalogType) {
|
|
63
|
+
code = addLine(`${name}: prismic.IntegrationField<${catalogType}>;`, code);
|
|
64
|
+
} else {
|
|
65
|
+
code = addLine(`${name}: prismic.IntegrationField;`, code);
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case CustomTypeModelFieldType.Link: {
|
|
70
|
+
switch ((_h = args.field.config) == null ? void 0 : _h.select) {
|
|
71
|
+
case CustomTypeModelLinkSelectType.Document: {
|
|
72
|
+
if ("customtypes" in args.field.config && args.field.config.customtypes && args.field.config.customtypes.length > 0) {
|
|
73
|
+
const customTypeIDsUnion = buildUnion(args.field.config.customtypes.map((type) => `"${type}"`));
|
|
74
|
+
code = addLine(`${name}: prismic.ContentRelationshipField<${customTypeIDsUnion}>;`, code);
|
|
75
|
+
} else {
|
|
76
|
+
code = addLine(`${name}: prismic.ContentRelationshipField;`, code);
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case CustomTypeModelLinkSelectType.Media: {
|
|
81
|
+
code = addLine(`${name}: prismic.LinkToMediaField;`, code);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
default: {
|
|
85
|
+
code = addLine(`${name}: prismic.LinkField;`, code);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case CustomTypeModelFieldType.Number: {
|
|
91
|
+
code = addLine(`${name}: prismic.NumberField;`, code);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case CustomTypeModelFieldType.StructuredText: {
|
|
95
|
+
const isTitleField = args.field.config && "single" in args.field.config && args.field.config.single && args.field.config.single.split(",").every((blockType) => /heading/.test(blockType));
|
|
96
|
+
if (isTitleField) {
|
|
97
|
+
code = addLine(`${name}: prismic.TitleField;`, code);
|
|
98
|
+
} else {
|
|
99
|
+
code = addLine(`${name}: prismic.RichTextField;`, code);
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case CustomTypeModelFieldType.Select: {
|
|
104
|
+
const options = ((_j = (_i = args.field.config) == null ? void 0 : _i.options) == null ? void 0 : _j.map((option) => `"${option}"`)) || [];
|
|
105
|
+
const optionsType = options.length ? buildUnion(options) : "string";
|
|
106
|
+
const hasDefault = Boolean((_k = args.field.config) == null ? void 0 : _k.default_value);
|
|
107
|
+
if (hasDefault) {
|
|
108
|
+
code = addLine(`${name}: prismic.SelectField<${optionsType}, "filled">;`, code);
|
|
109
|
+
} else {
|
|
110
|
+
code = addLine(`${name}: prismic.SelectField${options.length > 0 ? `<${optionsType}>` : ""};`, code);
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case CustomTypeModelFieldType.Text: {
|
|
115
|
+
code = addLine(`${name}: prismic.KeyTextField;`, code);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case CustomTypeModelFieldType.Timestamp: {
|
|
119
|
+
code = addLine(`${name}: prismic.TimestampField;`, code);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case CustomTypeModelFieldType.Group: {
|
|
123
|
+
const itemName = buildTypeName(args.path[0].name, "Document", "Data", args.name, "Item");
|
|
124
|
+
const path = [
|
|
125
|
+
...args.path,
|
|
126
|
+
{
|
|
127
|
+
name,
|
|
128
|
+
model: args.field
|
|
129
|
+
}
|
|
130
|
+
];
|
|
131
|
+
const humanReadablePath = getHumanReadablePath({ path });
|
|
132
|
+
const itemFieldProperties = buildFieldProperties({
|
|
133
|
+
fields: ((_l = args.field.config) == null ? void 0 : _l.fields) || {},
|
|
134
|
+
fieldConfigs: args.fieldConfigs,
|
|
135
|
+
path
|
|
136
|
+
});
|
|
137
|
+
auxiliaryTypes.push({
|
|
138
|
+
name: itemName,
|
|
139
|
+
code: source`
|
|
140
|
+
/**
|
|
141
|
+
* Item in *${humanReadablePath}*
|
|
142
|
+
*/
|
|
143
|
+
export interface ${itemName} {
|
|
144
|
+
${itemFieldProperties.code}
|
|
145
|
+
}
|
|
146
|
+
`
|
|
147
|
+
});
|
|
148
|
+
code = addLine(`${name}: prismic.GroupField<Simplify<${itemName}>>;`, code);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case CustomTypeModelFieldType.Slices: {
|
|
152
|
+
const choiceNames = [];
|
|
153
|
+
if ((_m = args.field.config) == null ? void 0 : _m.choices) {
|
|
154
|
+
for (const choiceID in args.field.config.choices) {
|
|
155
|
+
const choice = args.field.config.choices[choiceID];
|
|
156
|
+
if (choice.type === "SharedSlice") {
|
|
157
|
+
choiceNames.push(buildTypeName(choiceID, "Slice"));
|
|
158
|
+
} else if (choice.type === "Slice") {
|
|
159
|
+
const sliceName = buildTypeName(args.path[0].name, "Document", "Data", args.name, choiceID, "Slice");
|
|
160
|
+
let primaryInterfaceName;
|
|
161
|
+
if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
|
|
162
|
+
primaryInterfaceName = buildTypeName(sliceName, "Primary");
|
|
163
|
+
const path = [
|
|
164
|
+
...args.path,
|
|
165
|
+
{
|
|
166
|
+
name: args.name,
|
|
167
|
+
model: args.field
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: choiceID,
|
|
171
|
+
model: choice
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: "primary",
|
|
175
|
+
label: "Primary"
|
|
176
|
+
}
|
|
177
|
+
];
|
|
178
|
+
const humanReadablePath = getHumanReadablePath({ path });
|
|
179
|
+
const primaryFieldProperties = buildFieldProperties({
|
|
180
|
+
fields: choice["non-repeat"],
|
|
181
|
+
fieldConfigs: args.fieldConfigs,
|
|
182
|
+
path
|
|
183
|
+
});
|
|
184
|
+
let primaryCode = stripIndent`
|
|
185
|
+
/**
|
|
186
|
+
* Primary content in *${humanReadablePath}*
|
|
187
|
+
*/
|
|
188
|
+
`;
|
|
189
|
+
primaryCode = primaryFieldProperties.code ? addLine(source`
|
|
190
|
+
export interface ${primaryInterfaceName} {
|
|
191
|
+
${primaryFieldProperties.code}
|
|
192
|
+
}
|
|
193
|
+
`, primaryCode) : addLine(`export interface ${primaryInterfaceName} {}`, primaryCode);
|
|
194
|
+
auxiliaryTypes.push({
|
|
195
|
+
name: primaryInterfaceName,
|
|
196
|
+
code: primaryCode
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
let itemInterfaceName;
|
|
200
|
+
if (choice.repeat && Object.keys(choice.repeat).length > 0) {
|
|
201
|
+
itemInterfaceName = buildTypeName(sliceName, "Item");
|
|
202
|
+
const path = [
|
|
203
|
+
...args.path,
|
|
204
|
+
{
|
|
205
|
+
name: args.name,
|
|
206
|
+
model: args.field
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
name: choiceID,
|
|
210
|
+
model: choice
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "items",
|
|
214
|
+
label: "Items"
|
|
215
|
+
}
|
|
216
|
+
];
|
|
217
|
+
const humanReadablePath = getHumanReadablePath({ path });
|
|
218
|
+
const itemFieldProperties = buildFieldProperties({
|
|
219
|
+
fields: choice.repeat,
|
|
220
|
+
fieldConfigs: args.fieldConfigs,
|
|
221
|
+
path
|
|
222
|
+
});
|
|
223
|
+
let itemCode = stripIndent`
|
|
224
|
+
/**
|
|
225
|
+
* Item content in *${humanReadablePath}*
|
|
226
|
+
*/
|
|
227
|
+
`;
|
|
228
|
+
itemCode = itemFieldProperties.code ? addLine(source`
|
|
229
|
+
export interface ${itemInterfaceName} {
|
|
230
|
+
${itemFieldProperties.code}
|
|
231
|
+
}
|
|
232
|
+
`, itemCode) : addLine(`export interface ${itemInterfaceName} {}`, itemCode);
|
|
233
|
+
auxiliaryTypes.push({
|
|
234
|
+
name: itemInterfaceName,
|
|
235
|
+
code: itemCode
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
auxiliaryTypes.push({
|
|
239
|
+
name: sliceName,
|
|
240
|
+
code: stripIndent`
|
|
241
|
+
/**
|
|
242
|
+
* Slice for *${getHumanReadablePath({
|
|
243
|
+
path: [
|
|
244
|
+
...args.path,
|
|
245
|
+
{
|
|
246
|
+
name: args.name,
|
|
247
|
+
model: args.field
|
|
248
|
+
}
|
|
249
|
+
]
|
|
250
|
+
})}*
|
|
251
|
+
*/
|
|
252
|
+
export type ${sliceName} = prismic.Slice<"${choiceID}", ${primaryInterfaceName ? `Simplify<${primaryInterfaceName}>` : `Record<string, never>`}, ${itemInterfaceName ? `Simplify<${itemInterfaceName}>` : `never`}>
|
|
253
|
+
`
|
|
254
|
+
});
|
|
255
|
+
choiceNames.push(sliceName);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const choiceUnionName = buildTypeName(args.path[0].name, "Document", "Data", args.name, "Slice");
|
|
260
|
+
const choiceUnion = buildUnion(choiceNames);
|
|
261
|
+
auxiliaryTypes.push({
|
|
262
|
+
name: choiceUnionName,
|
|
263
|
+
code: `type ${choiceUnionName} = ${choiceUnion}`
|
|
264
|
+
});
|
|
265
|
+
code = addLine(`${name}: prismic.SliceZone<${choiceUnionName}>;`, code);
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
default: {
|
|
269
|
+
code = addLine(`${name}: unknown;`, code);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
code,
|
|
274
|
+
auxiliaryTypes
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
function buildFieldProperties(args) {
|
|
278
|
+
let code = "";
|
|
279
|
+
const auxiliaryTypes = [];
|
|
280
|
+
for (const name in args.fields) {
|
|
281
|
+
const field = args.fields[name];
|
|
282
|
+
const fieldProperty = buildFieldProperty({
|
|
283
|
+
name,
|
|
284
|
+
field,
|
|
285
|
+
path: args.path,
|
|
286
|
+
fieldConfigs: args.fieldConfigs,
|
|
287
|
+
tabName: args.tabName
|
|
288
|
+
});
|
|
289
|
+
code = addSection(fieldProperty.code, code);
|
|
290
|
+
auxiliaryTypes.push(...fieldProperty.auxiliaryTypes);
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
code,
|
|
294
|
+
auxiliaryTypes
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
export {
|
|
298
|
+
buildFieldProperties
|
|
299
|
+
};
|
|
300
|
+
//# sourceMappingURL=buildFieldProperties.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildFieldProperties.js","sources":["../../../src/lib/buildFieldProperties.ts"],"sourcesContent":["import {\n\tCustomTypeModelField,\n\tCustomTypeModelFieldType,\n\tCustomTypeModelLinkSelectType,\n} from \"@prismicio/client\";\nimport { source, stripIndent } from \"common-tags\";\n\nimport { AuxiliaryType, FieldConfigs, FieldPath } from \"../types\";\n\nimport { addLine } from \"./addLine\";\nimport { addSection } from \"./addSection\";\nimport { buildFieldDocs } from \"./buildFieldDocs\";\nimport { buildTypeName } from \"./buildTypeName\";\nimport { buildUnion } from \"./buildUnion\";\nimport { getHumanReadablePath } from \"./getHumanReadablePath\";\n\ntype BuildFieldPropertyArgs = Pick<\n\tBuildFieldPropertiesArgs,\n\t\"path\" | \"fieldConfigs\" | \"tabName\"\n> & {\n\tname: string;\n\tfield: CustomTypeModelField;\n};\n\ntype BuildFieldPropertyReturnType = {\n\tcode: string;\n\tauxiliaryTypes: AuxiliaryType[];\n};\n\nfunction buildFieldProperty(\n\targs: BuildFieldPropertyArgs,\n): BuildFieldPropertyReturnType {\n\tlet code = buildFieldDocs({\n\t\tname: args.name,\n\t\tfield: args.field,\n\t\tpath: args.path,\n\t\ttabName: args.tabName,\n\t});\n\n\tconst auxiliaryTypes: AuxiliaryType[] = [];\n\n\tconst name =\n\t\targs.name.includes(\"-\") ||\n\t\targs.name.includes(\":\") ||\n\t\t/^[0-9]/.test(args.name)\n\t\t\t? `\"${args.name}\"`\n\t\t\t: args.name;\n\n\tswitch (args.field.type) {\n\t\tcase CustomTypeModelFieldType.UID: {\n\t\t\t// UID fields are not included in Data.\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Boolean: {\n\t\t\tcode = addLine(`${name}: prismic.BooleanField;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Color: {\n\t\t\tcode = addLine(`${name}: prismic.ColorField;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Date: {\n\t\t\tcode = addLine(`${name}: prismic.DateField;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Embed: {\n\t\t\tconst providerTypes: string[] = [];\n\n\t\t\tif (args.fieldConfigs.embed?.providerTypes) {\n\t\t\t\tfor (const providerType in args.fieldConfigs.embed?.providerTypes) {\n\t\t\t\t\tconst configuredProviderType =\n\t\t\t\t\t\targs.fieldConfigs.embed?.providerTypes[providerType];\n\n\t\t\t\t\tproviderTypes.push(\n\t\t\t\t\t\t`({ provider_name: \"${providerType}\" } & ${configuredProviderType})`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst providerTypesUnion = buildUnion(providerTypes);\n\n\t\t\tcode =\n\t\t\t\tproviderTypes.length > 0\n\t\t\t\t\t? addLine(\n\t\t\t\t\t\t\t`${name}: prismic.EmbedField<prismic.AnyOEmbed & prismic.OEmbedExtra & (${providerTypesUnion})>`,\n\t\t\t\t\t\t\tcode,\n\t\t\t\t\t )\n\t\t\t\t\t: addLine(`${name}: prismic.EmbedField`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.GeoPoint: {\n\t\t\tcode = addLine(`${name}: prismic.GeoPointField;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Image: {\n\t\t\tif (\n\t\t\t\targs.field.config?.thumbnails &&\n\t\t\t\targs.field.config.thumbnails.length > 0\n\t\t\t) {\n\t\t\t\tconst thumbnailNames = buildUnion(\n\t\t\t\t\targs.field.config.thumbnails.map((thumb) => `\"${thumb.name}\"`),\n\t\t\t\t);\n\n\t\t\t\tcode = addLine(`${name}: prismic.ImageField<${thumbnailNames}>;`, code);\n\t\t\t} else {\n\t\t\t\tcode = addLine(`${name}: prismic.ImageField<never>;`, code);\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Integration: {\n\t\t\tconst catalogType = args.field.config?.catalog\n\t\t\t\t? args.fieldConfigs.integrationFields?.catalogTypes?.[\n\t\t\t\t\t\targs.field.config.catalog\n\t\t\t\t ]\n\t\t\t\t: undefined;\n\n\t\t\tif (catalogType) {\n\t\t\t\tcode = addLine(\n\t\t\t\t\t`${name}: prismic.IntegrationField<${catalogType}>;`,\n\t\t\t\t\tcode,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tcode = addLine(`${name}: prismic.IntegrationField;`, code);\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Link: {\n\t\t\tswitch (args.field.config?.select) {\n\t\t\t\tcase CustomTypeModelLinkSelectType.Document: {\n\t\t\t\t\tif (\n\t\t\t\t\t\t\"customtypes\" in args.field.config &&\n\t\t\t\t\t\targs.field.config.customtypes &&\n\t\t\t\t\t\targs.field.config.customtypes.length > 0\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst customTypeIDsUnion = buildUnion(\n\t\t\t\t\t\t\targs.field.config.customtypes.map((type) => `\"${type}\"`),\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tcode = addLine(\n\t\t\t\t\t\t\t`${name}: prismic.ContentRelationshipField<${customTypeIDsUnion}>;`,\n\t\t\t\t\t\t\tcode,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tcode = addLine(`${name}: prismic.ContentRelationshipField;`, code);\n\t\t\t\t\t}\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcase CustomTypeModelLinkSelectType.Media: {\n\t\t\t\t\tcode = addLine(`${name}: prismic.LinkToMediaField;`, code);\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tdefault: {\n\t\t\t\t\tcode = addLine(`${name}: prismic.LinkField;`, code);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Number: {\n\t\t\tcode = addLine(`${name}: prismic.NumberField;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.StructuredText: {\n\t\t\tconst isTitleField =\n\t\t\t\targs.field.config &&\n\t\t\t\t\"single\" in args.field.config &&\n\t\t\t\targs.field.config.single &&\n\t\t\t\targs.field.config.single\n\t\t\t\t\t.split(\",\")\n\t\t\t\t\t.every((blockType) => /heading/.test(blockType));\n\n\t\t\tif (isTitleField) {\n\t\t\t\tcode = addLine(`${name}: prismic.TitleField;`, code);\n\t\t\t} else {\n\t\t\t\tcode = addLine(`${name}: prismic.RichTextField;`, code);\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Select: {\n\t\t\tconst options: string[] =\n\t\t\t\targs.field.config?.options?.map((option) => `\"${option}\"`) || [];\n\t\t\tconst optionsType = options.length ? buildUnion(options) : \"string\";\n\n\t\t\tconst hasDefault = Boolean(args.field.config?.default_value);\n\n\t\t\tif (hasDefault) {\n\t\t\t\tcode = addLine(\n\t\t\t\t\t`${name}: prismic.SelectField<${optionsType}, \"filled\">;`,\n\t\t\t\t\tcode,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tcode = addLine(\n\t\t\t\t\t`${name}: prismic.SelectField${\n\t\t\t\t\t\toptions.length > 0 ? `<${optionsType}>` : \"\"\n\t\t\t\t\t};`,\n\t\t\t\t\tcode,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Text: {\n\t\t\tcode = addLine(`${name}: prismic.KeyTextField;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Timestamp: {\n\t\t\tcode = addLine(`${name}: prismic.TimestampField;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Group: {\n\t\t\tconst itemName = buildTypeName(\n\t\t\t\targs.path[0].name,\n\t\t\t\t\"Document\",\n\t\t\t\t\"Data\",\n\t\t\t\targs.name,\n\t\t\t\t\"Item\",\n\t\t\t);\n\n\t\t\tconst path: FieldPath = [\n\t\t\t\t...args.path,\n\t\t\t\t{\n\t\t\t\t\tname: name,\n\t\t\t\t\tmodel: args.field,\n\t\t\t\t},\n\t\t\t];\n\t\t\tconst humanReadablePath = getHumanReadablePath({ path });\n\n\t\t\tconst itemFieldProperties = buildFieldProperties({\n\t\t\t\tfields: args.field.config?.fields || {},\n\t\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\t\tpath,\n\t\t\t});\n\n\t\t\tauxiliaryTypes.push({\n\t\t\t\tname: itemName,\n\t\t\t\tcode: source`\n\t\t\t\t\t/**\n\t\t\t\t\t * Item in *${humanReadablePath}*\n\t\t\t\t\t */\n\t\t\t\t\texport interface ${itemName} {\n\t\t\t\t\t\t${itemFieldProperties.code}\n\t\t\t\t\t}\n\t\t\t\t`,\n\t\t\t});\n\n\t\t\tcode = addLine(\n\t\t\t\t`${name}: prismic.GroupField<Simplify<${itemName}>>;`,\n\t\t\t\tcode,\n\t\t\t);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase CustomTypeModelFieldType.Slices: {\n\t\t\tconst choiceNames: string[] = [];\n\n\t\t\tif (args.field.config?.choices) {\n\t\t\t\tfor (const choiceID in args.field.config.choices) {\n\t\t\t\t\tconst choice = args.field.config.choices[choiceID];\n\n\t\t\t\t\tif (choice.type === \"SharedSlice\") {\n\t\t\t\t\t\t// TODO: Verify that the Shared Slice\n\t\t\t\t\t\t// is provided to the global\n\t\t\t\t\t\t// `sharedSlices` array. If it is not,\n\t\t\t\t\t\t// the type won't exist, so we can't\n\t\t\t\t\t\t// add it to the union. We should\n\t\t\t\t\t\t// probably throw an error if we reach\n\t\t\t\t\t\t// that state, or maybe the input can\n\t\t\t\t\t\t// be validated early so we don't\n\t\t\t\t\t\t// generate any code using invalid\n\t\t\t\t\t\t// models.\n\t\t\t\t\t\tchoiceNames.push(buildTypeName(choiceID, \"Slice\"));\n\t\t\t\t\t} else if (choice.type === \"Slice\") {\n\t\t\t\t\t\tconst sliceName = buildTypeName(\n\t\t\t\t\t\t\targs.path[0].name,\n\t\t\t\t\t\t\t\"Document\",\n\t\t\t\t\t\t\t\"Data\",\n\t\t\t\t\t\t\targs.name,\n\t\t\t\t\t\t\tchoiceID,\n\t\t\t\t\t\t\t\"Slice\",\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tlet primaryInterfaceName: string | undefined;\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tchoice[\"non-repeat\"] &&\n\t\t\t\t\t\t\tObject.keys(choice[\"non-repeat\"]).length > 0\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tprimaryInterfaceName = buildTypeName(sliceName, \"Primary\");\n\n\t\t\t\t\t\t\tconst path: FieldPath = [\n\t\t\t\t\t\t\t\t...args.path,\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tname: args.name,\n\t\t\t\t\t\t\t\t\tmodel: args.field,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tname: choiceID,\n\t\t\t\t\t\t\t\t\tmodel: choice,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tname: \"primary\",\n\t\t\t\t\t\t\t\t\tlabel: \"Primary\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t\tconst humanReadablePath = getHumanReadablePath({ path });\n\n\t\t\t\t\t\t\tconst primaryFieldProperties = buildFieldProperties({\n\t\t\t\t\t\t\t\tfields: choice[\"non-repeat\"],\n\t\t\t\t\t\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\t\t\t\t\t\tpath,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tlet primaryCode = stripIndent`\n\t\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t\t * Primary content in *${humanReadablePath}*\n\t\t\t\t\t\t\t\t */\n\t\t\t\t\t\t\t`;\n\t\t\t\t\t\t\tprimaryCode = primaryFieldProperties.code\n\t\t\t\t\t\t\t\t? addLine(\n\t\t\t\t\t\t\t\t\t\tsource`\n\t\t\t\t\t\t\t\t\t\t\texport interface ${primaryInterfaceName} {\n\t\t\t\t\t\t\t\t\t\t\t\t${primaryFieldProperties.code}\n\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t`,\n\t\t\t\t\t\t\t\t\t\tprimaryCode,\n\t\t\t\t\t\t\t\t )\n\t\t\t\t\t\t\t\t: addLine(\n\t\t\t\t\t\t\t\t\t\t`export interface ${primaryInterfaceName} {}`,\n\t\t\t\t\t\t\t\t\t\tprimaryCode,\n\t\t\t\t\t\t\t\t );\n\n\t\t\t\t\t\t\tauxiliaryTypes.push({\n\t\t\t\t\t\t\t\tname: primaryInterfaceName,\n\t\t\t\t\t\t\t\tcode: primaryCode,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet itemInterfaceName: string | undefined;\n\t\t\t\t\t\tif (choice.repeat && Object.keys(choice.repeat).length > 0) {\n\t\t\t\t\t\t\titemInterfaceName = buildTypeName(sliceName, \"Item\");\n\n\t\t\t\t\t\t\tconst path: FieldPath = [\n\t\t\t\t\t\t\t\t...args.path,\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tname: args.name,\n\t\t\t\t\t\t\t\t\tmodel: args.field,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tname: choiceID,\n\t\t\t\t\t\t\t\t\tmodel: choice,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tname: \"items\",\n\t\t\t\t\t\t\t\t\tlabel: \"Items\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t\tconst humanReadablePath = getHumanReadablePath({ path });\n\n\t\t\t\t\t\t\tconst itemFieldProperties = buildFieldProperties({\n\t\t\t\t\t\t\t\tfields: choice.repeat,\n\t\t\t\t\t\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\t\t\t\t\t\tpath,\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tlet itemCode = stripIndent`\n\t\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t\t * Item content in *${humanReadablePath}*\n\t\t\t\t\t\t\t\t */\n\t\t\t\t\t\t\t`;\n\t\t\t\t\t\t\titemCode = itemFieldProperties.code\n\t\t\t\t\t\t\t\t? addLine(\n\t\t\t\t\t\t\t\t\t\tsource`\n\t\t\t\t\t\t\t\t\t\t\texport interface ${itemInterfaceName} {\n\t\t\t\t\t\t\t\t\t\t\t\t${itemFieldProperties.code}\n\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t`,\n\t\t\t\t\t\t\t\t\t\titemCode,\n\t\t\t\t\t\t\t\t )\n\t\t\t\t\t\t\t\t: addLine(`export interface ${itemInterfaceName} {}`, itemCode);\n\n\t\t\t\t\t\t\tauxiliaryTypes.push({\n\t\t\t\t\t\t\t\tname: itemInterfaceName,\n\t\t\t\t\t\t\t\tcode: itemCode,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tauxiliaryTypes.push({\n\t\t\t\t\t\t\tname: sliceName,\n\t\t\t\t\t\t\tcode: stripIndent`\n\t\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t\t * Slice for *${getHumanReadablePath({\n\t\t\t\t\t\t\t\t\t\tpath: [\n\t\t\t\t\t\t\t\t\t\t\t...args.path,\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\tname: args.name,\n\t\t\t\t\t\t\t\t\t\t\t\tmodel: args.field,\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t})}*\n\t\t\t\t\t\t\t\t */\n\t\t\t\t\t\t\t\texport type ${sliceName} = prismic.Slice<\"${choiceID}\", ${\n\t\t\t\t\t\t\t\tprimaryInterfaceName\n\t\t\t\t\t\t\t\t\t? `Simplify<${primaryInterfaceName}>`\n\t\t\t\t\t\t\t\t\t: `Record<string, never>`\n\t\t\t\t\t\t\t}, ${\n\t\t\t\t\t\t\t\titemInterfaceName ? `Simplify<${itemInterfaceName}>` : `never`\n\t\t\t\t\t\t\t}>\n\t\t\t\t\t\t\t`,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tchoiceNames.push(sliceName);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst choiceUnionName = buildTypeName(\n\t\t\t\targs.path[0].name,\n\t\t\t\t\"Document\",\n\t\t\t\t\"Data\",\n\t\t\t\targs.name,\n\t\t\t\t\"Slice\",\n\t\t\t);\n\t\t\tconst choiceUnion = buildUnion(choiceNames);\n\t\t\tauxiliaryTypes.push({\n\t\t\t\tname: choiceUnionName,\n\t\t\t\tcode: `type ${choiceUnionName} = ${choiceUnion}`,\n\t\t\t});\n\n\t\t\tcode = addLine(`${name}: prismic.SliceZone<${choiceUnionName}>;`, code);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tdefault: {\n\t\t\tcode = addLine(`${name}: unknown;`, code);\n\t\t}\n\t}\n\n\treturn {\n\t\tcode,\n\t\tauxiliaryTypes,\n\t};\n}\n\ntype BuildFieldPropertiesArgs = {\n\tfields: Record<string, CustomTypeModelField>;\n\tpath: FieldPath;\n\tfieldConfigs: FieldConfigs;\n\ttabName?: string;\n};\n\ntype BuildFieldPropertiesReturnType = {\n\tcode: string;\n\tauxiliaryTypes: AuxiliaryType[];\n};\n\nexport function buildFieldProperties(\n\targs: BuildFieldPropertiesArgs,\n): BuildFieldPropertiesReturnType {\n\tlet code = \"\";\n\n\tconst auxiliaryTypes: AuxiliaryType[] = [];\n\n\tfor (const name in args.fields) {\n\t\tconst field = args.fields[name];\n\n\t\tconst fieldProperty = buildFieldProperty({\n\t\t\tname,\n\t\t\tfield,\n\t\t\tpath: args.path,\n\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\ttabName: args.tabName,\n\t\t});\n\n\t\tcode = addSection(fieldProperty.code, code);\n\n\t\tauxiliaryTypes.push(...fieldProperty.auxiliaryTypes);\n\t}\n\n\treturn {\n\t\tcode,\n\t\tauxiliaryTypes,\n\t};\n}\n"],"names":[],"mappings":";;;;;;;;AA6BA,SAAS,mBACR,MAA4B;;AAE5B,MAAI,OAAO,eAAe;AAAA,IACzB,MAAM,KAAK;AAAA,IACX,OAAO,KAAK;AAAA,IACZ,MAAM,KAAK;AAAA,IACX,SAAS,KAAK;AAAA,EAAA,CACd;AAED,QAAM,iBAAkC,CAAA;AAExC,QAAM,OACL,KAAK,KAAK,SAAS,GAAG,KACtB,KAAK,KAAK,SAAS,GAAG,KACtB,SAAS,KAAK,KAAK,IAAI,IACpB,IAAI,KAAK,UACT,KAAK;AAED,UAAA,KAAK,MAAM,MAAM;AAAA,IACxB,KAAK,yBAAyB,KAAK;AAElC;AAAA,IACA;AAAA,IAED,KAAK,yBAAyB,SAAS;AAC/B,aAAA,QAAQ,GAAG,+BAA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MAAM;AAAA,QACN,MAAM,QAAQ,qBAAqB;AAAA,MAAA,CACnC;AAED,aAAO,QAAQ,GAAG,2BAA2B,qBAAqB,IAAI;AAEtE;AAAA,IACA;AAAA,IAED,SAAS;AACD,aAAA,QAAQ,GAAG,kBAAkB,IAAI;AAAA,IACxC;AAAA,EACD;AAEM,SAAA;AAAA,IACN;AAAA,IACA;AAAA,EAAA;AAEF;AAcM,SAAU,qBACf,MAA8B;AAE9B,MAAI,OAAO;AAEX,QAAM,iBAAkC,CAAA;AAE7B,aAAA,QAAQ,KAAK,QAAQ;AACzB,UAAA,QAAQ,KAAK,OAAO,IAAI;AAE9B,UAAM,gBAAgB,mBAAmB;AAAA,MACxC;AAAA,MACA;AAAA,MACA,MAAM,KAAK;AAAA,MACX,cAAc,KAAK;AAAA,MACnB,SAAS,KAAK;AAAA,IAAA,CACd;AAEM,WAAA,WAAW,cAAc,MAAM,IAAI;AAE3B,mBAAA,KAAK,GAAG,cAAc,cAAc;AAAA,EACnD;AAEM,SAAA;AAAA,IACN;AAAA,IACA;AAAA,EAAA;AAEF;"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const commonTags = require("common-tags");
|
|
4
|
+
const constants = require("../constants.cjs");
|
|
5
|
+
const addSection = require("./addSection.cjs");
|
|
6
|
+
const buildFieldProperties = require("./buildFieldProperties.cjs");
|
|
7
|
+
const buildTypeName = require("./buildTypeName.cjs");
|
|
8
|
+
const buildUnion = require("./buildUnion.cjs");
|
|
9
|
+
const getCacheKey = require("./getCacheKey.cjs");
|
|
10
|
+
const getHumanReadableModelName = require("./getHumanReadableModelName.cjs");
|
|
11
|
+
const getHumanReadablePath = require("./getHumanReadablePath.cjs");
|
|
12
|
+
function buildSharedSliceType(args) {
|
|
13
|
+
if (args.cache) {
|
|
14
|
+
const key = getCacheKey.getCacheKey([args.model, args.fieldConfigs]);
|
|
15
|
+
const cached = args.cache.get(key);
|
|
16
|
+
if (cached) {
|
|
17
|
+
return cached;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
let code = "";
|
|
21
|
+
const name = buildTypeName.buildTypeName(args.model.id, "Slice");
|
|
22
|
+
const humanReadableName = getHumanReadableModelName.getHumanReadableModelName({
|
|
23
|
+
name: args.model.id,
|
|
24
|
+
model: args.model
|
|
25
|
+
});
|
|
26
|
+
const variationNames = [];
|
|
27
|
+
for (const variationModel of args.model.variations) {
|
|
28
|
+
const variationName = buildTypeName.buildTypeName(name, variationModel.id);
|
|
29
|
+
let primaryInterfaceName;
|
|
30
|
+
if (variationModel.primary && Object.keys(variationModel.primary).length > 0) {
|
|
31
|
+
primaryInterfaceName = buildTypeName.buildTypeName(variationName, "Primary");
|
|
32
|
+
const path = [
|
|
33
|
+
{
|
|
34
|
+
name: args.model.id,
|
|
35
|
+
model: args.model
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: "primary",
|
|
39
|
+
label: "Primary"
|
|
40
|
+
}
|
|
41
|
+
];
|
|
42
|
+
const humanReadablePath = getHumanReadablePath.getHumanReadablePath({ path });
|
|
43
|
+
const primaryFieldProperties = buildFieldProperties.buildFieldProperties({
|
|
44
|
+
fields: variationModel.primary,
|
|
45
|
+
fieldConfigs: args.fieldConfigs,
|
|
46
|
+
path
|
|
47
|
+
});
|
|
48
|
+
const docs = commonTags.stripIndent`
|
|
49
|
+
/**
|
|
50
|
+
* Primary content in *${humanReadablePath}*
|
|
51
|
+
*/
|
|
52
|
+
`;
|
|
53
|
+
code = addSection.addSection(primaryFieldProperties.code ? commonTags.source`
|
|
54
|
+
${docs}
|
|
55
|
+
export interface ${primaryInterfaceName} {
|
|
56
|
+
${primaryFieldProperties.code}
|
|
57
|
+
}
|
|
58
|
+
` : commonTags.source`
|
|
59
|
+
${docs}
|
|
60
|
+
export interface ${primaryInterfaceName} {}
|
|
61
|
+
`, code);
|
|
62
|
+
}
|
|
63
|
+
let itemInterfaceName;
|
|
64
|
+
if (variationModel.items && Object.keys(variationModel.items).length > 0) {
|
|
65
|
+
itemInterfaceName = buildTypeName.buildTypeName(variationName, "Item");
|
|
66
|
+
const path = [
|
|
67
|
+
{
|
|
68
|
+
name: args.model.id,
|
|
69
|
+
model: args.model
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: "items",
|
|
73
|
+
label: "Items"
|
|
74
|
+
}
|
|
75
|
+
];
|
|
76
|
+
const humanReadablePath = getHumanReadablePath.getHumanReadablePath({ path });
|
|
77
|
+
const itemFieldProperties = buildFieldProperties.buildFieldProperties({
|
|
78
|
+
fields: variationModel.items,
|
|
79
|
+
fieldConfigs: args.fieldConfigs,
|
|
80
|
+
path
|
|
81
|
+
});
|
|
82
|
+
const docs = commonTags.stripIndent`
|
|
83
|
+
/**
|
|
84
|
+
* Primary content in *${humanReadablePath}*
|
|
85
|
+
*/
|
|
86
|
+
`;
|
|
87
|
+
code = addSection.addSection(itemFieldProperties.code ? commonTags.source`
|
|
88
|
+
${docs}
|
|
89
|
+
export interface ${itemInterfaceName} {
|
|
90
|
+
${itemFieldProperties.code}
|
|
91
|
+
}
|
|
92
|
+
` : commonTags.source`
|
|
93
|
+
${docs}
|
|
94
|
+
export interface ${itemInterfaceName} {}
|
|
95
|
+
`, code);
|
|
96
|
+
}
|
|
97
|
+
code = addSection.addSection(commonTags.source`
|
|
98
|
+
/**
|
|
99
|
+
* ${variationModel.name} variation for ${humanReadableName} Slice
|
|
100
|
+
*
|
|
101
|
+
* - **API ID**: \`${variationModel.id}\`
|
|
102
|
+
* - **Description**: ${variationModel.description || "*None*"}
|
|
103
|
+
* - **Documentation**: ${constants.SHARED_SLICES_DOCUMENTATION_URL}
|
|
104
|
+
*/
|
|
105
|
+
export type ${variationName} = prismic.SharedSliceVariation<"${variationModel.id}", ${primaryInterfaceName ? `Simplify<${primaryInterfaceName}>` : `Record<string, never>`}, ${itemInterfaceName ? `Simplify<${itemInterfaceName}>` : `never`}>;
|
|
106
|
+
`, code);
|
|
107
|
+
variationNames.push(variationName);
|
|
108
|
+
}
|
|
109
|
+
const variationUnionName = buildTypeName.buildTypeName(name, "Variation");
|
|
110
|
+
const variationsUnion = buildUnion.buildUnion(variationNames);
|
|
111
|
+
code = addSection.addSection(commonTags.source`
|
|
112
|
+
/**
|
|
113
|
+
* Slice variation for *${humanReadableName}*
|
|
114
|
+
*/
|
|
115
|
+
type ${variationUnionName} = ${variationNames.length > 0 ? variationsUnion : "never"}
|
|
116
|
+
`, code);
|
|
117
|
+
code = addSection.addSection(commonTags.source`
|
|
118
|
+
/**
|
|
119
|
+
* ${humanReadableName} Shared Slice
|
|
120
|
+
*
|
|
121
|
+
* - **API ID**: \`${args.model.id}\`
|
|
122
|
+
* - **Description**: ${args.model.description || "*None*"}
|
|
123
|
+
* - **Documentation**: ${constants.SHARED_SLICES_DOCUMENTATION_URL}
|
|
124
|
+
*/
|
|
125
|
+
export type ${name} = prismic.SharedSlice<"${args.model.id}", ${variationUnionName}>;
|
|
126
|
+
`, code);
|
|
127
|
+
const result = {
|
|
128
|
+
name,
|
|
129
|
+
variationUnionName,
|
|
130
|
+
variationNames,
|
|
131
|
+
code
|
|
132
|
+
};
|
|
133
|
+
if (args.cache) {
|
|
134
|
+
const key = getCacheKey.getCacheKey([args.model, args.fieldConfigs]);
|
|
135
|
+
args.cache.set(key, result);
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
exports.buildSharedSliceType = buildSharedSliceType;
|
|
140
|
+
//# sourceMappingURL=buildSharedSliceType.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildSharedSliceType.cjs","sources":["../../../src/lib/buildSharedSliceType.ts"],"sourcesContent":["import { SharedSliceModel } from \"@prismicio/client\";\nimport { source, stripIndent } from \"common-tags\";\nimport QuickLRU from \"quick-lru\";\n\nimport { FieldConfigs, FieldPath } from \"../types\";\n\nimport { SHARED_SLICES_DOCUMENTATION_URL } from \"../constants\";\n\nimport { addSection } from \"./addSection\";\nimport { buildFieldProperties } from \"./buildFieldProperties\";\nimport { buildTypeName } from \"./buildTypeName\";\nimport { buildUnion } from \"./buildUnion\";\nimport { getCacheKey } from \"./getCacheKey\";\nimport { getHumanReadableModelName } from \"./getHumanReadableModelName\";\nimport { getHumanReadablePath } from \"./getHumanReadablePath\";\n\ntype BuildSharedSliceTypeArgs = {\n\tmodel: SharedSliceModel;\n\tfieldConfigs: FieldConfigs;\n\tcache?: QuickLRU<string, unknown>;\n};\n\ntype BuildSharedSliceTypeReturnValue = {\n\tname: string;\n\tvariationUnionName: string;\n\tvariationNames: string[];\n\tcode: string;\n};\n\nexport function buildSharedSliceType(\n\targs: BuildSharedSliceTypeArgs,\n): BuildSharedSliceTypeReturnValue {\n\tif (args.cache) {\n\t\tconst key = getCacheKey([args.model, args.fieldConfigs]);\n\t\tconst cached = args.cache.get(key);\n\n\t\tif (cached) {\n\t\t\treturn cached as BuildSharedSliceTypeReturnValue;\n\t\t}\n\t}\n\n\tlet code = \"\";\n\n\tconst name = buildTypeName(args.model.id, \"Slice\");\n\tconst humanReadableName = getHumanReadableModelName({\n\t\tname: args.model.id,\n\t\tmodel: args.model,\n\t});\n\n\tconst variationNames: string[] = [];\n\tfor (const variationModel of args.model.variations) {\n\t\tconst variationName = buildTypeName(name, variationModel.id);\n\n\t\tlet primaryInterfaceName: string | undefined;\n\t\tif (\n\t\t\tvariationModel.primary &&\n\t\t\tObject.keys(variationModel.primary).length > 0\n\t\t) {\n\t\t\tprimaryInterfaceName = buildTypeName(variationName, \"Primary\");\n\n\t\t\tconst path: FieldPath = [\n\t\t\t\t{\n\t\t\t\t\tname: args.model.id,\n\t\t\t\t\tmodel: args.model,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: \"primary\",\n\t\t\t\t\tlabel: \"Primary\",\n\t\t\t\t},\n\t\t\t];\n\t\t\tconst humanReadablePath = getHumanReadablePath({ path });\n\n\t\t\tconst primaryFieldProperties = buildFieldProperties({\n\t\t\t\tfields: variationModel.primary,\n\t\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\t\tpath,\n\t\t\t});\n\n\t\t\tconst docs = stripIndent`\n\t\t\t\t/**\n\t\t\t\t * Primary content in *${humanReadablePath}*\n\t\t\t\t */\n\t\t\t`;\n\n\t\t\tcode = addSection(\n\t\t\t\tprimaryFieldProperties.code\n\t\t\t\t\t? source`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${primaryInterfaceName} {\n\t\t\t\t\t\t\t${primaryFieldProperties.code}\n\t\t\t\t\t\t}\n\t\t\t\t\t`\n\t\t\t\t\t: source`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${primaryInterfaceName} {}\n\t\t\t\t\t`,\n\t\t\t\tcode,\n\t\t\t);\n\t\t}\n\n\t\tlet itemInterfaceName: string | undefined;\n\t\tif (variationModel.items && Object.keys(variationModel.items).length > 0) {\n\t\t\titemInterfaceName = buildTypeName(variationName, \"Item\");\n\n\t\t\tconst path: FieldPath = [\n\t\t\t\t{\n\t\t\t\t\tname: args.model.id,\n\t\t\t\t\tmodel: args.model,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: \"items\",\n\t\t\t\t\tlabel: \"Items\",\n\t\t\t\t},\n\t\t\t];\n\t\t\tconst humanReadablePath = getHumanReadablePath({ path });\n\n\t\t\tconst itemFieldProperties = buildFieldProperties({\n\t\t\t\tfields: variationModel.items,\n\t\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\t\tpath,\n\t\t\t});\n\n\t\t\tconst docs = stripIndent`\n\t\t\t\t/**\n\t\t\t\t * Primary content in *${humanReadablePath}*\n\t\t\t\t */\n\t\t\t`;\n\n\t\t\tcode = addSection(\n\t\t\t\titemFieldProperties.code\n\t\t\t\t\t? source`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${itemInterfaceName} {\n\t\t\t\t\t\t\t${itemFieldProperties.code}\n\t\t\t\t\t\t}\n\t\t\t\t\t`\n\t\t\t\t\t: source`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${itemInterfaceName} {}\n\t\t\t\t\t`,\n\t\t\t\tcode,\n\t\t\t);\n\t\t}\n\n\t\tcode = addSection(\n\t\t\tsource`\n\t\t\t\t/**\n\t\t\t\t * ${variationModel.name} variation for ${humanReadableName} Slice\n\t\t\t\t *\n\t\t\t\t * - **API ID**: \\`${variationModel.id}\\`\n\t\t\t\t * - **Description**: ${variationModel.description || \"*None*\"}\n\t\t\t\t * - **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}\n\t\t\t\t */\n\t\t\t\texport type ${variationName} = prismic.SharedSliceVariation<\"${\n\t\t\t\tvariationModel.id\n\t\t\t}\", ${\n\t\t\t\tprimaryInterfaceName\n\t\t\t\t\t? `Simplify<${primaryInterfaceName}>`\n\t\t\t\t\t: `Record<string, never>`\n\t\t\t}, ${itemInterfaceName ? `Simplify<${itemInterfaceName}>` : `never`}>;\n\t\t\t`,\n\t\t\tcode,\n\t\t);\n\n\t\tvariationNames.push(variationName);\n\t}\n\n\tconst variationUnionName = buildTypeName(name, \"Variation\");\n\tconst variationsUnion = buildUnion(variationNames);\n\n\tcode = addSection(\n\t\tsource`\n\t\t\t/**\n\t\t\t * Slice variation for *${humanReadableName}*\n\t\t\t */\n\t\t\ttype ${variationUnionName} = ${\n\t\t\tvariationNames.length > 0 ? variationsUnion : \"never\"\n\t\t}\n\t\t`,\n\t\tcode,\n\t);\n\n\tcode = addSection(\n\t\tsource`\n\t\t\t/**\n\t\t\t * ${humanReadableName} Shared Slice\n\t\t\t *\n\t\t\t * - **API ID**: \\`${args.model.id}\\`\n\t\t\t * - **Description**: ${args.model.description || \"*None*\"}\n\t\t\t * - **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}\n\t\t\t */\n\t\t\texport type ${name} = prismic.SharedSlice<\"${\n\t\t\targs.model.id\n\t\t}\", ${variationUnionName}>;\n\t\t`,\n\t\tcode,\n\t);\n\n\tconst result = {\n\t\tname,\n\t\tvariationUnionName,\n\t\tvariationNames,\n\t\tcode,\n\t};\n\n\tif (args.cache) {\n\t\tconst key = getCacheKey([args.model, args.fieldConfigs]);\n\n\t\targs.cache.set(key, result);\n\t}\n\n\treturn result;\n}\n"],"names":["getCacheKey","buildTypeName","getHumanReadableModelName","getHumanReadablePath","buildFieldProperties","stripIndent","addSection","source","SHARED_SLICES_DOCUMENTATION_URL","buildUnion"],"mappings":";;;;;;;;;;;AA6BM,SAAU,qBACf,MAA8B;AAE9B,MAAI,KAAK,OAAO;AACf,UAAM,MAAMA,YAAAA,YAAY,CAAC,KAAK,OAAO,KAAK,YAAY,CAAC;AACvD,UAAM,SAAS,KAAK,MAAM,IAAI,GAAG;AAEjC,QAAI,QAAQ;AACJ,aAAA;AAAA,IACP;AAAA,EACD;AAED,MAAI,OAAO;AAEX,QAAM,OAAOC,cAAAA,cAAc,KAAK,MAAM,IAAI,OAAO;AACjD,QAAM,oBAAoBC,0BAAAA,0BAA0B;AAAA,IACnD,MAAM,KAAK,MAAM;AAAA,IACjB,OAAO,KAAK;AAAA,EAAA,CACZ;AAED,QAAM,iBAA2B,CAAA;AACtB,aAAA,kBAAkB,KAAK,MAAM,YAAY;AACnD,UAAM,gBAAgBD,cAAA,cAAc,MAAM,eAAe,EAAE;AAEvD,QAAA;AAEH,QAAA,eAAe,WACf,OAAO,KAAK,eAAe,OAAO,EAAE,SAAS,GAC5C;AACsB,6BAAAA,cAAA,cAAc,eAAe,SAAS;AAE7D,YAAM,OAAkB;AAAA,QACvB;AAAA,UACC,MAAM,KAAK,MAAM;AAAA,UACjB,OAAO,KAAK;AAAA,QACZ;AAAA,QACD;AAAA,UACC,MAAM;AAAA,UACN,OAAO;AAAA,QACP;AAAA,MAAA;AAEF,YAAM,oBAAoBE,qBAAAA,qBAAqB,EAAE,KAAM,CAAA;AAEvD,YAAM,yBAAyBC,qBAAAA,qBAAqB;AAAA,QACnD,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C;AAEK,QAAA,qBAAqBP,cAAAA,cAAc,MAAM,WAAW;AACpD,QAAA,kBAAkBQ,sBAAW,cAAc;AAEjD,SAAOH,WACN,WAAAC;;6BAE2B;AAAA;AAAA,UAEnB,wBACP,eAAe,SAAS,IAAI,kBAAkB;AAAA,KAG/C,IAAI;AAGL,SAAOD,WACN,WAAAC;;QAEM;AAAA;AAAA,wBAEgB,KAAK,MAAM;AAAA,2BACR,KAAK,MAAM,eAAe;AAAA,6BACxBC;;iBAEZ,+BACd,KAAK,MAAM,QACN;AAAA,KAEN,IAAI;AAGL,QAAM,SAAS;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAGD,MAAI,KAAK,OAAO;AACf,UAAM,MAAMR,YAAAA,YAAY,CAAC,KAAK,OAAO,KAAK,YAAY,CAAC;AAElD,SAAA,MAAM,IAAI,KAAK,MAAM;AAAA,EAC1B;AAEM,SAAA;AACR;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SharedSliceModel } from "@prismicio/client";
|
|
2
|
+
import QuickLRU from "quick-lru";
|
|
3
|
+
import { FieldConfigs } from "../types";
|
|
4
|
+
type BuildSharedSliceTypeArgs = {
|
|
5
|
+
model: SharedSliceModel;
|
|
6
|
+
fieldConfigs: FieldConfigs;
|
|
7
|
+
cache?: QuickLRU<string, unknown>;
|
|
8
|
+
};
|
|
9
|
+
type BuildSharedSliceTypeReturnValue = {
|
|
10
|
+
name: string;
|
|
11
|
+
variationUnionName: string;
|
|
12
|
+
variationNames: string[];
|
|
13
|
+
code: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function buildSharedSliceType(args: BuildSharedSliceTypeArgs): BuildSharedSliceTypeReturnValue;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { stripIndent, source } from "common-tags";
|
|
2
|
+
import { SHARED_SLICES_DOCUMENTATION_URL } from "../constants.js";
|
|
3
|
+
import { addSection } from "./addSection.js";
|
|
4
|
+
import { buildFieldProperties } from "./buildFieldProperties.js";
|
|
5
|
+
import { buildTypeName } from "./buildTypeName.js";
|
|
6
|
+
import { buildUnion } from "./buildUnion.js";
|
|
7
|
+
import { getCacheKey } from "./getCacheKey.js";
|
|
8
|
+
import { getHumanReadableModelName } from "./getHumanReadableModelName.js";
|
|
9
|
+
import { getHumanReadablePath } from "./getHumanReadablePath.js";
|
|
10
|
+
function buildSharedSliceType(args) {
|
|
11
|
+
if (args.cache) {
|
|
12
|
+
const key = getCacheKey([args.model, args.fieldConfigs]);
|
|
13
|
+
const cached = args.cache.get(key);
|
|
14
|
+
if (cached) {
|
|
15
|
+
return cached;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
let code = "";
|
|
19
|
+
const name = buildTypeName(args.model.id, "Slice");
|
|
20
|
+
const humanReadableName = getHumanReadableModelName({
|
|
21
|
+
name: args.model.id,
|
|
22
|
+
model: args.model
|
|
23
|
+
});
|
|
24
|
+
const variationNames = [];
|
|
25
|
+
for (const variationModel of args.model.variations) {
|
|
26
|
+
const variationName = buildTypeName(name, variationModel.id);
|
|
27
|
+
let primaryInterfaceName;
|
|
28
|
+
if (variationModel.primary && Object.keys(variationModel.primary).length > 0) {
|
|
29
|
+
primaryInterfaceName = buildTypeName(variationName, "Primary");
|
|
30
|
+
const path = [
|
|
31
|
+
{
|
|
32
|
+
name: args.model.id,
|
|
33
|
+
model: args.model
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "primary",
|
|
37
|
+
label: "Primary"
|
|
38
|
+
}
|
|
39
|
+
];
|
|
40
|
+
const humanReadablePath = getHumanReadablePath({ path });
|
|
41
|
+
const primaryFieldProperties = buildFieldProperties({
|
|
42
|
+
fields: variationModel.primary,
|
|
43
|
+
fieldConfigs: args.fieldConfigs,
|
|
44
|
+
path
|
|
45
|
+
});
|
|
46
|
+
const docs = stripIndent`
|
|
47
|
+
/**
|
|
48
|
+
* Primary content in *${humanReadablePath}*
|
|
49
|
+
*/
|
|
50
|
+
`;
|
|
51
|
+
code = addSection(primaryFieldProperties.code ? source`
|
|
52
|
+
${docs}
|
|
53
|
+
export interface ${primaryInterfaceName} {
|
|
54
|
+
${primaryFieldProperties.code}
|
|
55
|
+
}
|
|
56
|
+
` : source`
|
|
57
|
+
${docs}
|
|
58
|
+
export interface ${primaryInterfaceName} {}
|
|
59
|
+
`, code);
|
|
60
|
+
}
|
|
61
|
+
let itemInterfaceName;
|
|
62
|
+
if (variationModel.items && Object.keys(variationModel.items).length > 0) {
|
|
63
|
+
itemInterfaceName = buildTypeName(variationName, "Item");
|
|
64
|
+
const path = [
|
|
65
|
+
{
|
|
66
|
+
name: args.model.id,
|
|
67
|
+
model: args.model
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: "items",
|
|
71
|
+
label: "Items"
|
|
72
|
+
}
|
|
73
|
+
];
|
|
74
|
+
const humanReadablePath = getHumanReadablePath({ path });
|
|
75
|
+
const itemFieldProperties = buildFieldProperties({
|
|
76
|
+
fields: variationModel.items,
|
|
77
|
+
fieldConfigs: args.fieldConfigs,
|
|
78
|
+
path
|
|
79
|
+
});
|
|
80
|
+
const docs = stripIndent`
|
|
81
|
+
/**
|
|
82
|
+
* Primary content in *${humanReadablePath}*
|
|
83
|
+
*/
|
|
84
|
+
`;
|
|
85
|
+
code = addSection(itemFieldProperties.code ? source`
|
|
86
|
+
${docs}
|
|
87
|
+
export interface ${itemInterfaceName} {
|
|
88
|
+
${itemFieldProperties.code}
|
|
89
|
+
}
|
|
90
|
+
` : source`
|
|
91
|
+
${docs}
|
|
92
|
+
export interface ${itemInterfaceName} {}
|
|
93
|
+
`, code);
|
|
94
|
+
}
|
|
95
|
+
code = addSection(source`
|
|
96
|
+
/**
|
|
97
|
+
* ${variationModel.name} variation for ${humanReadableName} Slice
|
|
98
|
+
*
|
|
99
|
+
* - **API ID**: \`${variationModel.id}\`
|
|
100
|
+
* - **Description**: ${variationModel.description || "*None*"}
|
|
101
|
+
* - **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}
|
|
102
|
+
*/
|
|
103
|
+
export type ${variationName} = prismic.SharedSliceVariation<"${variationModel.id}", ${primaryInterfaceName ? `Simplify<${primaryInterfaceName}>` : `Record<string, never>`}, ${itemInterfaceName ? `Simplify<${itemInterfaceName}>` : `never`}>;
|
|
104
|
+
`, code);
|
|
105
|
+
variationNames.push(variationName);
|
|
106
|
+
}
|
|
107
|
+
const variationUnionName = buildTypeName(name, "Variation");
|
|
108
|
+
const variationsUnion = buildUnion(variationNames);
|
|
109
|
+
code = addSection(source`
|
|
110
|
+
/**
|
|
111
|
+
* Slice variation for *${humanReadableName}*
|
|
112
|
+
*/
|
|
113
|
+
type ${variationUnionName} = ${variationNames.length > 0 ? variationsUnion : "never"}
|
|
114
|
+
`, code);
|
|
115
|
+
code = addSection(source`
|
|
116
|
+
/**
|
|
117
|
+
* ${humanReadableName} Shared Slice
|
|
118
|
+
*
|
|
119
|
+
* - **API ID**: \`${args.model.id}\`
|
|
120
|
+
* - **Description**: ${args.model.description || "*None*"}
|
|
121
|
+
* - **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}
|
|
122
|
+
*/
|
|
123
|
+
export type ${name} = prismic.SharedSlice<"${args.model.id}", ${variationUnionName}>;
|
|
124
|
+
`, code);
|
|
125
|
+
const result = {
|
|
126
|
+
name,
|
|
127
|
+
variationUnionName,
|
|
128
|
+
variationNames,
|
|
129
|
+
code
|
|
130
|
+
};
|
|
131
|
+
if (args.cache) {
|
|
132
|
+
const key = getCacheKey([args.model, args.fieldConfigs]);
|
|
133
|
+
args.cache.set(key, result);
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
export {
|
|
138
|
+
buildSharedSliceType
|
|
139
|
+
};
|
|
140
|
+
//# sourceMappingURL=buildSharedSliceType.js.map
|