@teleporthq/teleport-shared 0.31.3 → 0.32.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.
Files changed (48) hide show
  1. package/dist/cjs/constants/index.d.ts +4 -1
  2. package/dist/cjs/constants/index.d.ts.map +1 -1
  3. package/dist/cjs/constants/index.js +25 -1
  4. package/dist/cjs/constants/index.js.map +1 -1
  5. package/dist/cjs/index.d.ts +2 -1
  6. package/dist/cjs/index.d.ts.map +1 -1
  7. package/dist/cjs/index.js +3 -1
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  10. package/dist/cjs/utils/generic.d.ts +2 -0
  11. package/dist/cjs/utils/generic.d.ts.map +1 -0
  12. package/dist/cjs/utils/generic.js +61 -0
  13. package/dist/cjs/utils/generic.js.map +1 -0
  14. package/dist/cjs/utils/string-utils.d.ts +3 -0
  15. package/dist/cjs/utils/string-utils.d.ts.map +1 -1
  16. package/dist/cjs/utils/string-utils.js +9 -1
  17. package/dist/cjs/utils/string-utils.js.map +1 -1
  18. package/dist/cjs/utils/uidl-utils.d.ts +3 -2
  19. package/dist/cjs/utils/uidl-utils.d.ts.map +1 -1
  20. package/dist/cjs/utils/uidl-utils.js +286 -18
  21. package/dist/cjs/utils/uidl-utils.js.map +1 -1
  22. package/dist/esm/constants/index.d.ts +4 -1
  23. package/dist/esm/constants/index.d.ts.map +1 -1
  24. package/dist/esm/constants/index.js +24 -0
  25. package/dist/esm/constants/index.js.map +1 -1
  26. package/dist/esm/index.d.ts +2 -1
  27. package/dist/esm/index.d.ts.map +1 -1
  28. package/dist/esm/index.js +2 -1
  29. package/dist/esm/index.js.map +1 -1
  30. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  31. package/dist/esm/utils/generic.d.ts +2 -0
  32. package/dist/esm/utils/generic.d.ts.map +1 -0
  33. package/dist/esm/utils/generic.js +57 -0
  34. package/dist/esm/utils/generic.js.map +1 -0
  35. package/dist/esm/utils/string-utils.d.ts +3 -0
  36. package/dist/esm/utils/string-utils.d.ts.map +1 -1
  37. package/dist/esm/utils/string-utils.js +5 -0
  38. package/dist/esm/utils/string-utils.js.map +1 -1
  39. package/dist/esm/utils/uidl-utils.d.ts +3 -2
  40. package/dist/esm/utils/uidl-utils.d.ts.map +1 -1
  41. package/dist/esm/utils/uidl-utils.js +284 -17
  42. package/dist/esm/utils/uidl-utils.js.map +1 -1
  43. package/package.json +3 -3
  44. package/src/constants/index.ts +28 -1
  45. package/src/index.ts +2 -1
  46. package/src/utils/generic.ts +68 -0
  47. package/src/utils/string-utils.ts +5 -0
  48. package/src/utils/uidl-utils.ts +335 -24
@@ -4,6 +4,7 @@ export const dashCaseToCamelCase = (str: string): string =>
4
4
  str.replace(/[-_]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : ''))
5
5
 
6
6
  export const capitalize = (str: string): string => str[0].toUpperCase() + str.slice(1)
7
+ export const camelize = (str: string): string => str[0].toLowerCase() + str.slice(1)
7
8
 
8
9
  export const dashCaseToUpperCamelCase = (str: string) => capitalize(dashCaseToCamelCase(str))
9
10
 
@@ -35,6 +36,10 @@ export const slugify = (str: string): string => {
35
36
  .replace(/&/g, '-and-') // Replace & with 'and'
36
37
  }
37
38
 
39
+ export const createStateOrPropStoringValue = (value: string) => camelize(dashCaseToCamelCase(value))
40
+ export const createStateStoringFunction = (value: string) =>
41
+ `set${capitalize(dashCaseToUpperCamelCase(value))}`
42
+
38
43
  export const addSpacesToEachLine = (spaces: string, str: string) => {
39
44
  // indent the first line
40
45
  const respaced = spaces + str
@@ -20,9 +20,11 @@ import {
20
20
  UIDLStyleSheetContent,
21
21
  UIDLComponentStyleReference,
22
22
  UIDLRootComponent,
23
+ UIDLResourceItem,
23
24
  GeneratorOptions,
24
25
  } from '@teleporthq/teleport-types'
25
26
  import { basename } from 'path'
27
+ import { StringUtils } from '..'
26
28
 
27
29
  export const extractRoutes = (rootComponent: UIDLRootComponent) => {
28
30
  // Assuming root element starts with a UIDLElementNode
@@ -47,6 +49,7 @@ export const setFriendlyOutputOptions = (uidl: ComponentUIDL) => {
47
49
  uidl.outputOptions = uidl.outputOptions || {}
48
50
  const defaultComponentName = 'AppComponent'
49
51
  const friendlyName = removeIllegalCharacters(uidl.name) || defaultComponentName
52
+
50
53
  if (!uidl.outputOptions.fileName) {
51
54
  uidl.outputOptions.fileName = camelCaseToDashCase(friendlyName)
52
55
  }
@@ -128,7 +131,11 @@ export const prefixAssetsPath = (
128
131
  return originalString
129
132
  }
130
133
 
131
- if (!originalString.startsWith('/')) {
134
+ if (typeof originalString !== 'string') {
135
+ return originalString
136
+ }
137
+
138
+ if (!originalString?.startsWith('/')) {
132
139
  return originalString
133
140
  }
134
141
 
@@ -222,6 +229,47 @@ export const traverseNodes = (
222
229
  }
223
230
  break
224
231
 
232
+ case 'cms-list':
233
+ traverseNodes(node.content.nodes.success, fn)
234
+ if (node.content.nodes.error) {
235
+ traverseNodes(node.content.nodes.error, fn)
236
+ }
237
+ if (node.content.nodes.loading) {
238
+ traverseNodes(node.content.nodes.loading, fn)
239
+ }
240
+ break
241
+
242
+ case 'cms-list-repeater':
243
+ traverseNodes(node.content.nodes.list, fn)
244
+ if (node.content.nodes.empty) {
245
+ traverseNodes(node.content.nodes.empty, fn)
246
+ }
247
+ break
248
+
249
+ case 'cms-item':
250
+ traverseNodes(node.content.nodes.success, fn)
251
+ if (node.content.nodes.error) {
252
+ traverseNodes(node.content.nodes.error, fn)
253
+ }
254
+ if (node.content.nodes.loading) {
255
+ traverseNodes(node.content.nodes.loading, fn)
256
+ }
257
+ break
258
+
259
+ case 'cms-mixed-type':
260
+ if (node.content.nodes?.fallback) {
261
+ traverseNodes(node.content.nodes.fallback, fn)
262
+ }
263
+
264
+ if (node.content.nodes?.error) {
265
+ traverseNodes(node.content.nodes.error, fn)
266
+ }
267
+
268
+ Object.keys(node.content?.mappings || {}).forEach((key) => {
269
+ traverseNodes(node.content.mappings[key], fn)
270
+ })
271
+ break
272
+
225
273
  case 'repeat':
226
274
  traverseNodes(node.content.node, fn, node)
227
275
  traverseNodes(node.content.dataSource, fn, node)
@@ -242,13 +290,97 @@ export const traverseNodes = (
242
290
  case 'comp-style':
243
291
  case 'dynamic':
244
292
  case 'import':
293
+ case 'expr':
245
294
  case 'raw':
246
295
  case 'inject':
247
296
  break
248
297
 
249
298
  default:
250
299
  throw new Error(
251
- `traverseNodes was given an unsupported node type ${JSON.stringify(node, null, 2)}`
300
+ `traverseNodes was given an unsupported node type: ${JSON.stringify(node, null, 2)}`
301
+ )
302
+ }
303
+ }
304
+
305
+ export const traverseResources = (
306
+ node: UIDLNode,
307
+ fn: (node: UIDLResourceItem, parentNode: UIDLNode) => void
308
+ ) => {
309
+ switch (node.type) {
310
+ case 'element':
311
+ const { children } = node.content
312
+
313
+ if (children) {
314
+ children.forEach((child) => {
315
+ traverseResources(child, fn)
316
+ })
317
+ }
318
+ break
319
+
320
+ case 'cms-list':
321
+ traverseResources(node.content.nodes.success, fn)
322
+ if (node.content.nodes.error) {
323
+ traverseResources(node.content.nodes.error, fn)
324
+ }
325
+ if (node.content.nodes.loading) {
326
+ traverseResources(node.content.nodes.loading, fn)
327
+ }
328
+ break
329
+
330
+ case 'cms-list-repeater':
331
+ traverseResources(node.content.nodes.list, fn)
332
+ if (node.content.nodes.empty) {
333
+ traverseResources(node.content.nodes.empty, fn)
334
+ }
335
+ break
336
+
337
+ case 'cms-item':
338
+ traverseResources(node.content.nodes.success, fn)
339
+ if (node.content.nodes.error) {
340
+ traverseResources(node.content.nodes.error, fn)
341
+ }
342
+ if (node.content.nodes.loading) {
343
+ traverseResources(node.content.nodes.loading, fn)
344
+ }
345
+ break
346
+
347
+ case 'cms-mixed-type':
348
+ if (node.content.nodes?.fallback) {
349
+ traverseResources(node.content.nodes.fallback, fn)
350
+ }
351
+
352
+ if (node.content.nodes?.error) {
353
+ traverseResources(node.content.nodes.error, fn)
354
+ }
355
+
356
+ Object.keys(node.content?.mappings || {}).forEach((key) => {
357
+ traverseResources(node.content.mappings[key], fn)
358
+ })
359
+ break
360
+
361
+ case 'repeat':
362
+ traverseResources(node.content.node, fn)
363
+ break
364
+
365
+ case 'conditional':
366
+ traverseResources(node.content.node, fn)
367
+ break
368
+
369
+ case 'slot':
370
+ if (node.content.fallback) {
371
+ traverseResources(node.content.fallback, fn)
372
+ }
373
+ break
374
+
375
+ case 'static':
376
+ case 'dynamic':
377
+ case 'import':
378
+ case 'raw':
379
+ break
380
+
381
+ default:
382
+ throw new Error(
383
+ `traverseResources was given an unsupported node type: ${JSON.stringify(node, null, 2)}`
252
384
  )
253
385
  }
254
386
  }
@@ -276,6 +408,48 @@ export const traverseElements = (node: UIDLNode, fn: (element: UIDLElement) => v
276
408
  }
277
409
  break
278
410
 
411
+ case 'cms-list':
412
+ traverseElements(node.content.nodes.success, fn)
413
+ if (node.content.nodes.error) {
414
+ traverseElements(node.content.nodes.error, fn)
415
+ }
416
+ if (node.content.nodes.loading) {
417
+ traverseElements(node.content.nodes.loading, fn)
418
+ }
419
+ break
420
+
421
+ case 'cms-list-repeater':
422
+ traverseElements(node.content.nodes.list, fn)
423
+ if (node.content.nodes.empty) {
424
+ traverseElements(node.content.nodes.empty, fn)
425
+ }
426
+
427
+ break
428
+
429
+ case 'cms-item':
430
+ traverseElements(node.content.nodes.success, fn)
431
+ if (node.content.nodes.error) {
432
+ traverseElements(node.content.nodes.error, fn)
433
+ }
434
+ if (node.content.nodes.loading) {
435
+ traverseElements(node.content.nodes.loading, fn)
436
+ }
437
+ break
438
+
439
+ case 'cms-mixed-type':
440
+ if (node.content.nodes?.fallback) {
441
+ traverseElements(node.content.nodes.fallback, fn)
442
+ }
443
+
444
+ if (node.content.nodes?.error) {
445
+ traverseElements(node.content.nodes.error, fn)
446
+ }
447
+
448
+ Object.keys(node.content?.mappings || {}).forEach((key) => {
449
+ traverseElements(node.content.mappings[key], fn)
450
+ })
451
+ break
452
+
279
453
  case 'repeat':
280
454
  traverseElements(node.content.node, fn)
281
455
  break
@@ -294,11 +468,16 @@ export const traverseElements = (node: UIDLNode, fn: (element: UIDLElement) => v
294
468
  case 'dynamic':
295
469
  case 'inject':
296
470
  case 'raw':
471
+ case 'expr':
297
472
  break
298
473
 
299
474
  default:
300
475
  throw new Error(
301
- `traverseElements was given an unsupported node type ${JSON.stringify(node, null, 2)}`
476
+ `traverseElements was given an unsupported node type : ${node.type}, ${JSON.stringify(
477
+ node,
478
+ null,
479
+ 2
480
+ )}`
302
481
  )
303
482
  }
304
483
  }
@@ -314,6 +493,48 @@ export const traverseRepeats = (node: UIDLNode, fn: (element: UIDLRepeatContent)
314
493
 
315
494
  break
316
495
 
496
+ case 'cms-list':
497
+ traverseRepeats(node.content.nodes.success, fn)
498
+ if (node.content.nodes.error) {
499
+ traverseRepeats(node.content.nodes.error, fn)
500
+ }
501
+ if (node.content.nodes.loading) {
502
+ traverseRepeats(node.content.nodes.loading, fn)
503
+ }
504
+ break
505
+
506
+ case 'cms-list-repeater':
507
+ traverseRepeats(node.content.nodes.list, fn)
508
+ if (node.content.nodes.empty) {
509
+ traverseRepeats(node.content.nodes.empty, fn)
510
+ }
511
+
512
+ break
513
+
514
+ case 'cms-item':
515
+ traverseRepeats(node.content.nodes.success, fn)
516
+ if (node.content.nodes.error) {
517
+ traverseRepeats(node.content.nodes.error, fn)
518
+ }
519
+ if (node.content.nodes.loading) {
520
+ traverseRepeats(node.content.nodes.loading, fn)
521
+ }
522
+ break
523
+
524
+ case 'cms-mixed-type':
525
+ if (node.content.nodes?.fallback) {
526
+ traverseRepeats(node.content.nodes.fallback, fn)
527
+ }
528
+
529
+ if (node.content.nodes?.error) {
530
+ traverseRepeats(node.content.nodes.error, fn)
531
+ }
532
+
533
+ Object.keys(node.content?.mappings || {}).forEach((key) => {
534
+ traverseRepeats(node.content.mappings[key], fn)
535
+ })
536
+ break
537
+
317
538
  case 'repeat':
318
539
  fn(node.content)
319
540
 
@@ -333,12 +554,17 @@ export const traverseRepeats = (node: UIDLNode, fn: (element: UIDLRepeatContent)
333
554
  case 'static':
334
555
  case 'dynamic':
335
556
  case 'raw':
557
+ case 'expr':
336
558
  case 'inject':
337
559
  break
338
560
 
339
561
  default:
340
562
  throw new Error(
341
- `traverseRepeats was given an unsupported node type ${JSON.stringify(node, null, 2)}`
563
+ `traverseRepeats was given an unsupported node type: ${node.type} - ${JSON.stringify(
564
+ node,
565
+ null,
566
+ 2
567
+ )}`
342
568
  )
343
569
  }
344
570
  }
@@ -468,7 +694,7 @@ export const transformStringAssignmentToJson = (
468
694
  type: 'dynamic',
469
695
  content: {
470
696
  referenceType,
471
- id: path,
697
+ id: StringUtils.createStateOrPropStoringValue(path),
472
698
  },
473
699
  }
474
700
  }
@@ -495,13 +721,27 @@ export const transformStylesAssignmentsToJson = (
495
721
 
496
722
  if (!Array.isArray(styleContentAtKey) && entityType === 'object') {
497
723
  // if this value is already properly declared, make sure it is not
498
- const { type } = styleContentAtKey as Record<string, unknown>
724
+ const { type, content } = styleContentAtKey as UIDLStaticValue | UIDLDynamicReference
499
725
 
500
- if (['dynamic', 'static'].indexOf(type as string) !== -1) {
501
- acc[key] = styleContentAtKey as UIDLStyleValue
726
+ if (type === 'static') {
727
+ acc[key] = styleContentAtKey as UIDLStaticValue
502
728
  return acc
503
729
  }
504
730
 
731
+ if (type === 'dynamic') {
732
+ if (['state', 'prop'].includes(content?.referenceType)) {
733
+ acc[key] = {
734
+ type,
735
+ content: {
736
+ ...content,
737
+ id: StringUtils.createStateOrPropStoringValue(content.id),
738
+ },
739
+ }
740
+ } else {
741
+ acc[key] = styleContentAtKey as UIDLDynamicReference
742
+ }
743
+ }
744
+
505
745
  return acc
506
746
  }
507
747
 
@@ -517,17 +757,27 @@ export const transformStylesAssignmentsToJson = (
517
757
  return newStyleObject
518
758
  }
519
759
 
760
+ /*
761
+ All the props passed to the components are transformed to a unique case
762
+ to minimize the collision of using cameCalse in one place and dashCase in
763
+ another place. So, all the attrs that are being passed to the local compoenents
764
+ need to be transformed since these are basically props.
765
+ */
766
+
520
767
  export const transformAttributesAssignmentsToJson = (
521
- attributesObject: Record<string, unknown>
768
+ attributesObject: Record<string, unknown>,
769
+ isLocalComponent = false
522
770
  ): Record<string, UIDLAttributeValue> => {
523
- const newStyleObject: Record<string, UIDLAttributeValue> = {}
771
+ const newAttrObject: Record<string, UIDLAttributeValue> = {}
524
772
 
525
773
  Object.keys(attributesObject).reduce((acc, key) => {
526
774
  const attributeContent = attributesObject[key]
527
775
  const entityType = typeof attributeContent
528
776
 
529
777
  if (['string', 'number'].indexOf(entityType) !== -1) {
530
- acc[key] = transformStringAssignmentToJson(
778
+ const propKey = isLocalComponent ? StringUtils.createStateOrPropStoringValue(key) : key
779
+
780
+ acc[propKey] = transformStringAssignmentToJson(
531
781
  attributeContent as string | number
532
782
  ) as UIDLAttributeValue
533
783
  return acc
@@ -535,9 +785,36 @@ export const transformAttributesAssignmentsToJson = (
535
785
 
536
786
  if (!Array.isArray(attributeContent) && entityType === 'object') {
537
787
  // if this value is already properly declared, make sure it is not
538
- const { type } = attributeContent as Record<string, unknown>
539
- if (['dynamic', 'static', 'import', 'comp-style', 'raw'].indexOf(type as string) !== -1) {
540
- acc[key] = attributeContent as UIDLAttributeValue
788
+ const { type } = attributeContent as UIDLAttributeValue
789
+ if (['static', 'import', 'raw', 'expr'].indexOf(type as string) !== -1) {
790
+ const propKey = isLocalComponent ? StringUtils.createStateOrPropStoringValue(key) : key
791
+ acc[propKey] = attributeContent as UIDLAttributeValue
792
+ return acc
793
+ }
794
+
795
+ if (type === 'comp-style') {
796
+ acc[key] = {
797
+ type: 'comp-style',
798
+ content: StringUtils.createStateOrPropStoringValue(
799
+ (attributeContent as UIDLComponentStyleReference).content
800
+ ),
801
+ }
802
+ return acc
803
+ }
804
+
805
+ const { content } = attributeContent as UIDLDynamicReference
806
+ if (type === 'dynamic') {
807
+ if (['state', 'prop'].includes(content?.referenceType)) {
808
+ acc[key] = {
809
+ type,
810
+ content: {
811
+ ...content,
812
+ id: StringUtils.createStateOrPropStoringValue(content.id),
813
+ },
814
+ }
815
+ } else {
816
+ acc[key] = attributeContent as UIDLAttributeValue
817
+ }
541
818
  return acc
542
819
  }
543
820
 
@@ -549,17 +826,9 @@ export const transformAttributesAssignmentsToJson = (
549
826
  )}`
550
827
  )
551
828
  }
829
+ }, newAttrObject)
552
830
 
553
- throw new Error(
554
- `transformAttributesAssignmentsToJson encountered a style value that is not supported ${JSON.stringify(
555
- attributeContent,
556
- null,
557
- 2
558
- )}`
559
- )
560
- }, newStyleObject)
561
-
562
- return newStyleObject
831
+ return newAttrObject
563
832
  }
564
833
 
565
834
  export const findFirstElementNode = (node: UIDLNode): UIDLElementNode => {
@@ -599,6 +868,47 @@ export const removeChildNodes = (
599
868
  removeChildNodes(node.content.node, criteria)
600
869
  break
601
870
 
871
+ case 'cms-list':
872
+ removeChildNodes(node.content.nodes.success, criteria)
873
+ if (node.content.nodes.error) {
874
+ removeChildNodes(node.content.nodes.error, criteria)
875
+ }
876
+ if (node.content.nodes.loading) {
877
+ removeChildNodes(node.content.nodes.loading, criteria)
878
+ }
879
+ break
880
+
881
+ case 'cms-list-repeater':
882
+ removeChildNodes(node.content.nodes.list, criteria)
883
+ if (node.content.nodes.empty) {
884
+ removeChildNodes(node.content.nodes.empty, criteria)
885
+ }
886
+
887
+ break
888
+ case 'cms-item':
889
+ removeChildNodes(node.content.nodes.success, criteria)
890
+ if (node.content.nodes.error) {
891
+ removeChildNodes(node.content.nodes.error, criteria)
892
+ }
893
+ if (node.content.nodes.loading) {
894
+ removeChildNodes(node.content.nodes.loading, criteria)
895
+ }
896
+ break
897
+
898
+ case 'cms-mixed-type':
899
+ if (node.content.nodes?.fallback) {
900
+ removeChildNodes(node.content.nodes.fallback, criteria)
901
+ }
902
+
903
+ if (node.content.nodes?.error) {
904
+ removeChildNodes(node.content.nodes.error, criteria)
905
+ }
906
+
907
+ Object.keys(node.content?.mappings || {}).forEach((key) => {
908
+ removeChildNodes(node.content.mappings[key], criteria)
909
+ })
910
+ break
911
+
602
912
  case 'conditional':
603
913
  removeChildNodes(node.content.node, criteria)
604
914
  break
@@ -612,6 +922,7 @@ export const removeChildNodes = (
612
922
  case 'static':
613
923
  case 'dynamic':
614
924
  case 'raw':
925
+ case 'expr':
615
926
  case 'inject':
616
927
  break
617
928