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

20 lines
1.0 KiB
TypeScript
Raw Normal View History

// Publish a new version of a library entry. Needs the edit key from the
// first publish; the entry keeps its id and publisher, its version goes up.
import { describeBundle } from '../../../app/lib/logic/library'
export default defineEventHandler(async event => {
rateLimit(event, 'library', 30, 10 * 60_000)
const id = getRouterParam(event, 'id')
const { bundle } = await readLibraryBody(event)
const key = getHeader(event, 'x-edit-key')
const meta = 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' })
const meta: LibraryItem = { ...record.meta, ...describeBundle(bundle), version: record.meta.version + 1, updated: Date.now() }
await libraryStorage().setItem(entryKey(meta.id), { ...record, meta, bundle })
await libraryStorage().setItem('index', (await libraryIndex()).map(m => (m.id === meta.id ? meta : m)))
return meta
})
return { id: meta.id, version: meta.version }
})