Files
shop/server/utils/coverSlug.ts
Michael Czechowski 44107c0734
Some checks failed
Build and publish / build (push) Failing after 19s
feat: extract shop from mp/shop — initial libreshop/shop
Source moved verbatim from mp/shop/ on 2026-04-29; mp was the first
concrete adapter consuming the libreshop toolkit. Builds and publishes
git.librete.ch/libreshop/shop on every main / v* push via the standard
.gitea/workflows/build.yml shared across libreshop components.
2026-04-29 17:48:56 +02:00

38 lines
976 B
TypeScript

import { fetchCms } from "./cmsApi";
export function slugify(text: string): string {
return text
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/ü/g, "ue")
.replace(/ä/g, "ae")
.replace(/ö/g, "oe")
.replace(/ß/g, "ss");
}
/**
* Resolves a cover slug or ID to the actual cover ID.
* If the input is already a numeric ID, returns it as-is.
* If it's a slug, looks up the cover by name and returns the ID.
*/
export async function resolveCoverId(idOrSlug: string): Promise<string | null> {
// If it's already a numeric ID, return it
if (/^\d+$/.test(idOrSlug)) {
return idOrSlug;
}
// Otherwise, fetch all covers and find by slug
try {
const allCovers = await fetchCms<{ data: any[] }>(`/product-covers`);
const cover = allCovers.data?.find((c: any) => {
const name = c.attributes?.name || c.name;
const slug = slugify(name || "");
return slug === idOrSlug;
});
return cover?.id?.toString() || null;
} catch {
return null;
}
}