Sink/server/api/link/create.post.ts
wudi 7cadbfe2b1 refactor: Migrate project files and directories to the app directory using the new compatibilityVersion: 4 feature
- Moved `app.config.ts` to the `app` directory
- Migrated `.vue` files and other assets to the `app` directory
- Updated import paths to reflect the new file locations
2025-03-24 11:05:59 +08:00

37 lines
962 B
TypeScript

import { LinkSchema } from '@@/schemas/link'
export default eventHandler(async (event) => {
const link = await readValidatedBody(event, LinkSchema.parse)
const { caseSensitive } = useRuntimeConfig(event)
if (!caseSensitive) {
link.slug = link.slug.toLowerCase()
}
const { cloudflare } = event.context
const { KV } = cloudflare.env
const existingLink = await KV.get(`link:${link.slug}`)
if (existingLink) {
throw createError({
status: 409, // Conflict
statusText: 'Link already exists',
})
}
else {
const expiration = getExpiration(event, link.expiration)
await KV.put(`link:${link.slug}`, JSON.stringify(link), {
expiration,
metadata: {
expiration,
url: link.url,
comment: link.comment,
},
})
setResponseStatus(event, 201)
const shortLink = `${getRequestProtocol(event)}://${getRequestHost(event)}/${link.slug}`
return { link, shortLink }
}
})