vendor slugs routes instead of ids

This commit is contained in:
2026-01-27 16:56:57 -05:00
parent aa2f30c086
commit c6ff651f21
10 changed files with 247 additions and 120 deletions

30
lib/utils/slugify.ts Normal file
View File

@@ -0,0 +1,30 @@
export function slugify(text: string): string {
return text
.toString()
.toLowerCase()
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')
}
export async function generateUniqueSlug(
baseSlug: string,
checkUnique: (slug: string) => Promise<boolean>,
maxAttempts = 10
): Promise<string> {
let slug = baseSlug
let attempt = 1
while (await checkUnique(slug)) {
if (attempt >= maxAttempts) {
throw new Error(`Could not generate unique slug after ${maxAttempts} attempts`)
}
slug = `${baseSlug}-${attempt}`
attempt++
}
return slug
}