The pages carry the CMS markers (data block, list regions, JSON-LD price, inline price); PackagesProvider reads the host's data block after hydration and asks the API only when there is none; the site address comes from the block. A stand-in for the host (config/hostInjector.ts, --cms-data in the preview server) and browser tests check the behaviour with different data. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
156 lines
5.4 KiB
TypeScript
156 lines
5.4 KiB
TypeScript
import type { PricingBanner, PricingPackage } from '@/content/types';
|
|
import { formatPrice } from './formatPrice.ts';
|
|
import {
|
|
mapBanner,
|
|
mapOfferings,
|
|
mapTopBanner,
|
|
type OfferingTopBanner,
|
|
} from './offerings.ts';
|
|
import { resolveSiteUrl } from './siteUrl.ts';
|
|
|
|
// The data island of a page: what the CMS host writes into
|
|
// <script type="application/json" id="cms-data"> after the <!--CMS:DATA-->
|
|
// marker, exactly in the shape of the public API:
|
|
// { "siteUrl": "...", "offerings": [...], "banners": { "top": ..., "bottom": ... } }
|
|
// The build writes the same block with the data it baked, so the page always
|
|
// has one. Everything here is lenient: a block that cannot be used is ignored
|
|
// and the page keeps what it was built with.
|
|
|
|
export interface CmsData {
|
|
// "build" in the block the build baked; absent in what the host writes. A
|
|
// baked block is the data the page was built with, not live data.
|
|
source?: string;
|
|
// Already checked by resolveSiteUrl
|
|
siteUrl?: string;
|
|
offerings?: unknown;
|
|
// undefined: not in the block; null: no active banner
|
|
banners?: { top?: unknown; bottom?: unknown };
|
|
}
|
|
|
|
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
|
|
export function parseCmsData(text: string | null | undefined): CmsData | null {
|
|
if (!text) return null;
|
|
let raw: unknown;
|
|
try {
|
|
raw = JSON.parse(text);
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (!isObject(raw)) return null;
|
|
const banners = isObject(raw.banners)
|
|
? { top: raw.banners.top, bottom: raw.banners.bottom }
|
|
: undefined;
|
|
return {
|
|
source: typeof raw.source === 'string' ? raw.source : undefined,
|
|
siteUrl:
|
|
typeof raw.siteUrl === 'string' && raw.siteUrl !== ''
|
|
? resolveSiteUrl(raw.siteUrl)
|
|
: undefined,
|
|
offerings: Array.isArray(raw.offerings) ? raw.offerings : undefined,
|
|
banners,
|
|
};
|
|
}
|
|
|
|
export const CMS_DATA_ID = 'cms-data';
|
|
export const BUILD_SOURCE = 'build';
|
|
|
|
// The block of the page, or null without a usable one (a preview, a test, a
|
|
// host that does not inject)
|
|
export function readCmsData(
|
|
doc: Pick<Document, 'getElementById'>,
|
|
): CmsData | null {
|
|
return parseCmsData(doc.getElementById(CMS_DATA_ID)?.textContent);
|
|
}
|
|
|
|
// The block the CMS host wrote, or null when the page has none or only the one
|
|
// the build baked (a host that does not inject): then the page asks the API
|
|
export function hostCmsData(
|
|
doc: Pick<Document, 'getElementById'>,
|
|
): CmsData | null {
|
|
const data = readCmsData(doc);
|
|
return data?.source === BUILD_SOURCE ? null : data;
|
|
}
|
|
|
|
// JSON that cannot end the script element or break a line terminator rule:
|
|
// < > & and U+2028/2029 as \u escapes (the host does the same)
|
|
export function scriptJson(value: unknown): string {
|
|
return JSON.stringify(value)
|
|
.replace(/</g, '\\u003c')
|
|
.replace(/>/g, '\\u003e')
|
|
.replace(/&/g, '\\u0026')
|
|
.replace(/\u2028/g, '\\u2028')
|
|
.replace(/\u2029/g, '\\u2029');
|
|
}
|
|
|
|
// The mapped packages back in the shape of GET /api/v1/offerings, for the
|
|
// block the build bakes (the price as the free text the CMS has)
|
|
export function rawOfferings(packages: readonly PricingPackage[]): unknown[] {
|
|
return packages.map((pkg, index) => ({
|
|
slug: pkg.id,
|
|
title: pkg.name,
|
|
price: formatPrice(pkg.price).replace(/\u00a0/g, ' '),
|
|
features: pkg.features,
|
|
displayOrder: index,
|
|
...(pkg.featured ? { featured: true } : {}),
|
|
...(pkg.description ? { description: pkg.description } : {}),
|
|
...(pkg.priceNote ? { priceNote: pkg.priceNote } : {}),
|
|
...(pkg.ctaLabel ? { ctaLabel: pkg.ctaLabel } : {}),
|
|
}));
|
|
}
|
|
|
|
// A banner back in the shape of GET /api/v1/offerings/banner, or null
|
|
export function rawBanner(
|
|
banner: PricingBanner | OfferingTopBanner | null,
|
|
position: 'top' | 'bottom',
|
|
): unknown {
|
|
if (!banner) return null;
|
|
return {
|
|
title: banner.title ?? '',
|
|
text: banner.text ?? null,
|
|
linkLabel: banner.link?.label ?? null,
|
|
linkUrl: banner.link?.url ?? null,
|
|
position,
|
|
};
|
|
}
|
|
|
|
export function bakedCmsData(
|
|
siteUrl: string,
|
|
packages: readonly PricingPackage[],
|
|
banner: PricingBanner | null,
|
|
topBanner: OfferingTopBanner | null,
|
|
): CmsData {
|
|
return {
|
|
source: BUILD_SOURCE,
|
|
siteUrl,
|
|
offerings: rawOfferings(packages),
|
|
banners: {
|
|
top: rawBanner(topBanner, 'top'),
|
|
bottom: rawBanner(banner, 'bottom'),
|
|
},
|
|
};
|
|
}
|
|
|
|
// What the page shows from a data block: only the parts the block has. The
|
|
// host's list is authoritative, so an empty list means no packages (the host
|
|
// switched them all off); null for a banner means none; a part that is not in
|
|
// the block, or a banner answer that cannot be used, leaves the baked one.
|
|
export interface LiveFromData {
|
|
packages?: PricingPackage[];
|
|
banner?: PricingBanner | null;
|
|
topBanner?: OfferingTopBanner | null;
|
|
}
|
|
|
|
export function liveFromData(data: CmsData | null): LiveFromData | null {
|
|
if (!data) return null;
|
|
const live: LiveFromData = {};
|
|
if (data.offerings !== undefined) {
|
|
live.packages = mapOfferings(data.offerings)?.packages ?? [];
|
|
}
|
|
const { top, bottom } = data.banners ?? {};
|
|
if (bottom !== undefined) live.banner = mapBanner(bottom);
|
|
if (top !== undefined) live.topBanner = mapTopBanner(top);
|
|
return live;
|
|
}
|