@prismicio/types-internal 0.2.6 → 0.2.9

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.
@@ -1,17 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Slices = exports.DynamicSlices = exports.StaticSlices = exports.slicesReader = exports.DynamicSlicesConfig = exports.StaticSlicesConfig = exports.slicesConfigReader = void 0;
3
+ exports.Slices = exports.DynamicSlices = exports.StaticSlices = exports.slicesReader = exports.DynamicSlicesConfig = exports.StaticSlicesConfig = exports.slicesConfigReader = exports.SlicesLabels = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const t = (0, tslib_1.__importStar)(require("io-ts"));
6
6
  const validators_1 = require("../../../validators");
7
- const Format_1 = require("../../Format");
8
7
  const WidgetTypes_1 = (0, tslib_1.__importDefault)(require("../WidgetTypes"));
9
8
  const CompositeSlice_1 = (0, tslib_1.__importDefault)(require("./CompositeSlice"));
10
9
  const LegacySlice_1 = (0, tslib_1.__importDefault)(require("./LegacySlice"));
11
10
  const SharedSlice_1 = require("./SharedSlice");
12
11
  const SharedSliceRef_1 = (0, tslib_1.__importDefault)(require("./SharedSliceRef"));
13
12
  const SlicesTypes_1 = (0, tslib_1.__importDefault)(require("./SlicesTypes"));
14
- const SlicesLabels = t.union([
13
+ exports.SlicesLabels = t.union([
15
14
  t.record(t.string, t.array(t.exact(t.intersection([
16
15
  t.type({
17
16
  name: t.string,
@@ -22,29 +21,16 @@ const SlicesLabels = t.union([
22
21
  ])))),
23
22
  t.null,
24
23
  ]);
25
- function slicesConfigReader(format) {
24
+ function slicesConfigReader(codec) {
26
25
  return t.exact(t.partial({
27
26
  label: validators_1.StringOrNull,
28
- labels: SlicesLabels,
29
- choices: t.record(t.string, t.union([
30
- LegacySlice_1.default,
31
- CompositeSlice_1.default,
32
- (() => {
33
- switch (format) {
34
- case Format_1.Format.Static:
35
- return SharedSlice_1.SharedSlice;
36
- case Format_1.Format.Dynamic:
37
- return SharedSliceRef_1.default;
38
- default:
39
- throw new Error(`Invalid Format Exception: ${format} doesn't exist`);
40
- }
41
- })(),
42
- ])),
27
+ labels: exports.SlicesLabels,
28
+ choices: t.record(t.string, t.union([LegacySlice_1.default, CompositeSlice_1.default, codec])),
43
29
  }));
44
30
  }
45
31
  exports.slicesConfigReader = slicesConfigReader;
46
- exports.StaticSlicesConfig = slicesConfigReader(Format_1.Format.Static);
47
- exports.DynamicSlicesConfig = slicesConfigReader(Format_1.Format.Dynamic);
32
+ exports.StaticSlicesConfig = slicesConfigReader(SharedSlice_1.SharedSlice);
33
+ exports.DynamicSlicesConfig = slicesConfigReader(SharedSliceRef_1.default);
48
34
  const SlicesConfig = {
49
35
  toStatic(config, sharedSlices) {
50
36
  const choices = Object.entries(config.choices || {}).reduce((acc, [ref, slice]) => {
@@ -62,7 +48,7 @@ const SlicesConfig = {
62
48
  return { ...config, choices };
63
49
  },
64
50
  };
65
- function slicesReader(format) {
51
+ function slicesReader(codec) {
66
52
  return t.exact(t.intersection([
67
53
  t.type({
68
54
  type: t.union([
@@ -72,13 +58,13 @@ function slicesReader(format) {
72
58
  }),
73
59
  t.partial({
74
60
  fieldset: validators_1.StringOrNull,
75
- config: slicesConfigReader(format),
61
+ config: codec,
76
62
  }),
77
63
  ]));
78
64
  }
79
65
  exports.slicesReader = slicesReader;
80
- exports.StaticSlices = slicesReader(Format_1.Format.Static);
81
- exports.DynamicSlices = slicesReader(Format_1.Format.Dynamic);
66
+ exports.StaticSlices = slicesReader(exports.StaticSlicesConfig);
67
+ exports.DynamicSlices = slicesReader(exports.DynamicSlicesConfig);
82
68
  exports.Slices = {
83
69
  toStatic(slices, sharedSlices) {
84
70
  if (!slices.config)
@@ -4,5 +4,14 @@ declare type DocumentData = Map<WidgetKey, O.Option<WidgetContent>>;
4
4
  declare const DocumentData: {
5
5
  getWidget(key: WidgetKey, widgets: Map<WidgetKey, unknown>, widgetTypes: Map<WidgetKey, string>, widgetPositions: Map<WidgetKey, number>, parsed: Map<string, O.Option<WidgetContent>>): O.Option<WidgetContent>;
6
6
  fromJson(widgets: Map<WidgetKey, unknown>, widgetTypes: Map<WidgetKey, string>, widgetPositions: Map<WidgetKey, number>): DocumentData;
7
+ partitionData(data: {
8
+ [p: string]: unknown;
9
+ }): {
10
+ types: Map<string, string>;
11
+ positions: Map<string, number>;
12
+ widgets: Map<WidgetKey, unknown>;
13
+ slugs: ReadonlyArray<string>;
14
+ uid: string | undefined;
15
+ };
7
16
  };
8
17
  export default DocumentData;
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
+ const Either_1 = require("fp-ts/lib/Either");
4
5
  const O = (0, tslib_1.__importStar)(require("fp-ts/lib/Option"));
6
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
5
7
  const widgets_1 = require("./widgets");
6
8
  function computeWidget(key, widgets, widgetTypes, widgetPositions) {
7
9
  const widget = widgets.get(key);
@@ -24,5 +26,45 @@ const DocumentData = {
24
26
  return new Map([...Array.from(acc.entries()), [widgetKey, parsedWidget]]);
25
27
  }, new Map());
26
28
  },
29
+ partitionData(data) {
30
+ const fields = Object.entries(data);
31
+ const { types, positions, widgets } = fields.reduce((acc, [k, v]) => {
32
+ if (k.endsWith("_TYPE")) {
33
+ const decodedValue = t.string.decode(v);
34
+ if ((0, Either_1.isRight)(decodedValue)) {
35
+ return {
36
+ ...acc,
37
+ types: acc.types.set(k.substring(0, k.length - 5), decodedValue.right),
38
+ };
39
+ }
40
+ }
41
+ if (k.endsWith("_POSITION") && typeof v === "number") {
42
+ return {
43
+ ...acc,
44
+ positions: acc.positions.set(k.substring(0, k.length - 9), v),
45
+ };
46
+ }
47
+ if (!k.endsWith("_POSITION") && !k.endsWith("_TYPE")) {
48
+ return {
49
+ ...acc,
50
+ widgets: acc.widgets.set(k, v),
51
+ };
52
+ }
53
+ return acc;
54
+ }, {
55
+ types: new Map(),
56
+ positions: new Map(),
57
+ widgets: new Map(),
58
+ });
59
+ const slugs = data["slugs_INTERNAL"] || [];
60
+ const uid = data["uid"];
61
+ return {
62
+ widgets,
63
+ types,
64
+ positions,
65
+ uid,
66
+ slugs,
67
+ };
68
+ },
27
69
  };
28
70
  exports.default = DocumentData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "0.2.6",
3
+ "version": "0.2.9",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -3,13 +3,7 @@ import * as t from "io-ts"
3
3
  import { withFallback } from "io-ts-types/lib/withFallback"
4
4
 
5
5
  import { StringOrNull } from "../validators"
6
- import { Format } from "./Format"
7
- import {
8
- DynamicSection,
9
- sectionReader,
10
- Sections,
11
- StaticSection,
12
- } from "./Section"
6
+ import { DynamicSection, Sections, StaticSection } from "./Section"
13
7
  import type { SharedSlice } from "./widgets/slices/SharedSlice"
14
8
  import type { DynamicSlice } from "./widgets/slices/Slice"
15
9
  import type { DynamicSlices } from "./widgets/slices/Slices"
@@ -35,14 +29,16 @@ ${slicesMsg.join("\n")}
35
29
  }
36
30
  }
37
31
 
38
- function customTypeReader<F extends Format>(format: F) {
32
+ function customTypeReader<T extends StaticSection | DynamicSection>(
33
+ codec: t.Type<T, unknown>,
34
+ ) {
39
35
  return t.exact(
40
36
  t.intersection([
41
37
  t.type({
42
38
  id: t.string,
43
39
  label: StringOrNull,
44
40
  repeatable: withFallback(t.boolean, true),
45
- json: t.record(t.string, sectionReader(format)),
41
+ json: t.record(t.string, codec),
46
42
  status: withFallback(t.boolean, true),
47
43
  }),
48
44
  t.partial({
@@ -52,10 +48,10 @@ function customTypeReader<F extends Format>(format: F) {
52
48
  )
53
49
  }
54
50
 
55
- export const StaticCustomType = customTypeReader(Format.Static)
51
+ export const StaticCustomType = customTypeReader(StaticSection)
56
52
  export type StaticCustomType = t.TypeOf<typeof StaticCustomType>
57
53
 
58
- export const CustomType = customTypeReader(Format.Dynamic)
54
+ export const CustomType = customTypeReader(DynamicSection)
59
55
  export type CustomType = t.TypeOf<typeof CustomType>
60
56
 
61
57
  export function flattenWidgets(
@@ -1,22 +1,18 @@
1
1
  import * as t from "io-ts"
2
2
 
3
- import { Format } from "./Format"
4
3
  import type { SharedSlice } from "./widgets/slices/SharedSlice"
5
- import {
6
- DynamicWidget,
7
- StaticWidget,
8
- widgetReader,
9
- Widgets,
10
- } from "./widgets/Widget"
4
+ import { DynamicWidget, StaticWidget, Widgets } from "./widgets/Widget"
11
5
 
12
- export function sectionReader<F extends Format>(format: F) {
13
- return t.record(t.string, widgetReader(format))
6
+ export function sectionReader<T extends StaticWidget | DynamicWidget>(
7
+ codec: t.Type<T, unknown>,
8
+ ) {
9
+ return t.record(t.string, codec)
14
10
  }
15
11
 
16
- export const StaticSection = sectionReader(Format.Static)
12
+ export const StaticSection = sectionReader(StaticWidget)
17
13
  export type StaticSection = t.TypeOf<typeof StaticSection>
18
14
 
19
- export const DynamicSection = sectionReader(Format.Dynamic)
15
+ export const DynamicSection = sectionReader(DynamicWidget)
20
16
  export type DynamicSection = t.TypeOf<typeof DynamicSection>
21
17
 
22
18
  export const Sections = {
@@ -1,4 +1,3 @@
1
1
  export * from "./CustomType"
2
- export { Format } from "./Format"
3
2
  export * as Section from "./Section"
4
3
  export * as Widgets from "./widgets"
@@ -1,20 +1,22 @@
1
1
  import * as t from "io-ts"
2
2
 
3
- import { Format } from "../Format"
4
3
  import Group from "./Group"
5
4
  import NestableWidget from "./nestable/NestableWidget"
6
5
  import type { SharedSlice } from "./slices/SharedSlice"
7
- import { Slices, slicesReader } from "./slices/Slices"
6
+ import { DynamicSlices, Slices, StaticSlices } from "./slices/Slices"
8
7
  import UID from "./UID"
9
8
  import WidgetTypes from "./WidgetTypes"
10
9
 
11
- export function widgetReader<F extends Format>(format: F) {
12
- return t.union([UID, NestableWidget, Group, slicesReader(format)])
10
+ export function widgetReader<T extends StaticSlices | DynamicSlices>(
11
+ codec: t.Type<T, unknown>,
12
+ ) {
13
+ return t.union([UID, NestableWidget, Group, codec])
13
14
  }
14
- export const StaticWidget = widgetReader(Format.Static)
15
+
16
+ export const StaticWidget = widgetReader(StaticSlices)
15
17
  export type StaticWidget = t.TypeOf<typeof StaticWidget>
16
18
 
17
- export const DynamicWidget = widgetReader(Format.Dynamic)
19
+ export const DynamicWidget = widgetReader(DynamicSlices)
18
20
  export type DynamicWidget = t.TypeOf<typeof DynamicWidget>
19
21
 
20
22
  export const Widgets = {
@@ -23,6 +25,7 @@ export const Widgets = {
23
25
  sharedSlices: Map<string, SharedSlice>,
24
26
  ): StaticWidget {
25
27
  switch (widget.type) {
28
+ case WidgetTypes.LegacySlices:
26
29
  case WidgetTypes.Slices:
27
30
  return Slices.toStatic(widget, sharedSlices)
28
31
  default:
@@ -202,7 +202,7 @@ const RichTextLabels = new t.Type<Array<string>, object, unknown>(
202
202
  (res) => res,
203
203
  )
204
204
 
205
- const RichTextConfig = t.exact(
205
+ export const RichTextConfig = t.exact(
206
206
  t.partial({
207
207
  label: StringOrNull,
208
208
  placeholder: t.string,
@@ -214,7 +214,7 @@ const RichTextConfig = t.exact(
214
214
  allowTargetBlank: t.boolean,
215
215
  }),
216
216
  )
217
- type RichTextConfig = t.TypeOf<typeof RichTextConfig>
217
+ export type RichTextConfig = t.TypeOf<typeof RichTextConfig>
218
218
 
219
219
  export const RichText = t.exact(
220
220
  t.intersection([
@@ -1,7 +1,6 @@
1
1
  import * as t from "io-ts"
2
2
 
3
3
  import { StringOrNull } from "../../../validators"
4
- import { Format } from "../../Format"
5
4
  import WidgetTypes from "../WidgetTypes"
6
5
  import CompositeSlice from "./CompositeSlice"
7
6
  import LegacySlice from "./LegacySlice"
@@ -9,7 +8,7 @@ import { SharedSlice } from "./SharedSlice"
9
8
  import SharedSliceRef from "./SharedSliceRef"
10
9
  import SlicesTypes from "./SlicesTypes"
11
10
 
12
- const SlicesLabels = t.union([
11
+ export const SlicesLabels = t.union([
13
12
  t.record(
14
13
  t.string,
15
14
  t.array(
@@ -27,39 +26,26 @@ const SlicesLabels = t.union([
27
26
  ),
28
27
  t.null,
29
28
  ])
30
- type SlicesLabels = t.TypeOf<typeof SlicesLabels>
29
+ export type SlicesLabels = t.TypeOf<typeof SlicesLabels>
31
30
 
32
- export function slicesConfigReader<F extends Format>(format: F) {
31
+ export function slicesConfigReader<T extends SharedSlice | SharedSliceRef>(
32
+ codec: t.Type<T, unknown>,
33
+ ) {
33
34
  return t.exact(
34
35
  t.partial({
35
36
  label: StringOrNull,
36
37
  labels: SlicesLabels,
37
38
  choices: t.record(
38
39
  t.string,
39
- t.union([
40
- LegacySlice,
41
- CompositeSlice,
42
- (() => {
43
- switch (format) {
44
- case Format.Static:
45
- return SharedSlice
46
- case Format.Dynamic:
47
- return SharedSliceRef
48
- default:
49
- throw new Error(
50
- `Invalid Format Exception: ${format} doesn't exist`,
51
- )
52
- }
53
- })(),
54
- ]),
40
+ t.union([LegacySlice, CompositeSlice, codec]),
55
41
  ),
56
42
  }),
57
43
  )
58
44
  }
59
- export const StaticSlicesConfig = slicesConfigReader(Format.Static)
45
+ export const StaticSlicesConfig = slicesConfigReader(SharedSlice)
60
46
  export type StaticSlicesConfig = t.TypeOf<typeof StaticSlicesConfig>
61
47
 
62
- export const DynamicSlicesConfig = slicesConfigReader(Format.Dynamic)
48
+ export const DynamicSlicesConfig = slicesConfigReader(SharedSliceRef)
63
49
  export type DynamicSlicesConfig = t.TypeOf<typeof DynamicSlicesConfig>
64
50
 
65
51
  const SlicesConfig = {
@@ -83,7 +69,9 @@ const SlicesConfig = {
83
69
  },
84
70
  }
85
71
 
86
- export function slicesReader<F extends Format>(format: F) {
72
+ export function slicesReader<
73
+ T extends StaticSlicesConfig | DynamicSlicesConfig,
74
+ >(codec: t.Type<T, unknown>) {
87
75
  return t.exact(
88
76
  t.intersection([
89
77
  t.type({
@@ -94,16 +82,16 @@ export function slicesReader<F extends Format>(format: F) {
94
82
  }),
95
83
  t.partial({
96
84
  fieldset: StringOrNull,
97
- config: slicesConfigReader(format),
85
+ config: codec,
98
86
  }),
99
87
  ]),
100
88
  )
101
89
  }
102
90
 
103
- export const StaticSlices = slicesReader(Format.Static)
91
+ export const StaticSlices = slicesReader(StaticSlicesConfig)
104
92
  export type StaticSlices = t.TypeOf<typeof StaticSlices>
105
93
 
106
- export const DynamicSlices = slicesReader(Format.Dynamic)
94
+ export const DynamicSlices = slicesReader(DynamicSlicesConfig)
107
95
  export type DynamicSlices = t.TypeOf<typeof DynamicSlices>
108
96
 
109
97
  export const Slices = {
@@ -1,4 +1,6 @@
1
+ import { isRight } from "fp-ts/lib/Either"
1
2
  import * as O from "fp-ts/lib/Option"
3
+ import * as t from "io-ts"
2
4
 
3
5
  import { type WidgetContent, type WidgetKey, Widget } from "./widgets"
4
6
 
@@ -49,6 +51,62 @@ const DocumentData = {
49
51
  return new Map([...Array.from(acc.entries()), [widgetKey, parsedWidget]])
50
52
  }, new Map<WidgetKey, O.Option<WidgetContent>>())
51
53
  },
54
+
55
+ partitionData(data: { [p: string]: unknown }): {
56
+ types: Map<string, string>
57
+ positions: Map<string, number>
58
+ widgets: Map<WidgetKey, unknown>
59
+ slugs: ReadonlyArray<string>
60
+ uid: string | undefined
61
+ } {
62
+ const fields: [string, unknown][] = Object.entries(data)
63
+
64
+ const { types, positions, widgets } = fields.reduce(
65
+ (acc, [k, v]) => {
66
+ if (k.endsWith("_TYPE")) {
67
+ const decodedValue = t.string.decode(v)
68
+ if (isRight(decodedValue)) {
69
+ return {
70
+ ...acc,
71
+ types: acc.types.set(
72
+ k.substring(0, k.length - 5),
73
+ decodedValue.right,
74
+ ),
75
+ }
76
+ }
77
+ }
78
+ if (k.endsWith("_POSITION") && typeof v === "number") {
79
+ return {
80
+ ...acc,
81
+ positions: acc.positions.set(k.substring(0, k.length - 9), v),
82
+ }
83
+ }
84
+ if (!k.endsWith("_POSITION") && !k.endsWith("_TYPE")) {
85
+ return {
86
+ ...acc,
87
+ widgets: acc.widgets.set(k, v),
88
+ }
89
+ }
90
+ return acc
91
+ },
92
+ {
93
+ types: new Map<string, string>(),
94
+ positions: new Map<string, number>(),
95
+ widgets: new Map<string, unknown>(),
96
+ },
97
+ )
98
+
99
+ const slugs = (data["slugs_INTERNAL"] as string[]) || []
100
+ const uid = data["uid"] as string | undefined
101
+
102
+ return {
103
+ widgets,
104
+ types,
105
+ positions,
106
+ uid,
107
+ slugs,
108
+ }
109
+ },
52
110
  }
53
111
 
54
112
  export default DocumentData
@@ -1,4 +0,0 @@
1
- export enum Format {
2
- Static = "static",
3
- Dynamic = "dynamic",
4
- }