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,35 @@
|
||||
import type { z } from 'zod'
|
||||
import { LinkSchema } from '@/schemas/link'
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const { previewMode } = useRuntimeConfig(event).public
|
||||
if (previewMode) {
|
||||
throw createError({
|
||||
status: 403,
|
||||
statusText: 'Preview mode cannot edit links.',
|
||||
})
|
||||
}
|
||||
const link = await readValidatedBody(event, LinkSchema.parse)
|
||||
const { cloudflare } = event.context
|
||||
const { KV } = cloudflare.env
|
||||
|
||||
const existingLink: z.infer<typeof LinkSchema> | null = await KV.get(`link:${link.slug}`, { type: 'json' })
|
||||
if (existingLink) {
|
||||
const newLink = {
|
||||
...existingLink,
|
||||
...link,
|
||||
id: existingLink.id, // don't update id
|
||||
createdAt: existingLink.createdAt, // don't update createdAt
|
||||
updatedAt: Math.floor(Date.now() / 1000),
|
||||
}
|
||||
const expiration = getExpiration(event, newLink.expiration)
|
||||
await KV.put(`link:${newLink.slug}`, JSON.stringify(newLink), {
|
||||
expiration,
|
||||
metadata: {
|
||||
expiration,
|
||||
},
|
||||
})
|
||||
setResponseStatus(event, 201)
|
||||
return { link: newLink }
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user