@prismicio/types-internal 2.1.0-alpha.4 → 2.2.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.
- package/lib/common/HexaColorCode.d.ts +4 -0
- package/lib/common/HexaColorCode.js +7 -0
- package/lib/content/Document.d.ts +1 -1
- package/lib/customtypes/CustomType.d.ts +875 -923
- package/lib/customtypes/CustomType.js +74 -13
- package/lib/customtypes/Section.d.ts +866 -924
- package/lib/customtypes/Section.js +1 -23
- package/lib/customtypes/diff/SharedSlice.d.ts +168 -184
- package/lib/customtypes/diff/Variation.d.ts +167 -183
- package/lib/customtypes/widgets/Group.d.ts +2 -20
- package/lib/customtypes/widgets/Group.js +1 -20
- package/lib/customtypes/widgets/Widget.d.ts +733 -821
- package/lib/customtypes/widgets/nestable/{Link/index.d.ts → Link.d.ts} +2 -19
- package/lib/customtypes/widgets/nestable/{Link/index.js → Link.js} +2 -4
- package/lib/customtypes/widgets/nestable/NestableWidget.d.ts +158 -9
- package/lib/customtypes/widgets/nestable/NestableWidget.js +17 -1
- package/lib/customtypes/widgets/slices/CompositeSlice.d.ts +2 -20
- package/lib/customtypes/widgets/slices/CompositeSlice.js +1 -26
- package/lib/customtypes/widgets/slices/LegacySlice.d.ts +2 -18
- package/lib/customtypes/widgets/slices/SharedSlice.d.ts +4 -38
- package/lib/customtypes/widgets/slices/SharedSlice.js +1 -35
- package/lib/customtypes/widgets/slices/Slices.d.ts +801 -947
- package/lib/customtypes/widgets/slices/Slices.js +1 -34
- package/lib/import/converters/Document.d.ts +3 -0
- package/lib/import/converters/Document.js +17 -0
- package/lib/import/converters/fields/index.d.ts +1 -0
- package/lib/import/converters/fields/index.js +4 -0
- package/lib/import/converters/fields/nestable/Color.d.ts +3 -0
- package/lib/import/converters/fields/nestable/Color.js +10 -0
- package/lib/import/converters/fields/nestable/Date.d.ts +3 -0
- package/lib/import/converters/fields/nestable/Date.js +15 -0
- package/lib/import/converters/fields/nestable/Nestable.d.ts +3 -0
- package/lib/import/converters/fields/nestable/Nestable.js +23 -0
- package/lib/import/converters/fields/nestable/Number.d.ts +3 -0
- package/lib/import/converters/fields/nestable/Number.js +10 -0
- package/lib/import/converters/fields/nestable/Select.d.ts +3 -0
- package/lib/import/converters/fields/nestable/Select.js +10 -0
- package/lib/import/converters/fields/nestable/Text.d.ts +3 -0
- package/lib/import/converters/fields/nestable/Text.js +10 -0
- package/lib/import/converters/fields/nestable/Timestamp.d.ts +3 -0
- package/lib/import/converters/fields/nestable/Timestamp.js +10 -0
- package/lib/import/converters/fields/nestable/index.d.ts +7 -0
- package/lib/import/converters/fields/nestable/index.js +10 -0
- package/lib/import/converters/fields/utils.d.ts +1 -0
- package/lib/import/converters/fields/utils.js +10 -0
- package/lib/import/converters/index.d.ts +2 -0
- package/lib/import/converters/index.js +5 -0
- package/lib/import/index.d.ts +2 -0
- package/lib/import/index.js +5 -0
- package/lib/import/validators/Document.d.ts +6 -0
- package/lib/import/validators/Document.js +58 -0
- package/lib/import/validators/fields/index.d.ts +6 -0
- package/lib/import/validators/fields/index.js +4 -0
- package/lib/import/validators/fields/nestable/Color.d.ts +5 -0
- package/lib/import/validators/fields/nestable/Color.js +15 -0
- package/lib/import/validators/fields/nestable/Date.d.ts +4 -0
- package/lib/import/validators/fields/nestable/Date.js +17 -0
- package/lib/import/validators/fields/nestable/Nestable.d.ts +9 -0
- package/lib/import/validators/fields/nestable/Nestable.js +34 -0
- package/lib/import/validators/fields/nestable/Number.d.ts +10 -0
- package/lib/import/validators/fields/nestable/Number.js +16 -0
- package/lib/import/validators/fields/nestable/Select.d.ts +9 -0
- package/lib/import/validators/fields/nestable/Select.js +17 -0
- package/lib/import/validators/fields/nestable/Text.d.ts +3 -0
- package/lib/import/validators/fields/nestable/Text.js +5 -0
- package/lib/import/validators/fields/nestable/Timestamp.d.ts +5 -0
- package/lib/import/validators/fields/nestable/Timestamp.js +17 -0
- package/lib/import/validators/fields/nestable/index.d.ts +7 -0
- package/lib/import/validators/fields/nestable/index.js +10 -0
- package/lib/import/validators/index.d.ts +2 -0
- package/lib/import/validators/index.js +5 -0
- package/lib/utils/Objects.d.ts +1 -0
- package/lib/utils/Objects.js +9 -1
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +2 -1
- package/lib/validators/BasicTypes.d.ts +3 -0
- package/lib/validators/BasicTypes.js +8 -0
- package/lib/validators/DateFromString.js +2 -1
- package/lib/validators/NullOrT.d.ts +2 -0
- package/lib/validators/NullOrT.js +13 -0
- package/lib/validators/index.d.ts +2 -0
- package/lib/validators/index.js +2 -0
- package/package.json +1 -1
- package/src/common/HexaColorCode.ts +11 -0
- package/src/customtypes/CustomType.ts +97 -24
- package/src/customtypes/Section.ts +1 -28
- package/src/customtypes/widgets/Group.ts +0 -24
- package/src/customtypes/widgets/nestable/{Link/index.ts → Link.ts} +2 -5
- package/src/customtypes/widgets/nestable/NestableWidget.ts +17 -0
- package/src/customtypes/widgets/slices/CompositeSlice.ts +0 -33
- package/src/customtypes/widgets/slices/SharedSlice.ts +0 -47
- package/src/customtypes/widgets/slices/Slices.ts +1 -44
- package/src/import/converters/Document.ts +22 -0
- package/src/import/converters/fields/index.ts +1 -0
- package/src/import/converters/fields/nestable/Color.ts +12 -0
- package/src/import/converters/fields/nestable/Date.ts +18 -0
- package/src/import/converters/fields/nestable/Nestable.ts +33 -0
- package/src/import/converters/fields/nestable/Number.ts +12 -0
- package/src/import/converters/fields/nestable/Select.ts +12 -0
- package/src/import/converters/fields/nestable/Text.ts +12 -0
- package/src/import/converters/fields/nestable/Timestamp.ts +12 -0
- package/src/import/converters/fields/nestable/index.ts +7 -0
- package/src/import/converters/fields/utils.ts +7 -0
- package/src/import/converters/index.ts +2 -0
- package/src/import/index.ts +2 -0
- package/src/import/validators/Document.ts +79 -0
- package/src/import/validators/fields/index.ts +8 -0
- package/src/import/validators/fields/nestable/Color.ts +28 -0
- package/src/import/validators/fields/nestable/Date.ts +35 -0
- package/src/import/validators/fields/nestable/Nestable.ts +41 -0
- package/src/import/validators/fields/nestable/Number.ts +36 -0
- package/src/import/validators/fields/nestable/Select.ts +36 -0
- package/src/import/validators/fields/nestable/Text.ts +7 -0
- package/src/import/validators/fields/nestable/Timestamp.ts +36 -0
- package/src/import/validators/fields/nestable/index.ts +7 -0
- package/src/import/validators/index.ts +2 -0
- package/src/utils/Objects.ts +8 -0
- package/src/utils/index.ts +1 -0
- package/src/validators/BasicTypes.ts +5 -0
- package/src/validators/DateFromString.ts +3 -1
- package/src/validators/NullOrT.ts +18 -0
- package/src/validators/index.ts +2 -0
- package/lib/customtypes/_internal/utils.d.ts +0 -3
- package/lib/customtypes/_internal/utils.js +0 -20
- package/lib/customtypes/widgets/nestable/Link/ContentRelationshipResolver.d.ts +0 -11
- package/lib/customtypes/widgets/nestable/Link/ContentRelationshipResolver.js +0 -15
- package/lib/validators/StringOrT.d.ts +0 -3
- package/lib/validators/StringOrT.js +0 -15
- package/src/customtypes/_internal/utils.ts +0 -25
- package/src/customtypes/widgets/nestable/Link/ContentRelationshipResolver.ts +0 -20
- package/src/validators/StringOrT.ts +0 -21
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TimestampContent } from "../../../../content"
|
|
2
|
+
import type { ImportTimestamp } from "../../.."
|
|
3
|
+
import { nullableConverter } from "../utils"
|
|
4
|
+
|
|
5
|
+
export const timestampConverter = (
|
|
6
|
+
field: ImportTimestamp["value"],
|
|
7
|
+
): TimestampContent | undefined =>
|
|
8
|
+
nullableConverter(field, (value: NonNullable<ImportTimestamp["value"]>) => ({
|
|
9
|
+
type: "Timestamp",
|
|
10
|
+
value: value.toISOString(),
|
|
11
|
+
__TYPE__: "FieldContent",
|
|
12
|
+
}))
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import * as Either from "fp-ts/Either"
|
|
2
|
+
import * as t from "io-ts"
|
|
3
|
+
|
|
4
|
+
import type { WidgetKey } from "../../common"
|
|
5
|
+
import {
|
|
6
|
+
isNestableWidget,
|
|
7
|
+
StaticCustomType,
|
|
8
|
+
StaticWidget,
|
|
9
|
+
} from "../../customtypes"
|
|
10
|
+
import { isObject } from "../../utils/Objects"
|
|
11
|
+
import { getNestableFieldCodec, ImportNestable } from "./fields"
|
|
12
|
+
|
|
13
|
+
export type ImportDocument = Record<WidgetKey, ImportNestable>
|
|
14
|
+
export const ImportDocument = (mask: StaticCustomType) =>
|
|
15
|
+
new t.Type<ImportDocument, ImportDocument, unknown>(
|
|
16
|
+
"ImportDocument",
|
|
17
|
+
(u): u is ImportDocument => isObject(u),
|
|
18
|
+
(raw: unknown) => {
|
|
19
|
+
if (!isObject(raw)) {
|
|
20
|
+
const error: t.ValidationError = {
|
|
21
|
+
value: raw,
|
|
22
|
+
context: [],
|
|
23
|
+
message: "document is not an object",
|
|
24
|
+
}
|
|
25
|
+
return Either.left([error])
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// flattening the fields within the tabs
|
|
29
|
+
const maskFields = Object.values(mask.json).reduce((acc, tab) => ({
|
|
30
|
+
...acc,
|
|
31
|
+
...tab,
|
|
32
|
+
}))
|
|
33
|
+
|
|
34
|
+
// Listing all errors for each fields
|
|
35
|
+
const { document, errors } = Object.entries(raw).reduce<{
|
|
36
|
+
document: ImportDocument
|
|
37
|
+
errors: t.Errors
|
|
38
|
+
}>(
|
|
39
|
+
(acc, [fieldKey, fieldValue]) => {
|
|
40
|
+
const maskFieldValue = maskFields[fieldKey]
|
|
41
|
+
|
|
42
|
+
// the field is not defined in the custom type -> extra field or typo
|
|
43
|
+
if (!maskFieldValue) {
|
|
44
|
+
const error: t.ValidationError = {
|
|
45
|
+
value: fieldValue,
|
|
46
|
+
context: [{ key: fieldKey, type: t.unknown }],
|
|
47
|
+
message: `The field ${fieldKey} is not part of the Custom type`,
|
|
48
|
+
}
|
|
49
|
+
return { ...acc, errors: [...acc.errors, error] }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// retrieving the right codec then testing out fieldValue with it.
|
|
53
|
+
const fieldCodec = getFieldCodec(maskFieldValue)
|
|
54
|
+
const result = fieldCodec.decode(fieldValue)
|
|
55
|
+
|
|
56
|
+
// returning validation errors
|
|
57
|
+
if (Either.isLeft(result))
|
|
58
|
+
return { ...acc, errors: [...acc.errors, ...result.left] }
|
|
59
|
+
|
|
60
|
+
// registering the field as validated
|
|
61
|
+
return {
|
|
62
|
+
...acc,
|
|
63
|
+
document: { ...acc.document, [fieldKey]: result.right },
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
{ document: {}, errors: [] },
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
// Returning either the errors of
|
|
70
|
+
if (errors.length > 0) return Either.left(errors)
|
|
71
|
+
return Either.right(document)
|
|
72
|
+
},
|
|
73
|
+
t.identity,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
function getFieldCodec(maskValue: StaticWidget) {
|
|
77
|
+
if (isNestableWidget(maskValue)) return getNestableFieldCodec(maskValue)
|
|
78
|
+
throw new Error(`Unsupported type of field ${maskValue.type}`)
|
|
79
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as t from "io-ts"
|
|
2
|
+
|
|
3
|
+
import { HexaColor, HexaColorCode } from "../../../../common/HexaColorCode"
|
|
4
|
+
import { NullOrT, String } from "../../../../validators"
|
|
5
|
+
import type { ImportFieldOutput } from ".."
|
|
6
|
+
|
|
7
|
+
export type ImportColor = ImportFieldOutput<"Color", HexaColorCode>
|
|
8
|
+
export const ImportColor = NullOrT<HexaColorCode, ImportColor>(
|
|
9
|
+
// @ts-expect-error HexaColorCode is not inferred as a string.
|
|
10
|
+
String.pipe(
|
|
11
|
+
new t.Type<HexaColorCode, HexaColorCode, string>(
|
|
12
|
+
"ImportColorField",
|
|
13
|
+
(u: unknown): u is HexaColorCode => String.is(u) && HexaColor.is(u),
|
|
14
|
+
(s: string, c: t.Context) => {
|
|
15
|
+
if (!HexaColor.is(s)) {
|
|
16
|
+
return t.failure(
|
|
17
|
+
s,
|
|
18
|
+
c,
|
|
19
|
+
"The color value must be an hexadecimal color code",
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return t.success(s)
|
|
24
|
+
},
|
|
25
|
+
t.identity,
|
|
26
|
+
),
|
|
27
|
+
),
|
|
28
|
+
)((s: HexaColorCode | null) => ({ type: "Color", value: s }))
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { chain } from "fp-ts/Either"
|
|
2
|
+
import { pipe } from "fp-ts/function"
|
|
3
|
+
import * as t from "io-ts"
|
|
4
|
+
|
|
5
|
+
import { NullOrT, String } from "../../../../validators"
|
|
6
|
+
import type { ImportFieldOutput } from ".."
|
|
7
|
+
|
|
8
|
+
const DATE_INPUT_REGEX = /^(\d{1,4})-(\d{1,2})-(\d{1,2})$/
|
|
9
|
+
|
|
10
|
+
export type ImportDate = ImportFieldOutput<"Date", Date>
|
|
11
|
+
export const ImportDate = NullOrT<Date, ImportDate>(
|
|
12
|
+
new t.Type<Date, Date, unknown>(
|
|
13
|
+
"ImportDate",
|
|
14
|
+
(u: unknown): u is Date => u instanceof Date,
|
|
15
|
+
(u: unknown, c: t.Context) =>
|
|
16
|
+
pipe(
|
|
17
|
+
String.validate(u, c),
|
|
18
|
+
chain((s: string) => {
|
|
19
|
+
const date = new Date(s)
|
|
20
|
+
const isInvalidDate = isNaN(date.getTime())
|
|
21
|
+
|
|
22
|
+
if (isInvalidDate || !DATE_INPUT_REGEX.test(s)) {
|
|
23
|
+
return t.failure(
|
|
24
|
+
u,
|
|
25
|
+
c,
|
|
26
|
+
"The value must be a Date in the format YYYY-MM-DD",
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return t.success(date)
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
t.identity,
|
|
34
|
+
),
|
|
35
|
+
)((s: Date | null) => ({ type: "Date", value: s }))
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { NestableWidget } from "../../../../customtypes"
|
|
2
|
+
import { ImportDate, ImportTimestamp } from "."
|
|
3
|
+
import { ImportColor } from "./Color"
|
|
4
|
+
import { ImportNumber } from "./Number"
|
|
5
|
+
import { ImportSelect } from "./Select"
|
|
6
|
+
import { ImportText } from "./Text"
|
|
7
|
+
|
|
8
|
+
export type ImportNestable =
|
|
9
|
+
| ImportColor
|
|
10
|
+
| ImportSelect
|
|
11
|
+
| ImportNumber
|
|
12
|
+
| ImportText
|
|
13
|
+
| ImportDate
|
|
14
|
+
| ImportTimestamp
|
|
15
|
+
|
|
16
|
+
export const isNestableFieldValue = (u: unknown): u is ImportNestable =>
|
|
17
|
+
ImportColor.is(u) ||
|
|
18
|
+
ImportSelect().is(u) ||
|
|
19
|
+
ImportNumber().is(u) ||
|
|
20
|
+
ImportText.is(u) ||
|
|
21
|
+
ImportDate.is(u) ||
|
|
22
|
+
ImportTimestamp.is(u)
|
|
23
|
+
|
|
24
|
+
export function getNestableFieldCodec(field: NestableWidget) {
|
|
25
|
+
switch (field.type) {
|
|
26
|
+
case "Color":
|
|
27
|
+
return ImportColor
|
|
28
|
+
case "Select":
|
|
29
|
+
return ImportSelect(field.config)
|
|
30
|
+
case "Number":
|
|
31
|
+
return ImportNumber(field.config)
|
|
32
|
+
case "Text":
|
|
33
|
+
return ImportText
|
|
34
|
+
case "Date":
|
|
35
|
+
return ImportDate
|
|
36
|
+
case "Timestamp":
|
|
37
|
+
return ImportTimestamp
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Unsupported type of nestable field ${field.type}`)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as t from "io-ts"
|
|
2
|
+
|
|
3
|
+
import type { NumberConfig } from "../../../../customtypes"
|
|
4
|
+
import { NullOrT, Number } from "../../../../validators"
|
|
5
|
+
import type { ImportFieldOutput } from ".."
|
|
6
|
+
|
|
7
|
+
export type ImportNumber = ImportFieldOutput<"Number", number>
|
|
8
|
+
export const ImportNumber = (config?: NumberConfig) =>
|
|
9
|
+
NullOrT<number, ImportNumber>(
|
|
10
|
+
Number.pipe(
|
|
11
|
+
new t.Type<number, number, number>(
|
|
12
|
+
"ImportColorField",
|
|
13
|
+
(u: unknown): u is number => Number.is(u),
|
|
14
|
+
(n: number, c: t.Context) => {
|
|
15
|
+
const min = config?.min
|
|
16
|
+
const max = config?.max
|
|
17
|
+
|
|
18
|
+
if (
|
|
19
|
+
(min !== undefined && n < min) ||
|
|
20
|
+
(max !== undefined && n > max)
|
|
21
|
+
) {
|
|
22
|
+
return t.failure(
|
|
23
|
+
n,
|
|
24
|
+
c,
|
|
25
|
+
`The value must be between [${min !== undefined ? min : ""}, ${
|
|
26
|
+
max !== undefined ? max : ""
|
|
27
|
+
}]`,
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return t.success(n)
|
|
32
|
+
},
|
|
33
|
+
t.identity,
|
|
34
|
+
),
|
|
35
|
+
),
|
|
36
|
+
)((n: number | null) => ({ type: "Number", value: n }))
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as t from "io-ts"
|
|
2
|
+
|
|
3
|
+
import type { SelectConfig } from "../../../../customtypes"
|
|
4
|
+
import { NullOrT, String } from "../../../../validators"
|
|
5
|
+
import type { ImportFieldOutput } from ".."
|
|
6
|
+
|
|
7
|
+
export type ImportSelect = ImportFieldOutput<"Select", string>
|
|
8
|
+
export const ImportSelect = (config?: SelectConfig) =>
|
|
9
|
+
NullOrT<string, ImportSelect>(
|
|
10
|
+
String.pipe(
|
|
11
|
+
new t.Type<string, string, string>(
|
|
12
|
+
"ImportColorField",
|
|
13
|
+
(u: unknown): u is string => String.is(u),
|
|
14
|
+
(s: string, c: t.Context) => {
|
|
15
|
+
if (!config?.options) {
|
|
16
|
+
return t.failure(
|
|
17
|
+
s,
|
|
18
|
+
c,
|
|
19
|
+
"The value must be defined in the custom type's configuration of the field",
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!config.options.includes(s)) {
|
|
24
|
+
return t.failure(
|
|
25
|
+
s,
|
|
26
|
+
c,
|
|
27
|
+
`The value must be one of these: ${config.options?.join(",")}`,
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return t.success(s)
|
|
32
|
+
},
|
|
33
|
+
t.identity,
|
|
34
|
+
),
|
|
35
|
+
),
|
|
36
|
+
)((s: string | null) => ({ type: "Select", value: s }))
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { NullOrT, String } from "../../../../validators"
|
|
2
|
+
import type { ImportFieldOutput } from ".."
|
|
3
|
+
|
|
4
|
+
export type ImportText = ImportFieldOutput<"Text", string>
|
|
5
|
+
export const ImportText = NullOrT<string, ImportText>(String)(
|
|
6
|
+
(s: string | null) => ({ type: "Text", value: s }),
|
|
7
|
+
)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { chain } from "fp-ts/Either"
|
|
2
|
+
import { pipe } from "fp-ts/function"
|
|
3
|
+
import * as t from "io-ts"
|
|
4
|
+
|
|
5
|
+
import { NullOrT, String } from "../../../../validators"
|
|
6
|
+
import type { ImportFieldOutput } from ".."
|
|
7
|
+
|
|
8
|
+
export const TIMESTAMP_INPUT_REGEX =
|
|
9
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:(?:[+-]{1}\d{2}:?\d{2})|Z)$/
|
|
10
|
+
|
|
11
|
+
export type ImportTimestamp = ImportFieldOutput<"Timestamp", Date>
|
|
12
|
+
export const ImportTimestamp = NullOrT<Date, ImportTimestamp>(
|
|
13
|
+
new t.Type<Date, Date, unknown>(
|
|
14
|
+
"ImportDate",
|
|
15
|
+
(u: unknown): u is Date => u instanceof Date,
|
|
16
|
+
(u: unknown, c: t.Context) =>
|
|
17
|
+
pipe(
|
|
18
|
+
String.validate(u, c),
|
|
19
|
+
chain((s: string) => {
|
|
20
|
+
const date = new Date(s)
|
|
21
|
+
const isInvalidDate = isNaN(date.getTime())
|
|
22
|
+
|
|
23
|
+
if (isInvalidDate || !TIMESTAMP_INPUT_REGEX.test(s)) {
|
|
24
|
+
return t.failure(
|
|
25
|
+
u,
|
|
26
|
+
c,
|
|
27
|
+
"The value must be a DateTime in the format YYYY-MM-DDTHH:MM:SS+0000",
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return t.success(date)
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
t.identity,
|
|
35
|
+
),
|
|
36
|
+
)((s: Date | null) => ({ type: "Timestamp", value: s }))
|
package/src/utils/Objects.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { isRight } from "fp-ts/lib/Either"
|
|
2
|
+
import * as t from "io-ts"
|
|
3
|
+
|
|
1
4
|
export function zipObjects<A, B = A>(
|
|
2
5
|
objLeft?: Partial<Record<string, A>> | undefined,
|
|
3
6
|
objRight?: Partial<Record<string, B>> | undefined,
|
|
@@ -33,3 +36,8 @@ export function withOptionals<T extends object>(
|
|
|
33
36
|
}, {}),
|
|
34
37
|
}
|
|
35
38
|
}
|
|
39
|
+
|
|
40
|
+
const recordCodec = t.record(t.string, t.unknown)
|
|
41
|
+
export function isObject(value: unknown): value is Record<string, unknown> {
|
|
42
|
+
return isRight(recordCodec.decode(value))
|
|
43
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -2,12 +2,14 @@ import { chain } from "fp-ts/Either"
|
|
|
2
2
|
import { pipe } from "fp-ts/function"
|
|
3
3
|
import * as t from "io-ts"
|
|
4
4
|
|
|
5
|
+
import { String } from "."
|
|
6
|
+
|
|
5
7
|
export default new t.Type<Date, string, unknown>(
|
|
6
8
|
"dateFromString",
|
|
7
9
|
(u): u is Date => u instanceof Date,
|
|
8
10
|
(u, c) =>
|
|
9
11
|
pipe(
|
|
10
|
-
|
|
12
|
+
String.validate(u, c),
|
|
11
13
|
chain((s) => {
|
|
12
14
|
const d = new Date(s)
|
|
13
15
|
return isNaN(d.getTime()) ? t.failure(u, c) : t.success(d)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { either } from "fp-ts"
|
|
2
|
+
import { pipe } from "fp-ts/function"
|
|
3
|
+
import * as t from "io-ts"
|
|
4
|
+
|
|
5
|
+
export const NullOrT = <T, O = T>(codec: t.Type<T>) => {
|
|
6
|
+
return (asO: (value: T | null) => O) =>
|
|
7
|
+
new t.Type<O>(
|
|
8
|
+
`NullOr${codec.name}`,
|
|
9
|
+
(u: unknown): u is O => t.null.is(u) || codec.is(u),
|
|
10
|
+
(u: unknown) => {
|
|
11
|
+
return pipe(
|
|
12
|
+
t.union([t.null, codec]).decode(u),
|
|
13
|
+
either.map((decoded) => asO(decoded)),
|
|
14
|
+
)
|
|
15
|
+
},
|
|
16
|
+
(f) => f,
|
|
17
|
+
)
|
|
18
|
+
}
|
package/src/validators/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export * from "./BasicTypes"
|
|
1
2
|
export { default as DateFromString } from "./DateFromString"
|
|
2
3
|
export { default as DateFromStringOrNumber } from "./DateFromStringOrNumber"
|
|
3
4
|
export { default as DateFromTsMs } from "./DateFromTsMs"
|
|
@@ -6,6 +7,7 @@ export { default as IntFromNumber } from "./IntFromNumber"
|
|
|
6
7
|
export { default as IntFromPixels } from "./IntFromPixels"
|
|
7
8
|
export { default as NonEmptyString } from "./NonEmptyString"
|
|
8
9
|
export { default as NonEmptyStringOrNull } from "./NonEmptyStringOrNull"
|
|
10
|
+
export * from "./NullOrT"
|
|
9
11
|
export { default as NumberOrNull } from "./NumberOrNull"
|
|
10
12
|
export { default as StringFromBoolean } from "./StringFromBoolean"
|
|
11
13
|
export { default as StringFromNumber } from "./StringFromNumber"
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { DynamicSlice, DynamicWidget } from "../widgets";
|
|
2
|
-
export declare type TraverseFn = <T extends DynamicWidget | DynamicSlice>(widget: T, key: string, path: ReadonlyArray<string>) => DynamicWidget | DynamicSlice | undefined;
|
|
3
|
-
export declare function fromEntriesWithValue<T>(entries: (string | T)[][]): {};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fromEntriesWithValue = void 0;
|
|
4
|
-
function fromEntriesWithValue(entries) {
|
|
5
|
-
return Array.from(entries).reduce((acc, entry) => {
|
|
6
|
-
/**
|
|
7
|
-
* Replace the untyped default `fromEntries`
|
|
8
|
-
* Role: filter pair with undefined value
|
|
9
|
-
* Error: Throw an error if malformed: key[string]/value[T]
|
|
10
|
-
*/
|
|
11
|
-
if (entry.length !== 2 || typeof entry[0] !== "string")
|
|
12
|
-
throw new Error("Malformed entry, should be a key[string]/value[T] pair.");
|
|
13
|
-
const [key, value] = entry;
|
|
14
|
-
if (value !== undefined) {
|
|
15
|
-
return { ...acc, [key]: entry[1] };
|
|
16
|
-
}
|
|
17
|
-
return acc;
|
|
18
|
-
}, {});
|
|
19
|
-
}
|
|
20
|
-
exports.fromEntriesWithValue = fromEntriesWithValue;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as t from "io-ts";
|
|
2
|
-
export declare const ContentRelationshipResolver: t.ReadonlyArrayC<t.Type<{
|
|
3
|
-
customTypeId: string;
|
|
4
|
-
} & {
|
|
5
|
-
fetchFields?: boolean;
|
|
6
|
-
}, {
|
|
7
|
-
customTypeId: string;
|
|
8
|
-
} & {
|
|
9
|
-
fetchFields?: boolean;
|
|
10
|
-
}, unknown>>;
|
|
11
|
-
export declare type ContentRelationshipResolver = t.TypeOf<typeof ContentRelationshipResolver>;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ContentRelationshipResolver = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const t = (0, tslib_1.__importStar)(require("io-ts"));
|
|
6
|
-
const StringOrT_1 = (0, tslib_1.__importDefault)(require("../../../../validators/StringOrT"));
|
|
7
|
-
const CustomTypeResolver = (0, StringOrT_1.default)(t.intersection([
|
|
8
|
-
t.type({
|
|
9
|
-
customTypeId: t.string,
|
|
10
|
-
}),
|
|
11
|
-
t.partial({
|
|
12
|
-
fetchFields: t.boolean,
|
|
13
|
-
}),
|
|
14
|
-
]))((id) => ({ customTypeId: id }));
|
|
15
|
-
exports.ContentRelationshipResolver = t.readonlyArray(CustomTypeResolver);
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const fp_ts_1 = require("fp-ts");
|
|
5
|
-
const function_1 = require("fp-ts/function");
|
|
6
|
-
const t = (0, tslib_1.__importStar)(require("io-ts"));
|
|
7
|
-
exports.default = (codec) => {
|
|
8
|
-
return (asObject) => new t.Type("StringOrT", (u) => u instanceof Object, (u) => {
|
|
9
|
-
return (0, function_1.pipe)(t.union([t.string, codec]).decode(u), fp_ts_1.either.map((decoded) => {
|
|
10
|
-
if (typeof decoded === "string")
|
|
11
|
-
return asObject(decoded);
|
|
12
|
-
return decoded;
|
|
13
|
-
}));
|
|
14
|
-
}, (f) => f);
|
|
15
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { DynamicSlice, DynamicWidget } from "../widgets"
|
|
2
|
-
|
|
3
|
-
export type TraverseFn = <T extends DynamicWidget | DynamicSlice>(
|
|
4
|
-
widget: T,
|
|
5
|
-
key: string,
|
|
6
|
-
path: ReadonlyArray<string>,
|
|
7
|
-
) => DynamicWidget | DynamicSlice | undefined
|
|
8
|
-
|
|
9
|
-
export function fromEntriesWithValue<T>(entries: (string | T)[][]) {
|
|
10
|
-
return Array.from(entries).reduce((acc, entry) => {
|
|
11
|
-
/**
|
|
12
|
-
* Replace the untyped default `fromEntries`
|
|
13
|
-
* Role: filter pair with undefined value
|
|
14
|
-
* Error: Throw an error if malformed: key[string]/value[T]
|
|
15
|
-
*/
|
|
16
|
-
if (entry.length !== 2 || typeof entry[0] !== "string")
|
|
17
|
-
throw new Error("Malformed entry, should be a key[string]/value[T] pair.")
|
|
18
|
-
|
|
19
|
-
const [key, value] = entry
|
|
20
|
-
if (value !== undefined) {
|
|
21
|
-
return { ...acc, [key]: entry[1] }
|
|
22
|
-
}
|
|
23
|
-
return acc
|
|
24
|
-
}, {})
|
|
25
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import * as t from "io-ts"
|
|
2
|
-
|
|
3
|
-
import StringOrT from "../../../../validators/StringOrT"
|
|
4
|
-
|
|
5
|
-
const CustomTypeResolver = StringOrT(
|
|
6
|
-
t.intersection([
|
|
7
|
-
t.type({
|
|
8
|
-
customTypeId: t.string,
|
|
9
|
-
}),
|
|
10
|
-
t.partial({
|
|
11
|
-
fetchFields: t.boolean,
|
|
12
|
-
}),
|
|
13
|
-
]),
|
|
14
|
-
)((id) => ({ customTypeId: id }))
|
|
15
|
-
type CustomTypeResolver = t.TypeOf<typeof CustomTypeResolver>
|
|
16
|
-
|
|
17
|
-
export const ContentRelationshipResolver = t.readonlyArray(CustomTypeResolver)
|
|
18
|
-
export type ContentRelationshipResolver = t.TypeOf<
|
|
19
|
-
typeof ContentRelationshipResolver
|
|
20
|
-
>
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { either } from "fp-ts"
|
|
2
|
-
import { pipe } from "fp-ts/function"
|
|
3
|
-
import * as t from "io-ts"
|
|
4
|
-
|
|
5
|
-
export default <T>(codec: t.Type<T>) => {
|
|
6
|
-
return (asObject: (strValue: string) => T) =>
|
|
7
|
-
new t.Type<T>(
|
|
8
|
-
"StringOrT",
|
|
9
|
-
(u: unknown): u is T => u instanceof Object,
|
|
10
|
-
(u: unknown) => {
|
|
11
|
-
return pipe(
|
|
12
|
-
t.union([t.string, codec]).decode(u),
|
|
13
|
-
either.map((decoded) => {
|
|
14
|
-
if (typeof decoded === "string") return asObject(decoded)
|
|
15
|
-
return decoded
|
|
16
|
-
}),
|
|
17
|
-
)
|
|
18
|
-
},
|
|
19
|
-
(f) => f,
|
|
20
|
-
)
|
|
21
|
-
}
|