Skip to content

Commit

Permalink
delete endpoint replaced with post
Browse files Browse the repository at this point in the history
  • Loading branch information
Serhii Chernenko committed Jan 27, 2025
1 parent cbfab47 commit 0b885cb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/layers/core/data/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const addItem = async () => {
const deleteItem = async (item: Pick<Item, 'id'>) => {
await $fetch('/api/v1/items', {
method: 'delete',
method: 'post',
body: {
id: item.id,
},
Expand Down
37 changes: 37 additions & 0 deletions src/layers/core/data/server/api/v1/items/index.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Item } from '@demo/data/types/items.d'
import { z } from 'zod'

export default defineEventHandler(async (event) => {
const body = await readValidatedBody(
event,
z.object({
id: z.string().nonempty().max(255),
}).safeParse)

if (body.error) {
throw createError({
statusCode: 400,
statusMessage: 'Invalid URL parameters',
data: body.error,
})
}

const items = await hubKV().get<Item[]>('items') ?? []
const index = items.findIndex((item) => {
return item.id === body.data.id
})

if (index === -1) {
throw createError({
statusCode: 404,
statusMessage: 'Item not found',
data: { id: body.data.id },
})
}

items.splice(index, 1)

await hubKV().set('items', items)

setResponseStatus(event, 200, `Item ${body.data.id} deleted`)
})
4 changes: 3 additions & 1 deletion src/layers/ui/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
role="alert"
class="alert alert-error w-full px-4 sm:px-8 shadow-lg pointer-events-auto"
>
<span>{{ error.value.statusMessage ?? 'Something went wrong' }}</span>
<span>
{{ error?.value?.statusMessage ?? 'Something went wrong' }}
</span>
<button
type="button"
class="btn btn-sm btn-ghost"
Expand Down

0 comments on commit 0b885cb

Please # to comment.