feat(SW-285): add support for tags
This commit is contained in:
@@ -2,6 +2,11 @@
|
|||||||
#import "../Fragments/PageLink/ContentPageLink.graphql"
|
#import "../Fragments/PageLink/ContentPageLink.graphql"
|
||||||
#import "../Fragments/PageLink/LoyaltyPageLink.graphql"
|
#import "../Fragments/PageLink/LoyaltyPageLink.graphql"
|
||||||
|
|
||||||
|
#import "../Fragments/Refs/MyPages/AccountPage.graphql"
|
||||||
|
#import "../Fragments/Refs/ContentPage/ContentPage.graphql"
|
||||||
|
#import "../Fragments/Refs/LoyaltyPage/LoyaltyPage.graphql"
|
||||||
|
#import "../Fragments/Refs/System.graphql"
|
||||||
|
|
||||||
query GetContentPage($locale: String!, $uid: String!) {
|
query GetContentPage($locale: String!, $uid: String!) {
|
||||||
content_page(uid: $uid, locale: $locale) {
|
content_page(uid: $uid, locale: $locale) {
|
||||||
blocks {
|
blocks {
|
||||||
@@ -106,6 +111,23 @@ query GetContentPageRefs($locale: String!, $uid: String!) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
... on ContentPageBlocksShortcuts {
|
||||||
|
__typename
|
||||||
|
shortcuts {
|
||||||
|
shortcuts {
|
||||||
|
linkConnection {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
__typename
|
||||||
|
...AccountPageRef
|
||||||
|
...ContentPageRef
|
||||||
|
...LoyaltyPageRef
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
system {
|
system {
|
||||||
...System
|
...System
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { imageVaultAssetSchema } from "../schemas/imageVault"
|
|||||||
import { ContentBlocksTypenameEnum } from "@/types/components/content/enums"
|
import { ContentBlocksTypenameEnum } from "@/types/components/content/enums"
|
||||||
import { ImageVaultAsset } from "@/types/components/imageVault"
|
import { ImageVaultAsset } from "@/types/components/imageVault"
|
||||||
import { Embeds } from "@/types/requests/embeds"
|
import { Embeds } from "@/types/requests/embeds"
|
||||||
|
import { RTEEmbedsEnum } from "@/types/requests/rte"
|
||||||
import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
|
import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
|
||||||
import { RTEDocument } from "@/types/rte/node"
|
import { RTEDocument } from "@/types/rte/node"
|
||||||
|
|
||||||
@@ -84,3 +85,56 @@ export type ContentPage = Omit<ContentPageRaw, "blocks" | "hero_image"> & {
|
|||||||
heroImage?: ImageVaultAsset
|
heroImage?: ImageVaultAsset
|
||||||
blocks: Block[]
|
blocks: Block[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const rteConnectionRefs = z.object({
|
||||||
|
edges: z.array(
|
||||||
|
z.object({
|
||||||
|
node: z.object({
|
||||||
|
__typename: z.nativeEnum(RTEEmbedsEnum),
|
||||||
|
system: z.object({
|
||||||
|
content_type_uid: z.string(),
|
||||||
|
uid: z.string(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
|
const contentPageBlockTextContentRefs = z.object({
|
||||||
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksContent),
|
||||||
|
content: z.object({
|
||||||
|
content: z.object({
|
||||||
|
embedded_itemsConnection: rteConnectionRefs,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const contentPageShortcutsRefs = z.object({
|
||||||
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksShortcuts),
|
||||||
|
shortcuts: z.object({
|
||||||
|
shortcuts: z.array(
|
||||||
|
z.object({
|
||||||
|
linkConnection: rteConnectionRefs,
|
||||||
|
})
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const contentPageBlockRefsItem = z.discriminatedUnion("__typename", [
|
||||||
|
contentPageBlockTextContentRefs,
|
||||||
|
contentPageShortcutsRefs,
|
||||||
|
])
|
||||||
|
|
||||||
|
export const validateContentPageRefsSchema = z.object({
|
||||||
|
content_page: z.object({
|
||||||
|
blocks: z.array(contentPageBlockRefsItem).nullable(),
|
||||||
|
system: z.object({
|
||||||
|
content_type_uid: z.string(),
|
||||||
|
uid: z.string(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type ContentPageRefsDataRaw = z.infer<
|
||||||
|
typeof validateContentPageRefsSchema
|
||||||
|
>
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { request } from "@/lib/graphql/request"
|
|||||||
import { notFound } from "@/server/errors/trpc"
|
import { notFound } from "@/server/errors/trpc"
|
||||||
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||||
|
|
||||||
import { generateTag } from "@/utils/generateTag"
|
|
||||||
import { makeImageVaultImage } from "@/utils/imageVault"
|
import { makeImageVaultImage } from "@/utils/imageVault"
|
||||||
import { removeMultipleSlashes } from "@/utils/url"
|
import { removeMultipleSlashes } from "@/utils/url"
|
||||||
|
|
||||||
@@ -15,6 +14,12 @@ import {
|
|||||||
ContentPageDataRaw,
|
ContentPageDataRaw,
|
||||||
validateContentPageSchema,
|
validateContentPageSchema,
|
||||||
} from "./output"
|
} from "./output"
|
||||||
|
import {
|
||||||
|
fetchContentPageRefs,
|
||||||
|
generatePageTags,
|
||||||
|
getContentPageCounter,
|
||||||
|
validateContentPageRefs,
|
||||||
|
} from "./utils"
|
||||||
|
|
||||||
import { ContentBlocksTypenameEnum } from "@/types/components/content/enums"
|
import { ContentBlocksTypenameEnum } from "@/types/components/content/enums"
|
||||||
import {
|
import {
|
||||||
@@ -26,13 +31,31 @@ export const contentPageQueryRouter = router({
|
|||||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||||
const { lang, uid } = ctx
|
const { lang, uid } = ctx
|
||||||
|
|
||||||
// TODO: Refs request should be done when adding more data to this query
|
const cleanedRefsData = await fetchContentPageRefs(lang, uid)
|
||||||
// which has references to other pages.
|
const validatedRefsData = validateContentPageRefs(
|
||||||
|
cleanedRefsData,
|
||||||
|
lang,
|
||||||
|
uid
|
||||||
|
)
|
||||||
|
const tags = generatePageTags(validatedRefsData, lang)
|
||||||
|
|
||||||
|
getContentPageCounter.add(1, { lang, uid })
|
||||||
|
console.info(
|
||||||
|
"contentstack.contentPage start",
|
||||||
|
JSON.stringify({
|
||||||
|
query: { lang, uid },
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
const response = await request<ContentPageDataRaw>(
|
const response = await request<ContentPageDataRaw>(
|
||||||
GetContentPage,
|
GetContentPage,
|
||||||
{ locale: lang, uid },
|
{ locale: lang, uid },
|
||||||
{ cache: "force-cache", next: { tags: [generateTag(lang, uid)] } }
|
{
|
||||||
|
cache: "force-cache",
|
||||||
|
next: {
|
||||||
|
tags,
|
||||||
|
},
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const { content_page } = removeEmptyObjects(response.data)
|
const { content_page } = removeEmptyObjects(response.data)
|
||||||
|
|||||||
132
server/routers/contentstack/contentPage/utils.ts
Normal file
132
server/routers/contentstack/contentPage/utils.ts
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
import { metrics } from "@opentelemetry/api"
|
||||||
|
|
||||||
|
import { Lang } from "@/constants/languages"
|
||||||
|
import { GetContentPageRefs } from "@/lib/graphql/Query/ContentPage.graphql"
|
||||||
|
import { request } from "@/lib/graphql/request"
|
||||||
|
import { notFound } from "@/server/errors/trpc"
|
||||||
|
|
||||||
|
import { generateTag, generateTags } from "@/utils/generateTag"
|
||||||
|
|
||||||
|
import { removeEmptyObjects } from "../../utils"
|
||||||
|
import { ContentPageRefsDataRaw, validateContentPageRefsSchema } from "./output"
|
||||||
|
|
||||||
|
import { ContentBlocksTypenameEnum } from "@/types/components/content/enums"
|
||||||
|
import { Edges } from "@/types/requests/utils/edges"
|
||||||
|
import { NodeRefs } from "@/types/requests/utils/refs"
|
||||||
|
|
||||||
|
const meter = metrics.getMeter("trpc.contentPage")
|
||||||
|
// OpenTelemetry metrics: ContentPage
|
||||||
|
|
||||||
|
export const getContentPageCounter = meter.createCounter(
|
||||||
|
"trpc.contentstack.contentPage.get"
|
||||||
|
)
|
||||||
|
|
||||||
|
const getContentPageRefsCounter = meter.createCounter(
|
||||||
|
"trpc.contentstack.contentPage.get"
|
||||||
|
)
|
||||||
|
const getContentPageRefsFailCounter = meter.createCounter(
|
||||||
|
"trpc.contentstack.contentPage.get-fail"
|
||||||
|
)
|
||||||
|
const getContentPageRefsSuccessCounter = meter.createCounter(
|
||||||
|
"trpc.contentstack.contentPage.get-success"
|
||||||
|
)
|
||||||
|
|
||||||
|
export async function fetchContentPageRefs(lang: Lang, uid: string) {
|
||||||
|
getContentPageRefsCounter.add(1, { lang, uid })
|
||||||
|
console.info(
|
||||||
|
"contentstack.contentPage.refs start",
|
||||||
|
JSON.stringify({
|
||||||
|
query: { lang, uid },
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const refsResponse = await request<ContentPageRefsDataRaw>(
|
||||||
|
GetContentPageRefs,
|
||||||
|
{ locale: lang, uid },
|
||||||
|
{ cache: "force-cache", next: { tags: [generateTag(lang, uid)] } }
|
||||||
|
)
|
||||||
|
if (!refsResponse.data) {
|
||||||
|
const notFoundError = notFound(refsResponse)
|
||||||
|
getContentPageRefsFailCounter.add(1, {
|
||||||
|
lang,
|
||||||
|
uid,
|
||||||
|
error_type: "http_error",
|
||||||
|
error: JSON.stringify({
|
||||||
|
code: notFoundError.code,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
console.error(
|
||||||
|
"contentstack.contentPage.refs not found error",
|
||||||
|
JSON.stringify({
|
||||||
|
query: {
|
||||||
|
lang,
|
||||||
|
uid,
|
||||||
|
},
|
||||||
|
error: { code: notFoundError.code },
|
||||||
|
})
|
||||||
|
)
|
||||||
|
throw notFoundError
|
||||||
|
}
|
||||||
|
|
||||||
|
return removeEmptyObjects(refsResponse.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateContentPageRefs(data: any, lang: Lang, uid: string) {
|
||||||
|
const validatedData = validateContentPageRefsSchema.safeParse(data)
|
||||||
|
if (!validatedData.success) {
|
||||||
|
getContentPageRefsFailCounter.add(1, {
|
||||||
|
lang,
|
||||||
|
uid,
|
||||||
|
error_type: "validation_error",
|
||||||
|
error: JSON.stringify(validatedData.error),
|
||||||
|
})
|
||||||
|
console.error(
|
||||||
|
"contentstack.contentPage.refs validation error",
|
||||||
|
JSON.stringify({
|
||||||
|
query: { lang, uid },
|
||||||
|
error: validatedData.error,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
getContentPageRefsSuccessCounter.add(1, { lang, uid })
|
||||||
|
console.info(
|
||||||
|
"contentstack.contentPage.refs success",
|
||||||
|
JSON.stringify({
|
||||||
|
query: { lang, uid },
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return validatedData.data
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generatePageTags(validatedData: any, lang: Lang): string[] {
|
||||||
|
const connections = getConnections(validatedData)
|
||||||
|
return [
|
||||||
|
generateTags(lang, connections),
|
||||||
|
generateTag(lang, validatedData.content_page.system.uid),
|
||||||
|
].flat()
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getConnections(refs: ContentPageRefsDataRaw) {
|
||||||
|
const connections: Edges<NodeRefs>[] = []
|
||||||
|
if (refs.content_page.blocks) {
|
||||||
|
refs.content_page.blocks.forEach((item) => {
|
||||||
|
switch (item.__typename) {
|
||||||
|
case ContentBlocksTypenameEnum.ContentPageBlocksContent: {
|
||||||
|
if (item.content.content.embedded_itemsConnection.edges.length) {
|
||||||
|
connections.push(item.content.content.embedded_itemsConnection)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case ContentBlocksTypenameEnum.ContentPageBlocksShortcuts: {
|
||||||
|
item.shortcuts.shortcuts.forEach((shortcut) => {
|
||||||
|
if (shortcut.linkConnection.edges.length) {
|
||||||
|
connections.push(shortcut.linkConnection)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return connections
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user