@platecms/delta-client 0.9.0 → 0.9.2
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/schema/lib/schemas/array.spec.ts +15 -0
- package/src/schema/lib/schemas/array.ts +12 -4
- package/src/schema/lib/schemas/asset.spec.ts +1 -1
- package/src/schema/lib/schemas/asset.ts +3 -2
- package/src/schema/lib/schemas/buildingBlock.ts +7 -1
- package/src/schema/lib/schemas/contentItem.ts +10 -1
- package/src/schema/lib/schemas/contentType.ts +2 -1
- package/src/schema/lib/schemas/gridPlacement.spec.ts +1 -1
- package/src/schema/lib/schemas/gridPlacement.ts +3 -2
- package/src/schema/lib/schemas/index.ts +2 -2
- package/src/schema/lib/schemas/pathPart.spec.ts +1 -1
- package/src/schema/lib/schemas/pathPart.ts +3 -2
- package/src/schema/lib/schemas/smartText.ts +2 -1
- package/src/schema/lib/schemas/tag.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platecms/delta-client",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"description": "Utilities and functions to interact with the Delta CMS.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"publishConfig": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"src/**/*"
|
|
19
19
|
],
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"@platecms/delta-cast": "0.9.
|
|
21
|
+
"@platecms/delta-cast": "0.9.2",
|
|
22
22
|
"@graphql-typed-document-node/core": "3.2.0",
|
|
23
23
|
"graphql": "16.11.0",
|
|
24
24
|
"lodash": "4.17.21",
|
|
@@ -79,6 +79,21 @@ describe("Array schema", () => {
|
|
|
79
79
|
});
|
|
80
80
|
|
|
81
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
|
+
|
|
82
97
|
it("should parse an array of content values", () => {
|
|
83
98
|
const testSchema = schemaBuilder.array(
|
|
84
99
|
schemaBuilder.contentItem({
|
|
@@ -2,7 +2,11 @@ import { compact, first } from "lodash";
|
|
|
2
2
|
import { Schema, SchemaConfig } from ".";
|
|
3
3
|
|
|
4
4
|
export class ArraySchema<T extends Schema | Schema[]>
|
|
5
|
-
implements
|
|
5
|
+
implements
|
|
6
|
+
Schema<
|
|
7
|
+
T extends Schema[] ? ReturnType<T[number]["parse"]>[] : T extends Schema ? ReturnType<T["parse"]>[] : never,
|
|
8
|
+
unknown
|
|
9
|
+
>
|
|
6
10
|
{
|
|
7
11
|
private _minLength: number = 0;
|
|
8
12
|
|
|
@@ -16,7 +20,7 @@ export class ArraySchema<T extends Schema | Schema[]>
|
|
|
16
20
|
public parse(
|
|
17
21
|
data: unknown,
|
|
18
22
|
config?: SchemaConfig,
|
|
19
|
-
): T extends Schema[] ? T[number]["parse"][] : T extends Schema ? T["parse"][] : never {
|
|
23
|
+
): T extends Schema[] ? ReturnType<T[number]["parse"]>[] : T extends Schema ? ReturnType<T["parse"]>[] : never {
|
|
20
24
|
const dataAsArray = Array.isArray(data) ? data : [];
|
|
21
25
|
|
|
22
26
|
if (dataAsArray.length < this._minLength) {
|
|
@@ -29,10 +33,14 @@ export class ArraySchema<T extends Schema | Schema[]>
|
|
|
29
33
|
const resultForStructures = structures.map((schema) => schema.parse(item, config));
|
|
30
34
|
|
|
31
35
|
return resultForStructures.find((result) => result !== null) ?? first(resultForStructures);
|
|
32
|
-
}) as T extends Schema[] ? T[number]["parse"][] : T extends Schema ? T["parse"][] : never;
|
|
36
|
+
}) as T extends Schema[] ? ReturnType<T[number]["parse"]>[] : T extends Schema ? ReturnType<T["parse"]>[] : never;
|
|
33
37
|
|
|
34
38
|
return config?.placeholders === true
|
|
35
39
|
? results
|
|
36
|
-
: (compact(results) as T extends Schema[]
|
|
40
|
+
: (compact(results) as T extends Schema[]
|
|
41
|
+
? ReturnType<T[number]["parse"]>[]
|
|
42
|
+
: T extends Schema
|
|
43
|
+
? ReturnType<T["parse"]>[]
|
|
44
|
+
: never);
|
|
37
45
|
}
|
|
38
46
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Asset } from "../../../__generated__/graphql";
|
|
2
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
2
3
|
import { BaseSchema } from "./baseSchema";
|
|
3
4
|
|
|
4
5
|
export class AssetSchema extends BaseSchema<Asset | null, unknown> {
|
|
5
6
|
protected override findValue(data: unknown): Asset | null {
|
|
6
|
-
if (
|
|
7
|
-
return (data as {
|
|
7
|
+
if (isContentValue(data) && "relatedAsset" in data) {
|
|
8
|
+
return (data as { relatedAsset: Asset }).relatedAsset;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
return null;
|
|
@@ -9,7 +9,13 @@ export class BuildingBlockSchema<T extends Record<string, Schema | Schema[]>>
|
|
|
9
9
|
public parse(
|
|
10
10
|
data: BuildingBlockFieldFulfillment | BuildingBlockFieldFulfillment[],
|
|
11
11
|
config?: SchemaConfig,
|
|
12
|
-
):
|
|
12
|
+
): {
|
|
13
|
+
[key in keyof T]: T[key] extends Schema[]
|
|
14
|
+
? ReturnType<T[key][number]["parse"]>
|
|
15
|
+
: T[key] extends Schema
|
|
16
|
+
? ReturnType<T[key]["parse"]>
|
|
17
|
+
: never;
|
|
18
|
+
} {
|
|
13
19
|
if (!Array.isArray(data)) {
|
|
14
20
|
data = [data];
|
|
15
21
|
}
|
|
@@ -7,7 +7,16 @@ export class ContentItemSchema<T extends Record<string, Schema | Schema[]>>
|
|
|
7
7
|
{
|
|
8
8
|
public constructor(private readonly structure: T) {}
|
|
9
9
|
|
|
10
|
-
public parse(
|
|
10
|
+
public parse(
|
|
11
|
+
data: unknown,
|
|
12
|
+
config?: SchemaConfig,
|
|
13
|
+
): {
|
|
14
|
+
[key in keyof T]: T[key] extends Schema[]
|
|
15
|
+
? ReturnType<T[key][number]["parse"]>
|
|
16
|
+
: T[key] extends Schema
|
|
17
|
+
? ReturnType<T[key]["parse"]>
|
|
18
|
+
: never;
|
|
19
|
+
} {
|
|
11
20
|
if (isArray(data)) {
|
|
12
21
|
data = data[0];
|
|
13
22
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { ContentType } from "../../../__generated__/graphql";
|
|
2
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
2
3
|
import { BaseSchema } from "./baseSchema";
|
|
3
4
|
|
|
4
5
|
export class ContentTypeSchema extends BaseSchema<ContentType | null, unknown> {
|
|
5
6
|
protected override findValue(data: unknown): ContentType | null {
|
|
6
|
-
if (
|
|
7
|
+
if (isContentValue(data) && "linkedContentType" in data) {
|
|
7
8
|
return (data as { linkedContentType: ContentType }).linkedContentType;
|
|
8
9
|
}
|
|
9
10
|
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { GridPlacement } from "../../../__generated__/graphql";
|
|
2
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
2
3
|
import { BaseSchema } from "./baseSchema";
|
|
3
4
|
|
|
4
5
|
export class GridPlacementSchema extends BaseSchema<GridPlacement | null, unknown> {
|
|
5
6
|
protected override findValue(data: unknown): GridPlacement | null {
|
|
6
|
-
if (
|
|
7
|
-
return (data as {
|
|
7
|
+
if (isContentValue(data) && "linkedGridPlacement" in data) {
|
|
8
|
+
return (data as { linkedGridPlacement: GridPlacement }).linkedGridPlacement;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
return null;
|
|
@@ -33,9 +33,9 @@ export interface Schema<TResult = unknown, TData = unknown> {
|
|
|
33
33
|
|
|
34
34
|
export type ObjectSchema<T extends Record<string, Schema | Schema[]>> = {
|
|
35
35
|
[key in keyof T]: T[key] extends Schema[]
|
|
36
|
-
? ReturnType<T[key][number]["parse"]>
|
|
36
|
+
? ReturnType<T[key][number]["parse"]>
|
|
37
37
|
: T[key] extends Schema
|
|
38
|
-
? ReturnType<T[key]["parse"]>
|
|
38
|
+
? ReturnType<T[key]["parse"]>
|
|
39
39
|
: never;
|
|
40
40
|
};
|
|
41
41
|
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { PathPart } from "../../../__generated__/graphql";
|
|
2
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
2
3
|
import { BaseSchema } from "./baseSchema";
|
|
3
4
|
|
|
4
5
|
export class PathPartSchema extends BaseSchema<PathPart | null, unknown> {
|
|
5
6
|
protected override findValue(data: unknown): PathPart | null {
|
|
6
|
-
if (
|
|
7
|
-
return (data as {
|
|
7
|
+
if (isContentValue(data) && "linkedPathPart" in data) {
|
|
8
|
+
return (data as { linkedPathPart: PathPart }).linkedPathPart;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
return null;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Root as SmartText } from "@platecms/delta-cast";
|
|
2
2
|
import { BaseSchema } from "./baseSchema";
|
|
3
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
3
4
|
|
|
4
5
|
export class SmartTextSchema extends BaseSchema<SmartText | null, unknown> {
|
|
5
6
|
protected override findValue(data: unknown): SmartText | null {
|
|
6
|
-
if (
|
|
7
|
+
if (isContentValue(data) && "interpolatedSmartText" in data) {
|
|
7
8
|
return (data as { interpolatedSmartText: SmartText }).interpolatedSmartText;
|
|
8
9
|
}
|
|
9
10
|
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Tag } from "../../../__generated__/graphql";
|
|
2
|
+
import { isContentValue } from "../utils/isContentValue";
|
|
2
3
|
import { BaseSchema } from "./baseSchema";
|
|
3
4
|
|
|
4
5
|
export class TagSchema extends BaseSchema<Tag | null, unknown> {
|
|
5
6
|
protected override findValue(data: unknown): Tag | null {
|
|
6
|
-
if (
|
|
7
|
+
if (isContentValue(data) && "linkedTag" in data) {
|
|
7
8
|
return (data as { linkedTag: Tag }).linkedTag;
|
|
8
9
|
}
|
|
9
10
|
|