prismic-ts-codegen 0.0.7 → 0.1.2

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/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 && "config" 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 && "non-repeat" in model && "repeat" in model;
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 && "variations" in model;
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) && "label" in config.model.config) {
78
- return config.model.config.label;
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
- return `Integration Fields (Catalog: \`${config.model.config.catalog}\`)`;
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.select) {
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,
@@ -290,7 +299,7 @@ const addInterfacePropertyForField = (config) => {
290
299
  } else {
291
300
  config.interface.addProperty({
292
301
  name: config.id,
293
- type: "prismicT.ImageField<null>",
302
+ type: "prismicT.ImageField<never>",
294
303
  docs: buildFieldDocs({
295
304
  id: config.id,
296
305
  model: config.model,
@@ -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.select) {
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.options.map((option) => `"${option}"`).join(" | ");
403
- const hasDefault = Boolean(config.model.config.default_value);
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
- addInterfacePropertiesForFields({
477
- interface: itemInterface,
478
- sourceFile: config.sourceFile,
479
- fields: config.model.config.fields,
480
- path: [
481
- ...config.path,
482
- {
483
- id: config.id,
484
- model: config.model
485
- }
486
- ],
487
- fieldConfigs: config.fieldConfigs
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
- for (const choiceId in config.model.config.choices) {
504
- const choice = config.model.config.choices[choiceId];
505
- if (choice.type === types.CustomTypeModelSliceType.SharedSlice) {
506
- choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
507
- } else if (choice.type === types.CustomTypeModelSliceType.Slice) {
508
- let primaryInterface;
509
- if (Object.keys(choice["non-repeat"]).length > 0) {
510
- primaryInterface = config.sourceFile.addInterface({
511
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`),
512
- docs: [
513
- {
514
- description: (writer) => {
515
- const humanReadablePath = getHumanReadableFieldPath({
516
- path: [
517
- ...config.path,
518
- {
519
- id: config.id,
520
- model: config.model
521
- },
522
- {
523
- id: choiceId,
524
- model: choice
525
- },
526
- {
527
- id: "primary",
528
- label: "Primary"
529
- }
530
- ]
531
- });
532
- writer.writeLine(`Primary content in ${humanReadablePath}`);
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
- addInterfacePropertiesForFields({
538
- interface: primaryInterface,
539
- sourceFile: config.sourceFile,
540
- fields: choice["non-repeat"],
541
- path: [
542
- ...config.path,
543
- {
544
- id: config.id,
545
- model: config.model
546
- },
547
- {
548
- id: choiceId,
549
- model: choice
550
- },
551
- {
552
- id: "primary",
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
- addInterfacePropertiesForFields({
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({
@@ -666,7 +680,7 @@ const addInterfacePropertiesForFields = (config) => {
666
680
  for (const name in config.fields) {
667
681
  addInterfacePropertyForField({
668
682
  ...config,
669
- id: name.includes("-") ? `"${name}"` : name,
683
+ id: name.includes("-") || /^[0-9]/.test(name) ? `"${name}"` : name,
670
684
  model: config.fields[name],
671
685
  tabName: config.tabName
672
686
  });
@@ -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: [