@prismicio/types-internal 2.2.0-alpha.2 → 2.2.0-alpha.21
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/Asset.d.ts +16 -0
- package/lib/common/Asset.js +11 -0
- package/lib/common/Embed.d.ts +15 -0
- package/lib/common/Embed.js +10 -0
- package/lib/common/UUID.d.ts +7 -0
- package/lib/common/UUID.js +8 -0
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.js +2 -0
- package/lib/content/Document.d.ts +40 -40
- package/lib/content/fields/GroupContent.d.ts +12 -12
- package/lib/content/fields/WidgetContent.d.ts +56 -56
- package/lib/content/fields/nestable/NestableContent.d.ts +7 -7
- package/lib/content/fields/nestable/RichTextContent/Blocks.d.ts +35 -12
- package/lib/content/fields/nestable/RichTextContent/Blocks.js +5 -5
- package/lib/content/fields/nestable/RichTextContent/index.d.ts +9 -9
- package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +14 -14
- package/lib/content/fields/slices/Slice/RepeatableContent.d.ts +5 -5
- package/lib/content/fields/slices/Slice/RepeatableContent.js +0 -1
- package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +14 -14
- package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +14 -14
- package/lib/content/fields/slices/Slice/index.d.ts +30 -30
- package/lib/content/fields/slices/SliceItem.d.ts +30 -30
- package/lib/content/fields/slices/SlicesContent.d.ts +42 -42
- package/lib/customtypes/CustomType.d.ts +2 -0
- package/lib/customtypes/CustomType.js +8 -1
- package/lib/import/converters/Document.d.ts +2 -2
- package/lib/import/converters/Document.js +6 -6
- package/lib/import/converters/fields/Slices/SharedSlice.d.ts +4 -0
- package/lib/import/converters/fields/Slices/SharedSlice.js +19 -0
- package/lib/import/converters/fields/Slices/SharedSliceContent.d.ts +10 -0
- package/lib/import/converters/fields/Slices/SharedSliceContent.js +58 -0
- package/lib/import/converters/fields/Slices/Slices.d.ts +4 -0
- package/lib/import/converters/fields/Slices/Slices.js +16 -0
- package/lib/import/converters/fields/Slices/index.d.ts +1 -0
- package/lib/import/converters/fields/Slices/index.js +4 -0
- package/lib/import/converters/fields/UID.d.ts +1 -1
- package/lib/import/converters/fields/UID.js +0 -2
- package/lib/import/converters/fields/index.d.ts +1 -0
- package/lib/import/converters/fields/index.js +1 -0
- package/lib/import/converters/fields/nestable/Embed.d.ts +2 -1
- package/lib/import/converters/fields/nestable/Embed.js +24 -5
- package/lib/import/converters/fields/nestable/GeoPoint.d.ts +3 -0
- package/lib/import/converters/fields/nestable/GeoPoint.js +15 -0
- package/lib/import/converters/fields/nestable/GeooPoint.d.ts +3 -0
- package/lib/import/converters/fields/nestable/GeooPoint.js +15 -0
- package/lib/import/converters/fields/nestable/Image.d.ts +6 -18
- package/lib/import/converters/fields/nestable/Image.js +47 -24
- package/lib/import/converters/fields/nestable/Link.d.ts +3 -2
- package/lib/import/converters/fields/nestable/Link.js +62 -11
- package/lib/import/converters/fields/nestable/Nestable.d.ts +4 -3
- package/lib/import/converters/fields/nestable/Nestable.js +9 -3
- package/lib/import/converters/fields/nestable/RichText/embedBlock.d.ts +4 -0
- package/lib/import/converters/fields/nestable/RichText/embedBlock.js +14 -0
- package/lib/import/converters/fields/nestable/RichText/imageBlock.d.ts +4 -0
- package/lib/import/converters/fields/nestable/RichText/imageBlock.js +12 -0
- package/lib/import/converters/fields/nestable/RichText/index.d.ts +4 -0
- package/lib/import/converters/fields/nestable/RichText/index.js +22 -0
- package/lib/import/converters/fields/nestable/RichText/textBlock.d.ts +4 -0
- package/lib/import/converters/fields/nestable/RichText/textBlock.js +34 -0
- package/lib/import/converters/fields/nestable/RichText.d.ts +4 -0
- package/lib/import/converters/fields/nestable/RichText.js +55 -0
- package/lib/import/converters/fields/nestable/index.d.ts +3 -0
- package/lib/import/converters/fields/nestable/index.js +3 -0
- package/lib/import/validators/Document.js +4 -9
- package/lib/import/validators/fields/ImportField.d.ts +68 -68
- package/lib/import/validators/fields/ImportField.js +8 -4
- package/lib/import/validators/fields/ImportSlices/ImportSlices.d.ts +8 -0
- package/lib/import/validators/fields/ImportSlices/ImportSlices.js +29 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.d.ts +14 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +61 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.d.ts +4 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/errors.js +7 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.d.ts +2 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.js +15 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.d.ts +17 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.js +30 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.d.ts +43 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.js +69 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.d.ts +4 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.js +6 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.d.ts +2 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.js +5 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.d.ts +1 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.js +2 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.d.ts +9 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.js +39 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.d.ts +11 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.js +24 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.d.ts +11 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.js +24 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.d.ts +5 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/index.js +8 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/index.d.ts +3 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/index.js +6 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.d.ts +339 -0
- package/lib/import/validators/fields/ImportSlices/SharedSlice/utils.js +11 -0
- package/lib/import/validators/fields/ImportSlices/index.d.ts +1 -0
- package/lib/import/validators/fields/ImportSlices/index.js +4 -0
- package/lib/import/validators/fields/ImportSlices/utils.d.ts +4 -0
- package/lib/import/validators/fields/ImportSlices/utils.js +35 -0
- package/lib/import/validators/fields/UID.d.ts +2 -2
- package/lib/import/validators/fields/UID.js +1 -1
- package/lib/import/validators/fields/nestable/Embed.d.ts +8 -22
- package/lib/import/validators/fields/nestable/Embed.js +5 -28
- package/lib/import/validators/fields/nestable/GeoPoint.d.ts +13 -0
- package/lib/import/validators/fields/nestable/GeoPoint.js +13 -0
- package/lib/import/validators/fields/nestable/Image/default.d.ts +22 -0
- package/lib/import/validators/fields/nestable/Image/default.js +19 -0
- package/lib/import/validators/fields/nestable/Image/index.d.ts +45 -0
- package/lib/import/validators/fields/nestable/Image/index.js +41 -0
- package/lib/import/validators/fields/nestable/Image/merge.d.ts +23 -0
- package/lib/import/validators/fields/nestable/Image/merge.js +44 -0
- package/lib/import/validators/fields/nestable/Image/model.d.ts +18 -0
- package/lib/import/validators/fields/nestable/Image/model.js +2 -0
- package/lib/import/validators/fields/nestable/Image/validators.d.ts +35 -0
- package/lib/import/validators/fields/nestable/Image/validators.js +31 -0
- package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.d.ts +6 -0
- package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.js +25 -0
- package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.d.ts +24 -0
- package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.js +26 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.d.ts +6 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.js +18 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.d.ts +10 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.js +11 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.d.ts +8 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.js +11 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.d.ts +70 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.js +43 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.d.ts +65 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.js +20 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.d.ts +4 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.js +7 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.d.ts +24 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.js +18 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.d.ts +5 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.js +10 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.d.ts +17 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.js +22 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.d.ts +2 -0
- package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.js +5 -0
- package/lib/import/validators/fields/nestable/ImportRichText/index.d.ts +2 -0
- package/lib/import/validators/fields/nestable/ImportRichText/index.js +7 -0
- package/lib/import/validators/fields/nestable/Link.d.ts +33 -0
- package/lib/import/validators/fields/nestable/Link.js +16 -11
- package/lib/import/validators/fields/nestable/Nestable.d.ts +65 -62
- package/lib/import/validators/fields/nestable/Nestable.js +18 -6
- package/lib/import/validators/fields/nestable/index.d.ts +5 -1
- package/lib/import/validators/fields/nestable/index.js +7 -1
- package/lib/utils/DocumentId.d.ts +2 -0
- package/lib/utils/DocumentId.js +7 -0
- package/lib/utils/Objects.d.ts +1 -1
- package/lib/utils/Objects.js +1 -1
- package/lib/utils/io-ts.d.ts +2 -0
- package/lib/utils/io-ts.js +22 -0
- package/lib/validators/BasicTypes.d.ts +0 -1
- package/lib/validators/BasicTypes.js +7 -8
- package/lib/validators/DefaultOrElse.js +1 -1
- package/lib/validators/NonEmptyString.js +1 -1
- package/lib/validators/NumberRange.d.ts +32 -0
- package/lib/validators/NumberRange.js +40 -0
- package/lib/validators/function.d.ts +20 -0
- package/lib/validators/function.js +41 -1
- package/lib/validators/index.d.ts +1 -0
- package/lib/validators/index.js +3 -1
- package/package.json +4 -2
- package/src/common/Asset.ts +25 -0
- package/src/common/Embed.ts +22 -0
- package/src/common/UUID.ts +19 -0
- package/src/common/index.ts +2 -0
- package/src/content/fields/nestable/RichTextContent/Blocks.ts +1 -1
- package/src/content/fields/slices/Slice/RepeatableContent.ts +0 -1
- package/src/customtypes/CustomType.ts +13 -0
- package/src/import/converters/Document.ts +10 -7
- package/src/import/converters/fields/Slices/SharedSlice.ts +24 -0
- package/src/import/converters/fields/Slices/SharedSliceContent.ts +94 -0
- package/src/import/converters/fields/Slices/Slices.ts +20 -0
- package/src/import/converters/fields/Slices/index.ts +1 -0
- package/src/import/converters/fields/UID.ts +1 -5
- package/src/import/converters/fields/index.ts +1 -0
- package/src/import/converters/fields/nestable/Embed.ts +29 -7
- package/src/import/converters/fields/nestable/GeoPoint.ts +16 -0
- package/src/import/converters/fields/nestable/Image.ts +69 -44
- package/src/import/converters/fields/nestable/Link.ts +77 -0
- package/src/import/converters/fields/nestable/Nestable.ts +17 -6
- package/src/import/converters/fields/nestable/RichText/embedBlock.ts +14 -0
- package/src/import/converters/fields/nestable/RichText/imageBlock.ts +14 -0
- package/src/import/converters/fields/nestable/RichText/index.ts +23 -0
- package/src/import/converters/fields/nestable/RichText/textBlock.ts +35 -0
- package/src/import/converters/fields/nestable/index.ts +3 -0
- package/src/import/validators/Document.ts +4 -12
- package/src/import/validators/fields/ImportField.ts +12 -6
- package/src/import/validators/fields/ImportSlices/ImportSlices.ts +54 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +116 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/errors.ts +6 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/OptionalSharedSliceId.ts +20 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContent.ts +64 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/SharedSliceContentEntry.ts +100 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/errors.ts +10 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/index.ts +2 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceContent/types.ts +1 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceId.ts +65 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.ts +45 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.ts +45 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/fields/index.ts +5 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/index.ts +3 -0
- package/src/import/validators/fields/ImportSlices/SharedSlice/utils.ts +21 -0
- package/src/import/validators/fields/ImportSlices/index.ts +1 -0
- package/src/import/validators/fields/ImportSlices/utils.ts +43 -0
- package/src/import/validators/fields/UID.ts +2 -2
- package/src/import/validators/fields/nestable/Embed.ts +6 -51
- package/src/import/validators/fields/nestable/GeoPoint.ts +21 -0
- package/src/import/validators/fields/nestable/Image/default.ts +25 -0
- package/src/import/validators/fields/nestable/Image/index.ts +73 -0
- package/src/import/validators/fields/nestable/Image/merge.ts +64 -0
- package/src/import/validators/fields/nestable/Image/model.ts +18 -0
- package/src/import/validators/fields/nestable/Image/validators.ts +45 -0
- package/src/import/validators/fields/nestable/ImportRichText/ImportBlock.ts +39 -0
- package/src/import/validators/fields/nestable/ImportRichText/ImportRichText.ts +41 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.ts +44 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.ts +13 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.ts +13 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.ts +56 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/Span.ts +44 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/index.ts +4 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.ts +24 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.ts +8 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.ts +26 -0
- package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/index.ts +2 -0
- package/src/import/validators/fields/nestable/ImportRichText/index.ts +2 -0
- package/src/import/validators/fields/nestable/Link.ts +59 -0
- package/src/import/validators/fields/nestable/Nestable.ts +19 -4
- package/src/import/validators/fields/nestable/index.ts +5 -1
- package/src/utils/DocumentId.ts +8 -0
- package/src/utils/Objects.ts +2 -2
- package/src/utils/io-ts.ts +29 -0
- package/src/validators/BasicTypes.ts +13 -11
- package/src/validators/DefaultOrElse.ts +1 -1
- package/src/validators/NonEmptyString.ts +4 -5
- package/src/validators/NumberRange.ts +51 -0
- package/src/validators/function.ts +44 -0
- package/src/validators/index.ts +1 -0
- package/lib/import/validators/fields/nestable/Image.d.ts +0 -24
- package/lib/import/validators/fields/nestable/Image.js +0 -45
- package/src/import/validators/fields/nestable/Image.ts +0 -90
|
@@ -12,3 +12,23 @@ export declare function isEmpty(obj: object): boolean;
|
|
|
12
12
|
export declare function addType<A, O extends object, I, T extends string>(codec: t.Type<A, O, I>, t: T): t.Type<A, O & {
|
|
13
13
|
__TYPE__: T;
|
|
14
14
|
}, I>;
|
|
15
|
+
/**
|
|
16
|
+
* Returns a clone of the given codec that tries to find sub-error with message already set.
|
|
17
|
+
* If there is such error it just returns sub-errors array.
|
|
18
|
+
* If there is no such error it generates new error with given message.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* expect(
|
|
22
|
+
* withCustomError(
|
|
23
|
+
* t.type({age: withCustomError(t.number, () => 'Invalid child')}),
|
|
24
|
+
* () => "Invalid parent"
|
|
25
|
+
* )
|
|
26
|
+
* ).toSubsetEqualLeft([{message: "Invalid child"}])
|
|
27
|
+
* expect(
|
|
28
|
+
* withCustomError(
|
|
29
|
+
* t.type({age: t.number}),
|
|
30
|
+
* () => "Invalid parent"
|
|
31
|
+
* )
|
|
32
|
+
* ).toSubsetEqualLeft([{message: "Invalid parent"}])
|
|
33
|
+
*/
|
|
34
|
+
export declare function withCustomError<C extends t.Any>(codec: C, message: (i: t.InputOf<C>, c: t.Context) => string): C;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.addType = exports.isEmpty = exports.objectToMap = exports.grouped = exports.formatDate = exports.formatDateTime = exports.filterDouble = exports.refineType = exports.nullable = void 0;
|
|
3
|
+
exports.withCustomError = exports.addType = exports.isEmpty = exports.objectToMap = exports.grouped = exports.formatDate = exports.formatDateTime = exports.filterDouble = exports.refineType = exports.nullable = 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/function");
|
|
7
7
|
const t = (0, tslib_1.__importStar)(require("io-ts"));
|
|
8
|
+
const io_ts_types_1 = require("io-ts-types");
|
|
8
9
|
const mapOutput_1 = require("io-ts-types/mapOutput");
|
|
9
10
|
function nullable(c) {
|
|
10
11
|
return t.union([c, t.null, t.undefined]);
|
|
@@ -58,3 +59,42 @@ function addType(codec, t) {
|
|
|
58
59
|
return (0, mapOutput_1.mapOutput)(codec, (o) => ({ ...o, __TYPE__: t }));
|
|
59
60
|
}
|
|
60
61
|
exports.addType = addType;
|
|
62
|
+
/**
|
|
63
|
+
* Returns a clone of the given codec that tries to find sub-error with message already set.
|
|
64
|
+
* If there is such error it just returns sub-errors array.
|
|
65
|
+
* If there is no such error it generates new error with given message.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* expect(
|
|
69
|
+
* withCustomError(
|
|
70
|
+
* t.type({age: withCustomError(t.number, () => 'Invalid child')}),
|
|
71
|
+
* () => "Invalid parent"
|
|
72
|
+
* )
|
|
73
|
+
* ).toSubsetEqualLeft([{message: "Invalid child"}])
|
|
74
|
+
* expect(
|
|
75
|
+
* withCustomError(
|
|
76
|
+
* t.type({age: t.number}),
|
|
77
|
+
* () => "Invalid parent"
|
|
78
|
+
* )
|
|
79
|
+
* ).toSubsetEqualLeft([{message: "Invalid parent"}])
|
|
80
|
+
*/
|
|
81
|
+
function withCustomError(codec, message) {
|
|
82
|
+
return (0, io_ts_types_1.withValidate)(codec, (i, c) => {
|
|
83
|
+
return fp_ts_1.either.mapLeft((errors) => {
|
|
84
|
+
if (errors.find((error) => error.message)) {
|
|
85
|
+
return errors;
|
|
86
|
+
}
|
|
87
|
+
return [
|
|
88
|
+
{
|
|
89
|
+
value: i,
|
|
90
|
+
context: c,
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
92
|
+
message: message(i, c),
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
94
|
+
actual: i,
|
|
95
|
+
},
|
|
96
|
+
];
|
|
97
|
+
})(codec.validate(i, c));
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
exports.withCustomError = withCustomError;
|
|
@@ -9,6 +9,7 @@ export { default as IntFromPixels } from "./IntFromPixels";
|
|
|
9
9
|
export { default as NonEmptyString } from "./NonEmptyString";
|
|
10
10
|
export { default as NonEmptyStringOrNull } from "./NonEmptyStringOrNull";
|
|
11
11
|
export { default as NumberOrNull } from "./NumberOrNull";
|
|
12
|
+
export { default as NumberRange } from "./NumberRange";
|
|
12
13
|
export { default as StringFromBoolean } from "./StringFromBoolean";
|
|
13
14
|
export { default as StringFromNumber } from "./StringFromNumber";
|
|
14
15
|
export { default as StringOrNull } from "./StringOrNull";
|
package/lib/validators/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StringOrNull = exports.StringFromNumber = exports.StringFromBoolean = exports.NumberOrNull = exports.NonEmptyStringOrNull = exports.NonEmptyString = exports.IntFromPixels = exports.IntFromNumber = exports.Function = exports.DateFromTsMs = exports.DateFromStringOrNumber = exports.DateFromString = void 0;
|
|
3
|
+
exports.StringOrNull = exports.StringFromNumber = exports.StringFromBoolean = exports.NumberRange = exports.NumberOrNull = exports.NonEmptyStringOrNull = exports.NonEmptyString = exports.IntFromPixels = exports.IntFromNumber = exports.Function = exports.DateFromTsMs = exports.DateFromStringOrNumber = exports.DateFromString = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
(0, tslib_1.__exportStar)(require("./BasicTypes"), exports);
|
|
6
6
|
var DateFromString_1 = require("./DateFromString");
|
|
@@ -21,6 +21,8 @@ var NonEmptyStringOrNull_1 = require("./NonEmptyStringOrNull");
|
|
|
21
21
|
Object.defineProperty(exports, "NonEmptyStringOrNull", { enumerable: true, get: function () { return (0, tslib_1.__importDefault)(NonEmptyStringOrNull_1).default; } });
|
|
22
22
|
var NumberOrNull_1 = require("./NumberOrNull");
|
|
23
23
|
Object.defineProperty(exports, "NumberOrNull", { enumerable: true, get: function () { return (0, tslib_1.__importDefault)(NumberOrNull_1).default; } });
|
|
24
|
+
var NumberRange_1 = require("./NumberRange");
|
|
25
|
+
Object.defineProperty(exports, "NumberRange", { enumerable: true, get: function () { return (0, tslib_1.__importDefault)(NumberRange_1).default; } });
|
|
24
26
|
var StringFromBoolean_1 = require("./StringFromBoolean");
|
|
25
27
|
Object.defineProperty(exports, "StringFromBoolean", { enumerable: true, get: function () { return (0, tslib_1.__importDefault)(StringFromBoolean_1).default; } });
|
|
26
28
|
var StringFromNumber_1 = require("./StringFromNumber");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/types-internal",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.21",
|
|
4
4
|
"description": "Prismic types for Custom Types and Prismic Data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -67,7 +67,9 @@
|
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"fp-ts": "^2.11.8",
|
|
69
69
|
"io-ts": "^2.2.16",
|
|
70
|
-
"io-ts-types": "^0.5.16"
|
|
70
|
+
"io-ts-types": "^0.5.16",
|
|
71
|
+
"uuid": "^9.0.0",
|
|
72
|
+
"@types/uuid": "^9.0.2"
|
|
71
73
|
},
|
|
72
74
|
"engines": {
|
|
73
75
|
"node": ">=12.7.0"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type Asset = {
|
|
2
|
+
id: string
|
|
3
|
+
last_modified: string
|
|
4
|
+
kind: "image" | "all"
|
|
5
|
+
filename?: string
|
|
6
|
+
extension?: string
|
|
7
|
+
size?: number
|
|
8
|
+
origin_url: string
|
|
9
|
+
url: string
|
|
10
|
+
width?: number
|
|
11
|
+
height?: number
|
|
12
|
+
notes?: string
|
|
13
|
+
credits?: string
|
|
14
|
+
alt?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const getAssetOrThrow =
|
|
18
|
+
(assets: Record<Asset["id"], Asset | undefined>) =>
|
|
19
|
+
(assetId: Asset["id"]) => {
|
|
20
|
+
const asset = assets[assetId]
|
|
21
|
+
if (!asset) {
|
|
22
|
+
throw new Error(`Missing asset with id '${assetId}'!`)
|
|
23
|
+
}
|
|
24
|
+
return asset
|
|
25
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// This type represents response format from oEmbed API
|
|
2
|
+
export type Embed = {
|
|
3
|
+
type: string
|
|
4
|
+
version?: string | null
|
|
5
|
+
author_name?: string | null
|
|
6
|
+
author_url?: string | null
|
|
7
|
+
provider_name?: string | null
|
|
8
|
+
provider_url?: string | null
|
|
9
|
+
cache_age?: string | null
|
|
10
|
+
thumbnail_url?: string | null
|
|
11
|
+
thumbnail_width?: number | null
|
|
12
|
+
thumbnail_height?: number | null
|
|
13
|
+
html?: string | null
|
|
14
|
+
title?: string | null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const getEmbedOrThrow =
|
|
18
|
+
(embeds: Record<string, Embed | undefined>) => (embedUrl: string) => {
|
|
19
|
+
const embed = embeds[embedUrl]
|
|
20
|
+
if (!embed) throw new Error(`Missing embed with url '${embedUrl}'`)
|
|
21
|
+
return embed
|
|
22
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as t from "io-ts"
|
|
2
|
+
import { validate as validateUUID } from "uuid"
|
|
3
|
+
|
|
4
|
+
import { withCustomError } from "../validators/function"
|
|
5
|
+
|
|
6
|
+
interface UUIDBrand {
|
|
7
|
+
readonly UUID: unique symbol
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const UUID = withCustomError(
|
|
11
|
+
t.brand(
|
|
12
|
+
t.string,
|
|
13
|
+
(s): s is t.Branded<string, UUIDBrand> => validateUUID(s),
|
|
14
|
+
"UUID",
|
|
15
|
+
),
|
|
16
|
+
() => "The value must be a valid UUID",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
export type UUID = t.TypeOf<typeof UUID>
|
package/src/common/index.ts
CHANGED
|
@@ -198,7 +198,7 @@ const TextBlockCodec = (linkCodec: LinkCodec) =>
|
|
|
198
198
|
export const TextBlock = TextBlockCodec(Link)
|
|
199
199
|
export type TextBlock = t.TypeOf<typeof TextBlock>
|
|
200
200
|
|
|
201
|
-
const TextBlockLegacy = TextBlockCodec(LinkLegacy)
|
|
201
|
+
export const TextBlockLegacy = TextBlockCodec(LinkLegacy)
|
|
202
202
|
type TextBlockLegacy = t.TypeOf<typeof TextBlockLegacy>
|
|
203
203
|
|
|
204
204
|
const legacyBlockCodec = t.union([
|
|
@@ -8,6 +8,7 @@ import type { SharedSlice } from "./widgets/slices/SharedSlice"
|
|
|
8
8
|
import type { DynamicSlice } from "./widgets/slices/Slice"
|
|
9
9
|
import type { DynamicSlices } from "./widgets/slices/Slices"
|
|
10
10
|
import type { DynamicWidget } from "./widgets/Widget"
|
|
11
|
+
import type { StaticWidget } from "./widgets/Widget"
|
|
11
12
|
|
|
12
13
|
export const CustomTypeFormat = {
|
|
13
14
|
page: "page",
|
|
@@ -194,3 +195,15 @@ export function filterMissingSharedSlices(
|
|
|
194
195
|
return widget
|
|
195
196
|
})
|
|
196
197
|
}
|
|
198
|
+
|
|
199
|
+
export function flattenCustomTypeFields(
|
|
200
|
+
customType: StaticCustomType,
|
|
201
|
+
): Partial<Record<string, StaticWidget>> {
|
|
202
|
+
return Object.values(customType.json).reduce(
|
|
203
|
+
(acc, tab) => ({
|
|
204
|
+
...acc,
|
|
205
|
+
...tab,
|
|
206
|
+
}),
|
|
207
|
+
{},
|
|
208
|
+
)
|
|
209
|
+
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
+
import type { Asset, Embed } from "../../common"
|
|
1
2
|
import type { Document, WidgetContent } from "../../content"
|
|
2
3
|
import type { ImportDocument } from "../validators"
|
|
3
4
|
import type { ImportField } from "../validators/fields/ImportField"
|
|
4
|
-
import {
|
|
5
|
+
import { convertNestableWidget, importSlicesConverter } from "./fields"
|
|
5
6
|
|
|
6
7
|
export function convertImportToContent(
|
|
7
8
|
document: ImportDocument,
|
|
8
|
-
|
|
9
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
10
|
+
embeds: Record<string, Embed | undefined>,
|
|
9
11
|
): Document {
|
|
10
12
|
return Object.entries(document).reduce<Document>(
|
|
11
13
|
(acc, [fieldKey, fieldValue]) => {
|
|
12
|
-
const newFieldValue = convertWidget(fieldValue,
|
|
14
|
+
const newFieldValue = convertWidget(fieldValue, assets, embeds)
|
|
13
15
|
return newFieldValue ? { ...acc, [fieldKey]: newFieldValue } : acc
|
|
14
16
|
},
|
|
15
17
|
{},
|
|
@@ -18,12 +20,13 @@ export function convertImportToContent(
|
|
|
18
20
|
|
|
19
21
|
function convertWidget(
|
|
20
22
|
field: ImportField,
|
|
21
|
-
|
|
23
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
24
|
+
embeds: Record<string, Embed | undefined>,
|
|
22
25
|
): WidgetContent | undefined {
|
|
23
26
|
switch (field.type) {
|
|
24
|
-
case "
|
|
25
|
-
return
|
|
27
|
+
case "Slices":
|
|
28
|
+
return importSlicesConverter(field.value, assets, embeds)
|
|
26
29
|
default:
|
|
27
|
-
return convertNestableWidget(field,
|
|
30
|
+
return convertNestableWidget(field, assets, embeds)
|
|
28
31
|
}
|
|
29
32
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { v4 as uuid } from "uuid"
|
|
2
|
+
|
|
3
|
+
import type { Asset, Embed } from "../../../../common"
|
|
4
|
+
import type { SliceItemContent } from "../../../../content"
|
|
5
|
+
import type { SharedSlice as ImportSharedSlice } from "../../../validators/fields/ImportSlices/SharedSlice"
|
|
6
|
+
import { importSharedSliceContentConverter } from "./SharedSliceContent"
|
|
7
|
+
|
|
8
|
+
// TODO should we put it together with SharedSliceId validator?
|
|
9
|
+
const buildSharedSliceId = (slice_type: string) => `${slice_type}$${uuid()}`
|
|
10
|
+
|
|
11
|
+
export const sharedSliceConverter = (
|
|
12
|
+
slice: ImportSharedSlice,
|
|
13
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
14
|
+
embeds: Record<string, Embed | undefined>,
|
|
15
|
+
): SliceItemContent => {
|
|
16
|
+
// Right now we only support SharedSlices, if we support more types of slices in the future we'll need to select a correct converter here
|
|
17
|
+
const widget = importSharedSliceContentConverter(slice, assets, embeds)
|
|
18
|
+
return {
|
|
19
|
+
key: slice.id ?? buildSharedSliceId(slice.slice_type),
|
|
20
|
+
name: slice.slice_type,
|
|
21
|
+
maybeLabel: slice.slice_label ?? undefined,
|
|
22
|
+
widget,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { pipe } from "fp-ts/function"
|
|
2
|
+
import * as O from "fp-ts/Option"
|
|
3
|
+
import * as R from "fp-ts/Record"
|
|
4
|
+
|
|
5
|
+
import type { Asset, Embed } from "../../../../common"
|
|
6
|
+
import type { NestableContent, SharedSliceContent } from "../../../../content"
|
|
7
|
+
import {
|
|
8
|
+
GroupItemContentType,
|
|
9
|
+
SharedSliceContentType,
|
|
10
|
+
} from "../../../../content"
|
|
11
|
+
import type {
|
|
12
|
+
SharedSlice as ImportSharedSlice,
|
|
13
|
+
SharedSliceContent as ImportSharedSliceContent,
|
|
14
|
+
} from "../../../validators/fields/ImportSlices/SharedSlice"
|
|
15
|
+
import { convertNestableWidget } from "../nestable"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Converts the ImportSharedSliceContent which is a record of widget keys and ImportNestable values to a record of widget keys and NestableContent values.
|
|
19
|
+
*
|
|
20
|
+
* @param content a single ImportSharedSliceContent
|
|
21
|
+
* @param assets assets that are required for a conversion of a nestable widget
|
|
22
|
+
* @param embeds embeds that are required for a conversion of a nestable widget
|
|
23
|
+
*/
|
|
24
|
+
const sharedSliceContentConverter = (
|
|
25
|
+
content: ImportSharedSliceContent,
|
|
26
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
27
|
+
embeds: Record<string, Embed | undefined>,
|
|
28
|
+
): Record<string, NestableContent> =>
|
|
29
|
+
pipe(
|
|
30
|
+
content,
|
|
31
|
+
// convertNestableWidget can theoretically return undefined, so we need to filter out those values
|
|
32
|
+
R.filterMap((contentValue) =>
|
|
33
|
+
pipe(
|
|
34
|
+
contentValue,
|
|
35
|
+
(content) => convertNestableWidget(content, assets, embeds),
|
|
36
|
+
O.fromNullable,
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Converts a list of items to a list of repeatable widgets.
|
|
43
|
+
* Each ImportSharedSliceContent element is a record of fields, but in the content model it is represented as a list of tuples (key, value)
|
|
44
|
+
*
|
|
45
|
+
* For example, given the following slice content:
|
|
46
|
+
* {
|
|
47
|
+
* "slice_text": (ImportNestable),
|
|
48
|
+
* "slice_number": (ImportNestable),
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* has to be converted to:
|
|
52
|
+
*
|
|
53
|
+
* {
|
|
54
|
+
* __TYPE__: "GroupItemContentType",
|
|
55
|
+
* value: [
|
|
56
|
+
* ["slice_text", (NestableContent)],
|
|
57
|
+
* ["slice_number", (NestableContent)],
|
|
58
|
+
* ]
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* @param items list of items to be converted
|
|
62
|
+
* @param assets assets that are required for a conversion of a nestable widget
|
|
63
|
+
*/
|
|
64
|
+
const itemsConverter = (
|
|
65
|
+
items: ImportSharedSliceContent[],
|
|
66
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
67
|
+
embeds: Record<string, Embed | undefined>,
|
|
68
|
+
): SharedSliceContent["items"] =>
|
|
69
|
+
items.map((item) =>
|
|
70
|
+
pipe(
|
|
71
|
+
sharedSliceContentConverter(item, assets, embeds),
|
|
72
|
+
(record) => Object.entries(record),
|
|
73
|
+
(entries) => ({ __TYPE__: GroupItemContentType, value: entries }),
|
|
74
|
+
),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Builds SharedSliceContent model from ImportSharedSlice
|
|
79
|
+
* @param field ImportSharedSlice to be converted - a single slice from the slices array in the import document
|
|
80
|
+
* @param assets assets that are required for a conversion of a nestable widget
|
|
81
|
+
* @param embeds embeds that are required for a conversion of a nestable widget
|
|
82
|
+
*/
|
|
83
|
+
export const importSharedSliceContentConverter = (
|
|
84
|
+
field: ImportSharedSlice,
|
|
85
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
86
|
+
embeds: Record<string, Embed | undefined>,
|
|
87
|
+
): SharedSliceContent => ({
|
|
88
|
+
__TYPE__: SharedSliceContentType,
|
|
89
|
+
primary: field.primary
|
|
90
|
+
? sharedSliceContentConverter(field.primary, assets, embeds)
|
|
91
|
+
: {},
|
|
92
|
+
items: field.items ? itemsConverter(field.items, assets, embeds) : [],
|
|
93
|
+
variation: field.variation,
|
|
94
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Asset, Embed } from "../../../../common"
|
|
2
|
+
import type { SliceItemContent, SlicesContent } from "../../../../content"
|
|
3
|
+
import type { ImportSlices } from "../../../validators/fields/ImportSlices"
|
|
4
|
+
import { sharedSliceConverter } from "./SharedSlice"
|
|
5
|
+
|
|
6
|
+
export const importSlicesConverter = (
|
|
7
|
+
field: ImportSlices["value"],
|
|
8
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
9
|
+
embeds: Record<string, Embed | undefined>,
|
|
10
|
+
): SlicesContent | undefined => {
|
|
11
|
+
if (field === null) return
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
value: field.map((slice): SliceItemContent => {
|
|
15
|
+
// Right now we only support SharedSlices, if we support more types of slices in the future we'll need to select a correct converter here
|
|
16
|
+
return sharedSliceConverter(slice, assets, embeds)
|
|
17
|
+
}),
|
|
18
|
+
__TYPE__: "SliceContentType",
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Slices"
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import type { UIDContent } from "../../../content"
|
|
2
2
|
import type { ImportUID } from "../../validators/fields/UID"
|
|
3
3
|
|
|
4
|
-
export const uidConverter = (
|
|
5
|
-
field: ImportUID["value"],
|
|
6
|
-
): UIDContent | undefined => {
|
|
7
|
-
if (field === null) return
|
|
8
|
-
|
|
4
|
+
export const uidConverter = (field: ImportUID["value"]): UIDContent => {
|
|
9
5
|
return {
|
|
10
6
|
value: field,
|
|
11
7
|
__TYPE__: "UIDContent",
|
|
@@ -1,15 +1,37 @@
|
|
|
1
|
+
import type { Embed } from "../../../../common"
|
|
2
|
+
import { getEmbedOrThrow } from "../../../../common"
|
|
1
3
|
import type { EmbedContent } from "../../../../content"
|
|
4
|
+
import { withOptionals } from "../../../../utils/Objects"
|
|
2
5
|
import type { ImportEmbed } from "../../../validators"
|
|
3
6
|
|
|
4
7
|
export const embedConverter = (
|
|
5
8
|
field: ImportEmbed["value"],
|
|
9
|
+
embeds: Record<string, Embed | undefined>,
|
|
6
10
|
): EmbedContent | undefined => {
|
|
7
11
|
if (field === null) return
|
|
8
|
-
|
|
9
|
-
return
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const embed = getEmbedOrThrow(embeds)(field.embed_url)
|
|
13
|
+
return withOptionals<EmbedContent>(
|
|
14
|
+
{
|
|
15
|
+
embed_url: field.embed_url,
|
|
16
|
+
type: embed.type,
|
|
17
|
+
all: {
|
|
18
|
+
embed_url: field.embed_url,
|
|
19
|
+
...embed,
|
|
20
|
+
},
|
|
21
|
+
__TYPE__: "EmbedContent",
|
|
22
|
+
},
|
|
23
|
+
[
|
|
24
|
+
["version", embed.version],
|
|
25
|
+
["title", embed.title],
|
|
26
|
+
["author_name", embed.author_name],
|
|
27
|
+
["author_url", embed.author_url],
|
|
28
|
+
["provider_name", embed.provider_name],
|
|
29
|
+
["provider_url", embed.provider_url],
|
|
30
|
+
["cache_age", embed.cache_age],
|
|
31
|
+
["thumbnail_url", embed.thumbnail_url],
|
|
32
|
+
["thumbnail_width", embed.thumbnail_width],
|
|
33
|
+
["thumbnail_height", embed.thumbnail_height],
|
|
34
|
+
["html", embed.html],
|
|
35
|
+
],
|
|
36
|
+
)
|
|
15
37
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GeoPointContent } from "../../../../content"
|
|
2
|
+
import type { ImportGeoPoint } from "../../../validators"
|
|
3
|
+
|
|
4
|
+
export const geopointConverter = (
|
|
5
|
+
field: ImportGeoPoint["value"],
|
|
6
|
+
): GeoPointContent | undefined => {
|
|
7
|
+
if (field === null) return
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
position: {
|
|
11
|
+
lat: field.latitude,
|
|
12
|
+
lng: field.longitude,
|
|
13
|
+
},
|
|
14
|
+
__TYPE__: "GeoPointContent",
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -1,68 +1,93 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
1
|
+
import type { Asset } from "../../../../common"
|
|
2
|
+
import { getAssetOrThrow } from "../../../../common"
|
|
3
|
+
import type { ImageContent, ImageContentView } from "../../../../content"
|
|
4
|
+
import { withOptionals } from "../../../../utils/Objects"
|
|
3
5
|
import type { ImageField, ImportImage } from "../../../validators"
|
|
6
|
+
import type { ImportImageBlock } from "../../../validators/fields/nestable/ImportRichText/blocks";
|
|
4
7
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return withOptionals<
|
|
8
|
+
export function convertImageView<Input extends ImageField | ImportImageBlock>(
|
|
9
|
+
inputImage: Input,
|
|
10
|
+
image: Asset,
|
|
11
|
+
): ImageContentView {
|
|
12
|
+
// We can do that because anyway all properties being read
|
|
13
|
+
const imageField: ImageField = {
|
|
14
|
+
...inputImage,
|
|
15
|
+
edit:
|
|
16
|
+
"edit" in inputImage
|
|
17
|
+
? inputImage.edit
|
|
18
|
+
: {
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
zoom: 1,
|
|
22
|
+
},
|
|
23
|
+
dimensions: "dimensions" in inputImage ? inputImage.dimensions : {},
|
|
24
|
+
}
|
|
25
|
+
return withOptionals<ImageContentView>(
|
|
23
26
|
{
|
|
24
27
|
origin: {
|
|
25
|
-
id:
|
|
28
|
+
id: image.id,
|
|
26
29
|
url: image.origin_url,
|
|
27
|
-
width
|
|
28
|
-
|
|
30
|
+
// All images returned form Asset API should have width and height properties.
|
|
31
|
+
width: image.width ?? 0,
|
|
32
|
+
height: image.height ?? 0,
|
|
29
33
|
},
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
// All images returned form Asset API should have width and height properties.
|
|
35
|
+
// Image width and height will be only applied if there are no constraints and user didn't overwrite it
|
|
36
|
+
width: imageField?.dimensions?.width ?? image.width ?? 0,
|
|
37
|
+
height: imageField?.dimensions?.height ?? image.height ?? 0,
|
|
38
|
+
// If edit is not provided, we crop constraint width and height from the left upper corner.
|
|
39
|
+
// WARN: If constraints are greater than image dimensions cut outside the image (background will fill extra space).
|
|
32
40
|
edit: {
|
|
33
|
-
zoom:
|
|
41
|
+
zoom: imageField.edit?.zoom ?? 1,
|
|
34
42
|
crop: {
|
|
35
|
-
x:
|
|
36
|
-
y:
|
|
43
|
+
x: imageField.edit?.x ?? 0,
|
|
44
|
+
y: imageField.edit?.y ?? 0,
|
|
37
45
|
},
|
|
38
|
-
background:
|
|
46
|
+
background:
|
|
47
|
+
imageField.edit?.background ??
|
|
48
|
+
(image.extension === "png" ? "transparent" : "#ffffff"),
|
|
39
49
|
},
|
|
40
50
|
url: image.url,
|
|
41
51
|
},
|
|
42
52
|
[
|
|
43
|
-
["alt",
|
|
44
|
-
["credits",
|
|
53
|
+
["alt", imageField?.alt || image.alt],
|
|
54
|
+
["credits", imageField?.copyright || image.credits],
|
|
45
55
|
],
|
|
46
56
|
)
|
|
47
57
|
}
|
|
48
58
|
|
|
59
|
+
function convertThumbnails(
|
|
60
|
+
imageField: Exclude<ImportImage["value"], null>,
|
|
61
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
62
|
+
) {
|
|
63
|
+
return Object.entries(imageField.thumbnails).reduce(
|
|
64
|
+
(acc, [thumbnailName, thumbnail]) => {
|
|
65
|
+
return {
|
|
66
|
+
...acc,
|
|
67
|
+
[thumbnailName]: convertImageView(
|
|
68
|
+
thumbnail,
|
|
69
|
+
getAssetOrThrow(assets)(thumbnail.id),
|
|
70
|
+
),
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
{},
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// All assets from `imageField` must be present in `assets`
|
|
78
|
+
// If not then function will throw an error
|
|
49
79
|
export const imageConverter = (
|
|
50
|
-
|
|
51
|
-
|
|
80
|
+
imageField: ImportImage["value"],
|
|
81
|
+
assets: Record<Asset["id"], Asset | undefined>,
|
|
52
82
|
): ImageContent | undefined => {
|
|
53
|
-
|
|
54
|
-
const image = images.get(id)
|
|
55
|
-
if (!image) throw new Error(`Missing asset with id '${id}'`)
|
|
56
|
-
return image
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!field) return
|
|
83
|
+
if (!imageField) return
|
|
60
84
|
|
|
61
85
|
return {
|
|
62
|
-
...
|
|
63
|
-
|
|
64
|
-
|
|
86
|
+
...convertImageView<ImageField>(
|
|
87
|
+
imageField,
|
|
88
|
+
getAssetOrThrow(assets)(imageField.id),
|
|
65
89
|
),
|
|
66
|
-
|
|
90
|
+
thumbnails: convertThumbnails(imageField, assets),
|
|
91
|
+
__TYPE__: "ImageContent" as const,
|
|
67
92
|
}
|
|
68
93
|
}
|