logigate/server/api/library/[id].delete.ts

15 lines
712 B
TypeScript

// Take an entry out of the library. Needs its edit key. Components already
// added to projects stay where they are.
export default defineEventHandler(async event => {
rateLimit(event, 'library', 30, 10 * 60_000)
const id = getRouterParam(event, 'id')
const key = getHeader(event, 'x-edit-key')
await locked('library', async () => {
const record = await findEntry(id)
if (!keyMatches(key, record.keyHash)) throw createError({ statusCode: 403, message: 'This component belongs to someone else' })
await libraryStorage().removeItem(entryKey(record.meta.id))
await libraryStorage().setItem('index', (await libraryIndex()).filter(m => m.id !== record.meta.id))
})
return { ok: true }
})