Feat/support for all page links * feat: added all page link connections to queries * feat: updated output files Approved-by: Fredrik Thorsson Approved-by: Matilda Landström
88 lines
2.2 KiB
TypeScript
88 lines
2.2 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { imageRefsSchema, imageSchema } from "../blocks/image"
|
|
import {
|
|
imageContainerRefsSchema,
|
|
imageContainerSchema,
|
|
} from "../blocks/imageContainer"
|
|
import {
|
|
linkRefsUnionSchema,
|
|
linkUnionSchema,
|
|
transformPageLink,
|
|
} from "../pageLinks"
|
|
|
|
import { ContentEnum } from "@/types/enums/content"
|
|
import { SidebarEnums } from "@/types/enums/sidebar"
|
|
|
|
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,
|
|
imageSchema,
|
|
...linkUnionSchema.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,
|
|
...linkRefsUnionSchema.options,
|
|
])
|
|
|
|
type Ref = typeof actualRefs._type
|
|
type Refs = {
|
|
node: Ref
|
|
}
|
|
|
|
export const contentRefsSchema = z.object({
|
|
content: z
|
|
.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.discriminatedUnion("__typename", [
|
|
imageRefsSchema,
|
|
...actualRefs.options,
|
|
]),
|
|
})
|
|
),
|
|
}),
|
|
}),
|
|
})
|
|
.transform((data) => {
|
|
const filtered = data.content.embedded_itemsConnection.edges.filter(
|
|
(block) => block.node.__typename !== ContentEnum.blocks.SysAsset
|
|
) as unknown as Refs[] // TS issues with filtered arrays
|
|
return filtered.map((block) => block.node.system)
|
|
}),
|
|
})
|