prismic-ts-codegen 0.1.2 → 0.1.4

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.
@@ -8,7 +8,7 @@ import { CUSTOM_TYPES_DOCUMENTATION_URL } from "../constants";
8
8
  import { FieldConfigs } from "../types";
9
9
 
10
10
  import { addInterfacePropertiesForFields } from "./addInterfacePropertiesForFields";
11
- import { pascalCase } from "./pascalCase";
11
+ import { buildTypeName } from "./buildTypeName";
12
12
 
13
13
  const collectCustomTypeFields = (
14
14
  model: CustomTypeModel,
@@ -36,7 +36,7 @@ export const addTypeAliasForCustomType = ({
36
36
  let dataInterface: InterfaceDeclaration | TypeAliasDeclaration;
37
37
  if (hasDataFields) {
38
38
  dataInterface = sourceFile.addInterface({
39
- name: pascalCase(`${model.id} Document Data`),
39
+ name: buildTypeName(model.id, "Document", "Data"),
40
40
  docs: [
41
41
  {
42
42
  description: `Content for ${model.label} documents`,
@@ -65,7 +65,7 @@ export const addTypeAliasForCustomType = ({
65
65
  }
66
66
  } else {
67
67
  dataInterface = sourceFile.addTypeAlias({
68
- name: pascalCase(`${model.id} Document Data`),
68
+ name: buildTypeName(model.id, "Document", "Data"),
69
69
  type: `Record<string, never>`,
70
70
  docs: [
71
71
  {
@@ -76,7 +76,7 @@ export const addTypeAliasForCustomType = ({
76
76
  }
77
77
 
78
78
  return sourceFile.addTypeAlias({
79
- name: pascalCase(`${model.id} Document`),
79
+ name: buildTypeName(model.id, "Document"),
80
80
  typeParameters: [
81
81
  {
82
82
  name: "Lang",
@@ -5,13 +5,14 @@ import type {
5
5
  TypeAliasDeclaration,
6
6
  } from "ts-morph";
7
7
 
8
- import { addInterfacePropertiesForFields } from "./addInterfacePropertiesForFields";
9
- import { buildSharedSliceInterfaceName } from "./buildSharedSliceInterfaceName";
10
- import { pascalCase } from "./pascalCase";
11
- import { getHumanReadableFieldPath } from "./getHumanReadableFieldPath";
12
8
  import { SHARED_SLICES_DOCUMENTATION_URL } from "../constants";
13
9
  import { FieldConfigs } from "../types";
14
10
 
11
+ import { addInterfacePropertiesForFields } from "./addInterfacePropertiesForFields";
12
+ import { buildSharedSliceInterfaceNamePart } from "./buildSharedSliceInterfaceNamePart";
13
+ import { buildTypeName } from "./buildTypeName";
14
+ import { getHumanReadableFieldPath } from "./getHumanReadableFieldPath";
15
+
15
16
  type AddTypeAliasForSharedSliceConfig = {
16
17
  model: SharedSliceModel;
17
18
  sourceFile: SourceFile;
@@ -27,10 +28,10 @@ export const addTypeAliasForSharedSlice = (
27
28
  let primaryInterface: InterfaceDeclaration | undefined;
28
29
  if (variation.primary && Object.keys(variation.primary).length > 0) {
29
30
  primaryInterface = config.sourceFile.addInterface({
30
- name: pascalCase(
31
- `${buildSharedSliceInterfaceName({ id: config.model.id })} ${
32
- variation.id
33
- } Primary`,
31
+ name: buildTypeName(
32
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
33
+ variation.id,
34
+ "Primary",
34
35
  ),
35
36
  docs: [
36
37
  {
@@ -74,10 +75,10 @@ export const addTypeAliasForSharedSlice = (
74
75
  let itemInterface: InterfaceDeclaration | undefined;
75
76
  if (variation.items && Object.keys(variation.items).length > 0) {
76
77
  itemInterface = config.sourceFile.addInterface({
77
- name: pascalCase(
78
- `${buildSharedSliceInterfaceName({ id: config.model.id })} ${
79
- variation.id
80
- } Item`,
78
+ name: buildTypeName(
79
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
80
+ variation.id,
81
+ "Item",
81
82
  ),
82
83
  docs: [
83
84
  {
@@ -120,10 +121,9 @@ export const addTypeAliasForSharedSlice = (
120
121
  }
121
122
 
122
123
  const variationType = config.sourceFile.addTypeAlias({
123
- name: pascalCase(
124
- `${buildSharedSliceInterfaceName({
125
- id: config.model.id,
126
- })} ${variation.id}`,
124
+ name: buildTypeName(
125
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
126
+ variation.id,
127
127
  ),
128
128
  type: `prismicT.SharedSliceVariation<"${variation.id}", ${
129
129
  primaryInterface
@@ -152,7 +152,7 @@ export const addTypeAliasForSharedSlice = (
152
152
  }
153
153
 
154
154
  const variationsType = config.sourceFile.addTypeAlias({
155
- name: pascalCase(`${config.model.id} Slice Variation`),
155
+ name: buildTypeName(config.model.id, "Slice", "Variation"),
156
156
  type:
157
157
  variationTypeNames.length > 0 ? variationTypeNames.join(" | ") : "never",
158
158
  docs: [
@@ -174,10 +174,8 @@ export const addTypeAliasForSharedSlice = (
174
174
  });
175
175
 
176
176
  return config.sourceFile.addTypeAlias({
177
- name: pascalCase(
178
- buildSharedSliceInterfaceName({
179
- id: config.model.id,
180
- }),
177
+ name: buildTypeName(
178
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
181
179
  ),
182
180
  type: `prismicT.SharedSlice<"${
183
181
  config.model.id
@@ -0,0 +1,11 @@
1
+ import { pascalCase } from "./pascalCase";
2
+
3
+ type BuildSharedSliceInterfaceNamePartConfig = {
4
+ id: string;
5
+ };
6
+
7
+ export const buildSharedSliceInterfaceNamePart = (
8
+ config: BuildSharedSliceInterfaceNamePartConfig,
9
+ ): string => {
10
+ return pascalCase(`${config.id} Slice`);
11
+ };
@@ -0,0 +1,11 @@
1
+ import { pascalCase } from "./pascalCase";
2
+
3
+ export const buildTypeName = (...parts: string[]) => {
4
+ let name = pascalCase(parts.filter(Boolean).join(" "));
5
+
6
+ if (/^[0-9]/.test(name) ? "_" : "") {
7
+ name = `_${name}`;
8
+ }
9
+
10
+ return name;
11
+ };
@@ -1,11 +0,0 @@
1
- import { pascalCase } from "./pascalCase";
2
-
3
- type BuildSharedSliceInterfaceNameConfig = {
4
- id: string;
5
- };
6
-
7
- export const buildSharedSliceInterfaceName = (
8
- config: BuildSharedSliceInterfaceNameConfig,
9
- ): string => {
10
- return pascalCase(`${config.id} Slice`);
11
- };