Files
web/apps/scandic-web/server/routers/contentstack/schemas/pageLinks.ts
Anton Gunnarsson 80100e7631 Merged in monorepo-step-1 (pull request #1080)
Migrate to a monorepo setup - step 1

* Move web to subfolder /apps/scandic-web

* Yarn + transitive deps

- Move to yarn
- design-system package removed for now since yarn doesn't
support the parameter for token (ie project currently broken)
- Add missing transitive dependencies as Yarn otherwise
prevents these imports
- VS Code doesn't pick up TS path aliases unless you open
/apps/scandic-web instead of root (will be fixed with monorepo)

* Pin framer-motion to temporarily fix typing issue

https://github.com/adobe/react-spectrum/issues/7494

* Pin zod to avoid typ error

There seems to have been a breaking change in the types
returned by zod where error is now returned as undefined
instead of missing in the type. We should just handle this
but to avoid merge conflicts just pin the dependency for
now.

* Pin react-intl version

Pin version of react-intl to avoid tiny type issue where formatMessage
does not accept a generic any more. This will be fixed in a future
commit, but to avoid merge conflicts just pin for now.

* Pin typescript version

Temporarily pin version as newer versions as stricter and results in
a type error. Will be fixed in future commit after merge.

* Setup workspaces

* Add design-system as a monorepo package

* Remove unused env var DESIGN_SYSTEM_ACCESS_TOKEN

* Fix husky for monorepo setup

* Update netlify.toml

* Add lint script to root package.json

* Add stub readme

* Fix react-intl formatMessage types

* Test netlify.toml in root

* Remove root toml

* Update netlify.toml publish path

* Remove package-lock.json

* Update build for branch/preview builds


Approved-by: Linus Flood
2025-02-26 10:36:17 +00:00

221 lines
6.2 KiB
TypeScript

import { z } from "zod"
import { removeMultipleSlashes } from "@/utils/url"
import { systemSchema } from "./system"
import { ContentEnum } from "@/types/enums/content"
export const pageLinkSchema = z.object({
system: systemSchema,
title: z.string().optional().default(""),
url: z.string().optional().default(""),
})
export const accountPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.AccountPage),
})
.merge(pageLinkSchema)
export const accountPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.AccountPage),
system: systemSchema,
})
export const extendedPageLinkSchema = pageLinkSchema.merge(
z.object({
web: z
.object({
original_url: z.string().optional().default(""),
})
.default({ original_url: "" }),
})
)
export const collectionPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.CollectionPage),
})
.merge(extendedPageLinkSchema)
export const collectionPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.CollectionPage),
system: systemSchema,
})
export const contentPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.ContentPage),
})
.merge(extendedPageLinkSchema)
export const contentPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.ContentPage),
system: systemSchema,
})
export const destinationCityPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.DestinationCityPage),
})
.merge(pageLinkSchema)
export const destinationCityPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.DestinationCityPage),
system: systemSchema,
})
export const destinationCountryPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.DestinationCountryPage),
})
.merge(pageLinkSchema)
export const destinationCountryPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.DestinationCountryPage),
system: systemSchema,
})
export const destinationOverviewPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.DestinationOverviewPage),
})
.merge(pageLinkSchema)
export const destinationOverviewPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.DestinationOverviewPage),
system: systemSchema,
})
export const hotelPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.HotelPage),
})
.merge(pageLinkSchema)
export const hotelPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.HotelPage),
system: systemSchema,
})
export const loyaltyPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.LoyaltyPage),
})
.merge(extendedPageLinkSchema)
export const loyaltyPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.LoyaltyPage),
system: systemSchema,
})
export const startPageSchema = z
.object({
__typename: z.literal(ContentEnum.blocks.StartPage),
})
.merge(pageLinkSchema)
export const startPageRefSchema = z.object({
__typename: z.literal(ContentEnum.blocks.StartPage),
system: systemSchema,
})
export const linkUnionSchema = z.discriminatedUnion("__typename", [
accountPageSchema,
collectionPageSchema,
contentPageSchema,
destinationCityPageSchema,
destinationCountryPageSchema,
destinationOverviewPageSchema,
hotelPageSchema,
loyaltyPageSchema,
startPageSchema,
])
type Data =
| z.output<typeof accountPageSchema>
| z.output<typeof collectionPageSchema>
| z.output<typeof contentPageSchema>
| z.output<typeof destinationCityPageSchema>
| z.output<typeof destinationCountryPageSchema>
| z.output<typeof destinationOverviewPageSchema>
| z.output<typeof hotelPageSchema>
| z.output<typeof loyaltyPageSchema>
| z.output<typeof startPageSchema>
| Object
export function transformPageLink(data: Data) {
if (data && "__typename" in data) {
switch (data.__typename) {
case ContentEnum.blocks.AccountPage:
case ContentEnum.blocks.HotelPage:
case ContentEnum.blocks.DestinationCityPage:
case ContentEnum.blocks.DestinationCountryPage:
case ContentEnum.blocks.DestinationOverviewPage:
case ContentEnum.blocks.StartPage:
return {
__typename: data.__typename,
system: data.system,
title: data.title,
url: removeMultipleSlashes(`/${data.system.locale}/${data.url}`),
}
case ContentEnum.blocks.ContentPage:
case ContentEnum.blocks.CollectionPage:
case ContentEnum.blocks.LoyaltyPage:
// TODO: Once all links use this transform
// `web` can be removed and not to be worried
// about throughout the application
return {
__typename: data.__typename,
system: data.system,
title: data.title,
url: data.web?.original_url
? data.web.original_url
: removeMultipleSlashes(`/${data.system.locale}/${data.url}`),
web: data.web,
}
}
}
}
export const linkRefsUnionSchema = z.discriminatedUnion("__typename", [
contentPageRefSchema,
hotelPageRefSchema,
loyaltyPageRefSchema,
accountPageRefSchema,
collectionPageRefSchema,
destinationCityPageRefSchema,
destinationCountryPageRefSchema,
destinationOverviewPageRefSchema,
startPageRefSchema,
])
type RefData =
| z.output<typeof accountPageRefSchema>
| z.output<typeof collectionPageRefSchema>
| z.output<typeof contentPageRefSchema>
| z.output<typeof hotelPageRefSchema>
| z.output<typeof loyaltyPageRefSchema>
| z.output<typeof destinationCountryPageRefSchema>
| z.output<typeof destinationCityPageRefSchema>
| z.output<typeof destinationOverviewPageRefSchema>
| z.output<typeof startPageRefSchema>
| Object
export function transformPageLinkRef(data: RefData) {
if (data && "__typename" in data) {
switch (data.__typename) {
case ContentEnum.blocks.AccountPage:
case ContentEnum.blocks.ContentPage:
case ContentEnum.blocks.CollectionPage:
case ContentEnum.blocks.HotelPage:
case ContentEnum.blocks.LoyaltyPage:
case ContentEnum.blocks.DestinationCityPage:
case ContentEnum.blocks.DestinationCountryPage:
case ContentEnum.blocks.DestinationOverviewPage:
case ContentEnum.blocks.StartPage:
return data.system
}
}
}