prismic-ts-codegen 0.1.3-alpha.0 → 0.1.3

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.
@@ -6,7 +6,7 @@ import { addTypeAliasForCustomType } from "./lib/addTypeAliasForCustomType";
6
6
  import { addTypeAliasForSharedSlice } from "./lib/addTypeAliasForSharedSlice";
7
7
  import { getSourceFileText } from "./lib/getSourceFileText";
8
8
  import { FieldConfigs } from "./types";
9
- import { pascalCase } from "./lib/pascalCase";
9
+ import { buildTypeName } from "./lib/buildTypeName";
10
10
 
11
11
  export type GenerateTypesConfig = {
12
12
  customTypeModels?: CustomTypeModel[];
@@ -15,7 +15,6 @@ export type GenerateTypesConfig = {
15
15
  fieldConfigs?: FieldConfigs;
16
16
  clientIntegration?: {
17
17
  includeCreateClientInterface?: boolean;
18
- includeContentNamespace?: boolean;
19
18
  };
20
19
  };
21
20
 
@@ -35,7 +34,7 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
35
34
 
36
35
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
37
36
 
38
- const simplifyTypeAlias = sourceFile.addTypeAlias({
37
+ sourceFile.addTypeAlias({
39
38
  name: "Simplify",
40
39
  typeParameters: [
41
40
  {
@@ -60,7 +59,7 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
60
59
  name: "AllDocumentTypes",
61
60
  type: config.customTypeModels
62
61
  .map((customTypeModel) =>
63
- pascalCase(`${customTypeModel.id} Document`),
62
+ buildTypeName(customTypeModel.id, "Document"),
64
63
  )
65
64
  .join(" | "),
66
65
  isExported: true,
@@ -78,10 +77,7 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
78
77
  }
79
78
  }
80
79
 
81
- if (
82
- config.clientIntegration?.includeCreateClientInterface ||
83
- config.clientIntegration?.includeContentNamespace
84
- ) {
80
+ if (config.clientIntegration?.includeCreateClientInterface) {
85
81
  sourceFile.addImportDeclaration({
86
82
  moduleSpecifier: "@prismicio/client",
87
83
  namespaceImport: "prismic",
@@ -94,55 +90,28 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
94
90
  declarationKind: ModuleDeclarationKind.Module,
95
91
  });
96
92
 
97
- if (config.clientIntegration.includeCreateClientInterface) {
98
- clientModuleDeclaration.addInterface({
99
- name: "CreateClient",
100
- callSignatures: [
101
- {
102
- parameters: [
103
- {
104
- name: "repositoryNameOrEndpoint",
105
- type: "string",
106
- },
107
- {
108
- name: "options",
109
- type: "prismic.ClientConfig",
110
- hasQuestionToken: true,
111
- },
112
- ],
113
- returnType:
114
- (config.customTypeModels?.length || 0) > 0
115
- ? "prismic.Client<AllDocumentTypes>"
116
- : "prismic.Client",
117
- },
118
- ],
119
- });
120
- }
121
-
122
- if (config.clientIntegration.includeContentNamespace) {
123
- const contentNamespaceDeclaration = clientModuleDeclaration.addModule({
124
- name: "Content",
125
- declarationKind: ModuleDeclarationKind.Namespace,
126
- });
127
-
128
- const exportSymbols = sourceFile
129
- .getExportSymbols()
130
- .filter((exportSymbol) => {
131
- // The Simplify utility type should not
132
- // be exported, but it is included in
133
- // `getExportSymbols()`'s result.
134
- return exportSymbol.getName() !== simplifyTypeAlias.getName();
135
- });
136
-
137
- contentNamespaceDeclaration.addExportDeclaration({
138
- isTypeOnly: true,
139
- namedExports: exportSymbols.map((exportSymbol) => {
140
- return {
141
- name: exportSymbol.getName(),
142
- };
143
- }),
144
- });
145
- }
93
+ clientModuleDeclaration.addInterface({
94
+ name: "CreateClient",
95
+ callSignatures: [
96
+ {
97
+ parameters: [
98
+ {
99
+ name: "repositoryNameOrEndpoint",
100
+ type: "string",
101
+ },
102
+ {
103
+ name: "options",
104
+ type: "prismic.ClientConfig",
105
+ hasQuestionToken: true,
106
+ },
107
+ ],
108
+ returnType:
109
+ (config.customTypeModels?.length || 0) > 0
110
+ ? "prismic.Client<AllDocumentTypes>"
111
+ : "prismic.Client",
112
+ },
113
+ ],
114
+ });
146
115
  }
147
116
 
148
117
  return getSourceFileText(sourceFile);
@@ -11,10 +11,11 @@ import {
11
11
  import type { InterfaceDeclaration, SourceFile } from "ts-morph";
12
12
 
13
13
  import { FieldConfigs, PathElement } from "../types";
14
+
14
15
  import { buildFieldDocs } from "./buildFieldDocs";
15
- import { buildSharedSliceInterfaceName } from "./buildSharedSliceInterfaceName";
16
+ import { buildSharedSliceInterfaceNamePart } from "./buildSharedSliceInterfaceNamePart";
17
+ import { buildTypeName } from "./buildTypeName";
16
18
  import { getHumanReadableFieldPath } from "./getHumanReadableFieldPath";
17
- import { pascalCase } from "./pascalCase";
18
19
 
19
20
  type AddInterfacePropertyFromFieldConfig = {
20
21
  id: string;
@@ -362,8 +363,12 @@ const addInterfacePropertyForField = (
362
363
 
363
364
  case "Group": {
364
365
  const itemInterface = config.sourceFile.addInterface({
365
- name: pascalCase(
366
- `${config.path[0].id} Document Data ${config.id} Item`,
366
+ name: buildTypeName(
367
+ config.path[0].id,
368
+ "Document",
369
+ "Data",
370
+ config.id,
371
+ "Item",
367
372
  ),
368
373
  docs: [
369
374
  {
@@ -424,7 +429,7 @@ const addInterfacePropertyForField = (
424
429
 
425
430
  if (choice.type === CustomTypeModelSliceType.SharedSlice) {
426
431
  choiceInterfaceNames.push(
427
- buildSharedSliceInterfaceName({ id: choiceId }),
432
+ buildSharedSliceInterfaceNamePart({ id: choiceId }),
428
433
  );
429
434
  } else if (choice.type === CustomTypeModelSliceType.Slice) {
430
435
  let primaryInterface: InterfaceDeclaration | undefined;
@@ -433,8 +438,14 @@ const addInterfacePropertyForField = (
433
438
  Object.keys(choice["non-repeat"]).length > 0
434
439
  ) {
435
440
  primaryInterface = config.sourceFile.addInterface({
436
- name: pascalCase(
437
- `${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`,
441
+ name: buildTypeName(
442
+ config.path[0].id,
443
+ "Document",
444
+ "Data",
445
+ config.id,
446
+ choiceId,
447
+ "Slice",
448
+ "Primary",
438
449
  ),
439
450
  docs: [
440
451
  {
@@ -490,8 +501,14 @@ const addInterfacePropertyForField = (
490
501
  let itemInterface: InterfaceDeclaration | undefined;
491
502
  if (choice.repeat && Object.keys(choice.repeat).length > 0) {
492
503
  itemInterface = config.sourceFile.addInterface({
493
- name: pascalCase(
494
- `${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`,
504
+ name: buildTypeName(
505
+ config.path[0].id,
506
+ "Document",
507
+ "Data",
508
+ config.id,
509
+ choiceId,
510
+ "Slice",
511
+ "Item",
495
512
  ),
496
513
  docs: [
497
514
  {
@@ -544,8 +561,13 @@ const addInterfacePropertyForField = (
544
561
  }
545
562
 
546
563
  const sliceType = config.sourceFile.addTypeAlias({
547
- name: pascalCase(
548
- `${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`,
564
+ name: buildTypeName(
565
+ config.path[0].id,
566
+ "Document",
567
+ "Data",
568
+ config.id,
569
+ choiceId,
570
+ "Slice",
549
571
  ),
550
572
  type: `prismicT.Slice<"${choiceId}", ${
551
573
  primaryInterface
@@ -563,8 +585,12 @@ const addInterfacePropertyForField = (
563
585
  }
564
586
 
565
587
  const slicesType = config.sourceFile.addTypeAlias({
566
- name: pascalCase(
567
- `${config.path[0].id} Document Data ${config.id} Slice`,
588
+ name: buildTypeName(
589
+ config.path[0].id,
590
+ "Document",
591
+ "Data",
592
+ config.id,
593
+ "Slice",
568
594
  ),
569
595
  type:
570
596
  choiceInterfaceNames.length > 0
@@ -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
- };