fix(BOOK-453): avoid linkConnection invalid * fix(BOOK-453): avoid linkConnection invalid * test * test * test * Merge master
84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { BlocksEnums } from "../../../../types/blocksEnum"
|
|
import { ContentEnum } from "../../../../types/content"
|
|
import {
|
|
rawLinkRefsUnionSchema,
|
|
rawLinkUnionSchema,
|
|
transformPageLink,
|
|
} from "../pageLinks"
|
|
import { sysAssetRefsSchema, sysAssetSchema } from "./sysAsset"
|
|
|
|
import type { linkUnionSchema } from "../pageLinks"
|
|
|
|
export const textColsSchema = z.object({
|
|
typename: z
|
|
.literal(BlocksEnums.block.TextCols)
|
|
.optional()
|
|
.default(BlocksEnums.block.TextCols),
|
|
text_cols: z.object({
|
|
columns: z.array(
|
|
z.object({
|
|
title: z.string().optional().default(""),
|
|
text: z.object({
|
|
json: z.any(), // JSON
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z
|
|
.discriminatedUnion("__typename", [
|
|
sysAssetSchema,
|
|
...rawLinkUnionSchema.options,
|
|
])
|
|
.transform((data) => {
|
|
const link = transformPageLink(data)
|
|
if (link) {
|
|
return link
|
|
}
|
|
return data
|
|
}),
|
|
})
|
|
),
|
|
}),
|
|
}),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
type Refs = {
|
|
node: z.TypeOf<typeof linkUnionSchema>
|
|
}
|
|
|
|
export const textColsRefsSchema = z.object({
|
|
text_cols: z
|
|
.object({
|
|
columns: z.array(
|
|
z.object({
|
|
text: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.discriminatedUnion("__typename", [
|
|
sysAssetRefsSchema,
|
|
...rawLinkRefsUnionSchema.options,
|
|
]),
|
|
})
|
|
),
|
|
}),
|
|
}),
|
|
})
|
|
),
|
|
})
|
|
.transform((data) => {
|
|
return data.columns
|
|
.map((column) => {
|
|
const filtered = column.text.embedded_itemsConnection.edges.filter(
|
|
(block) => block.node.__typename !== ContentEnum.blocks.SysAsset
|
|
) as unknown as Refs[] // TS issue with filtered out types
|
|
return filtered.map(({ node }) => node.system)
|
|
})
|
|
.flat()
|
|
}),
|
|
})
|