@prismicio/types-internal 0.1.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 (68) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +110 -0
  3. package/dist/customtypes/index.cjs +864 -0
  4. package/dist/customtypes/index.cjs.map +1 -0
  5. package/dist/customtypes/index.d.ts +18433 -0
  6. package/dist/customtypes/index.d.ts.map +1 -0
  7. package/dist/customtypes/index.js +864 -0
  8. package/dist/customtypes/index.js.map +1 -0
  9. package/dist/customtypes/widgets/index.cjs +735 -0
  10. package/dist/customtypes/widgets/index.cjs.map +1 -0
  11. package/dist/customtypes/widgets/index.d.ts +11697 -0
  12. package/dist/customtypes/widgets/index.d.ts.map +1 -0
  13. package/dist/customtypes/widgets/index.js +735 -0
  14. package/dist/customtypes/widgets/index.js.map +1 -0
  15. package/dist/customtypes/widgets/slices/index.cjs +671 -0
  16. package/dist/customtypes/widgets/slices/index.cjs.map +1 -0
  17. package/dist/customtypes/widgets/slices/index.d.ts +7079 -0
  18. package/dist/customtypes/widgets/slices/index.d.ts.map +1 -0
  19. package/dist/customtypes/widgets/slices/index.js +671 -0
  20. package/dist/customtypes/widgets/slices/index.js.map +1 -0
  21. package/dist/index.cjs +870 -0
  22. package/dist/index.cjs.map +1 -0
  23. package/dist/index.d.ts +18455 -0
  24. package/dist/index.js +844 -0
  25. package/dist/index.js.map +1 -0
  26. package/package.json +84 -0
  27. package/src/customtypes/CustomType.ts +142 -0
  28. package/src/customtypes/Format.ts +4 -0
  29. package/src/customtypes/Section.ts +25 -0
  30. package/src/customtypes/index.ts +4 -0
  31. package/src/customtypes/widgets/Group.ts +30 -0
  32. package/src/customtypes/widgets/UID.ts +27 -0
  33. package/src/customtypes/widgets/Widget.ts +33 -0
  34. package/src/customtypes/widgets/WidgetTypes.ts +24 -0
  35. package/src/customtypes/widgets/index.ts +7 -0
  36. package/src/customtypes/widgets/nestable/BooleanField.ts +30 -0
  37. package/src/customtypes/widgets/nestable/Color.ts +26 -0
  38. package/src/customtypes/widgets/nestable/Date.ts +27 -0
  39. package/src/customtypes/widgets/nestable/Embed.ts +27 -0
  40. package/src/customtypes/widgets/nestable/GeoPoint.ts +25 -0
  41. package/src/customtypes/widgets/nestable/Image.ts +40 -0
  42. package/src/customtypes/widgets/nestable/IntegrationField.ts +27 -0
  43. package/src/customtypes/widgets/nestable/Link.ts +85 -0
  44. package/src/customtypes/widgets/nestable/NestableWidget.ts +39 -0
  45. package/src/customtypes/widgets/nestable/Number.ts +30 -0
  46. package/src/customtypes/widgets/nestable/Range.ts +30 -0
  47. package/src/customtypes/widgets/nestable/RichText.ts +160 -0
  48. package/src/customtypes/widgets/nestable/Select.ts +32 -0
  49. package/src/customtypes/widgets/nestable/Separator.ts +24 -0
  50. package/src/customtypes/widgets/nestable/Text.ts +27 -0
  51. package/src/customtypes/widgets/nestable/Timestamp.ts +27 -0
  52. package/src/customtypes/widgets/nestable/index.ts +15 -0
  53. package/src/customtypes/widgets/shared/ImageConstraint.ts +39 -0
  54. package/src/customtypes/widgets/shared/index.ts +1 -0
  55. package/src/customtypes/widgets/slices/CompositeSlice.ts +32 -0
  56. package/src/customtypes/widgets/slices/LegacySlice.ts +15 -0
  57. package/src/customtypes/widgets/slices/SharedSlice.ts +44 -0
  58. package/src/customtypes/widgets/slices/SharedSliceRef.ts +12 -0
  59. package/src/customtypes/widgets/slices/Slice.ts +7 -0
  60. package/src/customtypes/widgets/slices/Slices.ts +100 -0
  61. package/src/customtypes/widgets/slices/SlicesTypes.ts +6 -0
  62. package/src/customtypes/widgets/slices/index.ts +7 -0
  63. package/src/index.ts +1 -0
  64. package/src/validators/IntFromNumber.ts +24 -0
  65. package/src/validators/IntFromPixels.ts +31 -0
  66. package/src/validators/StringFromBoolean.ts +21 -0
  67. package/src/validators/StringFromNumber.ts +21 -0
  68. package/src/validators/StringOrNull.ts +3 -0
@@ -0,0 +1,7 @@
1
+ import SharedSlice from "./SharedSlice";
2
+ import CompositeSlice from "./CompositeSlice";
3
+ import LegacySlice from "./LegacySlice";
4
+ import SharedSliceRef from "./SharedSliceRef";
5
+
6
+ export type DynamicSlice = CompositeSlice | LegacySlice | SharedSliceRef
7
+ export type StaticSlice = CompositeSlice | LegacySlice | SharedSlice
@@ -0,0 +1,100 @@
1
+ import * as t from 'io-ts'
2
+ import { StringOrNull } from '../../../validators/StringOrNull'
3
+ import WidgetTypes from '../WidgetTypes'
4
+ import { Format } from '../../Format'
5
+ import LegacySlice from './LegacySlice'
6
+ import CompositeSlice from './CompositeSlice'
7
+ import SharedSliceRef from './SharedSliceRef'
8
+ import SharedSlice from './SharedSlice'
9
+ import SlicesTypes from './SlicesTypes'
10
+
11
+ const SlicesLabels = t.union([
12
+ t.record(
13
+ t.string,
14
+ t.array(
15
+ t.exact(
16
+ t.intersection([
17
+ t.type({
18
+ name: t.string
19
+ }),
20
+ t.partial({
21
+ display: t.string
22
+ })
23
+ ])
24
+ )
25
+ )
26
+ ),
27
+ t.null
28
+ ])
29
+ type SlicesLabels = t.TypeOf<typeof SlicesLabels>
30
+
31
+ export function slicesConfigReader<F extends Format>(format: F) {
32
+ return t.exact(
33
+ t.partial({
34
+ label: StringOrNull,
35
+ labels: SlicesLabels,
36
+ choices: t.record(t.string, t.union([
37
+ LegacySlice,
38
+ CompositeSlice,
39
+ (() => {
40
+ switch(format) {
41
+ case Format.Static: return SharedSlice
42
+ case Format.Dynamic: return SharedSliceRef
43
+ default: throw new Error(`Invalid Format Exception: ${format} doesn't exist`)
44
+ }
45
+ })()
46
+ ]))
47
+ })
48
+ )
49
+ }
50
+ export const StaticSlicesConfig = slicesConfigReader(Format.Static)
51
+ export type StaticSlicesConfig = t.TypeOf<typeof StaticSlicesConfig>
52
+
53
+ export const DynamicSlicesConfig = slicesConfigReader(Format.Dynamic)
54
+ export type DynamicSlicesConfig = t.TypeOf<typeof DynamicSlicesConfig>
55
+
56
+ const SlicesConfig = {
57
+ toStatic(config: DynamicSlicesConfig, sharedSlices: Map<string, SharedSlice>): StaticSlicesConfig {
58
+ const choices: {[key: string]: LegacySlice | CompositeSlice | SharedSlice } = Object.entries(config.choices || {})
59
+ .reduce((acc, [ref, slice]) => {
60
+ if(slice.type === SlicesTypes.SharedSlice) {
61
+ const sharedSlice = sharedSlices.get(ref)
62
+ if(sharedSlice) return { ...acc, [ref]: sharedSlice }
63
+ else return acc
64
+ } else {
65
+ return { ...acc, [ref]: slice }
66
+ }
67
+ }, {})
68
+
69
+ return { ...config, choices } as StaticSlicesConfig
70
+ }
71
+ }
72
+
73
+ export function slicesReader<F extends Format>(format: F) {
74
+ return t.exact(
75
+ t.intersection([
76
+ t.type({
77
+ type: t.union([t.literal(WidgetTypes.Slices), t.literal(WidgetTypes.LegacySlices)])
78
+ }),
79
+ t.partial({
80
+ fieldset: StringOrNull,
81
+ config: slicesConfigReader(format)
82
+ })
83
+ ])
84
+ )
85
+ }
86
+
87
+ export const StaticSlices = slicesReader(Format.Static)
88
+ export type StaticSlices = t.TypeOf<typeof StaticSlices>
89
+
90
+ export const DynamicSlices = slicesReader(Format.Dynamic)
91
+ export type DynamicSlices = t.TypeOf<typeof DynamicSlices>
92
+
93
+ export const Slices = {
94
+ toStatic(slices: DynamicSlices, sharedSlices: Map<string, SharedSlice>): StaticSlices {
95
+ if(!slices.config) return slices as StaticSlices
96
+ else {
97
+ return { ...slices, config: SlicesConfig.toStatic(slices.config, sharedSlices) }
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,6 @@
1
+ enum SlicesTypes {
2
+ Slice = 'Slice',
3
+ SharedSlice = 'SharedSlice'
4
+ }
5
+
6
+ export default SlicesTypes
@@ -0,0 +1,7 @@
1
+ export { default as CompositeSlice } from './CompositeSlice'
2
+ export { default as LegacySlice } from './LegacySlice'
3
+ export { default as SharedSlice } from './SharedSlice'
4
+ export { default as SharedSliceRef } from './SharedSliceRef'
5
+ export { default as SlicesTypes } from './SlicesTypes'
6
+ export * as Slice from './Slice'
7
+ export * as SliceZone from './Slices'
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * as CustomTypes from './customtypes'
@@ -0,0 +1,24 @@
1
+ import * as t from 'io-ts'
2
+ import { pipe } from 'fp-ts/lib/pipeable'
3
+ import { chain } from 'fp-ts/lib/Either'
4
+
5
+ export interface IntFromNumberC extends t.Type<t.Int, number, unknown> {}
6
+
7
+ /**
8
+ * A codec that succeeds if a number can be parsed to an integer
9
+ */
10
+ export const IntFromNumber: IntFromNumberC = new t.Type<t.Int, number, unknown>(
11
+ 'IntFromNumber',
12
+ t.Int.is,
13
+ (u, c) =>
14
+ pipe(
15
+ t.number.validate(u, c),
16
+ chain(n => {
17
+ if(t.Int.is(n)) return t.success(n)
18
+ else {
19
+ return t.success(Math.round(n) as t.Int)
20
+ }
21
+ })
22
+ ),
23
+ t.Int.encode
24
+ )
@@ -0,0 +1,31 @@
1
+ import * as t from 'io-ts'
2
+ import { pipe } from 'fp-ts/lib/pipeable'
3
+ import { chain } from 'fp-ts/lib/Either'
4
+
5
+ export interface IntFromPixelsC extends t.Type<t.Int, string, unknown> {}
6
+
7
+ const PixelsRegex = /^([0-9]+)px$/
8
+ /**
9
+ * A codec that succeeds if a string representing pixels (eg: "200px") can be parsed to an integer
10
+ */
11
+ export const IntFromPixels: IntFromPixelsC = new t.Type<t.Int, string, unknown>(
12
+ 'IntFromPixels',
13
+ t.Int.is,
14
+ (u, c) =>
15
+ pipe(
16
+ t.string.validate(u, c),
17
+ chain(strPixels => {
18
+ try {
19
+ const matched = strPixels.match(PixelsRegex)
20
+ if(!matched) return t.failure(u, c)
21
+ else {
22
+ const parsed = parseInt(matched[1]) as t.Int
23
+ return t.success(parsed)
24
+ }
25
+ } catch {
26
+ return t.failure(u, c)
27
+ }
28
+ })
29
+ ),
30
+ String
31
+ )
@@ -0,0 +1,21 @@
1
+ import * as t from 'io-ts'
2
+ import { pipe } from 'fp-ts/lib/pipeable'
3
+ import { chain } from 'fp-ts/lib/Either'
4
+
5
+ export interface StringFromBooleanC extends t.Type<string, string, unknown> {}
6
+
7
+ /**
8
+ * A codec that validates a Boolean and convert it as a string
9
+ */
10
+ export const StringFromBoolean: StringFromBooleanC = new t.Type<string, string, unknown>(
11
+ 'StringFromInt',
12
+ t.string.is,
13
+ (u, c) =>
14
+ pipe(
15
+ t.boolean.validate(u, c),
16
+ chain(i => {
17
+ return t.success(i.toString())
18
+ })
19
+ ),
20
+ i => i
21
+ )
@@ -0,0 +1,21 @@
1
+ import * as t from 'io-ts'
2
+ import { pipe } from 'fp-ts/lib/pipeable'
3
+ import { chain } from 'fp-ts/lib/Either'
4
+
5
+ export interface StringFromNumberC extends t.Type<string, string, unknown> {}
6
+
7
+ /**
8
+ * A codec that validates a number and convert it as a string
9
+ */
10
+ export const StringFromNumber: StringFromNumberC = new t.Type<string, string, unknown>(
11
+ 'StringFromInt',
12
+ t.string.is,
13
+ (u, c) =>
14
+ pipe(
15
+ t.number.validate(u, c),
16
+ chain(i => {
17
+ return t.success(i.toString())
18
+ })
19
+ ),
20
+ i => i
21
+ )
@@ -0,0 +1,3 @@
1
+ import * as t from 'io-ts'
2
+
3
+ export const StringOrNull = t.union([ t.string, t.null, t.undefined ])