Merged in feat/revalidate-cache-my-pages-navigation (pull request #132)

Feat/revalidate cache my pages navigation

Approved-by: Michael Zetterberg
This commit is contained in:
Simon.Emanuelsson
2024-05-06 11:53:04 +00:00
committed by Michael Zetterberg
40 changed files with 600 additions and 144 deletions

View File

@@ -1,30 +1,44 @@
import { revalidateTag } from "next/cache" import { revalidateTag } from "next/cache"
import { headers } from "next/headers"
import { NextRequest } from "next/server" import { NextRequest } from "next/server"
import { z } from "zod"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server" import { env } from "@/env/server"
import { internalServerError } from "@/server/errors/next"
import {
generateRefsResponseTag,
generateRefTag,
generateTag,
} from "@/utils/generateTag"
const validateJsonBody = z.object({
api_key: z.string(),
module: z.string(),
data: z.object({
content_type: z.object({
uid: z.string(),
}),
entry: z.object({
locale: z.nativeEnum(Lang),
uid: z.string(),
url: z.string().optional(),
}),
}),
})
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
try { try {
const secret = request.nextUrl.searchParams.get("secret") ?? "" const headersList = headers()
const tagsParam = request.nextUrl.searchParams.get("tags") ?? "" const secret = headersList.get("x-revalidate-secret")
if (secret !== env.REVALIDATE_SECRET) { if (secret !== env.REVALIDATE_SECRET) {
console.error(`Invalid Secret`)
console.error({ secret })
return Response.json( return Response.json(
{ {
message: "Invalid secret",
now: Date.now(),
revalidated: false,
},
{
status: 401,
}
)
}
if (!tagsParam) {
return Response.json(
{
message: "Missing tags param",
now: Date.now(), now: Date.now(),
revalidated: false, revalidated: false,
}, },
@@ -34,31 +48,37 @@ export async function POST(request: NextRequest) {
) )
} }
const tags = tagsParam.split(",") const data = await request.json()
if (!tags.length) { const validatedData = validateJsonBody.safeParse(data)
return Response.json( if (!validatedData.success) {
{ console.error("Bad validation for `validatedData`")
message: "No tags", console.error(validatedData.error)
now: Date.now(), return internalServerError({ revalidated: false, now: Date.now() })
revalidated: false,
},
{
status: 400,
}
)
} }
tags.forEach((tag) => { const {
revalidateTag(tag) data: {
}) data: { content_type, entry },
},
} = validatedData
const identifier = entry.url ?? content_type.uid
const refsTag = generateRefsResponseTag(entry.locale, identifier)
const refTag = generateRefTag(entry.locale, content_type.uid, entry.uid)
const tag = generateTag(entry.locale, entry.uid)
console.info(`Revalidating refsTag: ${refsTag}`)
revalidateTag(refsTag)
console.info(`Revalidating refTag: ${refTag}`)
revalidateTag(refTag)
console.info(`Revalidating tag: ${tag}`)
revalidateTag(tag)
return Response.json({ revalidated: true, now: Date.now() }) return Response.json({ revalidated: true, now: Date.now() })
} catch (error) { } catch (error) {
console.info("Failed to revalidate tag(s)") console.info("Failed to revalidate tag(s)")
console.error(error) console.error(error)
return Response.json( return internalServerError({ revalidated: false, now: Date.now() })
{ revalidated: false, now: Date.now() },
{ status: 500 }
)
} }
} }

View File

@@ -1,19 +0,0 @@
import type {
MenuItem,
NavigationItem,
} from "@/types/requests/myPages/navigation"
export function mapMenuItems(navigationItems: NavigationItem[]) {
return navigationItems.map(({ item }): MenuItem => {
const { node } = item.pageConnection.edges[0]
return {
linkText: item.link_text || node.title,
lang: node.system.locale,
subItems: item.sub_items ? mapMenuItems(item.sub_items) : null,
uid: node.system.uid,
url: `/${node.system.locale}/${node.url}`.replaceAll(/\/\/+/g, "/"),
originalUrl: node.web?.original_url,
}
})
}

View File

@@ -1,38 +1,25 @@
import { Fragment } from "react" import { Fragment } from "react"
import { LogOut } from "react-feather" import { LogOut } from "react-feather"
import { GetNavigationMyPages } from "@/lib/graphql/Query/NavigationMyPages.graphql" import { serverClient } from "@/lib/trpc/server"
import { request } from "@/lib/graphql/request"
import Link from "@/components/TempDesignSystem/Link" import Link from "@/components/TempDesignSystem/Link"
import Title from "@/components/Title" import Title from "@/components/Title"
import { mapMenuItems } from "./helpers"
import styles from "./sidebar.module.css" import styles from "./sidebar.module.css"
import type { import type { SidebarProps } from "@/types/requests/myPages/navigation"
GetNavigationMyPagesData,
SidebarProps,
} from "@/types/requests/myPages/navigation"
export default async function Sidebar({ lang }: SidebarProps) { export default async function Sidebar({ lang }: SidebarProps) {
const response = await request<GetNavigationMyPagesData>( const navigation =
GetNavigationMyPages, await serverClient().contentstack.myPages.navigation.get(lang)
{
locale: lang,
}
)
// navigation_my_pages is of type Single, hence the hard [0]
const navigation = response.data.all_navigation_my_pages.items[0]
const menuItems = mapMenuItems(navigation.items)
return ( return (
<aside className={styles.sidebar}> <aside className={styles.sidebar}>
<nav className={styles.nav}> <nav className={styles.nav}>
<Title level="h5" uppercase> <Title level="h5" uppercase>
{navigation.title} {navigation.title}
</Title> </Title>
{menuItems.map((item) => ( {navigation.items.map((item) => (
<Fragment key={item.uid}> <Fragment key={item.uid}>
<Link <Link
href={item.originalUrl || item.url} href={item.originalUrl || item.url}
@@ -42,18 +29,16 @@ export default async function Sidebar({ lang }: SidebarProps) {
{item.linkText} {item.linkText}
</Link> </Link>
{item.subItems {item.subItems
? item.subItems.map((subItem) => { ? item.subItems.map((subItem) => (
return ( <Link
<Link key={subItem.uid}
key={subItem.uid} href={subItem.originalUrl || subItem.url}
href={subItem.originalUrl || subItem.url} partialMatch
partialMatch variant="sidebar"
variant="sidebar" >
> {subItem.linkText}
{subItem.linkText} </Link>
</Link> ))
)
})
: null} : null}
</Fragment> </Fragment>
))} ))}

View File

@@ -85,7 +85,7 @@ export default function FormContent({ control }: EditFormContentProps) {
<HouseIcon /> <HouseIcon />
</Field.Icon> </Field.Icon>
<Field.Label htmlFor="address.streetAddress"> <Field.Label htmlFor="address.streetAddress">
*{_("Address")} {_("Address")}
</Field.Label> </Field.Label>
<Field.Content> <Field.Content>
<Input <Input
@@ -93,7 +93,6 @@ export default function FormContent({ control }: EditFormContentProps) {
control={control} control={control}
name="address.streetAddress" name="address.streetAddress"
placeholder={_("Street 123")} placeholder={_("Street 123")}
registerOptions={{ required: true }}
/> />
</Field.Content> </Field.Content>
</Field> </Field>
@@ -102,14 +101,13 @@ export default function FormContent({ control }: EditFormContentProps) {
<Field.Icon> <Field.Icon>
<HouseIcon /> <HouseIcon />
</Field.Icon> </Field.Icon>
<Field.Label htmlFor="address.city">*{_("City/State")}</Field.Label> <Field.Label htmlFor="address.city">{_("City/State")}</Field.Label>
<Field.Content> <Field.Content>
<Input <Input
aria-label={_("City")} aria-label={_("City")}
control={control} control={control}
name="address.city" name="address.city"
placeholder={_("City")} placeholder={_("City")}
registerOptions={{ required: true }}
/> />
</Field.Content> </Field.Content>
</Field> </Field>

View File

@@ -1,4 +1,4 @@
fragment Breadcrumbs on AccountPage { fragment MyPagesBreadcrumbs on AccountPage {
breadcrumbs { breadcrumbs {
title title
parents: parentsConnection { parents: parentsConnection {

View File

@@ -0,0 +1,7 @@
#import "./System.graphql"
fragment AccountPageRef on AccountPage {
system {
...System
}
}

View File

@@ -0,0 +1,7 @@
#import "./System.graphql"
fragment ContentPageRef on ContentPage {
system {
...System
}
}

View File

@@ -0,0 +1,7 @@
#import "./System.graphql"
fragment LoyaltyPageRef on LoyaltyPage {
system {
...System
}
}

View File

@@ -0,0 +1,4 @@
fragment System on EntrySystemField {
content_type_uid
uid
}

View File

@@ -3,11 +3,10 @@
query GetMyPagesBreadcrumbs($locale: String!, $url: String!) { query GetMyPagesBreadcrumbs($locale: String!, $url: String!) {
all_account_page(locale: $locale, where: { url: $url }) { all_account_page(locale: $locale, where: { url: $url }) {
items { items {
...Breadcrumbs ...MyPagesBreadcrumbs
system { system {
uid uid
} }
} }
total
} }
} }

View File

@@ -1,9 +1,13 @@
#import "../Fragments/PageLink/AccountPageLink.graphql" #import "../Fragments/PageLink/AccountPageLink.graphql"
#import "../Fragments/PageLink/ContentPageLink.graphql" #import "../Fragments/PageLink/ContentPageLink.graphql"
#import "../Fragments/PageLink/LoyaltyPageLink.graphql" #import "../Fragments/PageLink/LoyaltyPageLink.graphql"
#import "../Fragments/Refs/AccountPage.graphql"
#import "../Fragments/Refs/ContentPage.graphql"
#import "../Fragments/Refs/LoyaltyPage.graphql"
#import "../Fragments/Refs/System.graphql"
query GetNavigationMyPages($locale: String!) { query GetNavigationMyPages($locale: String!) {
all_navigation_my_pages(locale: $locale) { all_navigation_my_pages(locale: $locale, limit: 1) {
items { items {
items { items {
... on NavigationMyPagesItemsItem { ... on NavigationMyPagesItemsItem {
@@ -20,8 +24,8 @@ query GetNavigationMyPages($locale: String!) {
node { node {
__typename __typename
...AccountPageLink ...AccountPageLink
...LoyaltyPageLink
...ContentPageLink ...ContentPageLink
...LoyaltyPageLink
} }
} }
} }
@@ -33,8 +37,8 @@ query GetNavigationMyPages($locale: String!) {
node { node {
__typename __typename
...AccountPageLink ...AccountPageLink
...LoyaltyPageLink
...ContentPageLink ...ContentPageLink
...LoyaltyPageLink
} }
} }
} }
@@ -45,3 +49,47 @@ query GetNavigationMyPages($locale: String!) {
} }
} }
} }
query GetNavigationMyPagesRefs($locale: String!) {
all_navigation_my_pages(locale: $locale, limit: 1) {
items {
items {
... on NavigationMyPagesItemsItem {
__typename
item {
sub_items {
... on NavigationMyPagesItemsItemBlockSubItemsItem {
__typename
item {
pageConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
}
}
}
}
}
pageConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
}
}
}
}
}
system {
...System
}
}
}
}

View File

@@ -66,6 +66,7 @@ export async function request<T>(
return { data: response } return { data: response }
} catch (error) { } catch (error) {
console.info(`GraphQL Request Error`)
console.error(error) console.error(error)
throw new Error("Something went wrong") throw new Error("Something went wrong")
} }

View File

@@ -1,13 +1,27 @@
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
export function badRequest() { export function badRequest(body: unknown | string = "Bad request") {
return new NextResponse("Bad request", { const resInit = {
status: 400, status: 400,
}) }
if (typeof body === "string") {
return new NextResponse(body, resInit)
}
return NextResponse.json(body, resInit)
} }
export function internalServerError() { export function internalServerError(
return new NextResponse("Internal Server Error", { body: unknown | string = "Internal Server Error"
) {
const resInit = {
status: 500, status: 500,
}) }
if (typeof body === "string") {
return new NextResponse(body, resInit)
}
return NextResponse.json(body, resInit)
} }

View File

@@ -27,7 +27,6 @@ export const validateBreadcrumbsConstenstackSchema = z.object({
}), }),
}) })
), ),
total: z.number(),
}), }),
}) })

View File

@@ -4,10 +4,12 @@ import { accountPageRouter } from "./accountPage"
import { breadcrumbsRouter } from "./breadcrumbs" import { breadcrumbsRouter } from "./breadcrumbs"
import { contactConfigRouter } from "./contactConfig" import { contactConfigRouter } from "./contactConfig"
import { loyaltyPageRouter } from "./loyaltyPage" import { loyaltyPageRouter } from "./loyaltyPage"
import { myPagesRouter } from "./myPages"
export const contentstackRouter = router({ export const contentstackRouter = router({
breadcrumbs: breadcrumbsRouter, breadcrumbs: breadcrumbsRouter,
loyaltyPage: loyaltyPageRouter,
accountPage: accountPageRouter, accountPage: accountPageRouter,
contactConfig: contactConfigRouter, contactConfig: contactConfigRouter,
loyaltyPage: loyaltyPageRouter,
myPages: myPagesRouter,
}) })

View File

@@ -9,7 +9,7 @@ import {
SidebarTypenameEnum, SidebarTypenameEnum,
} from "@/types/components/loyalty/enums" } from "@/types/components/loyalty/enums"
import { Embeds } from "@/types/requests/embeds" import { Embeds } from "@/types/requests/embeds"
import { Edges } from "@/types/requests/utils/edges" import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
import { RTEDocument } from "@/types/rte/node" import { RTEDocument } from "@/types/rte/node"
const loyaltyPageBlockCardGrid = z.object({ const loyaltyPageBlockCardGrid = z.object({
@@ -216,7 +216,7 @@ export interface RteBlockContent extends BlockContentRaw {
content: { content: {
content: { content: {
json: RTEDocument json: RTEDocument
embedded_itemsConnection: Edges<Embeds> embedded_itemsConnection: EdgesWithTotalCount<Embeds>
} }
} }
} }
@@ -243,7 +243,7 @@ export type RteSidebarContent = Omit<SidebarContentRaw, "content"> & {
content: { content: {
content: { content: {
json: RTEDocument json: RTEDocument
embedded_itemsConnection: Edges<Embeds> embedded_itemsConnection: EdgesWithTotalCount<Embeds>
} }
} }
} }

View File

@@ -35,6 +35,7 @@ export const loyaltyPageQueryRouter = router({
) )
if (!validatedLoyaltyPage.success) { if (!validatedLoyaltyPage.success) {
console.error("Bad validation for `validatedLoyaltyPage`")
console.error(validatedLoyaltyPage.error) console.error(validatedLoyaltyPage.error)
throw badRequestError() throw badRequestError()
} }

View File

@@ -0,0 +1,6 @@
import { router } from "@/server/trpc";
import { navigationRouter } from "./navigation";
export const myPagesRouter = router({
navigation: navigationRouter,
})

View File

@@ -0,0 +1,4 @@
import { mergeRouters } from "@/server/trpc";
import { navigationQueryRouter } from "./query";
export const navigationRouter = mergeRouters(navigationQueryRouter)

View File

@@ -0,0 +1,5 @@
import { z } from "zod";
import { Lang } from "@/constants/languages";
export const getNavigationInputSchema = z.nativeEnum(Lang)

View File

@@ -0,0 +1,127 @@
import { z } from "zod"
import { Lang } from "@/constants/languages"
import { PageLinkEnum } from "@/types/requests/myPages/navigation"
const pageConnection = z.object({
edges: z.array(
z.object({
node: z.object({
__typename: z.nativeEnum(PageLinkEnum),
system: z.object({
locale: z.nativeEnum(Lang),
uid: z.string(),
}),
title: z.string(),
url: z.string(),
}),
})
),
})
const pageConnectionRefs = z.object({
edges: z.array(
z.object({
node: z.object({
__typename: z.nativeEnum(PageLinkEnum),
system: z.object({
content_type_uid: z.string(),
uid: z.string(),
}),
}),
})
),
})
export const navigationRefsPayloadSchema = z.object({
all_navigation_my_pages: z.object({
items: z
.array(
z.object({
items: z.array(
z.object({
__typename: z.string(),
item: z.object({
sub_items: z.array(
z.object({
__typename: z.string(),
item: z.object({
pageConnection: pageConnectionRefs,
}),
})
),
pageConnection: pageConnectionRefs,
}),
})
),
system: z.object({
content_type_uid: z.string(),
uid: z.string(),
}),
})
)
.refine(
(input) => {
return input.length === 1
},
{
message: `Expected navigationRefsPayloadSchema 1 all_navigation_my_pages item`,
}
),
}),
})
export const navigationPayloadSchema = z.object({
all_navigation_my_pages: z.object({
items: z
.array(
z.object({
items: z.array(
z.object({
item: z.object({
link_text: z.string().default(""),
pageConnection,
sub_items: z.array(
z.object({
item: z.object({
link_text: z.string().default(""),
pageConnection,
}),
})
),
}),
})
),
title: z.string(),
})
)
.refine(
(input) => {
return input.length === 1
},
{
message: `Expected navigationPayloadSchema to containt 1 all_navigation_my_pages item`,
}
),
}),
})
const baseMenuItem = z.object({
lang: z.nativeEnum(Lang),
linkText: z.string(),
uid: z.string(),
url: z.string(),
originalUrl: z.string().optional(),
})
export const getNavigationSchema = z.object({
items: z.array(
z
.object({
subItems: z.array(baseMenuItem),
})
.merge(baseMenuItem)
),
title: z.string(),
})

View File

@@ -0,0 +1,123 @@
import {
GetNavigationMyPages,
GetNavigationMyPagesRefs,
} from "@/lib/graphql/Query/NavigationMyPages.graphql"
import { request } from "@/lib/graphql/request"
import { badRequestError, internalServerError } from "@/server/errors/trpc"
import { publicProcedure, router } from "@/server/trpc"
import {
generateRefsResponseTag,
generateTag,
generateTags,
} from "@/utils/generateTag"
import { getNavigationInputSchema } from "./input"
import {
getNavigationSchema,
navigationPayloadSchema,
navigationRefsPayloadSchema,
} from "./output"
import { getConnections } from "./utils"
import type {
GetNavigationMyPagesData,
GetNavigationMyPagesRefsData,
MenuItem,
NavigationItem,
} from "@/types/requests/myPages/navigation"
export function mapMenuItems(navigationItems: NavigationItem[]) {
return navigationItems.map(({ item }): MenuItem => {
const { node } = item.pageConnection.edges[0]
const menuItem: MenuItem = {
lang: node.system.locale,
linkText: item.link_text || node.title,
uid: node.system.uid,
url: `/${node.system.locale}/${node.url}`.replaceAll(/\/\/+/g, "/"),
}
if ("sub_items" in item) {
menuItem.subItems = mapMenuItems(item.sub_items)
}
return menuItem
})
}
export const navigationQueryRouter = router({
get: publicProcedure.input(getNavigationInputSchema).query(async function ({
input: lang,
}) {
try {
const refsResponse = await request<GetNavigationMyPagesRefsData>(
GetNavigationMyPagesRefs,
{ locale: lang },
{ tags: [generateRefsResponseTag(lang, "navigation_my_pages")] }
)
if (!refsResponse.data) {
console.error("Bad response for `GetNavigationMyPagesRefs`")
console.error(refsResponse)
throw internalServerError()
}
const validatedMyPagesNavigationRefs =
navigationRefsPayloadSchema.safeParse(refsResponse.data)
if (!validatedMyPagesNavigationRefs.success) {
console.error("Bad validation for `GetNavigationMyPagesRefs`")
console.error(validatedMyPagesNavigationRefs.error)
throw badRequestError()
}
const connections = getConnections(validatedMyPagesNavigationRefs.data)
const tags = generateTags(lang, connections)
const navigation =
validatedMyPagesNavigationRefs.data.all_navigation_my_pages.items[0]
tags.push(generateTag(lang, navigation.system.uid))
const response = await request<GetNavigationMyPagesData>(
GetNavigationMyPages,
{ locale: lang },
{ tags }
)
if (!response.data) {
console.error("Bad response for `GetNavigationMyPages`")
console.error(response)
throw internalServerError()
}
const validatedMyPagesNavigation = navigationPayloadSchema.safeParse(
response.data
)
if (!validatedMyPagesNavigation.success) {
console.error("Bad validation for `GetNavigationMyPages`")
console.error(validatedMyPagesNavigation.error)
throw badRequestError()
}
const menuItem =
validatedMyPagesNavigation.data.all_navigation_my_pages.items[0]
const nav = {
items: mapMenuItems(menuItem.items),
title: menuItem.title,
}
const validatedNav = getNavigationSchema.safeParse(nav)
if (!validatedNav.success) {
console.error("Bad validation for `getNavigationSchema`")
console.error(validatedNav.error)
throw badRequestError()
}
return validatedNav.data
} catch (error) {
console.info(`Get My Pages Navigation Error`)
console.error(error)
throw internalServerError()
}
}),
})

View File

@@ -0,0 +1,18 @@
import type { GetNavigationMyPagesRefsData } from "@/types/requests/myPages/navigation"
import type { Edges } from "@/types/requests/utils/edges"
import type { NodeRefs } from "@/types/requests/utils/refs"
export function getConnections(refs: GetNavigationMyPagesRefsData) {
const connections: Edges<NodeRefs>[] = []
refs.all_navigation_my_pages.items.forEach((ref) => {
ref.items.forEach(({ item }) => {
connections.push(item.pageConnection)
item.sub_items.forEach(({ item: subItem }) => {
connections.push(subItem.pageConnection)
})
})
})
return connections
}

View File

@@ -1,5 +1,5 @@
import type { Edges } from "../utils/edges"
import type { Lang } from "@/constants/languages" import type { Lang } from "@/constants/languages"
import type { EdgesWithTotalCount } from "../utils/edges"
import type { Typename } from "../utils/typename" import type { Typename } from "../utils/typename"
export enum Section { export enum Section {
@@ -75,6 +75,6 @@ export type ContactNode = {
export type Contact = { export type Contact = {
contact: { contact: {
contactConnection: Edges<ContactNode> contactConnection: EdgesWithTotalCount<ContactNode>
} }
} }

View File

@@ -1,11 +1,11 @@
import type { Edges } from "../utils/edges"
import type { Embeds } from "../embeds"
import type { RTEDocument } from "@/types/rte/node" import type { RTEDocument } from "@/types/rte/node"
import type { Embeds } from "../embeds"
import type { EdgesWithTotalCount } from "../utils/edges"
export type Text = { export type Text = {
text: { text: {
content: { content: {
embedded_itemsConnection: Edges<Embeds> embedded_itemsConnection: EdgesWithTotalCount<Embeds>
json: RTEDocument json: RTEDocument
} }
} }

View File

@@ -7,11 +7,8 @@ import type { List } from "./blocks/list"
import type { PuffBlock } from "./blocks/puff" import type { PuffBlock } from "./blocks/puff"
import type { Preamble } from "./preamble" import type { Preamble } from "./preamble"
import type { Text } from "./blocks/text" import type { Text } from "./blocks/text"
import type { AllRequestResponse } from "./utils/all" import type { AllRequestResponseWithTotal } from "./utils/all"
import type { import type { AsideTypenameEnum, Typename } from "./utils/typename"
AsideTypenameEnum,
Typename,
} from "./utils/typename"
export type Asides = export type Asides =
| Typename<Contact, AsideTypenameEnum.CurrentBlocksPageAsideContact> | Typename<Contact, AsideTypenameEnum.CurrentBlocksPageAsideContact>
@@ -48,5 +45,5 @@ export type BlockPage = {
} }
export type GetCurrentBlockPageData = { export type GetCurrentBlockPageData = {
all_current_blocks_page: AllRequestResponse<BlockPage> all_current_blocks_page: AllRequestResponseWithTotal<BlockPage>
} }

View File

@@ -1,10 +1,10 @@
import type { AllRequestResponse } from "./utils/all"
import type { Edges } from "./utils/edges"
import type { Image } from "../image" import type { Image } from "../image"
import type { AllRequestResponse } from "./utils/all"
import type { EdgesWithTotalCount } from "./utils/edges"
type AppDownload = { type AppDownload = {
href: string href: string
imageConnection: Edges<Image> imageConnection: EdgesWithTotalCount<Image>
} }
export type Link = { export type Link = {
@@ -33,7 +33,7 @@ export type Footer = {
app_store: AppDownload app_store: AppDownload
google_play: AppDownload google_play: AppDownload
} }
logoConnection: Edges<Image> logoConnection: EdgesWithTotalCount<Image>
navigation: NavigationItem[] navigation: NavigationItem[]
social_media: { social_media: {
title: string title: string
@@ -43,7 +43,7 @@ export type Footer = {
} }
trip_advisor: { trip_advisor: {
title: string title: string
logoConnection: Edges<Image> logoConnection: EdgesWithTotalCount<Image>
} }
} }

View File

@@ -1,5 +1,5 @@
import type { Edges } from "./utils/edges"
import type { Image } from "../image" import type { Image } from "../image"
import type { EdgesWithTotalCount } from "./utils/edges"
export type HeaderLink = { export type HeaderLink = {
href: string href: string
@@ -27,7 +27,7 @@ export type HeaderQueryData = {
all_header: { all_header: {
items: { items: {
frontpage_link_text: string frontpage_link_text: string
logoConnection: Edges<Image> logoConnection: EdgesWithTotalCount<Image>
menu: HeaderLinks menu: HeaderLinks
top_menu: TopMenuHeaderLinks top_menu: TopMenuHeaderLinks
}[] }[]

View File

@@ -1,6 +1,6 @@
import type { Image } from "../image" import type { Image } from "../image"
import type { Edges } from "./utils/edges" import type { EdgesWithTotalCount } from "./utils/edges"
export type Hero = { export type Hero = {
imagesConnection: Edges<Image> imagesConnection: EdgesWithTotalCount<Image>
} }

View File

@@ -1,10 +1,10 @@
import type { AllRequestResponse } from "../utils/all" import type { AllRequestResponse } from "../utils/all"
import type { Edges } from "../utils/edges" import type { EdgesWithTotalCount } from "../utils/edges"
interface AccountPageBreadcrumbs { interface AccountPageBreadcrumbs {
breadcrumbs: { breadcrumbs: {
title: string title: string
parents: Edges<{ parents: EdgesWithTotalCount<{
breadcrumbs: { breadcrumbs: {
title: string title: string
} }

View File

@@ -1,10 +1,10 @@
import type { Edges } from "../utils/edges"
import type { Image } from "../../image" import type { Image } from "../../image"
import type { EdgesWithTotalCount } from "../utils/edges"
export type LogoQueryData = { export type LogoQueryData = {
all_header: { all_header: {
items: { items: {
logoConnection: Edges<Image> logoConnection: EdgesWithTotalCount<Image>
}[] }[]
} }
} }

View File

@@ -1,4 +1,5 @@
import type { Lang } from "@/constants/languages" import type { Lang } from "@/constants/languages"
import type { System } from "../system"
import type { AllRequestResponse } from "../utils/all" import type { AllRequestResponse } from "../utils/all"
import type { Edges } from "../utils/edges" import type { Edges } from "../utils/edges"
import type { TypenameInterface } from "../utils/typename" import type { TypenameInterface } from "../utils/typename"
@@ -12,10 +13,10 @@ export enum PageLinkEnum {
export type MenuItem = { export type MenuItem = {
lang: Lang lang: Lang
linkText: string linkText: string
subItems: MenuItem[] | null
uid: string uid: string
url: string url: string
originalUrl: string | undefined originalUrl?: string
subItems?: MenuItem[]
} }
export type SidebarProps = { export type SidebarProps = {
@@ -27,8 +28,8 @@ interface NavigationLink {
locale: Lang locale: Lang
uid: string uid: string
} }
url: string
title: string title: string
url: string
web?: { original_url: string } web?: { original_url: string }
} }
@@ -46,16 +47,44 @@ export interface ContentPageLink
export type PageLink = ContentPageLink | AccountPageLink | LoyaltyPageLink export type PageLink = ContentPageLink | AccountPageLink | LoyaltyPageLink
export type NavigationItem = { interface Item {
item: { link_text: string
pageConnection: Edges<PageLink> pageConnection: Edges<PageLink>
link_text: string
sub_items: NavigationItem[] | null
}
} }
export type NavigationMyPages = { items: NavigationItem[]; title: string } interface ItemWithSubitem extends Item {
sub_items: NavigationItem[]
}
export type NavigationItem = {
item: Item | ItemWithSubitem
}
export type NavigationMyPages = {
items: NavigationItem[]
title: string
}
export type GetNavigationMyPagesData = { export type GetNavigationMyPagesData = {
all_navigation_my_pages: AllRequestResponse<NavigationMyPages> all_navigation_my_pages: AllRequestResponse<NavigationMyPages>
} }
/** Refs Request */
type NavigationItemRef = {
item: {
pageConnection: Edges<System>
sub_items: {
item: {
pageConnection: Edges<System>
}
}[]
}
}
interface NavigationMyPagesRefs extends System {
items: NavigationItemRef[]
}
export type GetNavigationMyPagesRefsData = {
all_navigation_my_pages: AllRequestResponse<NavigationMyPagesRefs>
}

View File

@@ -1,10 +1,10 @@
import type { Edges } from "./utils/edges"
import type { RTEDocument } from "../rte/node" import type { RTEDocument } from "../rte/node"
import type { Embeds } from "./embeds" import type { Embeds } from "./embeds"
import type { EdgesWithTotalCount } from "./utils/edges"
export type Preamble = { export type Preamble = {
text: { text: {
embedded_itemsConnection: Edges<Embeds> embedded_itemsConnection: EdgesWithTotalCount<Embeds>
json: RTEDocument json: RTEDocument
} }
} }

View File

@@ -1,6 +1,6 @@
import type { Image } from "../image" import type { Image } from "../image"
import type { Edges } from "./utils/edges"
import type { RTEDocument } from "../rte/node" import type { RTEDocument } from "../rte/node"
import type { EdgesWithTotalCount } from "./utils/edges"
export enum PuffStyleEnum { export enum PuffStyleEnum {
button = "button", button = "button",
@@ -8,7 +8,7 @@ export enum PuffStyleEnum {
} }
export type Puff = { export type Puff = {
imageConnection: Edges<Image> imageConnection: EdgesWithTotalCount<Image>
link: { link: {
href: string href: string
title?: string title?: string

6
types/requests/system.ts Normal file
View File

@@ -0,0 +1,6 @@
export interface System {
system: {
content_type_uid: string
uid: string
}
}

View File

@@ -1,4 +1,8 @@
export interface AllRequestResponse<T> { export interface AllRequestResponse<T> {
items: T[] items: T[]
}
export interface AllRequestResponseWithTotal<T> {
items: T[]
total: number total: number
} }

View File

@@ -4,5 +4,9 @@ export type Node<T> = {
export type Edges<T> = { export type Edges<T> = {
edges: Node<T>[] edges: Node<T>[]
}
export type EdgesWithTotalCount<T> = {
edges: Node<T>[]
totalCount: number totalCount: number
} }

View File

@@ -0,0 +1,3 @@
import type { System } from "../system"
export interface NodeRefs extends System {}

View File

@@ -38,6 +38,7 @@ export async function getContentTypeByPathName(
) )
if (!validatedContentTypeUid.success) { if (!validatedContentTypeUid.success) {
console.error("Bad validation for `validatedContentTypeUid`")
console.error(validatedContentTypeUid.error) console.error(validatedContentTypeUid.error)
return null return null
} }

56
utils/generateTag.ts Normal file
View File

@@ -0,0 +1,56 @@
import type { Edges } from "@/types/requests/utils/edges"
import type { Lang } from "@/constants/languages"
import type { NodeRefs } from "@/types/requests/utils/refs"
/**
* Function to generate tag for initial refs request
*
* @param lang
* @param identifier Should be uri for all pages and content_type_uid for
* everything else
* @returns string
*/
export function generateRefsResponseTag(lang: Lang, identifier: string) {
return `${lang}:${identifier}:refs`
}
/**
* Function to generate all tags to references on entity
*
* @param lang Lang
* @param contentTypeUid content_type_uid of reference
* @param uid system.uid of reference
* @returns string
*/
export function generateRefTag(
lang: Lang,
contentTypeUid: string,
uid: string
) {
return `${lang}:ref:${contentTypeUid}:${uid}`
}
/**
* Function to generate tag for entity being requested
*
* @param lang Lang
* @param uid system.uid of entity
* @returns string
*/
export function generateTag(lang: Lang, uid: string) {
return `${lang}:${uid}`
}
export function generateTags(lang: Lang, connections: Edges<NodeRefs>[]) {
return connections
.map((connection) => {
return connection.edges.map(({ node }) => {
return generateRefTag(
lang,
node.system.content_type_uid,
node.system.uid
)
})
})
.flat()
}