@prismicio/types-internal 3.3.0 → 3.4.0-alpha.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.
@@ -1,4 +1,5 @@
1
1
  import * as t from "io-ts";
2
+ import { Date as DateField } from "../../../../customtypes/widgets";
2
3
  import type { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx";
3
4
  export declare const isDateContent: (u: unknown) => u is {
4
5
  type: "Date";
@@ -16,3 +17,4 @@ export declare const DateContent: t.ExactC<t.TypeC<{
16
17
  __TYPE__: t.LiteralC<"FieldContent">;
17
18
  }>>;
18
19
  export declare type DateContent = t.TypeOf<typeof DateContent>;
20
+ export declare const DateContentDefaultValue: (field: DateField) => DateContent | undefined;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DateContent = exports.DateLegacy = exports.isDateContent = void 0;
3
+ exports.DateContentDefaultValue = exports.DateContent = exports.DateLegacy = exports.isDateContent = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
8
  const widgets_1 = require("../../../../customtypes/widgets");
9
+ const Date_1 = require("../../../../utils/Date");
9
10
  const TrimmedString_1 = (0, tslib_1.__importDefault)(require("../../../../validators/TrimmedString"));
10
11
  const utils_1 = require("../../../utils");
11
12
  const common_1 = require("./common");
@@ -32,3 +33,27 @@ exports.DateContent = t.strict({
32
33
  value: TrimmedString_1.default,
33
34
  __TYPE__: t.literal(common_1.FieldContentType),
34
35
  });
36
+ const DateContentDefaultValue = (field) => {
37
+ var _a;
38
+ const defaultDateString = (_a = field.config) === null || _a === void 0 ? void 0 : _a.default;
39
+ if (!defaultDateString) {
40
+ return undefined;
41
+ }
42
+ if (defaultDateString === "now") {
43
+ return {
44
+ __TYPE__: common_1.FieldContentType,
45
+ type: widgets_1.DateFieldType,
46
+ value: (0, Date_1.toPrismicDateValue)(new Date()),
47
+ };
48
+ }
49
+ const date = (0, Date_1.dateFromISOString)(defaultDateString);
50
+ if (!date) {
51
+ return undefined;
52
+ }
53
+ return {
54
+ __TYPE__: common_1.FieldContentType,
55
+ type: widgets_1.DateFieldType,
56
+ value: (0, Date_1.toPrismicDateValue)(date),
57
+ };
58
+ };
59
+ exports.DateContentDefaultValue = DateContentDefaultValue;
@@ -1,4 +1,5 @@
1
1
  import * as t from "io-ts";
2
+ import { Timestamp } from "../../../../customtypes/widgets";
2
3
  import type { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx";
3
4
  export declare const isTimestampContent: (u: unknown) => u is {
4
5
  type: "Timestamp";
@@ -16,3 +17,4 @@ export declare const TimestampContent: t.ExactC<t.TypeC<{
16
17
  __TYPE__: t.LiteralC<"FieldContent">;
17
18
  }>>;
18
19
  export declare type TimestampContent = t.TypeOf<typeof TimestampContent>;
20
+ export declare const TimestampContentDefaultValue: (field: Timestamp) => TimestampContent | undefined;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TimestampContent = exports.TimestampLegacy = exports.isTimestampContent = void 0;
3
+ exports.TimestampContentDefaultValue = exports.TimestampContent = exports.TimestampLegacy = exports.isTimestampContent = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
8
  const widgets_1 = require("../../../../customtypes/widgets");
9
+ const Date_1 = require("../../../../utils/Date");
9
10
  const TrimmedString_1 = (0, tslib_1.__importDefault)(require("../../../../validators/TrimmedString"));
10
11
  const utils_1 = require("../../../utils");
11
12
  const common_1 = require("./common");
@@ -32,3 +33,27 @@ exports.TimestampContent = t.strict({
32
33
  value: TrimmedString_1.default,
33
34
  __TYPE__: t.literal(common_1.FieldContentType),
34
35
  });
36
+ const TimestampContentDefaultValue = (field) => {
37
+ var _a;
38
+ const defaultDateString = (_a = field.config) === null || _a === void 0 ? void 0 : _a.default;
39
+ if (!defaultDateString) {
40
+ return undefined;
41
+ }
42
+ if (defaultDateString === "now") {
43
+ return {
44
+ __TYPE__: common_1.FieldContentType,
45
+ type: widgets_1.TimestampFieldType,
46
+ value: (0, Date_1.toPrismicTimestampValue)(new Date()),
47
+ };
48
+ }
49
+ const date = (0, Date_1.dateFromISOString)(defaultDateString);
50
+ if (!date) {
51
+ return undefined;
52
+ }
53
+ return {
54
+ __TYPE__: common_1.FieldContentType,
55
+ type: widgets_1.TimestampFieldType,
56
+ value: (0, Date_1.toPrismicTimestampValue)(date),
57
+ };
58
+ };
59
+ exports.TimestampContentDefaultValue = TimestampContentDefaultValue;
@@ -39,6 +39,10 @@ const NestableContentDefaultValue = (widgetDef) => {
39
39
  switch (widgetDef.type) {
40
40
  case "Boolean":
41
41
  return (0, BooleanContent_1.BooleanContentDefaultValue)(widgetDef);
42
+ case "Date":
43
+ return (0, FieldContent_1.DateContentDefaultValue)(widgetDef);
44
+ case "Timestamp":
45
+ return (0, FieldContent_1.TimestampContentDefaultValue)(widgetDef);
42
46
  case "Select":
43
47
  return (0, FieldContent_1.SelectContentDefaultValue)(widgetDef);
44
48
  default:
@@ -0,0 +1,9 @@
1
+ /** Create a valid Date from an ISO 8601 string or returns undefined. */
2
+ export declare function dateFromISOString(str: string): Date | undefined;
3
+ /**
4
+ * The Javascript Date constructor never throws an error, even when provided
5
+ * weird parameters. However, they can be internally invalid.
6
+ */
7
+ export declare function isValidDate(date: Date): boolean;
8
+ export declare function toPrismicDateValue(date: Date): string;
9
+ export declare function toPrismicTimestampValue(date: Date): string;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toPrismicTimestampValue = exports.toPrismicDateValue = exports.isValidDate = exports.dateFromISOString = void 0;
4
+ /** Create a valid Date from an ISO 8601 string or returns undefined. */
5
+ function dateFromISOString(str) {
6
+ const date = new Date(str);
7
+ return isValidDate(date) ? date : undefined;
8
+ }
9
+ exports.dateFromISOString = dateFromISOString;
10
+ /**
11
+ * The Javascript Date constructor never throws an error, even when provided
12
+ * weird parameters. However, they can be internally invalid.
13
+ */
14
+ function isValidDate(date) {
15
+ return !isNaN(date.getTime());
16
+ }
17
+ exports.isValidDate = isValidDate;
18
+ function toPrismicDateValue(date) {
19
+ const YYYY = `${date.getFullYear()}`.padStart(4, "0");
20
+ const MM = `${date.getMonth() + 1}`.padStart(2, "0");
21
+ const DD = `${date.getDate()}`.padStart(2, "0");
22
+ return `${YYYY}-${MM}-${DD}`;
23
+ }
24
+ exports.toPrismicDateValue = toPrismicDateValue;
25
+ // Historically, a prismic timestamp value is a very specific subset of ISO 8601 dates.
26
+ // It validates and reject any other valid ISO strings, like ones having a Z as a timezone, etc.
27
+ function toPrismicTimestampValue(date) {
28
+ const YYYY = `${date.getUTCFullYear()}`.padStart(4, "0");
29
+ const MM = `${date.getUTCMonth() + 1}`.padStart(2, "0");
30
+ const DD = `${date.getUTCDate()}`.padStart(2, "0");
31
+ const hh = `${date.getUTCHours()}`.padStart(2, "0");
32
+ const mm = `${date.getUTCMinutes()}`.padStart(2, "0");
33
+ return `${YYYY}-${MM}-${DD}T${hh}:${mm}:00+00:00`;
34
+ }
35
+ exports.toPrismicTimestampValue = toPrismicTimestampValue;
@@ -1,3 +1,4 @@
1
1
  export * as HexaColorCode from "../common/HexaColorCode";
2
2
  export * as Arrays from "./Arrays";
3
+ export * as Date from "./Date";
3
4
  export * as Objects from "./Objects";
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Objects = exports.Arrays = exports.HexaColorCode = void 0;
3
+ exports.Objects = exports.Date = exports.Arrays = exports.HexaColorCode = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  exports.HexaColorCode = (0, tslib_1.__importStar)(require("../common/HexaColorCode"));
6
6
  exports.Arrays = (0, tslib_1.__importStar)(require("./Arrays"));
7
+ exports.Date = (0, tslib_1.__importStar)(require("./Date"));
7
8
  exports.Objects = (0, tslib_1.__importStar)(require("./Objects"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "3.3.0",
3
+ "version": "3.4.0-alpha.0",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -2,7 +2,11 @@ import { either } from "fp-ts"
2
2
  import { pipe } from "fp-ts/lib/function"
3
3
  import * as t from "io-ts"
4
4
 
5
- import { DateFieldType } from "../../../../customtypes/widgets"
5
+ import {
6
+ Date as DateField,
7
+ DateFieldType,
8
+ } from "../../../../customtypes/widgets"
9
+ import { dateFromISOString, toPrismicDateValue } from "../../../../utils/Date"
6
10
  import TrimmedString from "../../../../validators/TrimmedString"
7
11
  import type { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx"
8
12
  import { hasContentType, hasFieldContentType } from "../../../utils"
@@ -46,3 +50,33 @@ export const DateContent = t.strict({
46
50
  })
47
51
 
48
52
  export type DateContent = t.TypeOf<typeof DateContent>
53
+
54
+ export const DateContentDefaultValue = (
55
+ field: DateField,
56
+ ): DateContent | undefined => {
57
+ const defaultDateString = field.config?.default
58
+
59
+ if (!defaultDateString) {
60
+ return undefined
61
+ }
62
+
63
+ if (defaultDateString === "now") {
64
+ return {
65
+ __TYPE__: FieldContentType,
66
+ type: DateFieldType,
67
+ value: toPrismicDateValue(new Date()),
68
+ }
69
+ }
70
+
71
+ const date = dateFromISOString(defaultDateString)
72
+
73
+ if (!date) {
74
+ return undefined
75
+ }
76
+
77
+ return {
78
+ __TYPE__: FieldContentType,
79
+ type: DateFieldType,
80
+ value: toPrismicDateValue(date),
81
+ }
82
+ }
@@ -2,7 +2,11 @@ import { either } from "fp-ts"
2
2
  import { pipe } from "fp-ts/lib/function"
3
3
  import * as t from "io-ts"
4
4
 
5
- import { TimestampFieldType } from "../../../../customtypes/widgets"
5
+ import { Timestamp, TimestampFieldType } from "../../../../customtypes/widgets"
6
+ import {
7
+ dateFromISOString,
8
+ toPrismicTimestampValue,
9
+ } from "../../../../utils/Date"
6
10
  import TrimmedString from "../../../../validators/TrimmedString"
7
11
  import type { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx"
8
12
  import { hasContentType, hasFieldContentType } from "../../../utils"
@@ -46,3 +50,33 @@ export const TimestampContent = t.strict({
46
50
  })
47
51
 
48
52
  export type TimestampContent = t.TypeOf<typeof TimestampContent>
53
+
54
+ export const TimestampContentDefaultValue = (
55
+ field: Timestamp,
56
+ ): TimestampContent | undefined => {
57
+ const defaultDateString = field.config?.default
58
+
59
+ if (!defaultDateString) {
60
+ return undefined
61
+ }
62
+
63
+ if (defaultDateString === "now") {
64
+ return {
65
+ __TYPE__: FieldContentType,
66
+ type: TimestampFieldType,
67
+ value: toPrismicTimestampValue(new Date()),
68
+ }
69
+ }
70
+
71
+ const date = dateFromISOString(defaultDateString)
72
+
73
+ if (!date) {
74
+ return undefined
75
+ }
76
+
77
+ return {
78
+ __TYPE__: FieldContentType,
79
+ type: TimestampFieldType,
80
+ value: toPrismicTimestampValue(date),
81
+ }
82
+ }
@@ -21,6 +21,7 @@ import {
21
21
  ColorContent,
22
22
  ColorLegacy,
23
23
  DateContent,
24
+ DateContentDefaultValue,
24
25
  DateLegacy,
25
26
  isColorContent,
26
27
  isDateContent,
@@ -39,6 +40,7 @@ import {
39
40
  TextContent,
40
41
  TextLegacy,
41
42
  TimestampContent,
43
+ TimestampContentDefaultValue,
42
44
  TimestampLegacy,
43
45
  } from "./FieldContent"
44
46
  import { FieldContentType } from "./FieldContent/common"
@@ -112,6 +114,10 @@ export const NestableContentDefaultValue = (
112
114
  switch (widgetDef.type) {
113
115
  case "Boolean":
114
116
  return BooleanContentDefaultValue(widgetDef)
117
+ case "Date":
118
+ return DateContentDefaultValue(widgetDef)
119
+ case "Timestamp":
120
+ return TimestampContentDefaultValue(widgetDef)
115
121
  case "Select":
116
122
  return SelectContentDefaultValue(widgetDef)
117
123
  default:
@@ -0,0 +1,33 @@
1
+ /** Create a valid Date from an ISO 8601 string or returns undefined. */
2
+ export function dateFromISOString(str: string): Date | undefined {
3
+ const date = new Date(str)
4
+ return isValidDate(date) ? date : undefined
5
+ }
6
+
7
+ /**
8
+ * The Javascript Date constructor never throws an error, even when provided
9
+ * weird parameters. However, they can be internally invalid.
10
+ */
11
+ export function isValidDate(date: Date) {
12
+ return !isNaN(date.getTime())
13
+ }
14
+
15
+ export function toPrismicDateValue(date: Date): string {
16
+ const YYYY = `${date.getFullYear()}`.padStart(4, "0")
17
+ const MM = `${date.getMonth() + 1}`.padStart(2, "0")
18
+ const DD = `${date.getDate()}`.padStart(2, "0")
19
+
20
+ return `${YYYY}-${MM}-${DD}`
21
+ }
22
+
23
+ // Historically, a prismic timestamp value is a very specific subset of ISO 8601 dates.
24
+ // It validates and reject any other valid ISO strings, like ones having a Z as a timezone, etc.
25
+ export function toPrismicTimestampValue(date: Date): string {
26
+ const YYYY = `${date.getUTCFullYear()}`.padStart(4, "0")
27
+ const MM = `${date.getUTCMonth() + 1}`.padStart(2, "0")
28
+ const DD = `${date.getUTCDate()}`.padStart(2, "0")
29
+ const hh = `${date.getUTCHours()}`.padStart(2, "0")
30
+ const mm = `${date.getUTCMinutes()}`.padStart(2, "0")
31
+
32
+ return `${YYYY}-${MM}-${DD}T${hh}:${mm}:00+00:00`
33
+ }
@@ -1,3 +1,4 @@
1
1
  export * as HexaColorCode from "../common/HexaColorCode"
2
2
  export * as Arrays from "./Arrays"
3
+ export * as Date from "./Date"
3
4
  export * as Objects from "./Objects"
@@ -1,162 +0,0 @@
1
- import * as t from "io-ts";
2
- import type { ContentPath, TraverseWidgetContentFn } from "../../_internal/utils";
3
- import type { Link, NestableWidget } from "../../customtypes";
4
- import type { LegacyContentCtx, WithTypes } from "../LegacyContentCtx";
5
- export declare const RepeatableContent: t.ExactC<t.TypeC<{
6
- __TYPE__: t.LiteralC<"RepeatableContent">;
7
- type: t.LiteralC<"Link">;
8
- value: t.ArrayC<t.ExactC<t.TypeC<{
9
- __TYPE__: t.LiteralC<"LinkContent">;
10
- value: t.UnionC<[t.IntersectionC<[t.ExactC<t.TypeC<{
11
- __TYPE__: t.LiteralC<"ImageLink">;
12
- }>>, t.UnionC<[t.IntersectionC<[t.IntersectionC<[t.ExactC<t.TypeC<{
13
- kind: t.StringC;
14
- id: t.StringC;
15
- url: t.StringC;
16
- height: t.StringC;
17
- width: t.StringC;
18
- size: t.StringC;
19
- name: t.StringC;
20
- }>>, t.ExactC<t.PartialC<{
21
- date: t.UnionC<[t.Type<string, string, unknown>, t.NullC, t.UndefinedC]>;
22
- }>>]>, t.ExactC<t.PartialC<{
23
- text: t.StringC;
24
- }>>]>, t.ExactC<t.TypeC<{
25
- kind: t.LiteralC<"image">;
26
- text: t.StringC;
27
- }>>]>]>, t.IntersectionC<[t.ExactC<t.TypeC<{
28
- __TYPE__: t.LiteralC<"FileLink">;
29
- }>>, t.UnionC<[t.IntersectionC<[t.ExactC<t.IntersectionC<[t.TypeC<{
30
- kind: t.StringC;
31
- id: t.StringC;
32
- url: t.StringC;
33
- name: t.StringC;
34
- size: t.StringC;
35
- }>, t.PartialC<{
36
- date: t.UnionC<[t.Type<string, string, unknown>, t.NullC, t.UndefinedC]>;
37
- }>]>>, t.ExactC<t.PartialC<{
38
- text: t.StringC;
39
- }>>]>, t.ExactC<t.TypeC<{
40
- kind: t.LiteralC<"file">;
41
- text: t.StringC;
42
- }>>]>]>, t.IntersectionC<[t.ExactC<t.TypeC<{
43
- __TYPE__: t.LiteralC<"DocumentLink">;
44
- }>>, t.UnionC<[t.IntersectionC<[t.ExactC<t.TypeC<{
45
- id: t.Type<string, string, unknown>;
46
- }>>, t.ExactC<t.PartialC<{
47
- text: t.StringC;
48
- }>>]>, t.ExactC<t.TypeC<{
49
- kind: t.LiteralC<"document">;
50
- text: t.StringC;
51
- }>>]>]>, t.IntersectionC<[t.ExactC<t.TypeC<{
52
- __TYPE__: t.LiteralC<"ExternalLink">;
53
- }>>, t.UnionC<[t.IntersectionC<[t.ExactC<t.IntersectionC<[t.TypeC<{
54
- url: t.StringC;
55
- }>, t.PartialC<{
56
- kind: t.LiteralC<"web">;
57
- target: t.UnionC<[t.Type<string, string, unknown>, t.NullC, t.UndefinedC]>;
58
- preview: t.UnionC<[t.Type<{
59
- title?: string;
60
- }, {
61
- title?: string;
62
- }, unknown>, t.NullC, t.UndefinedC]>;
63
- }>]>>, t.ExactC<t.PartialC<{
64
- text: t.StringC;
65
- }>>]>, t.ExactC<t.TypeC<{
66
- kind: t.LiteralC<"web">;
67
- text: t.StringC;
68
- }>>]>]>, t.IntersectionC<[t.ExactC<t.TypeC<{
69
- __TYPE__: t.LiteralC<"MediaLink">;
70
- }>>, t.ExactC<t.TypeC<{
71
- kind: t.LiteralC<"media">;
72
- text: t.StringC;
73
- }>>]>, t.IntersectionC<[t.ExactC<t.TypeC<{
74
- __TYPE__: t.LiteralC<"AnyLink">;
75
- }>>, t.ExactC<t.TypeC<{
76
- text: t.StringC;
77
- }>>]>]>;
78
- }>>>;
79
- }>>;
80
- export declare type RepeatableContent = t.TypeOf<typeof RepeatableContent>;
81
- export declare const isRepeatableContent: t.Is<{
82
- __TYPE__: "RepeatableContent";
83
- type: "Link";
84
- value: {
85
- __TYPE__: "LinkContent";
86
- value: ({
87
- __TYPE__: "ImageLink";
88
- } & (({
89
- kind: string;
90
- id: string;
91
- url: string;
92
- height: string;
93
- width: string;
94
- size: string;
95
- name: string;
96
- } & {
97
- date?: string | null | undefined;
98
- } & {
99
- text?: string;
100
- }) | {
101
- kind: "image";
102
- text: string;
103
- })) | ({
104
- __TYPE__: "FileLink";
105
- } & (({
106
- kind: string;
107
- id: string;
108
- url: string;
109
- name: string;
110
- size: string;
111
- } & {
112
- date?: string | null | undefined;
113
- } & {
114
- text?: string;
115
- }) | {
116
- kind: "file";
117
- text: string;
118
- })) | ({
119
- __TYPE__: "MediaLink";
120
- } & {
121
- kind: "media";
122
- text: string;
123
- }) | ({
124
- __TYPE__: "DocumentLink";
125
- } & (({
126
- id: string;
127
- } & {
128
- text?: string;
129
- }) | {
130
- kind: "document";
131
- text: string;
132
- })) | ({
133
- __TYPE__: "ExternalLink";
134
- } & (({
135
- url: string;
136
- } & {
137
- kind?: "web";
138
- target?: string | null | undefined;
139
- preview?: {
140
- title?: string;
141
- } | null | undefined;
142
- } & {
143
- text?: string;
144
- }) | {
145
- kind: "web";
146
- text: string;
147
- })) | ({
148
- __TYPE__: "AnyLink";
149
- } & {
150
- text: string;
151
- });
152
- }[];
153
- }>;
154
- export declare const RepeatableLegacy: (ctx: LegacyContentCtx, fieldType: "Link") => t.Type<RepeatableContent, WithTypes<Array<unknown>>, unknown>;
155
- export declare type RepeatableCustomType = Link;
156
- export declare function traverseRepeatableContent({ path, key, apiId, model, content, }: {
157
- path: ContentPath;
158
- key: string;
159
- apiId: string;
160
- content: RepeatableContent;
161
- model?: NestableWidget | undefined;
162
- }): (transform: TraverseWidgetContentFn) => RepeatableContent | undefined;
@@ -1,93 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.traverseRepeatableContent = exports.RepeatableLegacy = exports.isRepeatableContent = exports.RepeatableContent = void 0;
4
- const tslib_1 = require("tslib");
5
- const fp_ts_1 = require("fp-ts");
6
- const Either_1 = require("fp-ts/lib/Either");
7
- const function_1 = require("fp-ts/lib/function");
8
- const t = (0, tslib_1.__importStar)(require("io-ts"));
9
- const nestable_1 = require("./nestable");
10
- exports.RepeatableContent = t.strict({
11
- __TYPE__: t.literal("RepeatableContent"),
12
- type: t.literal("Link"),
13
- value: t.array(nestable_1.LinkContent),
14
- });
15
- exports.isRepeatableContent = exports.RepeatableContent.is;
16
- const RepeatableLegacy = (ctx, fieldType) => {
17
- return new t.Type("RepeatableLegacy", exports.isRepeatableContent, (items) => {
18
- const parsed = (0, function_1.pipe)(t.array(t.unknown).decode(items), fp_ts_1.either.map((items) => {
19
- const parsedItems = items.reduce((acc, item) => {
20
- let result;
21
- switch (fieldType) {
22
- case "Link":
23
- result = (0, nestable_1.LinkContentLegacy)(ctx).decode(item);
24
- break;
25
- }
26
- if (!result)
27
- return acc;
28
- if ((0, Either_1.isLeft)(result))
29
- return acc;
30
- return [...acc, result.right];
31
- }, []);
32
- return {
33
- value: parsedItems,
34
- type: fieldType,
35
- __TYPE__: "RepeatableContent",
36
- };
37
- }));
38
- return parsed;
39
- }, (r) => {
40
- const res = t.array(nestable_1.LinkContent).encode(r.value);
41
- const encodedItems = res.reduce((acc, item) => {
42
- let encoded;
43
- switch (item.__TYPE__) {
44
- case nestable_1.LinkContentType:
45
- encoded = (0, nestable_1.LinkContentLegacy)(ctx).encode(item);
46
- break;
47
- }
48
- if (!encoded)
49
- return acc;
50
- return [...acc, encoded];
51
- }, []);
52
- return {
53
- content: encodedItems.map((encodedItem) => encodedItem.content),
54
- types: { [ctx.keyOfType]: `Repeatable.${fieldType}` },
55
- };
56
- });
57
- };
58
- exports.RepeatableLegacy = RepeatableLegacy;
59
- function traverseRepeatableContent({ path, key, apiId, model, content, }) {
60
- return (transform) => {
61
- const items = content.value.reduce((acc, fieldContent, index) => {
62
- const itemPath = path.concat([
63
- { key: index.toString(), type: "Widget" },
64
- ]);
65
- const transformedField = transform({
66
- path: itemPath,
67
- key: key,
68
- apiId: apiId,
69
- model: model,
70
- content: fieldContent,
71
- });
72
- // Can happen if the transform function returns undefined to filter out a field
73
- if (!transformedField)
74
- return acc;
75
- // If the transformed field is not a link content, we don't include it
76
- if (!(0, nestable_1.isLinkContent)(transformedField))
77
- return acc;
78
- return acc.concat(transformedField);
79
- }, []);
80
- return transform({
81
- path,
82
- key,
83
- apiId,
84
- model,
85
- content: {
86
- __TYPE__: content.__TYPE__,
87
- type: content.type,
88
- value: items,
89
- },
90
- });
91
- };
92
- }
93
- exports.traverseRepeatableContent = traverseRepeatableContent;