All files / src/lib/seo live-city.ts

100% Statements 13/13
94.11% Branches 16/17
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                          6x 8x 5x 2x 6x 1x   1x           10x 9x    
import type { MarketplaceCity } from "@interioring/api-types";
 
export type ListedCity = Omit<MarketplaceCity, "state" | "sortOrder">;
 
export type CityParamDecision =
	| { status: 404 }
	| { status: 301; location: string; city: ListedCity }
	| { status: 200; city: ListedCity };
 
/**
 * Resolve an `/interiors/{param}` segment.
 * Unknown, unlisted, and not-live cities 404. A DB id that has a different
 * public slug 301s to that slug (only when the city is live). The 301 carries
 * the city so a child page can confirm its target exists before redirecting
 * — never a 301 into a 404.
 */
export function resolveCityParam(
	param: string | undefined,
	cities: ListedCity[],
	rest = "",
): CityParamDecision {
	if (!param) return { status: 404 };
	const city = cities.find((row) => row.slug === param || row.id === param);
	if (!city || city.marketplaceStatus !== "live") return { status: 404 };
	const suffix = rest ? (rest.startsWith("/") ? rest : `/${rest}`) : "";
	if (param !== city.slug) {
		return { status: 301, location: `/interiors/${city.slug}${suffix}`, city };
	}
	return { status: 200, city };
}
 
export function wikidataSameAs(
	wikidataId: string | null | undefined,
): string | undefined {
	if (!wikidataId) return undefined;
	return `https://www.wikidata.org/wiki/${wikidataId}`;
}