prismic-ts-codegen 0.0.3 → 0.0.6

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.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Project } from 'ts-morph';
1
+ import { Project, ModuleDeclarationKind } from 'ts-morph';
2
2
  import { stripIndent } from 'common-tags';
3
3
  import { CustomTypeModelFieldType, CustomTypeModelLinkSelectType, CustomTypeModelSliceType } from '@prismicio/types';
4
4
  import { pascalCase as pascalCase$1, pascalCaseTransformMerge } from 'pascal-case';
@@ -143,6 +143,9 @@ const writeFieldDocsDescription = (config) => {
143
143
  if (config.apiIDPath) {
144
144
  config.writer.writeLine(`- **API ID Path**: ${config.apiIDPath}`);
145
145
  }
146
+ if (config.tabName) {
147
+ config.writer.writeLine(`- **Tab**: ${config.tabName}`);
148
+ }
146
149
  if (config.documentationURL) {
147
150
  config.writer.writeLine(`- **Documentation**: ${config.documentationURL}`);
148
151
  }
@@ -174,6 +177,7 @@ const buildFieldDocs = (config) => {
174
177
  placeholder,
175
178
  defaultValue,
176
179
  apiIDPath,
180
+ tabName: config.tabName,
177
181
  documentationURL: getFieldDocumentationURL({ model: config.model })
178
182
  });
179
183
  }
@@ -189,7 +193,7 @@ const buildSharedSliceInterfaceName = (config) => {
189
193
  return pascalCase(`${config.id} Slice`);
190
194
  };
191
195
 
192
- const addInterfacePropertyFromField = (config) => {
196
+ const addInterfacePropertyForField = (config) => {
193
197
  switch (config.model.type) {
194
198
  case "UID": {
195
199
  break;
@@ -201,7 +205,8 @@ const addInterfacePropertyFromField = (config) => {
201
205
  docs: buildFieldDocs({
202
206
  id: config.id,
203
207
  model: config.model,
204
- path: config.path
208
+ path: config.path,
209
+ tabName: config.tabName
205
210
  })
206
211
  });
207
212
  break;
@@ -213,7 +218,8 @@ const addInterfacePropertyFromField = (config) => {
213
218
  docs: buildFieldDocs({
214
219
  id: config.id,
215
220
  model: config.model,
216
- path: config.path
221
+ path: config.path,
222
+ tabName: config.tabName
217
223
  })
218
224
  });
219
225
  break;
@@ -225,7 +231,8 @@ const addInterfacePropertyFromField = (config) => {
225
231
  docs: buildFieldDocs({
226
232
  id: config.id,
227
233
  model: config.model,
228
- path: config.path
234
+ path: config.path,
235
+ tabName: config.tabName
229
236
  })
230
237
  });
231
238
  break;
@@ -244,7 +251,8 @@ const addInterfacePropertyFromField = (config) => {
244
251
  docs: buildFieldDocs({
245
252
  id: config.id,
246
253
  model: config.model,
247
- path: config.path
254
+ path: config.path,
255
+ tabName: config.tabName
248
256
  })
249
257
  });
250
258
  break;
@@ -256,7 +264,8 @@ const addInterfacePropertyFromField = (config) => {
256
264
  docs: buildFieldDocs({
257
265
  id: config.id,
258
266
  model: config.model,
259
- path: config.path
267
+ path: config.path,
268
+ tabName: config.tabName
260
269
  })
261
270
  });
262
271
  break;
@@ -270,7 +279,8 @@ const addInterfacePropertyFromField = (config) => {
270
279
  docs: buildFieldDocs({
271
280
  id: config.id,
272
281
  model: config.model,
273
- path: config.path
282
+ path: config.path,
283
+ tabName: config.tabName
274
284
  })
275
285
  });
276
286
  } else {
@@ -280,7 +290,8 @@ const addInterfacePropertyFromField = (config) => {
280
290
  docs: buildFieldDocs({
281
291
  id: config.id,
282
292
  model: config.model,
283
- path: config.path
293
+ path: config.path,
294
+ tabName: config.tabName
284
295
  })
285
296
  });
286
297
  }
@@ -294,7 +305,8 @@ const addInterfacePropertyFromField = (config) => {
294
305
  docs: buildFieldDocs({
295
306
  id: config.id,
296
307
  model: config.model,
297
- path: config.path
308
+ path: config.path,
309
+ tabName: config.tabName
298
310
  })
299
311
  });
300
312
  break;
@@ -308,7 +320,8 @@ const addInterfacePropertyFromField = (config) => {
308
320
  docs: buildFieldDocs({
309
321
  id: config.id,
310
322
  model: config.model,
311
- path: config.path
323
+ path: config.path,
324
+ tabName: config.tabName
312
325
  })
313
326
  });
314
327
  break;
@@ -320,7 +333,8 @@ const addInterfacePropertyFromField = (config) => {
320
333
  docs: buildFieldDocs({
321
334
  id: config.id,
322
335
  model: config.model,
323
- path: config.path
336
+ path: config.path,
337
+ tabName: config.tabName
324
338
  })
325
339
  });
326
340
  break;
@@ -332,7 +346,8 @@ const addInterfacePropertyFromField = (config) => {
332
346
  docs: buildFieldDocs({
333
347
  id: config.id,
334
348
  model: config.model,
335
- path: config.path
349
+ path: config.path,
350
+ tabName: config.tabName
336
351
  })
337
352
  });
338
353
  }
@@ -346,7 +361,8 @@ const addInterfacePropertyFromField = (config) => {
346
361
  docs: buildFieldDocs({
347
362
  id: config.id,
348
363
  model: config.model,
349
- path: config.path
364
+ path: config.path,
365
+ tabName: config.tabName
350
366
  })
351
367
  });
352
368
  break;
@@ -360,7 +376,8 @@ const addInterfacePropertyFromField = (config) => {
360
376
  docs: buildFieldDocs({
361
377
  id: config.id,
362
378
  model: config.model,
363
- path: config.path
379
+ path: config.path,
380
+ tabName: config.tabName
364
381
  })
365
382
  });
366
383
  } else {
@@ -370,7 +387,8 @@ const addInterfacePropertyFromField = (config) => {
370
387
  docs: buildFieldDocs({
371
388
  id: config.id,
372
389
  model: config.model,
373
- path: config.path
390
+ path: config.path,
391
+ tabName: config.tabName
374
392
  })
375
393
  });
376
394
  }
@@ -386,7 +404,8 @@ const addInterfacePropertyFromField = (config) => {
386
404
  docs: buildFieldDocs({
387
405
  id: config.id,
388
406
  model: config.model,
389
- path: config.path
407
+ path: config.path,
408
+ tabName: config.tabName
390
409
  })
391
410
  });
392
411
  } else {
@@ -396,7 +415,8 @@ const addInterfacePropertyFromField = (config) => {
396
415
  docs: buildFieldDocs({
397
416
  id: config.id,
398
417
  model: config.model,
399
- path: config.path
418
+ path: config.path,
419
+ tabName: config.tabName
400
420
  })
401
421
  });
402
422
  }
@@ -409,7 +429,8 @@ const addInterfacePropertyFromField = (config) => {
409
429
  docs: buildFieldDocs({
410
430
  id: config.id,
411
431
  model: config.model,
412
- path: config.path
432
+ path: config.path,
433
+ tabName: config.tabName
413
434
  })
414
435
  });
415
436
  break;
@@ -421,7 +442,8 @@ const addInterfacePropertyFromField = (config) => {
421
442
  docs: buildFieldDocs({
422
443
  id: config.id,
423
444
  model: config.model,
424
- path: config.path
445
+ path: config.path,
446
+ tabName: config.tabName
425
447
  })
426
448
  });
427
449
  break;
@@ -466,7 +488,8 @@ const addInterfacePropertyFromField = (config) => {
466
488
  docs: buildFieldDocs({
467
489
  id: config.id,
468
490
  model: config.model,
469
- path: config.path
491
+ path: config.path,
492
+ tabName: config.tabName
470
493
  })
471
494
  });
472
495
  break;
@@ -615,7 +638,8 @@ const addInterfacePropertyFromField = (config) => {
615
638
  docs: buildFieldDocs({
616
639
  id: config.id,
617
640
  model: config.model,
618
- path: config.path
641
+ path: config.path,
642
+ tabName: config.tabName
619
643
  })
620
644
  });
621
645
  break;
@@ -627,7 +651,8 @@ const addInterfacePropertyFromField = (config) => {
627
651
  docs: buildFieldDocs({
628
652
  id: config.id,
629
653
  model: config.model,
630
- path: config.path
654
+ path: config.path,
655
+ tabName: config.tabName
631
656
  })
632
657
  });
633
658
  }
@@ -635,10 +660,11 @@ const addInterfacePropertyFromField = (config) => {
635
660
  };
636
661
  const addInterfacePropertiesForFields = (config) => {
637
662
  for (const name in config.fields) {
638
- addInterfacePropertyFromField({
663
+ addInterfacePropertyForField({
639
664
  ...config,
640
665
  id: name.includes("-") ? `"${name}"` : name,
641
- model: config.fields[name]
666
+ model: config.fields[name],
667
+ tabName: config.tabName
642
668
  });
643
669
  }
644
670
  };
@@ -665,18 +691,22 @@ const addTypeAliasForCustomType = ({
665
691
  }
666
692
  ]
667
693
  });
668
- addInterfacePropertiesForFields({
669
- fields,
670
- interface: dataInterface,
671
- sourceFile,
672
- path: [
673
- {
674
- id: model.id,
675
- model
676
- }
677
- ],
678
- fieldConfigs
679
- });
694
+ for (const tabName in model.json) {
695
+ const { uid: _uidField, ...tabFields } = model.json[tabName];
696
+ addInterfacePropertiesForFields({
697
+ fields: tabFields,
698
+ interface: dataInterface,
699
+ sourceFile,
700
+ path: [
701
+ {
702
+ id: model.id,
703
+ model
704
+ }
705
+ ],
706
+ fieldConfigs,
707
+ tabName
708
+ });
709
+ }
680
710
  } else {
681
711
  dataInterface = sourceFile.addTypeAlias({
682
712
  name: pascalCase(`${model.id} Document Data`),
@@ -899,6 +929,13 @@ const generateTypes = (config = {}) => {
899
929
  fieldConfigs: config.fieldConfigs || {}
900
930
  });
901
931
  }
932
+ if (config.customTypeModels.length > 0) {
933
+ sourceFile.addTypeAlias({
934
+ name: "AllDocumentTypes",
935
+ type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
936
+ isExported: true
937
+ });
938
+ }
902
939
  }
903
940
  if (config.sharedSliceModels) {
904
941
  for (const model of config.sharedSliceModels) {
@@ -909,6 +946,37 @@ const generateTypes = (config = {}) => {
909
946
  });
910
947
  }
911
948
  }
949
+ if (config.clientIntegration?.includeCreateClientInterface) {
950
+ sourceFile.addImportDeclaration({
951
+ moduleSpecifier: "@prismicio/client",
952
+ namespaceImport: "prismic",
953
+ isTypeOnly: true
954
+ });
955
+ const clientModuleDeclaration = sourceFile.addModule({
956
+ name: '"@prismicio/client"',
957
+ hasDeclareKeyword: true,
958
+ declarationKind: ModuleDeclarationKind.Module
959
+ });
960
+ clientModuleDeclaration.addInterface({
961
+ name: "CreateClient",
962
+ callSignatures: [
963
+ {
964
+ parameters: [
965
+ {
966
+ name: "repositoryNameOrEndpoint",
967
+ type: "string"
968
+ },
969
+ {
970
+ name: "options",
971
+ type: "prismic.ClientConfig",
972
+ hasQuestionToken: true
973
+ }
974
+ ],
975
+ returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
976
+ }
977
+ ]
978
+ });
979
+ }
912
980
  return getSourceFileText(sourceFile);
913
981
  };
914
982