660 lines
18 KiB
TypeScript
660 lines
18 KiB
TypeScript
import { metrics } from "@opentelemetry/api"
|
|
|
|
import { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.graphql"
|
|
import {
|
|
GetCurrentFooter,
|
|
GetCurrentFooterRef,
|
|
} from "@/lib/graphql/Query/CurrentFooter.graphql"
|
|
import {
|
|
GetCurrentHeader,
|
|
GetCurrentHeaderRef,
|
|
} from "@/lib/graphql/Query/CurrentHeader.graphql"
|
|
import { GetFooter, GetFooterRef } from "@/lib/graphql/Query/Footer.graphql"
|
|
import { GetHeader, GetHeaderRef } from "@/lib/graphql/Query/Header.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { notFound } from "@/server/errors/trpc"
|
|
import { contentstackBaseProcedure, router } from "@/server/trpc"
|
|
|
|
import {
|
|
generateRefsResponseTag,
|
|
generateTag,
|
|
generateTags,
|
|
} from "@/utils/generateTag"
|
|
|
|
import { langInput } from "./input"
|
|
import {
|
|
type ContactConfigData,
|
|
CurrentFooterDataRaw,
|
|
CurrentFooterRefDataRaw,
|
|
CurrentHeaderData,
|
|
CurrentHeaderDataRaw,
|
|
CurrentHeaderRefDataRaw,
|
|
getHeaderRefSchema,
|
|
getHeaderSchema,
|
|
validateContactConfigSchema,
|
|
validateCurrentFooterConfigSchema,
|
|
validateCurrentHeaderConfigSchema,
|
|
validateFooterConfigSchema,
|
|
validateFooterRefConfigSchema,
|
|
} from "./output"
|
|
import { getConnections, getFooterConnections } from "./utils"
|
|
|
|
import type {
|
|
FooterDataRaw,
|
|
FooterRefDataRaw,
|
|
} from "@/types/components/footer/footer"
|
|
import type { HeaderRefResponse, HeaderResponse } from "@/types/header"
|
|
|
|
const meter = metrics.getMeter("trpc.contentstack.base")
|
|
// OpenTelemetry metrics: ContactConfig
|
|
const getContactConfigCounter = meter.createCounter(
|
|
"trpc.contentstack.contactConfig.get"
|
|
)
|
|
const getContactConfigSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.contactConfig.get-success"
|
|
)
|
|
const getContactConfigFailCounter = meter.createCounter(
|
|
"trpc.contentstack.contactConfig.get-fail"
|
|
)
|
|
// OpenTelemetry metrics: CurrentHeader
|
|
const getCurrentHeaderRefCounter = meter.createCounter(
|
|
"trpc.contentstack.currentHeader.ref.get"
|
|
)
|
|
const getCurrentHeaderRefSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.currentHeader.ref.get-success"
|
|
)
|
|
const getCurrentHeaderRefFailCounter = meter.createCounter(
|
|
"trpc.contentstack.currentHeader.ref.get-fail"
|
|
)
|
|
const getCurrentHeaderCounter = meter.createCounter(
|
|
"trpc.contentstack.currentHeader.get"
|
|
)
|
|
const getCurrentHeaderSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.currentHeader.get-success"
|
|
)
|
|
const getCurrentHeaderFailCounter = meter.createCounter(
|
|
"trpc.contentstack.currentHeader.get-fail"
|
|
)
|
|
|
|
// OpenTelemetry metrics: Header
|
|
const getHeaderRefsCounter = meter.createCounter(
|
|
"trpc.contentstack.header.ref.get"
|
|
)
|
|
const getHeaderRefsSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.header.ref.get-success"
|
|
)
|
|
const getHeaderRefsFailCounter = meter.createCounter(
|
|
"trpc.contentstack.header.ref.get-fail"
|
|
)
|
|
const getHeaderCounter = meter.createCounter("trpc.contentstack.header.get")
|
|
const getHeaderSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.header.get-success"
|
|
)
|
|
const getHeaderFailCounter = meter.createCounter(
|
|
"trpc.contentstack.header.get-fail"
|
|
)
|
|
|
|
// OpenTelemetry metrics: CurrentHeader
|
|
const getCurrentFooterRefCounter = meter.createCounter(
|
|
"trpc.contentstack.currentFooter.ref.get"
|
|
)
|
|
const getCurrentFooterRefSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.currentFooter.ref.get-success"
|
|
)
|
|
const getCurrentFooterRefFailCounter = meter.createCounter(
|
|
"trpc.contentstack.currentFooter.ref.get-fail"
|
|
)
|
|
const getCurrentFooterCounter = meter.createCounter(
|
|
"trpc.contentstack.currentFooter.get"
|
|
)
|
|
const getCurrentFooterSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.currentFooter.get-success"
|
|
)
|
|
const getCurrentFooterFailCounter = meter.createCounter(
|
|
"trpc.contentstack.currentFooter.get-fail"
|
|
)
|
|
|
|
// OpenTelemetry metrics: Footer
|
|
const getFooterRefCounter = meter.createCounter(
|
|
"trpc.contentstack.footer.ref.get"
|
|
)
|
|
const getFooterRefSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.footer.ref.get-success"
|
|
)
|
|
const getFooterRefFailCounter = meter.createCounter(
|
|
"trpc.contentstack.footer.ref.get-fail"
|
|
)
|
|
const getFooterCounter = meter.createCounter("trpc.contentstack.footer.get")
|
|
const getFooterSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.footer.get-success"
|
|
)
|
|
const getFooterFailCounter = meter.createCounter(
|
|
"trpc.contentstack.footer.get-fail"
|
|
)
|
|
|
|
export const baseQueryRouter = router({
|
|
contact: contentstackBaseProcedure.query(async ({ ctx }) => {
|
|
const { lang } = ctx
|
|
getContactConfigCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.contactConfig start",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
const response = await request<ContactConfigData>(
|
|
GetContactConfig,
|
|
{
|
|
locale: lang,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [`${lang}:contact`],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
|
|
getContactConfigFailCounter.add(1, {
|
|
lang,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
|
|
console.error(
|
|
"contentstack.config not found error",
|
|
JSON.stringify({ query: { lang }, error: { code: notFoundError.code } })
|
|
)
|
|
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedContactConfigConfig = validateContactConfigSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedContactConfigConfig.success) {
|
|
getContactConfigFailCounter.add(1, {
|
|
lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedContactConfigConfig.error),
|
|
})
|
|
console.error(
|
|
"contentstack.contactConfig validation error",
|
|
JSON.stringify({
|
|
query: { lang },
|
|
error: validatedContactConfigConfig.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getContactConfigSuccessCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.contactConfig success",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
|
}),
|
|
header: contentstackBaseProcedure.query(async ({ ctx }) => {
|
|
const { lang } = ctx
|
|
getHeaderRefsCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.header.refs start",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
|
|
const responseRef = await request<HeaderRefResponse>(
|
|
GetHeaderRef,
|
|
{
|
|
locale: lang,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateRefsResponseTag(lang, "header")],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!responseRef.data) {
|
|
const notFoundError = notFound(responseRef)
|
|
getHeaderRefsFailCounter.add(1, {
|
|
lang,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.header.refs not found error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang,
|
|
},
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedHeaderRefs = getHeaderRefSchema.safeParse(responseRef.data)
|
|
|
|
if (!validatedHeaderRefs.success) {
|
|
getHeaderRefsFailCounter.add(1, {
|
|
lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedHeaderRefs.error),
|
|
})
|
|
console.error(
|
|
"contentstack.header.refs validation error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang,
|
|
},
|
|
error: validatedHeaderRefs.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getHeaderRefsSuccessCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.header.refs success",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
|
|
const connections = getConnections(validatedHeaderRefs.data)
|
|
|
|
getHeaderCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.header start",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
|
|
const tags = [
|
|
generateTags(lang, connections),
|
|
generateTag(
|
|
lang,
|
|
validatedHeaderRefs.data.all_header.items[0].system.uid
|
|
),
|
|
].flat()
|
|
|
|
const response = await request<HeaderResponse>(
|
|
GetHeader,
|
|
{ locale: lang },
|
|
{ cache: "force-cache", next: { tags } }
|
|
)
|
|
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getHeaderFailCounter.add(1, {
|
|
lang,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.header not found error",
|
|
JSON.stringify({
|
|
query: { lang },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedHeaderConfig = getHeaderSchema.safeParse(response.data)
|
|
|
|
if (!validatedHeaderConfig.success) {
|
|
getHeaderFailCounter.add(1, {
|
|
lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedHeaderConfig.error),
|
|
})
|
|
console.error(
|
|
"contentstack.header validation error",
|
|
JSON.stringify({
|
|
query: { lang },
|
|
error: validatedHeaderConfig.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getHeaderSuccessCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.header success",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
|
|
return validatedHeaderConfig.data
|
|
}),
|
|
currentHeader: contentstackBaseProcedure
|
|
.input(langInput)
|
|
.query(async ({ input }) => {
|
|
getCurrentHeaderRefCounter.add(1, { lang: input.lang })
|
|
console.info(
|
|
"contentstack.currentHeader.ref start",
|
|
JSON.stringify({ query: { lang: input.lang } })
|
|
)
|
|
const responseRef = await request<CurrentHeaderRefDataRaw>(
|
|
GetCurrentHeaderRef,
|
|
{
|
|
locale: input.lang,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateRefsResponseTag(input.lang, "current_header")],
|
|
},
|
|
}
|
|
)
|
|
getCurrentHeaderCounter.add(1, { lang: input.lang })
|
|
console.info(
|
|
"contentstack.currentHeader start",
|
|
JSON.stringify({
|
|
query: { lang: input.lang },
|
|
})
|
|
)
|
|
|
|
const currentHeaderUID =
|
|
responseRef.data.all_current_header.items[0].system.uid
|
|
// There's currently no error handling/validation for the responseRef, should it be added?
|
|
const response = await request<CurrentHeaderDataRaw>(
|
|
GetCurrentHeader,
|
|
{ locale: input.lang },
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateTag(input.lang, currentHeaderUID)],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getCurrentHeaderFailCounter.add(1, {
|
|
lang: input.lang,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.currentHeader not found error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang: input.lang,
|
|
},
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedHeaderConfig = validateCurrentHeaderConfigSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedHeaderConfig.success) {
|
|
getCurrentHeaderFailCounter.add(1, {
|
|
lang: input.lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedHeaderConfig.error),
|
|
})
|
|
console.error(
|
|
"contentstack.currentHeader validation error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang: input.lang,
|
|
},
|
|
error: validatedHeaderConfig.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getCurrentHeaderSuccessCounter.add(1, { lang: input.lang })
|
|
console.info(
|
|
"contentstack.currentHeader success",
|
|
JSON.stringify({
|
|
query: { lang: input.lang },
|
|
})
|
|
)
|
|
const logo =
|
|
validatedHeaderConfig.data.all_current_header.items[0].logoConnection
|
|
.edges?.[0]?.node
|
|
|
|
return {
|
|
...validatedHeaderConfig.data.all_current_header.items[0],
|
|
logo,
|
|
} as CurrentHeaderData
|
|
}),
|
|
currentFooter: contentstackBaseProcedure
|
|
.input(langInput)
|
|
.query(async ({ input }) => {
|
|
getCurrentFooterRefCounter.add(1, { lang: input.lang })
|
|
console.info(
|
|
"contentstack.currentFooter.ref start",
|
|
JSON.stringify({ query: { lang: input.lang } })
|
|
)
|
|
const responseRef = await request<CurrentFooterRefDataRaw>(
|
|
GetCurrentFooterRef,
|
|
{
|
|
locale: input.lang,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateRefsResponseTag(input.lang, "current_footer")],
|
|
},
|
|
}
|
|
)
|
|
// There's currently no error handling/validation for the responseRef, should it be added?
|
|
getCurrentFooterCounter.add(1, { lang: input.lang })
|
|
console.info(
|
|
"contentstack.currentFooter start",
|
|
JSON.stringify({
|
|
query: {
|
|
lang: input.lang,
|
|
},
|
|
})
|
|
)
|
|
const currentFooterUID =
|
|
responseRef.data.all_current_footer.items[0].system.uid
|
|
|
|
const response = await request<CurrentFooterDataRaw>(
|
|
GetCurrentFooter,
|
|
{
|
|
locale: input.lang,
|
|
},
|
|
{
|
|
next: {
|
|
tags: [generateTag(input.lang, currentFooterUID)],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getCurrentFooterFailCounter.add(1, {
|
|
lang: input.lang,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.currentFooter not found error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang: input.lang,
|
|
},
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedCurrentFooterConfig =
|
|
validateCurrentFooterConfigSchema.safeParse(response.data)
|
|
|
|
if (!validatedCurrentFooterConfig.success) {
|
|
getFooterFailCounter.add(1, {
|
|
lang: input.lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedCurrentFooterConfig.error),
|
|
})
|
|
console.error(
|
|
"contentstack.currentFooter validation error",
|
|
JSON.stringify({
|
|
query: { lang: input.lang },
|
|
error: validatedCurrentFooterConfig.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getCurrentFooterSuccessCounter.add(1, { lang: input.lang })
|
|
console.info(
|
|
"contentstack.currentFooter success",
|
|
JSON.stringify({ query: { lang: input.lang } })
|
|
)
|
|
return validatedCurrentFooterConfig.data.all_current_footer.items[0]
|
|
}),
|
|
footer: contentstackBaseProcedure.query(async ({ ctx }) => {
|
|
const { lang } = ctx
|
|
getFooterRefCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.footer.ref start",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
const responseRef = await request<FooterRefDataRaw>(
|
|
GetFooterRef,
|
|
{
|
|
locale: lang,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateRefsResponseTag(lang, "footer")],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!responseRef.data) {
|
|
const notFoundError = notFound(responseRef)
|
|
getFooterRefFailCounter.add(1, {
|
|
lang,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.footer.refs not found error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang,
|
|
},
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedFooterRefs = validateFooterRefConfigSchema.safeParse(
|
|
responseRef.data
|
|
)
|
|
|
|
if (!validatedFooterRefs.success) {
|
|
getFooterRefFailCounter.add(1, {
|
|
lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedFooterRefs.error),
|
|
})
|
|
console.error(
|
|
"contentstack.footer.refs validation error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang,
|
|
},
|
|
error: validatedFooterRefs.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getFooterRefSuccessCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.footer.refs success",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
|
|
const connections = getFooterConnections(validatedFooterRefs.data)
|
|
const footerUID = responseRef.data.all_footer.items[0].system.uid
|
|
|
|
getFooterCounter.add(1, { lang: lang })
|
|
console.info(
|
|
"contentstack.footer start",
|
|
JSON.stringify({
|
|
query: {
|
|
lang,
|
|
},
|
|
})
|
|
)
|
|
const tags = [
|
|
generateTags(lang, connections),
|
|
generateTag(lang, footerUID),
|
|
].flat()
|
|
|
|
const response = await request<FooterDataRaw>(
|
|
GetFooter,
|
|
{
|
|
locale: lang,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getFooterFailCounter.add(1, {
|
|
lang,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.footer not found error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang,
|
|
},
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedFooterConfig = validateFooterConfigSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedFooterConfig.success) {
|
|
getFooterFailCounter.add(1, {
|
|
lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedFooterConfig.error),
|
|
})
|
|
console.error(
|
|
"contentstack.footer validation error",
|
|
JSON.stringify({
|
|
query: { lang: lang },
|
|
error: validatedFooterConfig.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getFooterSuccessCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.footer success",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
|
|
return validatedFooterConfig.data
|
|
}),
|
|
})
|