mirror of
https://github.com/Nezumi-2711/Sink.git
synced 2026-09-22 20:00:55 +00:00
feat: init
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { LinkSchema } from '@/schemas/link'
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const link = await readValidatedBody(event, LinkSchema.parse)
|
||||
|
||||
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,
|
||||
},
|
||||
})
|
||||
setResponseStatus(event, 201)
|
||||
return { link }
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user