@platecms/delta-client 1.0.0 → 1.3.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/README.md +8 -2
- package/package.json +17 -9
- package/src/__generated__/fragment-masking.ts +87 -0
- package/src/__generated__/gql.ts +142 -0
- package/src/__generated__/graphql.ts +3787 -0
- package/src/__generated__/{index.d.ts → index.ts} +1 -1
- package/src/api/fetchContentExperiences.spec.ts +82 -0
- package/src/api/fetchContentExperiences.ts +42 -0
- package/src/api/fetchContentItems.spec.ts +62 -0
- package/src/api/fetchContentItems.ts +42 -0
- package/src/api/fetchPathParts.spec.ts +62 -0
- package/src/api/fetchPathParts.ts +42 -0
- package/src/api/fetchTags.spec.ts +61 -0
- package/src/api/fetchTags.ts +38 -0
- package/src/api/index.ts +4 -0
- package/src/api/types.ts +35 -0
- package/src/apollo/index.ts +69 -0
- package/src/app/utils/paginated-response.type.ts +19 -0
- package/src/factories/BaseFactory.ts +19 -0
- package/src/factories/ChannelFactory.ts +19 -0
- package/src/factories/ContentExperienceFactory.ts +54 -0
- package/src/factories/PathPartFactory.ts +26 -0
- package/src/factories/TagFactory.ts +30 -0
- package/src/graphql/content-experiences/content-experiences.query.gql +82 -0
- package/src/graphql/content-items/content-items.query.gql +47 -0
- package/src/graphql/fragments/asset.fragment.gql +10 -0
- package/src/graphql/fragments/asset.fragments.gql +10 -0
- package/src/graphql/fragments/building-block-field-fullfillment.fragments.gql +45 -0
- package/src/graphql/fragments/building-block-field.fragments.gql +8 -0
- package/src/graphql/fragments/building-block.fragments.gql +11 -0
- package/src/graphql/fragments/content-experience.fragments.gql +8 -0
- package/src/graphql/fragments/content-field.fragments.gql +7 -0
- package/src/graphql/fragments/content-item.fragments.gql +11 -0
- package/src/graphql/fragments/content-type.fragments.gql +11 -0
- package/src/graphql/fragments/content-values.fragments.gql +33 -0
- package/src/graphql/fragments/experience-component.fragments.gql +13 -0
- package/src/graphql/fragments/grid-placement.fragments.gql +7 -0
- package/src/graphql/fragments/path-part.fragments.gql +8 -0
- package/src/graphql/fragments/tag.fragment.gql +17 -0
- package/src/graphql/path-parts/path-parts.query.gql +52 -0
- package/src/graphql/tags/tag.query.gql +17 -0
- package/src/graphql/tags/tags.fragments.gql +17 -0
- package/src/index.ts +1 -0
- package/src/schema/index.spec.ts +211 -0
- package/src/schema/{index.d.ts → index.ts} +4 -0
- package/src/schema/lib/schemas/array.spec.ts +126 -0
- package/src/schema/lib/schemas/array.ts +46 -0
- package/src/schema/lib/schemas/asset.spec.ts +101 -0
- package/src/schema/lib/schemas/asset.ts +13 -0
- package/src/schema/lib/schemas/baseSchema.ts +42 -0
- package/src/schema/lib/schemas/boolean.spec.ts +65 -0
- package/src/schema/lib/schemas/boolean.ts +12 -0
- package/src/schema/lib/schemas/buildingBlock.spec.ts +56 -0
- package/src/schema/lib/schemas/buildingBlock.ts +39 -0
- package/src/schema/lib/schemas/contentItem.spec.ts +61 -0
- package/src/schema/lib/schemas/contentItem.ts +39 -0
- package/src/schema/lib/schemas/contentType.spec.ts +113 -0
- package/src/schema/lib/schemas/contentType.ts +13 -0
- package/src/schema/lib/schemas/date.spec.ts +82 -0
- package/src/schema/lib/schemas/date.ts +17 -0
- package/src/schema/lib/schemas/gridPlacement.spec.ts +77 -0
- package/src/schema/lib/schemas/gridPlacement.ts +13 -0
- package/src/schema/lib/schemas/index.ts +66 -0
- package/src/schema/lib/schemas/number.spec.ts +65 -0
- package/src/schema/lib/schemas/number.ts +12 -0
- package/src/schema/lib/schemas/pathPart.spec.ts +120 -0
- package/src/schema/lib/schemas/pathPart.ts +13 -0
- package/src/schema/lib/schemas/smartText.spec.ts +105 -0
- package/src/schema/lib/schemas/smartText.ts +13 -0
- package/src/schema/lib/schemas/string.spec.ts +65 -0
- package/src/schema/lib/schemas/string.ts +12 -0
- package/src/schema/lib/schemas/tag.spec.ts +89 -0
- package/src/schema/lib/schemas/tag.ts +13 -0
- package/src/schema/lib/utils/index.spec.ts +160 -0
- package/src/schema/lib/utils/index.ts +11 -0
- package/src/schema/lib/utils/isContentValue.spec.ts +111 -0
- package/src/schema/lib/utils/isContentValue.ts +17 -0
- package/src/schema/lib/utils/isPrimitiveValue.spec.ts +38 -0
- package/src/schema/lib/utils/isPrimitiveValue.ts +7 -0
- package/src/slate/index.ts +95 -0
- package/src/utils/index.ts +15 -0
- package/src/utils/lib/connectors/BaseConnector.ts +26 -0
- package/src/utils/lib/connectors/WindowConnector.ts +70 -0
- package/src/utils/lib/events/ConnectorEvents.ts +89 -0
- package/src/utils/lib/events/EventEmitter.ts +19 -0
- package/src/__generated__/fragment-masking.d.ts +0 -19
- package/src/__generated__/fragment-masking.js +0 -22
- package/src/__generated__/fragment-masking.js.map +0 -1
- package/src/__generated__/gql.d.ts +0 -12
- package/src/__generated__/gql.js +0 -13
- package/src/__generated__/gql.js.map +0 -1
- package/src/__generated__/graphql.d.ts +0 -2157
- package/src/__generated__/graphql.js +0 -70
- package/src/__generated__/graphql.js.map +0 -1
- package/src/__generated__/index.js +0 -6
- package/src/__generated__/index.js.map +0 -1
- package/src/api/index.d.ts +0 -2
- package/src/api/index.js +0 -4
- package/src/api/index.js.map +0 -1
- package/src/apollo/index.d.ts +0 -7
- package/src/apollo/index.js +0 -40
- package/src/apollo/index.js.map +0 -1
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -3
- package/src/index.js.map +0 -1
- package/src/schema/index.js +0 -18
- package/src/schema/index.js.map +0 -1
- package/src/schema/lib/schemas/array.d.ts +0 -6
- package/src/schema/lib/schemas/array.js +0 -20
- package/src/schema/lib/schemas/array.js.map +0 -1
- package/src/schema/lib/schemas/asset.d.ts +0 -5
- package/src/schema/lib/schemas/asset.js +0 -14
- package/src/schema/lib/schemas/asset.js.map +0 -1
- package/src/schema/lib/schemas/baseSchema.d.ts +0 -10
- package/src/schema/lib/schemas/baseSchema.js +0 -36
- package/src/schema/lib/schemas/baseSchema.js.map +0 -1
- package/src/schema/lib/schemas/boolean.d.ts +0 -4
- package/src/schema/lib/schemas/boolean.js +0 -15
- package/src/schema/lib/schemas/boolean.js.map +0 -1
- package/src/schema/lib/schemas/buildingBlock.d.ts +0 -7
- package/src/schema/lib/schemas/buildingBlock.js +0 -26
- package/src/schema/lib/schemas/buildingBlock.js.map +0 -1
- package/src/schema/lib/schemas/contentItem.d.ts +0 -6
- package/src/schema/lib/schemas/contentItem.js +0 -26
- package/src/schema/lib/schemas/contentItem.js.map +0 -1
- package/src/schema/lib/schemas/contentType.d.ts +0 -5
- package/src/schema/lib/schemas/contentType.js +0 -14
- package/src/schema/lib/schemas/contentType.js.map +0 -1
- package/src/schema/lib/schemas/date.d.ts +0 -5
- package/src/schema/lib/schemas/date.js +0 -19
- package/src/schema/lib/schemas/date.js.map +0 -1
- package/src/schema/lib/schemas/gridPlacement.d.ts +0 -5
- package/src/schema/lib/schemas/gridPlacement.js +0 -14
- package/src/schema/lib/schemas/gridPlacement.js.map +0 -1
- package/src/schema/lib/schemas/index.d.ts +0 -55
- package/src/schema/lib/schemas/index.js +0 -33
- package/src/schema/lib/schemas/index.js.map +0 -1
- package/src/schema/lib/schemas/number.d.ts +0 -4
- package/src/schema/lib/schemas/number.js +0 -15
- package/src/schema/lib/schemas/number.js.map +0 -1
- package/src/schema/lib/schemas/pathPart.d.ts +0 -5
- package/src/schema/lib/schemas/pathPart.js +0 -14
- package/src/schema/lib/schemas/pathPart.js.map +0 -1
- package/src/schema/lib/schemas/smartText.d.ts +0 -5
- package/src/schema/lib/schemas/smartText.js +0 -14
- package/src/schema/lib/schemas/smartText.js.map +0 -1
- package/src/schema/lib/schemas/string.d.ts +0 -4
- package/src/schema/lib/schemas/string.js +0 -15
- package/src/schema/lib/schemas/string.js.map +0 -1
- package/src/schema/lib/schemas/tag.d.ts +0 -5
- package/src/schema/lib/schemas/tag.js +0 -14
- package/src/schema/lib/schemas/tag.js.map +0 -1
- package/src/schema/lib/utils/isContentValue.d.ts +0 -2
- package/src/schema/lib/utils/isContentValue.js +0 -17
- package/src/schema/lib/utils/isContentValue.js.map +0 -1
- package/src/schema/lib/utils/isPrimitiveValue.d.ts +0 -5
- package/src/schema/lib/utils/isPrimitiveValue.js +0 -8
- package/src/schema/lib/utils/isPrimitiveValue.js.map +0 -1
- package/src/slate/index.d.ts +0 -65
- package/src/slate/index.js +0 -3
- package/src/slate/index.js.map +0 -1
- package/src/utils/index.d.ts +0 -6
- package/src/utils/index.js +0 -6
- package/src/utils/index.js.map +0 -1
- package/src/utils/lib/connectors/BaseConnector.d.ts +0 -16
- package/src/utils/lib/connectors/BaseConnector.js +0 -17
- package/src/utils/lib/connectors/BaseConnector.js.map +0 -1
- package/src/utils/lib/connectors/WindowConnector.d.ts +0 -10
- package/src/utils/lib/connectors/WindowConnector.js +0 -53
- package/src/utils/lib/connectors/WindowConnector.js.map +0 -1
- package/src/utils/lib/events/ConnectorEvents.d.ts +0 -63
- package/src/utils/lib/events/ConnectorEvents.js +0 -24
- package/src/utils/lib/events/ConnectorEvents.js.map +0 -1
- package/src/utils/lib/events/EventEmitter.d.ts +0 -7
- package/src/utils/lib/events/EventEmitter.js +0 -21
- package/src/utils/lib/events/EventEmitter.js.map +0 -1
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { BuildingBlockField, ContentItem, ExperienceComponent } from "../__generated__/graphql";
|
|
3
|
+
import { schema } from "./lib/schemas";
|
|
4
|
+
import { c } from "@platecms/delta-castscript";
|
|
5
|
+
|
|
6
|
+
const johnDoe: ContentItem = {
|
|
7
|
+
contentValues: [
|
|
8
|
+
{
|
|
9
|
+
contentField: {
|
|
10
|
+
name: "name",
|
|
11
|
+
},
|
|
12
|
+
primitiveValue: "John Doe",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
contentField: {
|
|
16
|
+
name: "email",
|
|
17
|
+
},
|
|
18
|
+
primitiveValue: "john.doe@example.com",
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
} as unknown as ContentItem;
|
|
22
|
+
|
|
23
|
+
const broccoli: ContentItem = {
|
|
24
|
+
contentValues: [
|
|
25
|
+
{
|
|
26
|
+
contentField: {
|
|
27
|
+
name: "name",
|
|
28
|
+
},
|
|
29
|
+
primitiveValue: "Broccoli",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
contentField: {
|
|
33
|
+
name: "calories",
|
|
34
|
+
},
|
|
35
|
+
primitiveValue: 200,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
contentField: {
|
|
39
|
+
name: "author",
|
|
40
|
+
},
|
|
41
|
+
relatedContentItem: johnDoe,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
} as unknown as ContentItem;
|
|
45
|
+
|
|
46
|
+
const carrot: ContentItem = {
|
|
47
|
+
contentValues: [
|
|
48
|
+
{
|
|
49
|
+
contentField: {
|
|
50
|
+
name: "name",
|
|
51
|
+
},
|
|
52
|
+
primitiveValue: "Carrot",
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
contentField: {
|
|
56
|
+
name: "calories",
|
|
57
|
+
},
|
|
58
|
+
primitiveValue: 100,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
} as unknown as ContentItem;
|
|
62
|
+
|
|
63
|
+
const experienceComponent: ExperienceComponent = {
|
|
64
|
+
buildingBlockFieldFulfillments: [
|
|
65
|
+
{
|
|
66
|
+
buildingBlockField: {
|
|
67
|
+
prn: "123",
|
|
68
|
+
slug: "title",
|
|
69
|
+
} as unknown as BuildingBlockField,
|
|
70
|
+
contentValue: {
|
|
71
|
+
primitiveValue: "Hello back!",
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
buildingBlockField: {
|
|
76
|
+
prn: "123",
|
|
77
|
+
slug: "description",
|
|
78
|
+
} as unknown as BuildingBlockField,
|
|
79
|
+
contentValue: {
|
|
80
|
+
interpolatedSmartText: c("root", [c("paragraph", "Hello back again!")]),
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
buildingBlockField: {
|
|
85
|
+
prn: "123",
|
|
86
|
+
slug: "vegatables",
|
|
87
|
+
} as unknown as BuildingBlockField,
|
|
88
|
+
contentValue: {
|
|
89
|
+
relatedContentItem: broccoli,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
buildingBlockField: {
|
|
94
|
+
prn: "123",
|
|
95
|
+
slug: "vegatables",
|
|
96
|
+
} as unknown as BuildingBlockField,
|
|
97
|
+
contentValue: {
|
|
98
|
+
relatedContentItem: carrot,
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
buildingBlockField: {
|
|
103
|
+
prn: "123",
|
|
104
|
+
slug: "tags",
|
|
105
|
+
} as unknown as BuildingBlockField,
|
|
106
|
+
contentValue: {
|
|
107
|
+
linkedTag: {
|
|
108
|
+
prn: "123",
|
|
109
|
+
name: "title",
|
|
110
|
+
path: "title",
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
buildingBlockField: {
|
|
116
|
+
prn: "123",
|
|
117
|
+
slug: "type",
|
|
118
|
+
} as unknown as BuildingBlockField,
|
|
119
|
+
contentValue: {
|
|
120
|
+
linkedContentType: {
|
|
121
|
+
prn: "123",
|
|
122
|
+
name: "title",
|
|
123
|
+
contentFields: [
|
|
124
|
+
{
|
|
125
|
+
name: "title",
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
} as unknown as ExperienceComponent;
|
|
133
|
+
|
|
134
|
+
describe("Schema", () => {
|
|
135
|
+
it("should parse an building block with its values", () => {
|
|
136
|
+
const buildingBlock = schema.buildingBlock({
|
|
137
|
+
title: [schema.number(), schema.string()],
|
|
138
|
+
description: schema.smartText(),
|
|
139
|
+
vegatables: schema.array(
|
|
140
|
+
schema.contentItem({
|
|
141
|
+
name: [schema.string(), schema.number()],
|
|
142
|
+
calories: schema.number(),
|
|
143
|
+
author: schema.contentItem({
|
|
144
|
+
name: schema.string(),
|
|
145
|
+
email: schema.string(),
|
|
146
|
+
}),
|
|
147
|
+
}),
|
|
148
|
+
),
|
|
149
|
+
tags: schema.array(schema.tag()),
|
|
150
|
+
type: schema.contentType(),
|
|
151
|
+
isVisible: schema.boolean().placeholder(true),
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const result = buildingBlock.parse(experienceComponent.buildingBlockFieldFulfillments, {
|
|
155
|
+
placeholders: true,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
expect(result).toEqual({
|
|
159
|
+
title: "Hello back!",
|
|
160
|
+
isVisible: true,
|
|
161
|
+
description: {
|
|
162
|
+
type: "root",
|
|
163
|
+
children: [
|
|
164
|
+
{
|
|
165
|
+
type: "paragraph",
|
|
166
|
+
children: [
|
|
167
|
+
{
|
|
168
|
+
type: "text",
|
|
169
|
+
value: "Hello back again!",
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
},
|
|
175
|
+
vegatables: [
|
|
176
|
+
{
|
|
177
|
+
name: "Broccoli",
|
|
178
|
+
calories: 200,
|
|
179
|
+
author: {
|
|
180
|
+
name: "John Doe",
|
|
181
|
+
email: "john.doe@example.com",
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
name: "Carrot",
|
|
186
|
+
calories: 100,
|
|
187
|
+
author: {
|
|
188
|
+
name: null,
|
|
189
|
+
email: null,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
tags: [
|
|
194
|
+
{
|
|
195
|
+
prn: "123",
|
|
196
|
+
name: "title",
|
|
197
|
+
path: "title",
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
type: {
|
|
201
|
+
prn: "123",
|
|
202
|
+
name: "title",
|
|
203
|
+
contentFields: [
|
|
204
|
+
{
|
|
205
|
+
name: "title",
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from "./lib/schemas";
|
|
2
|
+
|
|
2
3
|
export * from "./lib/schemas/contentItem";
|
|
3
4
|
export * from "./lib/schemas/buildingBlock";
|
|
4
5
|
export * from "./lib/schemas/array";
|
|
6
|
+
|
|
5
7
|
export * from "./lib/schemas/string";
|
|
6
8
|
export * from "./lib/schemas/number";
|
|
7
9
|
export * from "./lib/schemas/boolean";
|
|
@@ -12,3 +14,5 @@ export * from "./lib/schemas/gridPlacement";
|
|
|
12
14
|
export * from "./lib/schemas/contentType";
|
|
13
15
|
export * from "./lib/schemas/pathPart";
|
|
14
16
|
export * from "./lib/schemas/tag";
|
|
17
|
+
|
|
18
|
+
export * from "./../__generated__/graphql";
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { schema as schemaBuilder } from ".";
|
|
3
|
+
|
|
4
|
+
describe("Array schema", () => {
|
|
5
|
+
it("should parse an array of content values", () => {
|
|
6
|
+
const testSchema = schemaBuilder.array(schemaBuilder.string());
|
|
7
|
+
|
|
8
|
+
const result = testSchema.parse([
|
|
9
|
+
{
|
|
10
|
+
primitiveValue: "hello",
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
primitiveValue: "world",
|
|
14
|
+
},
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
expect(result).toEqual(["hello", "world"]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return an empty array when parsing a non array", () => {
|
|
21
|
+
const testSchema = schemaBuilder.array(schemaBuilder.string());
|
|
22
|
+
|
|
23
|
+
const result = testSchema.parse([]);
|
|
24
|
+
|
|
25
|
+
expect(result).toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should return an empty array when parsing an empty array", () => {
|
|
29
|
+
const testSchema = schemaBuilder.array(schemaBuilder.string());
|
|
30
|
+
|
|
31
|
+
const result = testSchema.parse([]);
|
|
32
|
+
|
|
33
|
+
expect(result).toEqual([]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should display string placholders when parsing an array", () => {
|
|
37
|
+
const testSchema = schemaBuilder
|
|
38
|
+
.array(schemaBuilder.string().placeholder("placeholder"))
|
|
39
|
+
.placeholder({ minLength: 2 });
|
|
40
|
+
|
|
41
|
+
const result = testSchema.parse([], {
|
|
42
|
+
placeholders: true,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
expect(result).toEqual(["placeholder", "placeholder"]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should display placholders for content items when parsing an arraray", () => {
|
|
49
|
+
const testSchema = schemaBuilder
|
|
50
|
+
.array(
|
|
51
|
+
schemaBuilder.contentItem({
|
|
52
|
+
title: schemaBuilder.string().placeholder("title"),
|
|
53
|
+
}),
|
|
54
|
+
)
|
|
55
|
+
.placeholder({ minLength: 2 });
|
|
56
|
+
|
|
57
|
+
const result = testSchema.parse([], {
|
|
58
|
+
placeholders: true,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(result).toEqual([
|
|
62
|
+
{
|
|
63
|
+
title: "title",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
title: "title",
|
|
67
|
+
},
|
|
68
|
+
]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should display placholders when parsing an array", () => {
|
|
72
|
+
const testSchema = schemaBuilder
|
|
73
|
+
.array(schemaBuilder.string().placeholder("placeholder"))
|
|
74
|
+
.placeholder({ minLength: 1 });
|
|
75
|
+
|
|
76
|
+
const result = testSchema.parse([]);
|
|
77
|
+
|
|
78
|
+
expect(result).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("when using multiple schemas", () => {
|
|
82
|
+
it("should parse an array of strings and numbers", () => {
|
|
83
|
+
const testSchema = schemaBuilder.array([schemaBuilder.string(), schemaBuilder.number()]);
|
|
84
|
+
|
|
85
|
+
const result = testSchema.parse([
|
|
86
|
+
{
|
|
87
|
+
primitiveValue: "hello",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
primitiveValue: 42,
|
|
91
|
+
},
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
expect(result).toEqual(["hello", 42]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should parse an array of content values", () => {
|
|
98
|
+
const testSchema = schemaBuilder.array(
|
|
99
|
+
schemaBuilder.contentItem({
|
|
100
|
+
name: schemaBuilder.string(),
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const result = testSchema.parse([
|
|
105
|
+
{
|
|
106
|
+
relatedContentItem: {
|
|
107
|
+
contentValues: [
|
|
108
|
+
{
|
|
109
|
+
contentField: {
|
|
110
|
+
name: "name",
|
|
111
|
+
},
|
|
112
|
+
primitiveValue: "hello",
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
expect(result).toEqual([
|
|
120
|
+
{
|
|
121
|
+
name: "hello",
|
|
122
|
+
},
|
|
123
|
+
]);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Schema, SchemaConfig } from ".";
|
|
2
|
+
import { compact, first } from "../utils";
|
|
3
|
+
|
|
4
|
+
export class ArraySchema<T extends Schema | Schema[]>
|
|
5
|
+
implements
|
|
6
|
+
Schema<
|
|
7
|
+
T extends Schema[] ? ReturnType<T[number]["parse"]>[] : T extends Schema ? ReturnType<T["parse"]>[] : never,
|
|
8
|
+
unknown
|
|
9
|
+
>
|
|
10
|
+
{
|
|
11
|
+
private _minLength: number = 0;
|
|
12
|
+
|
|
13
|
+
public constructor(private readonly structure: T) {}
|
|
14
|
+
|
|
15
|
+
public placeholder({ minLength }: { minLength: number }): this {
|
|
16
|
+
this._minLength = minLength;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public parse(
|
|
21
|
+
data: unknown,
|
|
22
|
+
config?: SchemaConfig,
|
|
23
|
+
): T extends Schema[] ? ReturnType<T[number]["parse"]>[] : T extends Schema ? ReturnType<T["parse"]>[] : never {
|
|
24
|
+
const dataAsArray = Array.isArray(data) ? data : [];
|
|
25
|
+
|
|
26
|
+
if (dataAsArray.length < this._minLength) {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
28
|
+
dataAsArray.push(...Array(this._minLength - dataAsArray.length).fill(null));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const results = dataAsArray.map((item) => {
|
|
32
|
+
const structures: Schema[] = Array.isArray(this.structure) ? this.structure : [this.structure];
|
|
33
|
+
const resultForStructures = structures.map((schema) => schema.parse(item, config));
|
|
34
|
+
|
|
35
|
+
return resultForStructures.find((result) => result !== null) ?? first(resultForStructures);
|
|
36
|
+
}) as T extends Schema[] ? ReturnType<T[number]["parse"]>[] : T extends Schema ? ReturnType<T["parse"]>[] : never;
|
|
37
|
+
|
|
38
|
+
return config?.placeholders === true
|
|
39
|
+
? results
|
|
40
|
+
: (compact(results) as T extends Schema[]
|
|
41
|
+
? ReturnType<T[number]["parse"]>[]
|
|
42
|
+
: T extends Schema
|
|
43
|
+
? ReturnType<T["parse"]>[]
|
|
44
|
+
: never);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { AssetSchema } from "./asset";
|
|
3
|
+
|
|
4
|
+
describe("Asset schema", () => {
|
|
5
|
+
it("should parse to an asset when parsing an asset content value", () => {
|
|
6
|
+
const schema = new AssetSchema();
|
|
7
|
+
|
|
8
|
+
const result = schema.parse([
|
|
9
|
+
{
|
|
10
|
+
relatedAsset: {
|
|
11
|
+
prn: "123",
|
|
12
|
+
name: "title",
|
|
13
|
+
url: "url",
|
|
14
|
+
mimeType: "image/png",
|
|
15
|
+
fileSize: 100,
|
|
16
|
+
width: 100,
|
|
17
|
+
height: 100,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
expect(result).toEqual({
|
|
23
|
+
prn: "123",
|
|
24
|
+
name: "title",
|
|
25
|
+
url: "url",
|
|
26
|
+
mimeType: "image/png",
|
|
27
|
+
fileSize: 100,
|
|
28
|
+
width: 100,
|
|
29
|
+
height: 100,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should return null when parsing an a non asset content value", () => {
|
|
34
|
+
const schema = new AssetSchema();
|
|
35
|
+
|
|
36
|
+
const result = schema.parse([
|
|
37
|
+
{
|
|
38
|
+
primitiveValue: "hello",
|
|
39
|
+
},
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
expect(result).toEqual(null);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("when placeholders are enabled", () => {
|
|
46
|
+
it("should return the placeholder when parsing an empty array", () => {
|
|
47
|
+
const schema = new AssetSchema().placeholder({
|
|
48
|
+
prn: "123",
|
|
49
|
+
url: "url",
|
|
50
|
+
mimeType: "image/png",
|
|
51
|
+
fileSize: 100,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const result = schema.parse([], {
|
|
55
|
+
placeholders: true,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(result).toEqual({
|
|
59
|
+
prn: "123",
|
|
60
|
+
url: "url",
|
|
61
|
+
mimeType: "image/png",
|
|
62
|
+
fileSize: 100,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should return the placeholder when parsing an non asset content value", () => {
|
|
67
|
+
const schema = new AssetSchema().placeholder({
|
|
68
|
+
prn: "123",
|
|
69
|
+
url: "url",
|
|
70
|
+
mimeType: "image/png",
|
|
71
|
+
fileSize: 100,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const result = schema.parse(
|
|
75
|
+
[
|
|
76
|
+
{
|
|
77
|
+
primitiveValue: true,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
{
|
|
81
|
+
placeholders: true,
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
expect(result).toEqual({
|
|
86
|
+
prn: "123",
|
|
87
|
+
url: "url",
|
|
88
|
+
mimeType: "image/png",
|
|
89
|
+
fileSize: 100,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("when nullable is disabled", () => {
|
|
95
|
+
it("should throw an error when parsing an non asset content value", () => {
|
|
96
|
+
const schema = new AssetSchema().nullable(false);
|
|
97
|
+
|
|
98
|
+
expect(() => schema.parse([{ primitiveValue: 42 }])).toThrow("Data could not be found and is not nullable");
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Asset } from "../../../__generated__/graphql";
|
|
2
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
3
|
+
import { BaseSchema } from "./baseSchema";
|
|
4
|
+
|
|
5
|
+
export class AssetSchema extends BaseSchema<Asset | null, unknown> {
|
|
6
|
+
protected override findValue(data: unknown): Asset | null {
|
|
7
|
+
if (isContentValue(data) && "relatedAsset" in data) {
|
|
8
|
+
return (data as { relatedAsset: Asset }).relatedAsset;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ResultType, Schema, SchemaConfig } from ".";
|
|
2
|
+
import { isArray } from "../utils";
|
|
3
|
+
|
|
4
|
+
export abstract class BaseSchema<TResult extends ResultType, TData> implements Schema<TResult, TData> {
|
|
5
|
+
protected _isNullable = true;
|
|
6
|
+
protected _placeholder: TResult | null = null;
|
|
7
|
+
|
|
8
|
+
public nullable(value: boolean = false): this {
|
|
9
|
+
this._isNullable = value;
|
|
10
|
+
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public placeholder(placeholder: Partial<TResult>): this {
|
|
15
|
+
this._placeholder = placeholder as TResult;
|
|
16
|
+
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public parse(data: TData | TData[], config?: SchemaConfig): TResult | null {
|
|
21
|
+
if (isArray(data)) {
|
|
22
|
+
data = data[this.findIndex(data)];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (this.findValue(data) !== null) {
|
|
26
|
+
return this.findValue(data);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!this._isNullable) {
|
|
30
|
+
throw new Error("Data could not be found and is not nullable");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return config?.placeholders === true ? this._placeholder : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private findIndex(data: TData[]): number {
|
|
37
|
+
const index = data.findIndex((item) => this.findValue(item) !== null);
|
|
38
|
+
return index !== -1 ? index : 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected abstract findValue(data: TData): TResult | null;
|
|
42
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { BooleanSchema } from "./boolean";
|
|
3
|
+
|
|
4
|
+
describe("Boolean schema", () => {
|
|
5
|
+
it("should parse to a boolean when parsing a boolean content value", () => {
|
|
6
|
+
const schema = new BooleanSchema();
|
|
7
|
+
|
|
8
|
+
const result = schema.parse([
|
|
9
|
+
{
|
|
10
|
+
primitiveValue: true,
|
|
11
|
+
},
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
expect(result).toEqual(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return null when parsing an a non boolean content value", () => {
|
|
18
|
+
const schema = new BooleanSchema();
|
|
19
|
+
|
|
20
|
+
const result = schema.parse([
|
|
21
|
+
{
|
|
22
|
+
primitiveValue: "hello",
|
|
23
|
+
},
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
expect(result).toEqual(null);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("when placeholders are enabled", () => {
|
|
30
|
+
it("should return the placeholder when parsing an empty array", () => {
|
|
31
|
+
const schema = new BooleanSchema().placeholder(true);
|
|
32
|
+
|
|
33
|
+
const result = schema.parse([], {
|
|
34
|
+
placeholders: true,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(result).toEqual(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should return the placeholder when parsing an non boolean content value", () => {
|
|
41
|
+
const schema = new BooleanSchema().placeholder(true);
|
|
42
|
+
|
|
43
|
+
const result = schema.parse(
|
|
44
|
+
[
|
|
45
|
+
{
|
|
46
|
+
primitiveValue: "hello",
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
{
|
|
50
|
+
placeholders: true,
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(result).toEqual(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("when nullable is disabled", () => {
|
|
59
|
+
it("should throw an error when parsing an non boolean content value", () => {
|
|
60
|
+
const schema = new BooleanSchema().nullable(false);
|
|
61
|
+
|
|
62
|
+
expect(() => schema.parse([{ primitiveValue: "hello" }])).toThrow("Data could not be found and is not nullable");
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseSchema } from "./baseSchema";
|
|
2
|
+
import { isPrimitiveValue } from "../utils/isPrimitiveValue";
|
|
3
|
+
|
|
4
|
+
export class BooleanSchema extends BaseSchema<boolean, unknown> {
|
|
5
|
+
protected override findValue(data: unknown): boolean | null {
|
|
6
|
+
if (isPrimitiveValue(data) && typeof data.primitiveValue === "boolean") {
|
|
7
|
+
return data.primitiveValue;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
import { BuildingBlockSchema } from "./buildingBlock";
|
|
3
|
+
import { schema } from ".";
|
|
4
|
+
import { BuildingBlockFieldFulfillment } from "../../../__generated__/graphql";
|
|
5
|
+
|
|
6
|
+
describe("BuildingBlock schema", () => {
|
|
7
|
+
it("should parse an empty building block", () => {
|
|
8
|
+
const buildingBlockSchema = new BuildingBlockSchema({});
|
|
9
|
+
|
|
10
|
+
const result = buildingBlockSchema.parse([]);
|
|
11
|
+
|
|
12
|
+
expect(result).toEqual({});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should parse a building block with its values", () => {
|
|
16
|
+
const buildingBlockSchema = new BuildingBlockSchema({
|
|
17
|
+
title: schema.string(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const result = buildingBlockSchema.parse([
|
|
21
|
+
{
|
|
22
|
+
buildingBlockField: {
|
|
23
|
+
slug: "title",
|
|
24
|
+
},
|
|
25
|
+
contentValue: {
|
|
26
|
+
primitiveValue: "hello",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
] as BuildingBlockFieldFulfillment[]);
|
|
30
|
+
|
|
31
|
+
expect(result).toEqual({
|
|
32
|
+
title: "hello",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should parse a building block with multiple values", () => {
|
|
37
|
+
const buildingBlockSchema = schema.buildingBlock({
|
|
38
|
+
title: schema.string(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const result = buildingBlockSchema.parse([
|
|
42
|
+
{
|
|
43
|
+
buildingBlockField: {
|
|
44
|
+
slug: "title",
|
|
45
|
+
},
|
|
46
|
+
contentValue: {
|
|
47
|
+
primitiveValue: "hello",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
] as unknown as BuildingBlockFieldFulfillment[]);
|
|
51
|
+
|
|
52
|
+
expect(result).toEqual({
|
|
53
|
+
title: "hello",
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|