-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Serhii Chernenko
committed
Jan 27, 2025
1 parent
cbfab47
commit 0b885cb
Showing
3 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters