feat(BOOK-609): Using embedded url for assets instead of href since that is not updated when the asset is updated)

* feat(BOOK-609): Updated refs handling for assets inside content pages

Approved-by: Linus Flood
This commit is contained in:
Erik Tiekstra
2026-01-13 10:40:36 +00:00
parent 6ae4c7c805
commit 8d34089637
19 changed files with 194 additions and 147 deletions

View File

@@ -73,12 +73,15 @@ export const contentRefsSchema = z.object({
.nullish()
.transform((data) => {
return data?.content?.embedded_itemsConnection.edges
.filter(({ node }) => node.__typename !== ContentEnum.blocks.SysAsset)
.map(({ node }) => {
if ("system" in node) {
return node.system
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
}
return null
})
.filter((node) => !!node)
}),

View File

@@ -1,6 +1,7 @@
import { z } from "zod"
import { ContentEnum } from "../../../../types/content"
import { assetSystemSchema } from "../system"
// SysAsset is used for several different assets in ContentStack, such as images, pdf-files, etc.
// It is a generic asset type that can represent any file type.
@@ -22,15 +23,21 @@ export const sysAssetSchema = z.object({
// the exact same structure, that's why systemSchema
// is not used as that correlates to the
// EntrySystemField type
system: z
.object({
uid: z.string(),
})
.nullish(),
system: assetSystemSchema.nullish(),
title: z.string().nullish(),
url: z.string().nullish(),
permanent_url: z
.string()
.nullish()
.transform((val) => (val === "Permanent URL Not Defined!" ? null : val)), // ContentStack returns this string when permanent_url is not defined
})
export const sysAssetRefsSchema = z.object({
__typename: z.literal(ContentEnum.blocks.SysAsset),
url: z.string().nullish(),
permanent_url: z
.string()
.nullish()
.transform((val) => (val === "Permanent URL Not Defined!" ? null : val)), // ContentStack returns this string when permanent_url is not defined
system: assetSystemSchema.nullish(),
})

View File

@@ -56,11 +56,6 @@ const actualRefs = z.discriminatedUnion("__typename", [
...rawLinkRefsUnionSchema.options,
])
type Ref = typeof actualRefs._type
type Refs = {
node: Ref
}
export const contentRefsSchema = z.object({
content: z
.object({
@@ -78,9 +73,17 @@ export const contentRefsSchema = z.object({
}),
})
.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)
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)
}),
})

View File

@@ -19,4 +19,6 @@ export const assetSystemSchema = z.object({
export interface AssetSystem {
system: z.output<typeof assetSystemSchema>
url?: string | null
permanent_url?: string | null
}

View File

@@ -78,6 +78,7 @@ export const videoRefSchema = z.object({
z.object({
node: z.object({
system: assetSystemSchema,
url: z.string(),
}),
})
),