@platecms/delta-client 1.7.0 → 1.9.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/package.json +2 -2
- package/src/__generated__/gql.ts +15 -9
- package/src/__generated__/graphql.ts +187 -141
- package/src/graphql/fragments/grid-placement.fragments.gql +0 -1
- package/src/graphql/fragments/related-asset.fragment.gql +9 -0
- package/src/graphql/path-parts/path-parts.query.gql +4 -1
- package/src/schema/index.spec.ts +1 -4
- package/src/schema/lib/schemas/array.ts +1 -0
- package/src/schema/lib/schemas/relatedContentItem.spec.ts +106 -1
- package/src/schema/lib/schemas/relatedContentItem.ts +23 -9
package/src/schema/index.spec.ts
CHANGED
|
@@ -30,6 +30,7 @@ export class ArraySchema<T extends Schema | Schema[]>
|
|
|
30
30
|
|
|
31
31
|
const results = dataAsArray.map((item) => {
|
|
32
32
|
const structures: Schema[] = Array.isArray(this.structure) ? this.structure : [this.structure];
|
|
33
|
+
|
|
33
34
|
const resultForStructures = structures.map((schema) => schema.parse(item, config));
|
|
34
35
|
|
|
35
36
|
return resultForStructures.find((result) => result !== null) ?? first(resultForStructures);
|
|
@@ -10,7 +10,7 @@ describe("RelatedContentItem schema", () => {
|
|
|
10
10
|
contentValues: [],
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
expect(result).toEqual(
|
|
13
|
+
expect(result).toEqual(null);
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
it("should parse a content item with its values", () => {
|
|
@@ -58,4 +58,109 @@ describe("RelatedContentItem schema", () => {
|
|
|
58
58
|
title: "hello",
|
|
59
59
|
});
|
|
60
60
|
});
|
|
61
|
+
|
|
62
|
+
it("should parse a related content item with a specific content type", () => {
|
|
63
|
+
const relatedContentItemSchema = new RelatedContentItemSchema({
|
|
64
|
+
title: schema.string(),
|
|
65
|
+
}).ofContentType("tag");
|
|
66
|
+
|
|
67
|
+
const result = relatedContentItemSchema.parse({
|
|
68
|
+
relatedContentItem: {
|
|
69
|
+
contentType: {
|
|
70
|
+
slug: "tag",
|
|
71
|
+
},
|
|
72
|
+
contentValues: [
|
|
73
|
+
{
|
|
74
|
+
contentField: {
|
|
75
|
+
slug: "title",
|
|
76
|
+
},
|
|
77
|
+
primitiveValue: "hello",
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(result).toEqual({
|
|
84
|
+
title: "hello",
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should return null when parsing a related content item with a different content type", () => {
|
|
89
|
+
const relatedContentItemSchema = new RelatedContentItemSchema({
|
|
90
|
+
title: schema.string(),
|
|
91
|
+
}).ofContentType("tag");
|
|
92
|
+
|
|
93
|
+
const result = relatedContentItemSchema.parse({
|
|
94
|
+
relatedContentItem: {
|
|
95
|
+
contentType: {
|
|
96
|
+
slug: "author",
|
|
97
|
+
},
|
|
98
|
+
contentValues: [
|
|
99
|
+
{
|
|
100
|
+
contentField: {
|
|
101
|
+
slug: "title",
|
|
102
|
+
},
|
|
103
|
+
primitiveValue: "hello",
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(result).toEqual(null);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("when using an array of schemas", () => {
|
|
113
|
+
it("should parse an array of related content items", () => {
|
|
114
|
+
const relatedContentItemArraySchema = schema.array([
|
|
115
|
+
new RelatedContentItemSchema({
|
|
116
|
+
email: schema.string(),
|
|
117
|
+
}).ofContentType("person"),
|
|
118
|
+
new RelatedContentItemSchema({
|
|
119
|
+
name: schema.string(),
|
|
120
|
+
}).ofContentType("author"),
|
|
121
|
+
]);
|
|
122
|
+
|
|
123
|
+
const result = relatedContentItemArraySchema.parse([
|
|
124
|
+
{
|
|
125
|
+
relatedContentItem: {
|
|
126
|
+
contentType: {
|
|
127
|
+
slug: "author",
|
|
128
|
+
},
|
|
129
|
+
contentValues: [
|
|
130
|
+
{
|
|
131
|
+
contentField: {
|
|
132
|
+
slug: "name",
|
|
133
|
+
},
|
|
134
|
+
primitiveValue: "John Doe",
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
relatedContentItem: {
|
|
141
|
+
contentType: {
|
|
142
|
+
slug: "person",
|
|
143
|
+
},
|
|
144
|
+
contentValues: [
|
|
145
|
+
{
|
|
146
|
+
contentField: {
|
|
147
|
+
slug: "email",
|
|
148
|
+
},
|
|
149
|
+
primitiveValue: "john.doe@example.com",
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
]);
|
|
155
|
+
|
|
156
|
+
expect(result).toEqual([
|
|
157
|
+
{
|
|
158
|
+
name: "John Doe",
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
email: "john.doe@example.com",
|
|
162
|
+
},
|
|
163
|
+
]);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
61
166
|
});
|
|
@@ -4,28 +4,42 @@ import { isArray } from "../utils";
|
|
|
4
4
|
export class RelatedContentItemSchema<T extends Record<string, Schema | Schema[]>>
|
|
5
5
|
implements Schema<ObjectSchema<T>, unknown>
|
|
6
6
|
{
|
|
7
|
+
private type?: string = undefined;
|
|
8
|
+
|
|
7
9
|
public constructor(private readonly structure: T) {}
|
|
8
10
|
|
|
11
|
+
public ofContentType(slug: string): this {
|
|
12
|
+
this.type = slug;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
|
|
9
16
|
public parse(
|
|
10
17
|
data: unknown,
|
|
11
18
|
config?: SchemaConfig,
|
|
12
|
-
):
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
):
|
|
20
|
+
| {
|
|
21
|
+
[key in keyof T]: T[key] extends Schema[]
|
|
22
|
+
? ReturnType<T[key][number]["parse"]>
|
|
23
|
+
: T[key] extends Schema
|
|
24
|
+
? ReturnType<T[key]["parse"]>
|
|
25
|
+
: never;
|
|
26
|
+
}
|
|
27
|
+
| null {
|
|
19
28
|
if (isArray(data)) {
|
|
20
29
|
data = data[0];
|
|
21
30
|
}
|
|
22
31
|
|
|
32
|
+
const relatedContentItem = isContentValue(data) ? data.relatedContentItem : null;
|
|
33
|
+
|
|
34
|
+
if (!relatedContentItem || (this.type !== null && relatedContentItem.contentType?.slug !== this.type)) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
23
38
|
return Object.entries(this.structure).reduce(
|
|
24
39
|
(acc, [key, value]) => {
|
|
25
|
-
const relatedContentItem = isContentValue(data) ? data.relatedContentItem : null;
|
|
26
|
-
|
|
27
40
|
const contentValuesForKey =
|
|
28
41
|
relatedContentItem?.contentValues.filter((contentValue) => contentValue.contentField?.slug === key) ?? [];
|
|
42
|
+
|
|
29
43
|
const shapes = Array.isArray(value) ? value : [value];
|
|
30
44
|
const parsedValues = shapes.map((shape) => shape.parse(contentValuesForKey, config));
|
|
31
45
|
|