fix: enhance slug generation to fallback to default if length is not specified

This commit is contained in:
MinerAle 2025-02-17 11:48:00 +01:00
parent 69018f0ed5
commit 3e9f3bb02d

View file

@ -3,10 +3,10 @@ import { LinkSchema, nanoid } from '@/schemas/link'
export default eventHandler(async (event) => {
const body = await readBody(event)
// If no slug provided, generate one with default length from env
// If no slug provided, generate one with default length from env or fall back to default
if (!body.slug) {
const { slugDefaultLength } = useRuntimeConfig().public
body.slug = nanoid(+slugDefaultLength)()
body.slug = slugDefaultLength ? nanoid(+slugDefaultLength)() : nanoid()()
}
const link = await LinkSchema.parse(body)