- Adds new path aliases for improved code organization - Updates dependencies to latest versions including vue-sonner v2.0.0 - Removes sourcemap configuration from Nuxt config - Enhances API documentation with OpenAPI schemas - Removes unused security schemes and Wrangler configuration These changes modernize the project setup while improving documentation and removing unnecessary configurations.
61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import { LinkSchema } from '@@/schemas/link'
|
|
|
|
defineRouteMeta({
|
|
openAPI: {
|
|
description: 'Create a new short link',
|
|
requestBody: {
|
|
required: true,
|
|
content: {
|
|
'application/json': {
|
|
// Need: https://github.com/nitrojs/nitro/issues/2974
|
|
schema: {
|
|
type: 'object',
|
|
required: ['url'],
|
|
properties: {
|
|
url: {
|
|
type: 'string',
|
|
description: 'The URL to shorten',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
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 }
|
|
}
|
|
})
|