prismic-ts-codegen 0.0.7 → 0.1.0
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/bin/cli.mjs +162 -148
- package/dist/index.cjs +161 -147
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +162 -148
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/constants.ts +2 -1
- package/src/lib/addInterfacePropertiesForFields.ts +168 -150
- package/src/lib/addTypeAliasForSharedSlice.ts +2 -2
- package/src/lib/buildFieldDocs.ts +2 -1
- package/src/lib/getFieldDocumentationURL.ts +4 -2
- package/src/lib/getHumanReadableFieldType.ts +8 -2
- package/src/lib/getHumanReadableModelName.ts +12 -10
- package/src/lib/hasOwnProperty.ts +15 -0
- package/src/lib/isCustomTypeModelField.ts +1 -6
- package/src/lib/isCustomTypeModelSlice.ts +8 -3
- package/src/lib/isSharedSliceModel.ts +9 -2
package/dist/index.cjs
CHANGED
|
@@ -30,19 +30,24 @@ const FIELD_DOCUMENTATION_URLS = {
|
|
|
30
30
|
GeoPoint: "https://prismic.io/docs/core-concepts/geopoint",
|
|
31
31
|
Group: "https://prismic.io/docs/core-concepts/group",
|
|
32
32
|
IntegrationFields: "https://prismic.io/docs/core-concepts/integration-fields",
|
|
33
|
-
Slices: "https://prismic.io/docs/core-concepts/slices"
|
|
33
|
+
Slices: "https://prismic.io/docs/core-concepts/slices",
|
|
34
|
+
LegacySlices: "https://prismic.io/docs/core-concepts/slices"
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
const isCustomTypeModelField = (model) => {
|
|
37
|
-
return typeof model === "object" && model !== null && "type" in model
|
|
38
|
+
return typeof model === "object" && model !== null && "type" in model;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const hasOwnProperty = (obj, prop) => {
|
|
42
|
+
return obj.hasOwnProperty(prop);
|
|
38
43
|
};
|
|
39
44
|
|
|
40
45
|
const isCustomTypeModelSlice = (model) => {
|
|
41
|
-
return typeof model === "object" && model !== null && "
|
|
46
|
+
return typeof model === "object" && model !== null && hasOwnProperty(model, "type") && model.type === types.CustomTypeModelSliceType.Slice;
|
|
42
47
|
};
|
|
43
48
|
|
|
44
49
|
const isSharedSliceModel = (model) => {
|
|
45
|
-
return typeof model === "object" && model !== null && "
|
|
50
|
+
return typeof model === "object" && model !== null && hasOwnProperty(model, "type") && model.type === types.CustomTypeModelSliceType.SharedSlice;
|
|
46
51
|
};
|
|
47
52
|
|
|
48
53
|
const getAPIIDPath = (config) => {
|
|
@@ -71,16 +76,19 @@ const isCustomTypeModel = (model) => {
|
|
|
71
76
|
|
|
72
77
|
const getHumanReadableModelName = (config) => {
|
|
73
78
|
if (isCustomTypeModel(config.model)) {
|
|
74
|
-
return config.model.label;
|
|
79
|
+
return config.model.label || config.model.id;
|
|
75
80
|
} else if (isSharedSliceModel(config.model)) {
|
|
76
81
|
return config.model.name;
|
|
77
|
-
} else if (isCustomTypeModelField(config.model)
|
|
78
|
-
|
|
82
|
+
} else if (isCustomTypeModelField(config.model)) {
|
|
83
|
+
if (config.model.config && "label" in config.model.config) {
|
|
84
|
+
return config.model.config.label || config.id;
|
|
85
|
+
} else if (config.model.config && "fieldset" in config.model) {
|
|
86
|
+
return config.model.fieldset || config.id;
|
|
87
|
+
}
|
|
79
88
|
} else if (isCustomTypeModelSlice(config.model)) {
|
|
80
|
-
return config.model.fieldset;
|
|
81
|
-
} else {
|
|
82
|
-
return `Slice Zone (\`${config.id}\`)`;
|
|
89
|
+
return config.model.fieldset || config.id;
|
|
83
90
|
}
|
|
91
|
+
return `\`${config.id}\``;
|
|
84
92
|
};
|
|
85
93
|
|
|
86
94
|
const getHumanReadableFieldPath = (config) => {
|
|
@@ -101,14 +109,15 @@ const getHumanReadableFieldPath = (config) => {
|
|
|
101
109
|
const getHumanReadableFieldType = (config) => {
|
|
102
110
|
switch (config.model.type) {
|
|
103
111
|
case "StructuredText": {
|
|
104
|
-
const isTitleField = "single" in config.model.config && config.model.config.single.split(",").every((blockType) => /heading/.test(blockType));
|
|
112
|
+
const isTitleField = config.model.config && "single" in config.model.config && config.model.config.single && config.model.config.single.split(",").every((blockType) => /heading/.test(blockType));
|
|
105
113
|
return isTitleField ? "Title" : "Rich Text";
|
|
106
114
|
}
|
|
107
115
|
case "IntegrationFields": {
|
|
108
|
-
|
|
116
|
+
const catalog = config.model.config?.catalog;
|
|
117
|
+
return `Integration Fields (Catalog: ${catalog ? `\`${catalog}\`` : "*unknown*"})`;
|
|
109
118
|
}
|
|
110
119
|
case "Link": {
|
|
111
|
-
switch (config.model.config
|
|
120
|
+
switch (config.model.config?.select) {
|
|
112
121
|
case types.CustomTypeModelLinkSelectType.Document: {
|
|
113
122
|
return "Content Relationship";
|
|
114
123
|
}
|
|
@@ -169,8 +178,8 @@ const buildFieldDocs = (config) => {
|
|
|
169
178
|
const fieldType = getHumanReadableFieldType({
|
|
170
179
|
model: config.model
|
|
171
180
|
});
|
|
172
|
-
const placeholder = "placeholder" in config.model.config && config.model.config.placeholder ? config.model.config.placeholder : `*None*`;
|
|
173
|
-
const defaultValue = "default_value" in config.model.config ? config.model.config.default_value : void 0;
|
|
181
|
+
const placeholder = config.model.config && "placeholder" in config.model.config && config.model.config.placeholder ? config.model.config.placeholder : `*None*`;
|
|
182
|
+
const defaultValue = config.model.config && "default_value" in config.model.config ? config.model.config.default_value : void 0;
|
|
174
183
|
const apiIDPath = getAPIIDPath({
|
|
175
184
|
path: [...config.path, { id: config.id, model: config.model }]
|
|
176
185
|
});
|
|
@@ -275,7 +284,7 @@ const addInterfacePropertyForField = (config) => {
|
|
|
275
284
|
break;
|
|
276
285
|
}
|
|
277
286
|
case "Image": {
|
|
278
|
-
if (config.model.config.thumbnails.length > 0) {
|
|
287
|
+
if (config.model.config?.thumbnails && config.model.config.thumbnails.length > 0) {
|
|
279
288
|
const thumbnailNames = config.model.config.thumbnails.map((thumbnail) => `"${thumbnail.name}"`).join(" | ");
|
|
280
289
|
config.interface.addProperty({
|
|
281
290
|
name: config.id,
|
|
@@ -302,7 +311,7 @@ const addInterfacePropertyForField = (config) => {
|
|
|
302
311
|
break;
|
|
303
312
|
}
|
|
304
313
|
case "IntegrationFields": {
|
|
305
|
-
const catalogType = config.fieldConfigs.integrationFields?.catalogTypes?.[config.model.config.catalog];
|
|
314
|
+
const catalogType = config.model.config?.catalog ? config.fieldConfigs.integrationFields?.catalogTypes?.[config.model.config.catalog] : void 0;
|
|
306
315
|
config.interface.addProperty({
|
|
307
316
|
name: config.id,
|
|
308
317
|
type: catalogType ? `prismicT.IntegrationFields<${catalogType}>` : "prismicT.IntegrationFields",
|
|
@@ -316,11 +325,12 @@ const addInterfacePropertyForField = (config) => {
|
|
|
316
325
|
break;
|
|
317
326
|
}
|
|
318
327
|
case "Link": {
|
|
319
|
-
switch (config.model.config
|
|
328
|
+
switch (config.model.config?.select) {
|
|
320
329
|
case types.CustomTypeModelLinkSelectType.Document: {
|
|
330
|
+
config.model.config;
|
|
321
331
|
config.interface.addProperty({
|
|
322
332
|
name: config.id,
|
|
323
|
-
type: config.model.config.customtypes && config.model.config.customtypes.length > 0 ? `prismicT.RelationField<${config.model.config.customtypes.map((type) => `"${type}"`).join(" | ")}>` : "prismicT.RelationField",
|
|
333
|
+
type: "customtypes" in config.model.config && config.model.config.customtypes && config.model.config.customtypes.length > 0 ? `prismicT.RelationField<${config.model.config.customtypes.map((type) => `"${type}"`).join(" | ")}>` : "prismicT.RelationField",
|
|
324
334
|
docs: buildFieldDocs({
|
|
325
335
|
id: config.id,
|
|
326
336
|
model: config.model,
|
|
@@ -372,7 +382,7 @@ const addInterfacePropertyForField = (config) => {
|
|
|
372
382
|
break;
|
|
373
383
|
}
|
|
374
384
|
case "StructuredText": {
|
|
375
|
-
const isTitleField = "single" in config.model.config && config.model.config.single.split(",").every((blockType) => /heading/.test(blockType));
|
|
385
|
+
const isTitleField = config.model.config && "single" in config.model.config && config.model.config.single && config.model.config.single.split(",").every((blockType) => /heading/.test(blockType));
|
|
376
386
|
if (isTitleField) {
|
|
377
387
|
config.interface.addProperty({
|
|
378
388
|
name: config.id,
|
|
@@ -399,12 +409,12 @@ const addInterfacePropertyForField = (config) => {
|
|
|
399
409
|
break;
|
|
400
410
|
}
|
|
401
411
|
case "Select": {
|
|
402
|
-
const options = config.model.config
|
|
403
|
-
const hasDefault = Boolean(config.model.config
|
|
412
|
+
const options = config.model.config?.options?.map((option) => `"${option}"`).join(" | ");
|
|
413
|
+
const hasDefault = Boolean(config.model.config?.default_value);
|
|
404
414
|
if (hasDefault) {
|
|
405
415
|
config.interface.addProperty({
|
|
406
416
|
name: config.id,
|
|
407
|
-
type: `prismicT.SelectField<${options}, "filled">`,
|
|
417
|
+
type: `prismicT.SelectField<${options || "string"}, "filled">`,
|
|
408
418
|
docs: buildFieldDocs({
|
|
409
419
|
id: config.id,
|
|
410
420
|
model: config.model,
|
|
@@ -473,19 +483,21 @@ const addInterfacePropertyForField = (config) => {
|
|
|
473
483
|
],
|
|
474
484
|
isExported: true
|
|
475
485
|
});
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
486
|
+
if (config.model.config?.fields) {
|
|
487
|
+
addInterfacePropertiesForFields({
|
|
488
|
+
interface: itemInterface,
|
|
489
|
+
sourceFile: config.sourceFile,
|
|
490
|
+
fields: config.model.config.fields,
|
|
491
|
+
path: [
|
|
492
|
+
...config.path,
|
|
493
|
+
{
|
|
494
|
+
id: config.id,
|
|
495
|
+
model: config.model
|
|
496
|
+
}
|
|
497
|
+
],
|
|
498
|
+
fieldConfigs: config.fieldConfigs
|
|
499
|
+
});
|
|
500
|
+
}
|
|
489
501
|
config.interface.addProperty({
|
|
490
502
|
name: config.id,
|
|
491
503
|
type: `prismicT.GroupField<Simplify<${itemInterface.getName()}>>`,
|
|
@@ -500,120 +512,122 @@ const addInterfacePropertyForField = (config) => {
|
|
|
500
512
|
}
|
|
501
513
|
case "Slices": {
|
|
502
514
|
const choiceInterfaceNames = [];
|
|
503
|
-
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
515
|
+
if (config.model.config?.choices) {
|
|
516
|
+
for (const choiceId in config.model.config.choices) {
|
|
517
|
+
const choice = config.model.config.choices[choiceId];
|
|
518
|
+
if (choice.type === types.CustomTypeModelSliceType.SharedSlice) {
|
|
519
|
+
choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
|
|
520
|
+
} else if (choice.type === types.CustomTypeModelSliceType.Slice) {
|
|
521
|
+
let primaryInterface;
|
|
522
|
+
if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
|
|
523
|
+
primaryInterface = config.sourceFile.addInterface({
|
|
524
|
+
name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`),
|
|
525
|
+
docs: [
|
|
526
|
+
{
|
|
527
|
+
description: (writer) => {
|
|
528
|
+
const humanReadablePath = getHumanReadableFieldPath({
|
|
529
|
+
path: [
|
|
530
|
+
...config.path,
|
|
531
|
+
{
|
|
532
|
+
id: config.id,
|
|
533
|
+
model: config.model
|
|
534
|
+
},
|
|
535
|
+
{
|
|
536
|
+
id: choiceId,
|
|
537
|
+
model: choice
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
id: "primary",
|
|
541
|
+
label: "Primary"
|
|
542
|
+
}
|
|
543
|
+
]
|
|
544
|
+
});
|
|
545
|
+
writer.writeLine(`Primary content in ${humanReadablePath}`);
|
|
546
|
+
}
|
|
533
547
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
label: "Primary"
|
|
554
|
-
}
|
|
555
|
-
],
|
|
556
|
-
fieldConfigs: config.fieldConfigs
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
let itemInterface;
|
|
560
|
-
if (Object.keys(choice.repeat).length > 0) {
|
|
561
|
-
itemInterface = config.sourceFile.addInterface({
|
|
562
|
-
name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`),
|
|
563
|
-
docs: [
|
|
564
|
-
{
|
|
565
|
-
description: (writer) => {
|
|
566
|
-
const humanReadablePath = getHumanReadableFieldPath({
|
|
567
|
-
path: [
|
|
568
|
-
...config.path,
|
|
569
|
-
{
|
|
570
|
-
id: config.id,
|
|
571
|
-
model: config.model
|
|
572
|
-
},
|
|
573
|
-
{
|
|
574
|
-
id: choiceId,
|
|
575
|
-
model: choice
|
|
576
|
-
},
|
|
577
|
-
{
|
|
578
|
-
id: "items",
|
|
579
|
-
label: "Items"
|
|
580
|
-
}
|
|
581
|
-
]
|
|
582
|
-
});
|
|
583
|
-
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
548
|
+
]
|
|
549
|
+
});
|
|
550
|
+
addInterfacePropertiesForFields({
|
|
551
|
+
interface: primaryInterface,
|
|
552
|
+
sourceFile: config.sourceFile,
|
|
553
|
+
fields: choice["non-repeat"],
|
|
554
|
+
path: [
|
|
555
|
+
...config.path,
|
|
556
|
+
{
|
|
557
|
+
id: config.id,
|
|
558
|
+
model: config.model
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
id: choiceId,
|
|
562
|
+
model: choice
|
|
563
|
+
},
|
|
564
|
+
{
|
|
565
|
+
id: "primary",
|
|
566
|
+
label: "Primary"
|
|
584
567
|
}
|
|
585
|
-
|
|
586
|
-
|
|
568
|
+
],
|
|
569
|
+
fieldConfigs: config.fieldConfigs
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
let itemInterface;
|
|
573
|
+
if (choice.repeat && Object.keys(choice.repeat).length > 0) {
|
|
574
|
+
itemInterface = config.sourceFile.addInterface({
|
|
575
|
+
name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`),
|
|
576
|
+
docs: [
|
|
577
|
+
{
|
|
578
|
+
description: (writer) => {
|
|
579
|
+
const humanReadablePath = getHumanReadableFieldPath({
|
|
580
|
+
path: [
|
|
581
|
+
...config.path,
|
|
582
|
+
{
|
|
583
|
+
id: config.id,
|
|
584
|
+
model: config.model
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
id: choiceId,
|
|
588
|
+
model: choice
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
id: "items",
|
|
592
|
+
label: "Items"
|
|
593
|
+
}
|
|
594
|
+
]
|
|
595
|
+
});
|
|
596
|
+
writer.writeLine(`Item in ${humanReadablePath}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
],
|
|
600
|
+
isExported: true
|
|
601
|
+
});
|
|
602
|
+
addInterfacePropertiesForFields({
|
|
603
|
+
interface: itemInterface,
|
|
604
|
+
sourceFile: config.sourceFile,
|
|
605
|
+
fields: choice.repeat,
|
|
606
|
+
path: [
|
|
607
|
+
...config.path,
|
|
608
|
+
{
|
|
609
|
+
id: config.id,
|
|
610
|
+
model: config.model
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
id: choiceId,
|
|
614
|
+
model: choice
|
|
615
|
+
},
|
|
616
|
+
{
|
|
617
|
+
id: "items",
|
|
618
|
+
label: "Items"
|
|
619
|
+
}
|
|
620
|
+
],
|
|
621
|
+
fieldConfigs: config.fieldConfigs
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
const sliceType = config.sourceFile.addTypeAlias({
|
|
625
|
+
name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
|
|
626
|
+
type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
|
|
587
627
|
isExported: true
|
|
588
628
|
});
|
|
589
|
-
|
|
590
|
-
interface: itemInterface,
|
|
591
|
-
sourceFile: config.sourceFile,
|
|
592
|
-
fields: choice.repeat,
|
|
593
|
-
path: [
|
|
594
|
-
...config.path,
|
|
595
|
-
{
|
|
596
|
-
id: config.id,
|
|
597
|
-
model: config.model
|
|
598
|
-
},
|
|
599
|
-
{
|
|
600
|
-
id: choiceId,
|
|
601
|
-
model: choice
|
|
602
|
-
},
|
|
603
|
-
{
|
|
604
|
-
id: "items",
|
|
605
|
-
label: "Items"
|
|
606
|
-
}
|
|
607
|
-
],
|
|
608
|
-
fieldConfigs: config.fieldConfigs
|
|
609
|
-
});
|
|
629
|
+
choiceInterfaceNames.push(sliceType.getName());
|
|
610
630
|
}
|
|
611
|
-
const sliceType = config.sourceFile.addTypeAlias({
|
|
612
|
-
name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
|
|
613
|
-
type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
|
|
614
|
-
isExported: true
|
|
615
|
-
});
|
|
616
|
-
choiceInterfaceNames.push(sliceType.getName());
|
|
617
631
|
}
|
|
618
632
|
}
|
|
619
633
|
const slicesType = config.sourceFile.addTypeAlias({
|
|
@@ -757,7 +771,7 @@ const addTypeAliasForSharedSlice = (config) => {
|
|
|
757
771
|
const variationTypeNames = [];
|
|
758
772
|
for (const variation of config.model.variations) {
|
|
759
773
|
let primaryInterface;
|
|
760
|
-
if (Object.keys(variation.primary).length > 0) {
|
|
774
|
+
if (variation.primary && Object.keys(variation.primary).length > 0) {
|
|
761
775
|
primaryInterface = config.sourceFile.addInterface({
|
|
762
776
|
name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Primary`),
|
|
763
777
|
docs: [
|
|
@@ -798,7 +812,7 @@ const addTypeAliasForSharedSlice = (config) => {
|
|
|
798
812
|
});
|
|
799
813
|
}
|
|
800
814
|
let itemInterface;
|
|
801
|
-
if (Object.keys(variation.items).length > 0) {
|
|
815
|
+
if (variation.items && Object.keys(variation.items).length > 0) {
|
|
802
816
|
itemInterface = config.sourceFile.addInterface({
|
|
803
817
|
name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Item`),
|
|
804
818
|
docs: [
|