* feat(BOOK-609): Updated refs handling for assets inside content pages Approved-by: Linus Flood
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { ContentEnum } from "../../../../types/content"
|
|
import { SidebarEnums } from "../../../../types/sidebar"
|
|
import {
|
|
imageContainerRefsSchema,
|
|
imageContainerSchema,
|
|
} from "../blocks/imageContainer"
|
|
import { sysAssetRefsSchema, sysAssetSchema } from "../blocks/sysAsset"
|
|
import {
|
|
rawLinkRefsUnionSchema,
|
|
rawLinkUnionSchema,
|
|
transformPageLink,
|
|
} from "../pageLinks"
|
|
|
|
export const contentSchema = z.object({
|
|
typename: z
|
|
.literal(SidebarEnums.blocks.Content)
|
|
.optional()
|
|
.default(SidebarEnums.blocks.Content),
|
|
content: z
|
|
.object({
|
|
content: z.object({
|
|
json: z.any(), // JSON
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z
|
|
.discriminatedUnion("__typename", [
|
|
imageContainerSchema,
|
|
sysAssetSchema,
|
|
...rawLinkUnionSchema.options,
|
|
])
|
|
.transform((data) => {
|
|
const link = transformPageLink(data)
|
|
if (link) {
|
|
return link
|
|
}
|
|
return data
|
|
}),
|
|
})
|
|
),
|
|
}),
|
|
}),
|
|
})
|
|
.transform((data) => {
|
|
return {
|
|
embedded_itemsConnection: data.content.embedded_itemsConnection,
|
|
json: data.content.json,
|
|
}
|
|
}),
|
|
})
|
|
|
|
const actualRefs = z.discriminatedUnion("__typename", [
|
|
imageContainerRefsSchema,
|
|
...rawLinkRefsUnionSchema.options,
|
|
])
|
|
|
|
export const contentRefsSchema = z.object({
|
|
content: z
|
|
.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.discriminatedUnion("__typename", [
|
|
sysAssetRefsSchema,
|
|
...actualRefs.options,
|
|
]),
|
|
})
|
|
),
|
|
}),
|
|
}),
|
|
})
|
|
.transform((data) => {
|
|
return data?.content?.embedded_itemsConnection.edges
|
|
.map(({ node }) => {
|
|
switch (node.__typename) {
|
|
case ContentEnum.blocks.SysAsset:
|
|
return node.system && (node.permanent_url || node.url)
|
|
? { system: node.system, url: node.permanent_url || node.url }
|
|
: null
|
|
default:
|
|
return node.system
|
|
}
|
|
})
|
|
.filter((node) => !!node)
|
|
}),
|
|
})
|